How to detect and count kiwi fruit on a tree?

3 views (last 30 days)
I need to make a matlab program that can count kiwi fruits on a tree. How can I do this? Are there are any examples, codes, tutorials? Ccan you please help me?

Answers (2)

Image Analyst
Image Analyst on 23 May 2021
Edited: Image Analyst on 23 May 2021
Click on the fruit tag on the right hand side of this page. You'll probably see examples where the Color Thresholder App on the Apps tab of the tool ribbon is used.
Do your images look like this:
Try LAB color space to segment out brown fruit. Anything else will be sky or leaves. Then you can get an idea of the harvest potential by computing the area fraction of the brown. You can calibrate by harvesting a few scenes and making a calibration curve relating area fraction to number of individual fruits, if you insist on an individual fruit count (which is actually probably not needed).
% Demo by Image Analyst, May, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'kiwis.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image full size.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
%--------------------------------------------------------------------------------------------------------
% Threshold image.
[mask, maskedRGBImage] = createMask(rgbImage);
% Display mask image.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
title('Initial Color Segmentation Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display masked image of kiwis
subplot(2, 2, 3);
imshow(maskedRGBImage, []);
axis('on', 'image');
title('Initial Color Segmentation Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display masked image of leaves and sky
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
leaves = bsxfun(@times, rgbImage, cast(~mask, 'like', rgbImage));
subplot(2, 2, 4);
imshow(leaves, []);
axis('on', 'image');
title('Leaves and Sky', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
fprintf('Done running %s.m\n', mfilename);
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
  9 Comments
Mohandis Bencaus
Mohandis Bencaus on 30 May 2021
Edited: Mohandis Bencaus on 30 May 2021
@Image Analyst sir how can i calculate area fraction of the brown and create table like you said. I have to make this matlab program.
Image Analyst
Image Analyst on 30 May 2021
Try this manual counting program. Adapt as needed.
% Demo to manually count items in an image by clicking on them.
clc; % Clear command window.
fprintf('Running %s.m ...\n', mfilename);
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
% Specify the folder where the files live.
myFolder = pwd; %'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, 'kiwi*.jp*'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
g = gcf;
g.WindowState = 'maximized';
again = true;
count = 1;
x = [];
y = [];
while again
if count == 1
% Give user instructions.
promptMessage = sprintf('Click on one kiwi');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
count = 0;
break; % or break or continue.
end
end
p = drawpoint('Color', 'y');
x(count) = p.Position(1);
y(count) = p.Position(2);
caption = sprintf('Counted %d so far', count);
title(caption, 'FontSize', 18);
promptMessage = sprintf('Locate another kiwi?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
again = false;
break; % or break or continue.
end
count = count + 1;
end
% Save the x and y for this particular image.
xy{k} = [x(:), y(:)];
end
message = sprintf('You counted %d items in this image', count);
uiwait(helpdlg(message));
fprintf('Done running %s.m\n', mfilename);

Sign in to comment.


DGM
DGM on 23 May 2021
Edited: DGM on 23 May 2021
Let us assume the subject is a spherical red kiwi tree in an evenly illuminated vacuum.
totally realistic
You might begin your attempt by choosing a color space within which the color properties of the objects of interest are most easily seperable from those of the background. If you have Image Processing Toolbox, see the Color Thresholder app. The rest is simply logical comparison.
inpict = imread('tree2.png');
labpict = rgb2lab(inpict);
% Define thresholds based on observation of histogram
Lrange = [50 70];
Arange = [70 79];
Brange = [60 70];
% Create mask based on a logical combination of thresholds
mask = (labpict(:,:,1) >= Lrange(1) ) & (labpict(:,:,1) <= Lrange(2)) & ...
(labpict(:,:,2) >= Arange(1) ) & (labpict(:,:,2) <= Arange(2)) & ...
(labpict(:,:,3) >= Brange(1) ) & (labpict(:,:,3) <= Brange(2));
% identify connected groups and return just the count
[~,numberoffruits] = bwlabel(mask)
The script identifies 11 fruits in the image.
It's worth pointing out that there are actually 20 fruits on the tree. As this illustrates, practical applications often have trivial complications which require non-trivial efforts to accomodate. Your task may be far more challenging than this oversimplified example.
  1 Comment
Mohandis Bencaus
Mohandis Bencaus on 24 May 2021
Edited: Mohandis Bencaus on 25 May 2021
@DGM Thank you for explaining. How can apply this to kiwifruit on tree.

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!