what is wrong with my code

1 view (last 30 days)
cem yanbul
cem yanbul on 17 Jun 2021
Commented: cem yanbul on 14 May 2022
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 Comments
Image Analyst
Image Analyst on 18 Jun 2021
I doubt that would work. Did you try the way I suggested below?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 17 Jun 2021
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!