I am currently writing a program on Matlab for Image Processing. I am using an image (below) to attempt to count the number of white cars in the image. I have used filtering commands, strel(disk, 2), and have managed to detect the two white cars in the image but due to the way the binary image (below) displays a car it counts one car as two.
Are there any solutions to overcome this problem or are there any particular methods I should be using as an alternative to the code below?
a = imread('Cars2.jpg'); %Read the image Car1.jpg
subplot(3,3,1), imshow (a); %Display RGB image Car1.jpg
b = rgb2gray(a); %Turn Car1 from RGB to greyscale
subplot(3,3,2), imshow (b); %Display greyscale image Car1.jpg
c = graythresh (a); %Automatically set appropriate threshold for foreground & background (Otsu's Method)
d = im2bw (b,0.8); %Convert from greyscale to binary image
subplot (3,3,3), imshow(d); %Display binary image Car1.jpg
subplot(3,3,4), imhist (b,256); %Display histogram for greyscale values (image, samples)
SE = strel ('disk',2); %Set Disk radius for filtering unnecessary pixels
e = imopen (d,SE); %Erode then Dilate image with Disk radius
subplot (3,3,5), imshow(e); %Display openned/filtered image Car1.jpg
B = bwboundaries(e);
imshow(e)
text(10,10,strcat('\color{red}Objects Found:',num2str(length(B))))
hold on
EDIT: As i have under 10 reputation I can't post the image displayed from the code but the theory is pretty generic so I hope you understand what I'm getting across. The images are similar to http://www.mathworks.co.uk/help/images/examples/detecting-cars-in-a-video-of-traffic.html