You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to detect boundaries of a plant's leaves in an image?
8 views (last 30 days)
Show older comments
I need to detect boundaries of a plant's leaves in an image. But i dont know where to start and which way i should follow.
3 Comments
Image Analyst
on 19 Jan 2021
Halil has not been seen here in over 3 years so you probably will not get a response here.
Accepted Answer
Image Analyst
on 3 Dec 2017
Use the code there to get a binary image. Then call bwboundaries:
boundaries = bwboundaries(binaryImage);
20 Comments
najwa anuar
on 7 Dec 2017
Hi, I have the binary image for my plants. And I need help in putting boundaries on each leaves. However, the leaves is connected and using bwboundaries is reading two leaves as one leaf. Can you suggest an easier way to help me. Also, is there any way I can scribble the image without creating a GUI? Thank you
Image Analyst
on 7 Dec 2017
If you want to hand trace the leaves, see the attached demos.
If you want to split apart your original binary image, see this link: http://blogs.mathworks.com/steve/2013/11/19/watershed-transform-question-from-tech-support/
Keerthi D
on 26 Jun 2020
It will give good result. But l am tried to change the image(leaf image) to do like this.Then l got the binary mask of the selected region and some errors
Image Analyst
on 28 Jun 2020
% Demo to have the user freehand draw an irregular shape over a gray scale image.
% Then it creates new images:
% (1) where the drawn region is untouched inside the region and all black outside the region.
clc; % Clear command window.
% clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = pwd; % Determine where demo folder is.
baseFileName = 'imgsp1466.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Ask user to draw freehand mask.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand(); % Actual line of code to do the drawing.
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
% Now make it smaller so we can show more images.
subplot(3, 1, 1);
imshow(grayImage, []);
axis on;
drawnow;
title('Original gray scale image', 'FontSize', fontSize);
% Display the freehand mask.
subplot(3, 1, 2);
imshow(binaryImage);
axis on;
title('Binary mask of the region', 'FontSize', fontSize);
% Calculate the area, in pixels, that they drew.
numberOfPixels1 = sum(binaryImage(:))
% Another way to calculate it that takes fractional pixels into account.
numberOfPixels2 = bwarea(binaryImage)
% Mask the image outside the mask (make it black outside the white mask region), and display it.
% Will keep only the part of the image that's inside the mask, zero outside mask.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
blackMaskedImage = bsxfun(@times, grayImage, cast(binaryImage, 'like', grayImage));
subplot(3, 1, 3);
imshow(blackMaskedImage);
axis on;
title('Masked black outside region', 'FontSize', fontSize);
Manoj Prasad S.G
on 24 Oct 2020
the above what you have mention the code isn't working. Its showing error.
Image Analyst
on 24 Oct 2020
It's only for you Manoj. And you forgot to tell us the error. I just copied and pasted the code and ran it, and it ran fine -- no errors at all. You must have modified it somehow. Please attach your code, the image you used it on, and your error message.
Manoj Prasad S.G
on 26 Oct 2020
Sir actually now its working but i need a code that where it as to make the segmentation by itself without asking us to select the edges or draw line on the edges and we need the output like how you have shown.
Image Analyst
on 26 Oct 2020
That will be difficult if your images look like that. If your images are of a single leaf against a uniform background, then that is easy - just use the Color Thresholder App on the Apps tab of the tool ribbon.
Manoj Prasad S.G
on 27 Oct 2020
Sir for the above image Segmentation should be done automatically. I need a code for that and where you have to merge that code with the previous code.
Image Analyst
on 27 Oct 2020
Use the Color Thresholder app on the Apps tab of the tool ribbon. Then you can export code from there.
Manoj Prasad S.G
on 28 Oct 2020
Sir after clicking on colour threshold and exporting code from that but applying this code also again it would be asking me to draw on the edges.
this the code
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 28-Oct-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 76.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 255.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 255.000;
% 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
I attached this at the end of this below code:
% Demo to have the user freehand draw an irregular shape over a gray scale image.
% Then it creates new images:
% (1) where the drawn region is untouched inside the region and all black outside the region.
clc; % Clear command window.
% clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = pwd; % Determine where demo folder is.
baseFileName = 'C:\Users\Manoj Prasad\Downloads\SHAREit\vivo 1601\photo\betel.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Ask user to draw freehand mask.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand(); % Actual line of code to do the drawing.
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
% Now make it smaller so we can show more images.
subplot(3, 1, 1);
imshow(grayImage, []);
axis on;
drawnow;
title('Original gray scale image', 'FontSize', fontSize);
% Display the freehand mask.
subplot(3, 1, 2);
imshow(binaryImage);
axis on;
title('Binary mask of the region', 'FontSize', fontSize);
% Calculate the area, in pixels, that they drew.
numberOfPixels1 = sum(binaryImage(:))
% Another way to calculate it that takes fractional pixels into account.
numberOfPixels2 = bwarea(binaryImage)
% Mask the image outside the mask (make it black outside the white mask region), and display it.
% Will keep only the part of the image that's inside the mask, zero outside mask.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
blackMaskedImage = bsxfun(@times, grayImage, cast(binaryImage, 'like', grayImage));
subplot(3, 1, 3);
imshow(blackMaskedImage);
axis on;
title('Masked black outside region', 'FontSize', fontSize);
Image Analyst
on 1 Nov 2020
Take out the code that asks you to draw on the image and replace it with a call to createMask().
If you need more help, please start your own question so we don't keep bugging Halil with emails about your problem. Remember to attach both your code and your image there in that new thread.
Abdullah Mazin
on 28 Jan 2022
Hellow Mr.
I wish with good healthy. Can u help me with extract feature from this type of image (palms) and remove the background and detect every leaf of palms healthy or unhealthy!!
Thank u for any thing.
Image Analyst
on 28 Jan 2022
@Abdullah Mazin try stdfilt() on the color channel with the most contrast.
If you still need more help, start a new question. It's been 5 years so we probably don't want to bother @Halil Ibrahim Bozoglu anymore with your question.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)