Incorrect output in image processing.
17 views (last 30 days)
Show older comments
I have a code which is counting the number of empty parking slots. The method used is background subtraction, but I am getting incorrect output. close all; set(0,'DefaultFigureWindowStyle','docked') %dock the figures base_dir = 'C:\Users\USER\Desktop\final year project'; cd(base_dir);
%% get listing of frames f_list = dir('*jpg');
%% make average of background images N = 4; % num of frames to use to make averaged background tps=10; status = [0 0 0 0 0 0 0 0 0 0]; img = zeros(960,540,N); for i = 1:N img_tmp = imread(f_list(i).name); img(:,:,i) = img_tmp(:,:,1); %img(:,:,i) = rgb2gray(img_tmp); end bck_img = (mean(img,3)); %average background template %subplot(121);imagesc(bck_img) %subplot(122);imagesc(img(:,:,1)) clear img; pause(5) %gaussian filter hsize = 80; sigma = 20; gaus_filt = fspecial('gaussian',hsize , sigma); %gaus_filt = fspecial('log',hsize , sigma); %subplot(121); imagesc(gaus_filt) %subplot(122); mesh(gaus_filt) colormap(gray)
SE = strel('diamond', 0); %this makes a matrice object for passing into imdilate())
%% Operations on frames
for i = N:1:length(f_list)
img_tmp = double(imread(f_list(i).name)); %load image and convert to double for computation
img = img_tmp(:,:,1); %reduce to just the first dimension
%img = rgb2gray(img_tmp)
subplot(221);imagesc(img);
title('Raw');
%subtract background from the image
sub_img = (img - bck_img);
subplot(222);imagesc(sub_img);
title('background subtracted');
%gaussian blurr the image
gaus_img = filter2(gaus_filt,sub_img,'same');
subplot(223);imagesc(gaus_img);
title('gaussian smoothed');
%threshold the image
subplot(224);hist(gaus_img(:));
thres_img = (gaus_img < 10);
thres_img = ~thres_img;
thres_img = bwareaopen(thres_img,10);
se2 = strel('disk',1);
thres_img = imerode(thres_img,se2);
%counting no of blobs
[L, num] = bwlabel(thres_img);
stats = regionprops(L, 'Centroid');
thres_img = ~thres_img;
subplot(224);imagesc(thres_img);
%subplot(224);imagesc(gaus_img);
title('thresholded');
clc
fprintf('Total parking spaces : %i\n',tps)
fprintf('filled parking spaces : %i\n',num)
fprintf('vacant parking spaces : %i\n',tps-num)
fprintf('coordinates :\n')
for x=1:1:num
fprintf('point %i : %f , %f \n',x,stats(x,1).Centroid(1),stats(x,1).Centroid(2));
for c=1:1:4
if(stats(x,1).Centroid(1)>50+100*c && stats(x,1).Centroid(1)<150+100*c && stats(x,1).Centroid(2)>70 && stats(x,1).Centroid(2)<180 )
status(c)=true;
end
end
for c=4:1:tps
if(stats(x,1).Centroid(1)>50+100*(c-4) && stats(x,1).Centroid(1)<150+100*(c-4) && stats(x,1).Centroid(2)>350 && stats(x,1).Centroid(2)<470 )
status(c)=true;
end
end
end
%disp(status);
fprintf('\nOccupied slots : ')
for c=1:1:10
if(status(c)==1)
fprintf(' %i ',c);
end
end
status = [0 0 0 0 0 0 0 0 0 0];
pause(.001)
end
I don't know how to proceed futher and what is wrong with the code.
Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!