Step 7: The Code





B = bwboundaries(img2);
text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
hold on

for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 0.2)

<p>how finding object in picture?</p><p>how much fruit in this picture?</p><p>ttnx</p>
<p>Dear min15,</p><p>I have developed one code for your image.Although it is not perfect, I hope you can get something from this.Keep in touch! Enjoy your strawberries !</p><p>Here is the code for you<br><br>clc;<br>clearvars;<br>close all;<br>imtool close all; % Close all imtool figures.<br>workspace;<br>format longg;<br>format compact;<br>fontSize = 20;<br>tic;<br>% Read image<br>original = imread('F04U2MPIXQG3WQS.LARGE.jpeg');<br>figure,imshow(original)<br>title('Original Image','FontSize',fontSize);<br><br>% RGB component<br>R=original(:,:,1);<br>G=original(:,:,2);<br>B=original(:,:,3);<br>figure,imshow(R)<br>title('Red Component Image','FontSize',fontSize);<br>figure,imshow(G)<br>title('Green Component Image','FontSize',fontSize);<br>figure,imshow(B)<br>title('Blue Component Image','FontSize',fontSize);<br><br>% Thresholding<br>% Select Red compoent<br>level=graythresh(R);<br>binary=im2bw(R,level);<br>figure,imshow(binary)<br>title('Thresholed Image','FontSize',fontSize);<br><br>% Use Morphological Operations <br>erosion=imerode(binary,strel('disk',2));<br>figure,imshow(erosion)<br>title('Eroded Image','FontSize',fontSize);<br><br>% Use Watershed segmentation <br>D = bwdist(erosion);<br>figure<br>imshow(D,[],'InitialMagnification','fit')<br>title('Distance transform of ~bw')<br>D = -D;<br>D(~erosion) = -Inf;<br>L = watershed(D);<br>Lrgb = label2rgb(L,'jet',[.5 .5 .5]);<br>figure<br>imshow(Lrgb,'InitialMagnification','fit')<br>title('Watershed transform of D')<br>figure,imshow(imfuse(erosion,Lrgb))<br>axis([])<br>bw2 = ~bwareaopen(~erosion, 10);<br>imshow(bw2)<br>D = -bwdist(~erosion);<br>figure,imshow(D,[])<br>Ld = watershed(~D);<br>figure,imshow(label2rgb(Ld))<br>bw2 = erosion;<br>bw2(Ld == 0) = 0;<br>mask = imextendedmin(D,2);<br>D2 = imimposemin(D,mask);<br>Ld2 = watershed(D2);<br>bw3 = erosion;<br>bw3(Ld2 == 0) = 0;<br>figure,imshow(erosion);<br>title('Watershed Segmentation','FontSize',fontSize)<br><br>% labelling <br>figure,imshow(original);<br>title('Labelling','FontSize',fontSize)<br>s=regionprops(logical(bw3),'Centroid','Area','Eccentricity','Perimeter');<br>hold on<br>for k=1:numel(s)<br>c=s(k).Centroid;<br>text(c(1),c(2),sprintf('%d',k),...<br>'HorizontalAlignment','center',...<br>'VerticalAlignment','middle');<br>end<br>hold off<br><br>Num = numel(s);<br>message= sprintf('You have counted %i Strawberries',Num);<br>h=msgbox(message);</p>
<p>Dear min15,</p><p>I developed one code for you.Although it is not perfect,I hope you can get something from this.Enjoy it! Please your knowledge and develope it more</p>
<p>I tried it in Matlab. But, the number of objects its detecting is false.</p><p>could u explain a way where I can obtain correct number of detected objects</p>
<p>thank you </p>
Hello!<br>How can i take one frame or image from live streaming video to process it?
<p>hy guys</p><p>i need some help</p><p>i have an image of a scanned txt and i want to do kmean clustering....into 3 clusters of background, upground and dity areas...</p>
<p>thnx buddy</p>
<p>thnx buddy</p>
<p>Thanks you :)</p>
thank you.. a great help for me.. :) .. i only found this easy toturial to do that task after searching .. God bless u dear
good job.. <br>my question: how about vehicle...? how to counting vehicle? <br>here the picture: http://postimg.org/image/m1u3jyqlp/
NIce work done. Like this can u tell how to detect objects in an image and find their locations. It will be very useful.
It is possible that the objects appear overlapped in image. Is it still possible to count their numbers?
My girlfriend had to do something like this for a class using Matlab. Too bad she didn't read this first!
thx bro
nice work
Aditya bhai this is slick......... I am loving it great work
Great work, I think everyone should learn the basics like this! You could also use the watershed filter to pick our objects not so clearly defined. It could be useful to show something like that as an next step for people who want to try it with any blob structures that may touch.
maybe im planning 2 fill those up in my next instructable. Thanx anyway!!
When I first looked at that first image, I thought it looked familiar but I couldn't place it. When I closed out of the browser twenty minutes later, i realized that my desktop has that as the background. That's basically my life for you.
Ya, i could'nt think of anything when i have that on my desktop.
Matlab rules. Nice one.
More people need to know this stuff!

About This Instructable




Bio: Oh, hi there! My name is Aditya Reddy. I am an Electronics Engineer, Software developer and a tech-blogger. You can see more of my works ... More »
More by adityareddy:Image Processing and Counting using MATLAB 
Add instructable to: