Object-counting in image processing.
显示 更早的评论
I am doing a project on object-counting from images. For that, I have taken two images- one is the background containing no objects and the other containing some objects(toy-cars) over it.
Background Image:

------------------------------------------------------------
Image with objects over it:

The difference-image between the two images is taken and converted to black-and-white using a threshold value. The problem is that some of the pixels inside the objects match with the background and so these pixels are also getting subtracted completely and as a result, some black pixels are occurring inside the objects as you can see from the b/w image below and so the objects are getting divided into parts.

I need each object to occur as one object so that we get the exact no of objects. Can someone please help me regarding this.
The code I have used is below:
I = imread('Snap5.png');
J = imread('bg1.png');
K = imabsdiff(I,J);
th = graythresh(K);
L = im2bw(K,th);
[LabeledIm,ConnectedObj] = bwlabel(L,4);
coloredLabels = label2rgb (LabeledIm, 'hsv', 'k', 'shuffle');
ConnectedObj
figure,
subplot(221), imshow(K), title('Difference'),
subplot(222), imshow(L), title('Thresholded BW'),
subplot(223), imshow(coloredLabels), title('Color-labeled Objects');
9 个评论
Shivam Garg
2015-5-30
Hello I am Also Trying to Calculate the Number Of Vehicle on Road.
I am using OPENCV for that.
I am not able to understant what all u have done. Can u please explain what is the basic aglorithm u have used.
I am trying with background subtraction But it says that u need to have continuous frame to do that.
I will be very thankful to u
Shivam Garg
Image Analyst
2015-5-30
You should ask in an OpenCV forum instead of a MATLAB/Simulink forum.
Project Traffic
2017-3-25
can you please upload the exact matlab code for counting the objects even if there is another object within the same object and give a count of one
Image Analyst
2017-3-25
Call imfill() to combine internal blobs, then call regionprops().
Saqib Ali
2020-9-18
use strel (structuring elements) method for helping MATLAB to guess the size of object.
Image Analyst
2020-9-18
编辑:Image Analyst
2020-9-18
"Project Traffic", 3.5 years old so I realize you probably won't answer but re-reading the question, when you say "can you please upload the exact matlab code for counting the objects even if there is another object within the same object and give a count of one" it sounds ambiguous. I'm unsure if you have an object containing another object, like a bullseye target,

do you want that to count as one white object or two? Normally bwlabel() whould give a count of 2.
[labeledImage, numRegions] = bwlabel(binaryImage);
If you want it to count as 1, then you can either fill the outer objects and label
binaryImage = imfill(binaryImage, 'holes');
[labeledImage, numRegions] = bwlabel(binaryImage);
However this would change the measurements returned by regionprops since now the blob's area is different - it's just one giant disc instead of a thin ring. Or you could get rid of "interior" blobs using some morphological tricks so that there is just one thin white ring. Let me know if you need that.
Jeje Ahmad
2020-10-28
hi
can try your code to counting the number of cars?
@KangKan
Jeje Ahmad
2020-10-28
@KangKan
Image Analyst
2020-10-28
Kangkan hasn't been here in 7 years, but you can sure give it try. Just copy, paste, and change image filename, and run.
采纳的回答
更多回答(1 个)
Fasih Chishti
2015-1-29
0 个投票
Hello! I'm doing project on Image counting... How could i count the objects from any picture? Thanks
类别
在 帮助中心 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!