How can I use the elements of 'PixelValues' of 'regionprops'?

Faraz · Feb 8, 2024 · 2.3K views
Question
Hello,   I have been able to extract the pixel values of the grayscale image using regionprops (I think). I did so by using this code     img = rgb2gray(imread('W1\Writer1_01_02.jpg')); bw_normal = im2bw(img, graythresh(img)); bw = imcomplement(bw_normal); [label,n] = bwlabel(bw); stats = regionprops(label, img, {'Area', 'BoundingBox', 'PixelValues'}); Now I want to extract or see visually the values extracted. I mean with the case of "stats.Area" I can do this to extract its values to another vector areas = [stats.Area] But this does not work with PixelValues. If I do the same with PixelValues I am presented with the following error: >> test = [stats.PixelValues] Error using horzcat CAT arguments dimensions are not consistent. Basically what I am trying to do is to visually plot the pixel values, as in plot a histogram of not the entire image but the PixelValues of the connected components.   This was done using C#. I want to produce something like this. In the image below the pixel values of the connected components is detected and then plotted onto the image. Is this possible? (The red dots are the detected pixel values)   Thank you
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Aug 12, 2026

You can do that by using imshow on the binary image:

 

imshow(bw);

If you want to mask it to show the original pixels in grayscale, then you can do that

maskedIMage = img; % Initialize
maskedImage(~bw) = 0; % Non-selected pixels set to zero.
imshow(maskedImage); % Display.

 

Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!