Skip to content
Advertisement

Extract N number of patches from an image

I have an image of dimension 155 x 240. Like the following:

Example of image

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.

JavaScript

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:

JavaScript

If you plot the only_desired_area you will get the following image: enter image description here

This is the main logic if you wish an even tighter bound you should adjust the slicing properly.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement