Код, кажется, в порядке, я только изменил размер диска до 8, а затем вы должны игнорировать небольшие объекты, скажем, менее 1000 пикселей. Попробуйте это:
% set car size threshold
minCarSize = 1000;
% read image from stackoverflow
img = imread('https://i.stack.imgur.com/0JVK9.jpg');
it = img(31:425,83:1035,1) > 200;
% process the image with disk size 8
c = imfill(it,'holes');
se = strel('disk',8);
iopenned = imopen(c,se);
[labeled,numObjects]=bwlabel(iopenned,4);
% check object sizes
objSize = nan(numObjects,1);
loc = nan(numObjects,2);
for iObj = 1:numObjects
objSize(iObj,1) = sum(sum(labeled == iObj));
[loc(iObj,1),loc(iObj,2)] = find(labeled == iObj,1); % these are x-y to display car numbers
end
% select big shapes
car = find(objSize > minCarSize);
% create image with car count
carNum = insertText(uint8(iopenned*255),fliplr(loc(car,:)),regexp(num2str(1:length(car)),' *','split'));
figure
imshow(carNum);
title('iopenned + car count')
axis off