Pergunta

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:

enter image description here

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

enter image description here

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:

enter image description here

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

Foi útil?

Solução

Based on your existing code:

img = rgb2gray(imread('W1\Writer1_01_02.jpg'));
k = 1:-0.01:0.1;
bins = 1:100 % change depending on your image

% preallocate output - this will be filled with histograms
histout = zeros(length(k),length(bins); 

for m = 1:length(k); 
     bw_normal = im2bw(img, k(m));
     bw = imcomplement(bw_normal);
     [label,n] = bwlabel(bw);
     stats = regionprops(label,img, {'Area'});
     A = cell2mat(struct2cell(stats));
     histout(m,:) = hist(A,bins);
end

I changed the output of regionprops to just Area because it simplifies conversion of the output struct into something that can be read by hist. Changing from looping through k to predefining a vector k and using k(m) in the loop just makes indexing into histout a little more straight forward.

You can display with imagesc and then correct the tick labelling:

imagesc(histout)
colormap('jet')
set(gca,'XTickLabel',bins(get(gca,'XTick')));
set(gca,'YTickLabel',k(get(gca,'YTick')));
xlabel('Area')
ylabel('Threshold')
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top