Question

In: Computer Science

Extract an 8 × 8 patch from the image. To access to the (i, j)th pixel...

Extract an 8 × 8 patch from the image. To access to the (i, j)th pixel of the image, you can type Y(i,j). To access the an 8×8 patch at pixel (i, j), you can do Y(i:i+7, j:j+7) for some index i and j. Extract all the available 8 × 8 patches from the image, and store them as a 64 × K matrix where K is the number of patches. The following code will be useful.

for i=1:M-8

for j=1:N-8

z = Y(i+[0:7], j+[0:7]);

... % other steps; your job.

end

end

Here, M and N are the number of rows and columns of the image, respectively. No need to worry about the boundary pixels; just drop them.

Hint: Use the command reshape in MATLAB (and Python) to turn an 8 × 8 patch into a 64 × 1 column vector. Submit the first 2 columns and the last 2 columns of your data matrix.

Solutions

Expert Solution

import cv2
import numpy as np
import matplotlib.pyplot as plt
file_path='pic\logo.jpg'        #location of file from current directory
img = cv2.imread(file_path)     
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)                #since the image is an 
                                                #3d(r,g,b) array but for this question
#cv2.imshow('image',gray)               ## covert into a gray image(2d) by taking only
                                         ## first channel.
m,n=gray.shape                          ##shape of your pic
size=(m-7)*(n-7)                        ##total number of distinct matrix can be formed
arr=np.empty((64,size))   
k=0

for i in range(m-7):
    for j in range(n-7):
        patch=gray[i:i+8,j:j+8]
        patch=patch.reshape(64,1)
        arr[:,k]=patch[:,0]    
        k=k+1
print(arr.shape)  
cv2.waitKey(0)      ###pause screen until key press
cv2.destroyAllWindows()     

Dear,if you like the solution pls leave a +ve feedback :) . or,

if you have any query ,comment below i will try to solve it ASAP :)


Related Solutions

class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def...
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def __str__(self): s = "" for j in range(7, -1, -1): #for each row for i in range(8): # for each column if self.pieces[j][i] is None: # if self.pieces[j][i] is None s += "." # populate the row with '.' val for each column else: s += self.pieces [j][i].get_symbol() s += "\n" #after each row add a new line return s # return after iterating...
In SAS, how do I extract the city, state, and zipcode into separate columns from a...
In SAS, how do I extract the city, state, and zipcode into separate columns from a single variable with all three? Example- Bremen, KS, 66412 Little River, KS, 67457 Bird City, KS, 67731 The above is stored in one variable each. But I want to make three separate variables with it. Some cities also have two words in it.
Python I am creating a program that allows a user to extract data from a .csv...
Python I am creating a program that allows a user to extract data from a .csv file and print the statistics of a certain column in that file. The statistics include Count, Mean, Standard Deviation, Min, and Max. Here is the code I have so far: import csv import json class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 =...
There exists a group G of order 8 having the following presentation: G=〈i,j,k | ij=k, jk=I,...
There exists a group G of order 8 having the following presentation: G=〈i,j,k | ij=k, jk=I, ki=j, i^2 =j^2 =k^2〉. Denotei2 bym. Showthat every element of G can be written in the form e, i, j, k, m, mi, mj, mk, and hence that these are precisely the distinct elements of G. Furthermore, write out the multiplication table for G (really, this should be going on while you do the first part of the problem).
3: Access Inc. is in operation from last 8 years, and have a balance of $640,000...
3: Access Inc. is in operation from last 8 years, and have a balance of $640,000 in the machinery account as at Jan 01, 2000. the remaning life of this machinery is 7 years and had a salvage value of 63000. On August 01, 2000, the Company made additions to the machinery a/c of $98,000. The machinery is expected to produce a total of 200,000 units. The machinery was used in the production in the following manner: Year Production 2000...
g[8]=4[A[3j] + B[i+6j] I and j are variables. When doing MIPS instructions for above statement what...
g[8]=4[A[3j] + B[i+6j] I and j are variables. When doing MIPS instructions for above statement what needs to be done to 4[A[3j]? Choose your own temporary and store registers. Explain show step by step
I have an 8 by 8 array that has values ranging from 1-64. I am wanting...
I have an 8 by 8 array that has values ranging from 1-64. I am wanting to display the array evenly but due to some digits being single and others double, it displays lopsided. What would be the best way to get everything in the array to display in a nice even square?
C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from...
C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from a text file) and looks for patterns of 2 or more duplicate values. The program should replace these values with the following pattern:  2 such characters followed by an Integer ( which represents number of occurrences of each character), followed by an asterisk. For example say the input of the non-compressed image was: ,,,,)H 7. i.e. it consists of four commas, one closed bracket, the...
HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S...
HOW DO I ACCESS THE HEAD OF A LINKED LIST FROM INT MAIN () WHEN IT'S IN ANOTHER CLASS. HERE IS MY CODE FOR MY MAIN AND HEADER FILES. HEADER FILE: #ifndef HANOISTACK_H #define HANOISTACK_H #include <iostream> using namespace std; class HanoiStack { //class private: struct Disc{ //linked list for towers int num; Disc* next; }; Disc* head; public: HanoiStack(){ //constructor head = nullptr; }; //HanoiStack operator+=(const Disc&); //HanoiStack operator<<(const Disc&); void push(int); //push function int pop(); //pop function void...
The following function describes how the value of import of country i from country j, Fij...
The following function describes how the value of import of country i from country j, Fij is determined: ln Fij = −14.44 + 0.852 ln Gi + 0.178 ln Gj − 1.119 ln Dij , where Gi is GDP in country i, Gj GDP in country j, and Dij the distance between the two countries. Economists usually convert many aggregate measures of an economy in nature logs. According to the chain-rule d ln x = dx x , which measures...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT