I want to detect leaf shape by line tracing.

Hi, I am trying to mask leaf shapes using Matlab.
I reached to the stage where I can detect the difference between leaf / non-leaf by pixel (as you can see in the figure),
but I can't find a way to mask the whole leaf area.
Yes - the masking isn't perfectly done - but this is the best masking I can do right now.
The concept I am thinking about is making a leaf figure by line tracing, and masking the whole pixel inside the lines.
I've tried several ways, but can't find a way to mask the leaf.
(if possible, I want to divide three leaves in seperate masking - but if not, adding a straight line between the leaves is also possible.)
openfig Q;

2 Comments

Matt J
Matt J on 27 Feb 2025
Edited: Matt J on 27 Feb 2025
I don't really understand the difference between,
"detect the difference between leaf / non-leaf by pixel "
and
"mask the whole leaf area"
The very definition of a mask is an image that shows the difference between one region and another, which you clearly have created because that's what your figure shows.
Aside from that, it is not clear what help you are looking for. You have shown us no code or input data for us to recommend changes to.
@Matt J Thank you for your advice, I have uploaded additional content to make my question clear.

Sign in to comment.

 Accepted Answer

Image Analyst
Image Analyst on 27 Feb 2025
Edited: Image Analyst on 27 Feb 2025
Do a search of this forum for the tag "leaf". There are lots of examples. Basically you can probably segment the code by color (green) and then fill the mask with imfill and then call bwboundaries. Use the Color Thresholder on the Apps tab of the tool ribbon.
Why do you want to do this? Did someone hire you to do this for some reason? Is it your homework? Some other reason?
Post your original image if you need more help, or at the very least attach your binary image (not the pseudocolored one like you did). We'd rather have a PNG image file than a fig file. Make it easy for us to help you, not hard.
To split the full leaf into parts, you might take a look at watershed, superpixels, or grabcut.

5 Comments

Thanks for your recommendation.
It's the first time I'm asking questions for Matlab, and I found out that there are some missing information on my question.
First, it is not possible for me to get RGB images for the leaf, since the image is taken in a special condition.
The only information I can get is a set of numerical value of each pixel, and if filtered and run by imagesc, we can get the shape of the image, shown in the original question.
condition_3 = result_2 > 0.3;
result_3 = NaN(700, 900);
result_3(condition_3) = result_2(condition_3);
figure();
imagesc(result_3);%caxis([]) range for the CF
caxis([0 0.2]);
title("Shaping?");
colorbar_handle_CF = colorbar;
This is the last code lines I used - so I have done masking to detect the difference between 'leaf' (The pixels that have value) - 'non leaf' (I changed these pixels to NaN).
If a certain pixel passed three conditions(condition_1, 2, 3), it will be sorted as 'leaf' (and maintain its value) - if not, it will be sorted as 'non-leaf' (and change its value to NaN). The final result is named as 'result_3'.
So what I want to get help is, detecting a leaf shape based on the image I got by masking, and select every pixel that is inside that shape - for further study and processing. Since there are lots of different leaf images that are taken in the same condition, I want to make a pipeline for image processing.
Below I put an image that shows what I am trying to do.
So basically I want to get three leaf image shape detection (as shape_1, shape_2, shape_3), and I tried some tools such as line tracing, but couldn't figure it out.
The reason I want to get the image from the right is because I want to get rid of stems or other trifoliolates, and fill up the empty pixels that is "actually inside" the leaf shape. (Those pixels didn't make the condition, but are actually leaves, so we should put them back to the masking for further processing.)
The shaping doesn't have to be perfect, since the shape masking only requires an equal standard for each image.
Any coding besides that shape-making can be done by myself, and it will be helpful if you can recommend me a way to figure it out. Thank you again for your help, and I also welcome further questions.
Can you post the actual data that you put into the call to imagesc() in a .mat file? It will be easier than dealing with a figure or screenshot.
Also, did you try my first suggestions of superpixels, watershed and imfill, etc.?
You could try morphological operations like imopen to get rid of small tendrils sticking out of a larger shape.
Here I uploaded the actual data in .mat file.
I tried superpixels and imfill, since I thought watershed should be applied after leaf shape masking.
I came up to a code where I can designate the superpixels as leaves by clicking manually, and the accuracy was quite good.
The problem is that it takes a long time to click every pixels, since i had to make at least 1000 superpixels for leaf border classification. (I don't know if I am using superpixels correctly, but in my opinion, less than 1000 pixels can't classify the borders correctly.)
I used imfill for improving the image before putting into superpixels, which worked quite well.
If you could give me comments about superpixels or automation on imaging, I am pleased to hear more about it. Thank you.
I don't know what you're clicking. I just call it and tell it some parameter and it does it automatically, like
L = superpixels(rgbImage, 30);
imshow(L, []);
axis on image
I made an upgrade code using superpixels, and it seems it's quite working well, regarding its accuracy.
Thanks for your help! Your comments were useful on image analyzing.

Sign in to comment.

More Answers (0)

Asked:

on 27 Feb 2025

Commented:

on 5 Mar 2025

Community Treasure Hunt

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

Start Hunting!