Plotting the area of an object in grayscale against its intensity level in the grayscale image

2 次查看(过去 30 天)
Basically what I am trying to produce is the histogram of the image at varying grayscale intensities showing me the area of the connected components in the image.
Let me explain further, I plan on finding the areas of all the connected components of the image at varying threshold levels. Then combine them all in a graphical way and show them plotted against the intensity level of a grayscale image i.e. 0 - 255.
I hope my code will explain what I am trying to do.
img = rgb2gray(imread('W1\Writer1_01_02.jpg'));
for k = 1:-0.01:0.1
bw_normal = im2bw(img, k);
bw = imcomplement(bw_normal);
[label,n] = bwlabel(bw);
stats = regionprops(label,img, {'Area', 'Centroid'});
plot([stats.Area],k,'o');
axis([0 1000 0.1 1])
hold on;
end
As you can tell I used a for loop to produce a the varying threshold level, calculate the areas of the CC and plot them against the selected threshold level. This is what it produces:
this is not what I want. I am trying to replicate this result. It does not have to look exactle like this but anything closely similar would do
I then found out that I can find the properties of CC from the grayscale image directly using
STATS = regionprops(..., I, properties)
So I wrote this:
img = rgb2gray(imread('W1\Writer1_01_02.jpg'));
for k = 1:-0.01:0.1
bw_normal = im2bw(img, k);
bw = imcomplement(bw_normal);
[label,n] = bwlabel(bw);
stats = regionprops(label,img, {'Area', 'Centroid'});
% plot([stats.Area],k,'o');
% axis([0 1000 0.1 1])
imshow(img);
hold on;
for j = 1:numel(stats)
text(stats(j).Centroid(1),stats(j).Centroid(2), ...
sprintf('%2.1f', stats(j).Area), ...
'EdgeColor','b','Color','r');
end
end
This produced the following:
So now I have found the areas of the connected components in grayscale. How do I plot them to show as my desired output (the blue one I showed above)?
thank you for reading

采纳的回答

Image Analyst
Image Analyst 2014-2-13
Do you mean how do you show the boxed area values over your original image in all 91 threshold cases? Try this:
img = rgb2gray(imread('W1\Writer1_01_02.jpg'));
figure;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
plotNumber = 1;
for k = 1:-0.01:0.1
bw_normal = im2bw(img, k);
bw = imcomplement(bw_normal);
[label,n] = bwlabel(bw);
stats = regionprops(label,img, {'Area', 'Centroid'});
subplot(10,10,plotNumber);
imshow(img);
hold on;
for j = 1:numel(stats)
text(stats(j).Centroid(1),stats(j).Centroid(2), ...
sprintf('%2.1f', stats(j).Area), ...
'EdgeColor','b','Color','r');
end
plotNumber = plotNumber + 1; % Move to the next plot for next iteration.
end
  2 个评论
Faraz
Faraz 2014-2-13
No. Sorry but thats not what I want to do. I showed the boxed area values in the end only to show that I was able to calculate the area of the connected components in grayscale (which I dont think I completely accomplished).
but what I want to do is to plot (or histogram) the areas of the connected components against the grayscale levels of the image (0 - 255). Like I showed in the image here:
this was generated using C# code. I have the code by the original author but since I cannot understand C# its basically useless to me. Thats why I'm trying to replicate it in matlab.
Thank you for your help, ImageAnalyst P.S. If you know C# and if it could help can I add the code here?
Image Analyst
Image Analyst 2014-2-13
You can get the total area of all regions above the threshold simply by taking the cumulative distribution function of the histogram:
areaAboveThresh = cumsum(pixelCounts);
but that's the area of all regions summed together. If you want it by blob, then you need to decide what you're going to do when the blobs start merging together.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by