How to calculate percentage of white portion of an image to measure the density of the vehicles ?

5 vues (au cours des 30 derniers jours)
I have tried one program. I m getting the white portion of an image in pixels. But i want it in percentage.
And I wanted to calculate the density of the vehicles on the road. Wanted to remove the other areas like trees, divider of the road etc.
Image is attached herewith.
Program is:
clc;
clear;
close all;
a=imread('frame1.jpg');% read RGB image (frame no. 1)
imshow(a);
%Selection of ROI
[r c]=ginput(4);
BW=roipoly(a,r,c);
figure;imshow(BW);
[R C]=size(BW);
for i=1:R
for j=1:C
if BW(i,j)==1
Out(i,j)=a(i,j);
else
Out(i,j)=0;
end
end
end
figure;imshow(Out,[]);title('Output Image');
%Convert ROI Selecetd image to binary image
threshold = 165; % threshold set at 165
z= Out>threshold; % convert grayscale image to binary image
figure;imshow(z)% display binary image
% Dilation
y2 = bwmorph(z,'dilate',1) ;
figure ; imshow(y2)
%Connected component labelling
bwlabel(y2,4)% containing labels for the connected objects in y2, where 4 specifies 4-connected objects
%Blob analysis
%Filling holes
C=imfill(y2,'holes');
figure;imshow(C);
%Apply Median filter to remove Noise
FilteredImage=medfilt2(C,[5 5]);
figure;imshow(FilteredImage)
%Boundary Label the Filtered Image
[L num]=bwlabel(FilteredImage);%returns in num the number of connected objects found in FilteredImage
STATS=regionprops(L,'Area');
cc=[];
removed=0;
%Remove the noisy regions
for i=1:num
dd=STATS(i).Area;
if (dd < 50)% when we change the value as 20, 50, 100,500,5000 no. of blobs will change
L(L==i)=0;
removed = removed + 1;
num=num-1;
else
end
end
[L2 num2]=bwlabel(L);
% Trace region boundaries in a binary image.
[B,L,N,A] = bwboundaries(L2);
%Display results
subplot(1,2,1), imshow(L2);
title('BackGround Detected');
subplot(1,2,2), imshow(L2);
title('blob detected');
hold on;
for k=1:length(B),
if(~sum(A(k,:)))
boundary = B{k};
plot(boundary(:,2), boundary(:,1),'r','LineWidth',2);
end
end
%find area of each blob
%labeledImage = bwlabel(L2); %Label connected components in image L2
%measurements = regionprops(labeledImage, 'Area');%measures the property "area"in the labeled image
%allAreas = [measurements.Area]; % List of all the blob areas.
%totalAreaOfAllBlobs = sum(allAreas); % Will be the same as [pixelSum2 = sum(binaryImage(:)]
% calculate white portion of the image%
nblack=sum(L2(:));
nwhite=numel(L2)-nblack;

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by