MATLAB: How to select a region of pixels using IMPIXEL in Image Processing Toolbox 5.0.1 (R14SP1)

colorimageImage Processing Toolboximpixeliptpixelregionrgbselect

The function IMPIXEL allows me to get the color of a single pixel in an image but I am interested in a region surrounding the pixel. I would like to know if there is a way to select the surrounding region without clicking on individual pixels.

Best Answer

The ability to select regions of pixels is not available in IMPIXEL in Image Processing Toolbox.
To work around this issue, you can call IMPIXEL twice: the first time to get the base pixel coordinates from the image, and then again to get the RGB data from the region of interest. For example, the following example shows a function MATLAB file which gets the RGB data from a 3x3 grid of pixels surrounding the selected point.
function Q = impixel9(image)
x = [-1 0 1 -1 0 1 -1 0 1];
y = [1 1 1 0 0 0 -1 -1 -1];
[C,R,P] = impixel(image);
Q = impixel(image,x+C,y+R);
Note that this function only works if one point is selected from the image. The attached MATLAB file below contains a more general function that returns 3x3 grids for any number of selected pixels.
By changing the x and y vectors in these functions, you can change the region of interest. For large regions, the MESHGRID function may help to create these vectors. For more information on MESHGRID, type:
doc meshgrid
at the MATLAB Command prompt.