my algorithm doesn't counts exact number of objects. Don't know where am i lacking or whats more to do with this code.

1 view (last 30 days)
clear; RGB= imread('F:\och.jpg'); I= rgb2gray(RGB); imshow(I); figure;imshow(RGB); % % Gradient Magnitude as the Segmentation Function hy = fspecial('sobel'); hx = hy'; Iy = imfilter(double(I), hy, 'replicate'); Ix = imfilter(double(I), hx, 'replicate'); gradmag = sqrt(Ix.^2 + Iy.^2); figure, imshow(gradmag,[]), title('Gradient magnitude (gradmag)')
% % Mark the Foreground Objects se = strel('disk',10,4); Io = imopen(I, se); figure, imshow(Io), title('Opening (Io)')
Ie = imerode(I, se); Iobr = imreconstruct(Ie, I); figure, imshow(Iobr), title('Opening-by-reconstruction (Iobr)')
Ioc = imclose(Io, se); figure, imshow(Ioc), title('Opening-closing (Ioc)')
Iobrd = imdilate(Iobr, se); Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr)); Iobrcbr = imcomplement(Iobrcbr); figure, imshow(Iobrcbr), title('Opening-closing by reconstruction (Iobrcbr)')
fgm = imregionalmax(Iobrcbr); figure, imshow(fgm), title('Regional maxima of opening-closing by reconstruction (fgm)') % now To help interpret the result, superimpose the foreground marker image on the original image. I2 = I; I2(fgm) = 255; figure, imshow(I2), title('Regional maxima superimposed on original image (I2)') % now clean the edges of the marker blobs and then shrink them a bit. You can do this by a closing followed by an erosion. se2 = strel(ones(5,5)); fgm2 = imclose(fgm, se2); fgm3 = imerode(fgm2, se2); % now removes all blobs that have fewer than a certain number of pixels. fgm4 = bwareaopen(fgm3, 20); I3 = I; I3(fgm4) = 255; figure, imshow(I3) title('Modified regional maxima superimposed on original image (fgm4)')
% % % % % % now Compute Background Markers BW = im2bw(Iobrcbr, graythresh(Iobrcbr)); figure, imshow(BW), title('Thresholded opening-closing by reconstruction (bw)')
[B,L,N,A] = bwboundaries(BW);
txt=text(40,40,strcat('\color{green}Objects Found:',num2str(length(B))));
hold on
bt =0;
% wri(:,:,:)=[];
figure; imshow(BW); hold on;
for k=1:length(B),
boundary = B{k};
a=size(boundary);
m=a(1);
if m>200
bt = bt+1;
% wri(bt,:,:)=B{k};
% wri(bt,:)=cat(boundary);
if(k > N)
plot(boundary(:,2),...
boundary(:,1),'g','LineWidth',2);
else
plot(boundary(:,2),...
boundary(:,1),'r','LineWidth',2);
end
end
end
% text(100,100,strcat('\color{blue}Objects Found:',num2str(bt))) % hold on %
figure; [B,L] = bwboundaries(BW,'noholes'); imshow(label2rgb(L, @jet, [.5 .5 .5])) hold on for k = 1:length(B) boundary = B{k}; plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2) end
enclosed_boundaries = find(A(:,m));

Answers (0)

Community Treasure Hunt

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

Start Hunting!