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

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

4 Comments

pixel = cat(1, stats.PixelValues)
@Dishant Arora Please add this as an answer so that I can accept it as the solution. This is what I was looking for.
Thanks
Note that this will give you the same as what I told you
allThePixelValues = img(bw);
Both essentially give you all the gray levels in the binary image that you sent to regionprops, just in a different order.
Oh right, I didnt see that comment you added later. Thanks

Sign in to comment.

 Accepted Answer

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.

3 Comments

Thanks for the answer image analyst but I want to access the data of pixel values. So that I can work on it further or use it in my functions. Also so that I can plot it and see how many of each value I have and so on.
I mean using pixel =[stats.PixelValues] does not work I'm presented with the error I showed above.
Thank you
Then you can just get them from the binary image that you sent into regionprops:
allThePixelValues = img(bw);
Worked for me too. Thanks

Sign in to comment.

More Answers (1)

Categories

Asked:

on 17 Feb 2014

Answered:

on 10 Jan 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!