what do I get by calling 'PixelIdxList'?

Hi! I recently using 'PixelIdxList' in the 'regionprop' function to calculate the color of the particle I labeled. But I don't really understand what color that 'PixelIdxList' gives?
I mean, I extract the 'PixelIdxList' from a RGB image, and calculate the mean value of pixels. But as we know, RGB image has red, green and blue in its array. When I calculate the mean(PixelIdxList) in an RGB image, I only get one value. So what colour does this mean value present?
My goal is to generate the mean grey value from the RGB image. Can I use this mean value from PixelIdxList?
Thank you!

3 Comments

Bonsoir s'il vous plait comment je peux converti les pixels de stats(i).area en image en matlab par exemple j'ai le resultat comme suit cc# 1 - area 202965 nuemoro de pixel et taille j'aime l'affiche dans une image binaire
Approximate translation:
Good evening please how I can convert the pixels of stats (i) .area to image in matlab for example I have the result as follows cc # 1 - area 202965 nuemoro of pixel and size I like the poster in a binary image
marwa, you can't convert an area (a single scalar), or a list of pixel values in an irregularly-shaped blob, into an image - you need more than that. See my Image Segmentation Tutorial to see how you can crop out the regions from the image into sub-images using the bounding box from regionprops(), and imcrop(). Start your own question with your own image if you need more help.

Sign in to comment.

 Accepted Answer

PixelIDxList does not give any image value. PixelIDxList gives array locations.
R = original_image(:,:,1);
G = original_image(:,:,2);
B = original_image(:,:,3);
blobinfo = regionprops(BWorLabeledImage, 'PixelIdxList');
one_idxlist = blobinfo(1).PixelIdxList;
R_in_region = R(one_idxlist);
G_in_region = G(one_idxlist);
B_in_region = B(one_idxlist);
mean_R_in_region = mean(R_in_region);
mean_G_in_region = mean(G_in_region);
mean_B_in_region = mean(B_in_region);

14 Comments

