我有这个MATLab代码来计算图像中的对象数。我选择的图像中有两个物体(一辆汽车和一名自行车手)。但是,程序返回错误的输出,说有0个对象。有人可以在代码中找到错误吗?谢谢。错误的对象数
代码背后的逻辑是:
1.给出两个输入图像,一个没有对象,一个没有对象。
2.将输入图像从RGB转换为灰度。
3.比较两幅图像并找出差异。
4.将获取的图像转换为二进制。
5.在图像中,只打开面积大于4000的斑点。
6.显示计数和密度。
clc;
MV = imread('car.png'); %To read image
MV1 = imread('backgnd.png');
A = double(rgb2gray(MV)); %convert to gray
B= double(rgb2gray(MV1)); %convert 2nd image to gray
[height, width] = size(A); %image size?
h1 = figure(1);
%Foreground Detection
thresh=11;
fr_diff = abs(A-B);
for j = 1:width
for k = 1:height
if (fr_diff(k,j)>thresh)
fg(k,j) = A(k,j);
else
fg(k,j) = 0;
end
end
end
subplot(2,2,1) , imagesc(MV), title ({'Orignal Frame'});
subplot(2,2,2) , imshow(mat2gray(A)), title ('converted Frame');
subplot(2,2,3) , imshow(mat2gray(B)), title ('BACKGND Frame ');
sd=imadjust(fg); % adjust the image intensity values to the color map
level=graythresh(sd);
m=imnoise(sd,'gaussian',0,0.025); % apply Gaussian noise
k=wiener2(m,[5,5]); %filtering using Weiner filter
bw=im2bw(k,level);
bw2=imfill(bw,'holes');
bw3 = bwareaopen(bw2,5000);
labeled = bwlabel(bw3,8);
cc=bwconncomp(bw3);
Densityoftraffic = cc.NumObjects/(size(bw3,1)*size(bw3,2));
blobMeasurements = regionprops(labeled,'all');
numberofcars = size(blobMeasurements, 1);
subplot(2,2,4) , imagesc(labeled), title ({'Foreground'});
hold off;
disp(numberofcars); % display number of cars
disp(Densityoftraffic); %display number of vehicles
An empty image(of a road) with no objects(vehicles) in it
An image of the same road but with 2 objects(car and cyclist) in it
你试过用'imshow()'或'imagesc()',特别是'm','k','bw','bw2'和'bw3'显示任何中间结果吗? –
感谢队友,它工作!看起来像我在bwareaopen()中保留的区域是一个太大的区域。 – Sabhijiit