Pergunta

I a trying to plot the area of the connected components at a specific threshold against their intensity level in grayscale.

The code I came up with tries to accomplish this by first converting a text image to binary at a selected threshold, and find the area using regionprops, convert this image back to grayscale. I then found the intensity levels using hist. then plot the intensity versus the area. This basically gives me an intensity vector of roughly 6000 elements and the connected components are only 513. When I try plot I get the following error:

??? Error using ==> plot Vectors must be the same lengths.

Here is my code

 img = rgb2gray(imread('urdu.jpg'));

for k = 0:0.01:1
    bw_normal = im2bw(img, k);
    bw = imcomplement(bw_normal);
    [label,n] = bwlabel(bw);
    stats = regionprops(label, 'Area');
    gray = double(bw_normal);
    h = hist(gray);
    h = h(:);
    plot([stats.Area],h);
    %axis([0 350 0 1.1])
    hold on;
end

So as you can tell I am trying to find the area of connected component at increments of threshold.

Is my implementation of the challenge wrong? Can this be done any other way?

Just to make it clear, the end product is supposed to look like this:

enter image description here

Foi útil?

Solução

You can not do the histogram on the original gray image since you are counting the areas after bwlabel. One dirty method might work without calling hist function, but you need to roughly estimate your maximum area of the connected components. Note that sometimes with one threshold you set, the area of some connected components are very large, but such a large value only occurs very few times (most times it is 1). You may consider ignore such a large area in your final image. Based on the image you showed, I used 200 as the permitted maximum connected component area.

MAP=zeros(101,200);
for k=0:0.01:1
    bw_normal = im2bw(img, k);
    bw = imcomplement(bw_normal);
    [label,n] = bwlabel(bw);
    stats = regionprops(label, 'Area');

    for j=1:length(stats)
        if stats(j).Area<=200
            MAP(fix(k/0.01+1),stats(j).Area)=MAP(fix(k/0.01+1),stats(j).Area)+1;
        end
    end
end
imagesc(MAP)

Outras dicas

You should do a histogram of the areas for each threshold not a histogram of the pixel intensities. Assuming your interested in the property of area for the collection of objects found using regionprops.

If you are interested in the intensity of each object found by regionprops then use one or more of the 'Pixel Value Measurements' then use a histogram of these.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top