But if I use the code you suggested, does it mean that the color I get is not the grey value? What about I want the grey value, which ranges from 0-255?
You could
mean_RGB = reshape( uint8([mean_R_in_region, mean_G_in_region, mean_B_in_region]), 1, 1, 3);
mean_gray = rgb2gray(mean_RGB);
or you could do
gray_image = rgb2gray(original_image);
blobinfo = regionprops(BWorLabeledImage, 'PixelIdxList');
one_idxlist = blobinfo(1).PixelIdxList;
gray_in_region = gray_image(one_idxlist);
mean_gray = uint8( mean(gray_in_region) );
Thanks!! One more question: after I convert the RGB into grey image, will the grey value changed? I mean, grey value (the amount of melanin) is an phenotype to measure among different colonies. Will the difference of grey value change after the image conversion? Thank you very much!!!!
The color image that you are reading in does not contain measures of the amount of melanin.
Cameras take time-integrated electron charge readings of sets of sensors that are tuned to be most efficient at different wavelengths, and process those readings through an internal lighting model to come up with red, green, and blue readings for each pixel. The lighting model that is used has the property that the luminance (brightness) can be calculated by a linear combination of the R, G, and B values. rgb2gray does that calculation, calculating luminance. It is not measuring grey in the original image: it is calculating brightness of the image.
You are taking some color measure as a proxy for the amount of melanin. The validity of using color as a proxy depends a lot on the spectra qualities of the illumination of the sample, and upon how those frequencies are absorbed differently by the melanin and non-melanin portions of the sample. For example if the illumination contains significant 785 nm then the melanin is going to fluoresce, giving it a heightened brightness -- but areas can also be bright without containing melanin, and the lack of brightness for a spot could mean the lack of 785 nm illumination rather than a lack of melanin.
Wow! Thank you very much for such detailed and helpful explanation!!! I got your point now. But if I only use the visible light, then I can use the luminance from rgb2gray to roughly present the amount of melanin, though the luminance can still be affected by other minor factors in this situation. Is that correct? Thank you very much again!!!!
melanin is a mix of pheomelanin, which is red to yellow in color, and eumelanin, which is dark brown to black. In biological samples, blood flow and cartotine in fat are also influencing skin color. Melanin is going to change the color of whatever it is in, but depending on the situation the result could be brighter or darker than the background. In isolated samples, probably darker. But darkness in a sample can also be because of thickness.
should make it clear that darkness cannot be used as melanin detector in your samples, not by itself. Possibly within any one dot.
What happened to the three original RGB images? Why were they removed?
The original three RGB images are still over in https://www.mathworks.com/matlabcentral/answers/365102-how-to-get-the-threshold-automatically-when-converting-image-from-hsv-to-bw -- I imported the one image to this question to point out that "dark" in itself often cannot be used to detect "melanin" .
Hmm, so it's not only the amount of melanin, but also thickness and the texture of the surface affect the illuminance. Now you remind me that I have colonies in different textures. Some are hairy and some are glassy. So this texture difference will be a big problem too. Em, then then I need to think out another way to measure the amount of melanin... Thank you very much Wlater!!!!
In general, how do you do linear indexing with an RGB image? Do you always need to split the image into its 3 RGB channles? Fo example if I do MyGrayImage(:) I get the pixel values serialized in linear indexing order, but if I do MyRGBImage(:) I don't get a MNx3 array of RGB values listed in linear indexing order. Is there an easy way to do something like MyRGBImage(linear_index,:) and get the 1x3 RGB value of the pixel at that linear index?
@Jason Butler, no I never do MyRGBImage(linear_index,:) with 2 indexes into the 3 dimensional array. I'm not sure how to interpret it. It works and will give you a 1-D vector that is a serialized version of all colors at the "linear_index" row but it's just too confusing. It's much clearer, intuitive, and understandable to index the array in the normal manner, either with separate row, column, and color channel, or else with a 3-D logical array (like you'd get from creating a mask, say, by thresholding).
So to "get the 1x3 RGB value of the pixel" you'd do
rgbOnePixel = rgbImage(row, column, :);
% or
rgbOnePixel = impixel(rgbImage, column, row);
Be careful with impixel because the 2nd and 3rd argument is x, y, which is column, row not row, column like you might think.
DGM
DGM on 4 Dec 2022
Edited: DGM on 4 Dec 2022
If we're still talking about using regionprops(), then usually you would want to split the image anyway. When given an RGB image, regionprops will treat it as a volumetric image. The region information that's returned pertains to blobs which potentially span multiple channels. While you might think that two colocated blobs in different channels should be treated as a single object, that's not necessarily how they will be.
Consider the following image.
As far as regionprops() is concerned, this image (when reduced to a MxNx3 binary image) contains 5 blobs. The large triad in the center is a single connected region, as are the R-G and G-B groups. Since R and B are not adjacent volumetrically, the B-R group is not treated as a single region.
When dealing with volumetric images, this behavior makes sense, but I'd have a hard time trying to find a good reason to use it with RGB inputs.
selectedpixels = A(idxpage+prod(sz(1:2))*(0:sz(3)-1))
That is the kind of calculation done by sub2ind and ind2sub
Augh. I was thinking too much about addressing pagewise.
I don't have time to fix it now.

Sign in to comment.

More Answers (1)

PixelIdxList is the linear index of pixels in the blob.
From what you say, it appears you want the PixelValues measurement instead.

4 Comments

Now I'm confused. I used 'PixelIdxList' to extract the color value because I read your blog Gray scale pixel values in labeled region, in which you use the code (below) to replace the color in the original value. I guess `mean(I(idx))` is where you get the mean color from the original image, right? Or do I understand wrong? I'm just started Matlab nearly one week ago. Sorry if I understand wrong.
for k = 1:numel(s)
idx = s(k).PixelIdxList;
F(idx) = mean(I(idx));
end
That's right, but notice how it's mean(I(idx)) where idx is the linear indexes of pixels in the blob, not mean(PixelIdxList) like you said. You don't have the I in there like the blog did. The blog computes the mean of the intensities whereas you compute the mean of the locations (linear indexes).
Ziming Zhong
Ziming Zhong on 5 Nov 2017
Edited: Ziming Zhong on 5 Nov 2017
Ah! So I guess I just understood it wrong but I wrote the code right. I wrote the code for extracting the color value as what you did on the blog, like mean(mypicture(idx)). So this mean intensity in RGB picture is the mean for three colors. Do I understand correct?
No. You wrote the code incorrectly. See Walter's answer for the correct version. Again, your code is not correct.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!