what is wrong with my code

조회 수: 1 (최근 30일)
cem yanbul
cem yanbul 2021년 6월 17일
댓글: cem yanbul 2022년 5월 14일
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?
cem yanbul
cem yanbul 2021년 6월 18일
i will

댓글을 달려면 로그인하십시오.

채택된 답변

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