Does the im2gray function give greater 'weight' to red than green and blue?
3 次查看(过去 30 天)
显示 更早的评论
I have the following colour coded echo strength image where the red indicates high levels of reflection, then yellow, then green:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1332390/image.bmp)
I want to compare the actual image centroid with the weighted centroid and have done so using the following code:
clc
clear all
% import the Image %
imdata = imread("RCS_Example3.bmp");
% Display the image %
imshow(imdata)
% Convert to grayscale - Grayscale Images are 2D arrays because only
% need one value of intensity for each pixel %
RCSGray = im2gray(imdata);
imshow(RCSGray);
title("RCS Gray Scale");
BW = RCSGray > 0;
imshow(BW)
title('Binary Image')
s = regionprops(BW,RCSGray,{'Centroid','WeightedCentroid'});
imshow(RCSGray)
title('Weighted (red) and Unweighted (blue) Centroids');
hold on
numObj = numel(s);
for k = 1 : numObj
plot(s(k).WeightedCentroid(1), s(k).WeightedCentroid(2), 'r*')
plot(s(k).Centroid(1), s(k).Centroid(2), 'bo')
end
hold off
This produces the following figure (red star = weighted centroid, blue circle = actual centroid):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1332395/image.png)
when converted to grayscale its hard to tell if the 'intensity' of the red spots on the original image is high and I want to know if the red spots are contributing a higher weighted value as they represent points of highest intensity reflection.
If this is correct, is there a way to work out how far apart the weighted and unweighted centroids are?
Thanks
8 个评论
Stephen23
2023-3-22
编辑:Stephen23
2023-3-22
@Isaak: the task is not clearly defined. Although you can use RGB2IND (as Image Analyst shows) to "undo" the pseudo-coloring, doing so requires knowing the colormap distribution, otherwise we simply do not know the scale of values that those colors represent. Are they a logarithmic scale, or evenly spaced, or unevenly spaced? We simply don't know.
For example, consider are the red pixels equally distant from green as green is from yellow? How far apart are light gray and dark gray values? What color is zero? The centroids will be essentially meaningless until this color scale is specified.
You might find this information in the documentation for the product/tool that generates that image, or perhaps online.
回答(1 个)
Image Analyst
2023-3-22
"I have the following colour coded echo strength image where the red indicates high levels of reflection, then yellow, then green"
What about gray and teal/aqua?
If you can, it's definitely best to get the original image values, not a pseudocolored image. Can you do that? If you can, it's easy. But WHY do you want the weighted centroid of this image when it looks like there are parts of the image that are clearly different and (perhaps) should not just be lumped in with the pixels of the entire image? I agree with Stephen that converting that to gray scale will not get you what you asked for (plus I'm not even sure what you're asking for is the proper thing to measure). What you would have to do, if you can't get the original image, is to use the colormap that was applied to this image to "undo" the pseudocoloring and get you back to the original values. It's what I do in the attached thermal image demo. What is your image of? It looks like maybe it's some kind of simulated inverse synthetic aperture radar image of a ship..
4 个评论
Stephen23
2023-3-22
"Is there a way of assigning my own weighted values to colours so that matlab processes the r,g,b accordingly"
Yes, you would need to first specify the colormap (in order to correctly define the sequence and its distribution), then use RGB2IND (or DITHER or something similar) to "undo" the pseudo-coloring. This is what Image Analyst already showed you in their answer.
Simply multiplying the R,G,B, channels with some scalar values will not work.
DGM
2023-3-22
编辑:DGM
2023-3-22
If the software provides you with a colorbar, you may be able to use that to get a rough estimate of the colormap. At that point, IA's demo would work. If you have to take a screenshot, save it as PNG, not JPG, as capturing the ends of the colorbar is important for scaling, and JPG will skew them.
If there's a chance that the mapping is nonlinear, then you'd have to figure out what it is.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!