I have an image of dimension 155 x 240
. Like the following:
I want to extract certain shape of patchs (25 x 25)
.
I don’t want to patch from the whole image.
I want to extract N number of patch from non-zero (not background) area of the image. How can I do that? Any idea or suggestion or implementation will be appreciated. You can try with either Matlab or Python.
Note:
I have generated a random image so that you can process it for patching. image_process
variable is that image in this code.
import numpy as np from scipy.ndimage.filters import convolve import matplotlib.pyplot as plt background = np.ones((155,240)) background[78,120] = 2 n_d = 50 y,x = np.ogrid[-n_d: n_d+1, -n_d: n_d+1] mask = x**2+y**2 <= n_d**2 mask = 254*mask.astype(float) image_process = convolve(background, mask)-sum(sum(mask))+1 image_process[image_process==1] = 0 image_process[image_process==255] = 1 plt.imshow(image_process)
Advertisement
Answer
Lets assume that the pixels values you want to omit is 0.
In this case what you could do, is first find the indices of the non-zero values, then slice the image in the min
/max
position to get only the desired area, and then simply apply extract_patches_2d
with the desired window size and number of patches.
For example, given the dummy image you supplied:
import numpy as np from scipy.ndimage.filters import convolve import matplotlib.pyplot as plt background = np.ones((155,240)) background[78,120] = 2 n_d = 50 y,x = np.ogrid[-n_d: n_d+1, -n_d: n_d+1] mask = x**2+y**2 <= n_d**2 mask = 254*mask.astype(float) image_process = convolve(background, mask)-sum(sum(mask))+1 image_process[image_process==1] = 0 image_process[image_process==255] = 1 plt.figure() plt.imshow(image_process) plt.show() from sklearn.feature_extraction.image import extract_patches_2d x, y = np.nonzero(image_process) xl,xr = x.min(),x.max() yl,yr = y.min(),y.max() only_desired_area = image_process[xl:xr+1, yl:yr+1] window_shape = (25, 25) B = extract_patches_2d(only_desired_area, window_shape, max_patches=100) # B shape will be (100, 25, 25)
If you plot the only_desired_area
you will get the following image:
This is the main logic if you wish an even tighter bound you should adjust the slicing properly.