Find summation of white pixel of detected rectangle from a binary iamge??

2 ビュー (過去 30 日間)
Rajat
Rajat 2014 年 5 月 6 日
回答済み: Rajat 2014 年 5 月 8 日
im=imread('8.jpg'); %read above picture
im=im2bw(im,.555);
[row,col]=size(im);
area=(row*col)*.1;
stats = regionprops(im);
imshow(im);
hold on;
density=0;
for i = 1:numel(stats)
rect = [stats(i).BoundingBox];
count = 0;
if(rect(3)*rect(4)>area)
for j= fix(rect(1)):fix(rect(3))+fix(rect(1))% for every pixel of detected rectangle
for k= fix(rect(2)):fix(rect(4))+fix(rect(2))
if((j<=row && j>0 && k<=col && k>0)&&(im(j,k)== 1)) %count white pixel
count =count +1;
end
end
end
density=count/fix(rect(3))*fix(rect(4)); %count sum of white pixel/area
disp(count);
disp(density);
textRect =i;
rectangle('Position', stats(textRect).BoundingBox, ...
'Linewidth', 1, 'EdgeColor', 'r', 'LineStyle', '--');
f = getframe(gca);
im = frame2im(f);
imwrite(im,'8_rect.tif');
end
end
But i didnt get the appropriate value of the density of that detected area,it should be <1.
Can any one please help me to find out the pixel density of that detected rectangle??
Thank You in Advance.

回答 (2 件)

Kevin Claytor
Kevin Claytor 2014 年 5 月 6 日
You're making this too complex. It's a binary image, just sum the values of your sub-image and divide by the # elements. Pseudocode:
% BWrect = %code to extract BW sub-image
density = sum(BWrect(:))/numel(BWrect);
Also, you're missing a parenthesis:
density=count/ ( fix(rect(3))*fix(rect(4)) ); %count sum of white pixel/area

Rajat
Rajat 2014 年 5 月 8 日
Hi Kevin,
I modify my code as per ur suggestion,but didn't get the expected result,can u please look into the below code and suggest me what to do?It will be very helpful to me Kevin :-)
density=0;
for i = 1:numel(stats)
rect = [stats(i).BoundingBox];
if(rect(3)*rect(4)>area)
density = sum(rect(:))/numel(rect);
disp(density);
disp(rect(3)*rect(4));
textRect =i;
rectangle('Position', stats(textRect).BoundingBox, ...
'Linewidth', 1, 'EdgeColor', 'r', 'LineStyle', '--');
f = getframe(gca);
im = frame2im(f);
The result shown is : Density->349.7500 and area ->478400.
I want to find the pixel density of that detected area,as in sea or sky most of of the pixel are white.So it will be more dense than other set of image like street view or sign board.
Thank You Kevin very very much :)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by