what is wrong with my code

3 次查看(过去 30 天)
1-Write the program that determines the number of balloons in the picture below and the green rectangle around each balloon in the picture.
Bal=imread('ballons.jpg');
B=Bal(:,:,3);
d1x = [1 0 -1];
d1y = [1;0-1];
Bx = conv2(B,d1x,'same');
By = conv2(B,d1y,'same');
J = sqrt((Bx.^2)+(By.^2));
K = J>25;
L = imclearborder(K);
K=edge(K, 'Canny');
M = imfill(L,'holes');
N = bwareaopen(M,350);
figure, imshow(Bal);
stats = regionprops(N, 'BoundingBox', 'Centroid');
hold on;
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','g','LineWidth',2)
plot(bc(1),bc(2), '')
end
hold off
area = sum(sum(N))
O = bwmorph(N,'remove');
perimeter = sum(sum(O))
[B,L] = bwboundaries(N,'noholes');
hold on
[row,col] = find(N==1);
[a,b] = size(N);
mask = false(a,b);
mask(min(row):max(row),min(col):max(col)) = 1;
mask = bwperim(mask,8);
mask = imdilate(mask,strel('square',4));
R = Bal(:,:,1);
G = Bal(:,:,2);
B = Bal(:,:,3);
R(mask) = 255;
G(mask) = 255;
B(mask) = 0;
RGB = cat(3,R,G,B);
hsv=rgb2hsv(Bal);
v=hsv(:,:,3);
range=[50 600]/750;
mask=(v>range(1)) & (v<range(2));
mask=imcomplement(mask);
se=strel('disk',2);
mask=imerode(mask,se);
mask=bwareaopen(mask,20);
[info, total]=bwlabel(mask);
mrk=regionprops(mask,'centroid');
XY=cat(1,mrk.Centroid);
hold on;
title(['All balons numbers : ',num2str(total)]);
B= double(rgb2gray(Bal));
d1x = [-1 0 1];
d1y = [-1;0;1];
Bx = conv2(B,d1x,'same');
By = conv2(B,d1y,'same');
J = sqrt((Bx.^2)+(By.^2));
K = J>50;
L = imclearborder(K);
M = imfill(L,'holes');
N = bwareaopen(M,6000);
[row,col] = find(N==1);
  3 个评论
Image Analyst
Image Analyst 2021-6-18
I doubt that would work. Did you try the way I suggested below?

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-6-17
I'd start by using the Color Thresholder in HSV space to find green, then mask out the grass. Then I'd convert to HSV color space and look for blobs that have high S value. Use imclose() on the mask to merge together blobs that are not quite touching though they should be. Use bwareafilt() to get only those in a certain size range. Then call regionprops(mask, 'BoundingBox') and finally put in a loop where you call rectangle().
Attach your code if you need any more help.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by