In: Computer Science
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.
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 :)