How to detect and count kiwi fruit on a tree?
1 view (last 30 days)
Show older comments
I need to make a matlab program that can count kiwi fruits on a tree. It dont need to count perfectly.
I just need a matlab program that takes kiwi fruit tree image and count kiwis and give me a number. It dont need to be perfect.
Example inputs:


I have code like this, i get it from here and little bit modifed.
folder = [];
baseFileName = 'kiwis.jpg';
fullFileName = fullfile(folder, baseFileName);
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
[mask, maskedRGBImage] = createMask(rgbImage);
% Sobel filter code start %
B= rgb2gray(maskedRGBImage);
C=double(B);
for i=1:size(C,1)-2
for j=1:size(C,2)-2
% Sobel Mask for x direction:
Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
% Sobel Mask for y direction:
Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
%gradyan of image
B(i,j)=sqrt(Gx.^2+Gy.^2);
end
end
% Sobel filter code end %
figure,imshow(maskedRGBImage);
figure,imshow(B);
figure,imshow(U);
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 22-May-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2lab(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 1.176;
channel1Max = 79.447;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -4.567;
channel2Max = 31.611;
% Define thresholds for channel 3 based on histogram settings
channel3Min = -5.773;
channel3Max = 70.934;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
This is output of this program.

I am newbie, can you guys help me please.
0 Comments
Answers (0)
See Also
Categories
Find more on Medical Physics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!