How to extract 4 portions of main object of image with black background

Hi,
I have an image with a black background. I would like to extract 4 portions of the main object of the image, a leaf (as I show in the image B.png). I would like to extract 4 portions as matrixes of 100 x 100 pixels. I thought one way to do that it would be to split the leaf in 4 quadrants and then extract 1 portion of each quadrant.
The problem is that I need to do it without considering that black background of the image. So, when extracting those portions of the image, I don't want to get those black pixels too, only parts that are inside the leaf. Is this possible?
I need those portions to do a texture analysis with a Local Binary Pattern code I've developed.
I've attached both images: the original (A.png) and and example of what I need (B.png).
I really appreciate your help. Thank you in advance!

 Accepted Answer

Just extract the parts you want with indexing
rect1 = grayImage(row1:row1+99, col1:col1+99);
rect2 = grayImage(row1:row1+99, col2:col2+99);
rect3 = grayImage(row2:row2+99, col1:col1+99);
rect4 = grayImage(row2:row2+99, col2:col2+99);
Just define row1, row2, col1, and col2. Just put your leaf in the middle of the field of view. If, for some reason, you're sloppy at laying down the leaves, then you can find the centroid with regionprops() and then use imtranslate() to shift it to the middle where you can then use your predefined template rectangle locations like I showed you above.

7 Comments

Hi, Image Analyst.
First of all, thank you for your answer, it made me learn about the rectangles. Meanwhile, I found a very good code that get's the center of mass in the leaf (it works perfectly for me, so I can get the row and column of the leaf's center.
Can you please give me an idea about how can I make sure my portion selection (rectangles) won't get any part of those background black pixels? I'm trying to develop it for a set of images that have a different size each one, so I'm working on a code that will work for all of them.
I've attached the code that gets the center of mass and also an image that shows how it works fine.
Thank you very much for your help!
Unless you have a rectangular leaf, there will be background pixels in there. Why does that matter anyway? Even if you use that code to get the WeightedCentroid (which I don't recommend, I recommend you use MATLAB's regionprops for that), the pixels are zero (since they've been masked) so those pixels won't contribute to the weighted centroid at all.
Hi, Image Analyst
You are right and I agree with you. Here it's my explanation about it: I don't want those black pixels because I'm going to run a LBP (Local Binary Pattern) on the image to extract some texture information. After that, I will run another code to calculate similarity between images.
I talked to my teacher last week about those black pixels 'cause I didn't think they would impact so much on final result (as they are zero), but then he told me I should really care about them and try to extract only portions inside the leaf. This has been a pain, but I keep trying.
Do you have any ideas? Sorry I keep bothering you with this, but I just want to make sure only rectangles inside the leaf will be extracted, without the need of setting the rectangles for each image manually.
Thank you again for you answer.
Yes, he's right. So when you're making your local binary pattern, if you encounter a gray level that's exactly zero in the ring of 8 surrounding pixels, just bail out and move on to the next pixel.
Thank you, Image analyst. I've talked to my teacher and we decided not to extract the portions automatically as this could take us away from our project focus. Now I'm going to research a way to develop a matlab interface where a person could select some parts of the leaf and then this parts will be used to compare to another leaves parts. Thank you again!
Well somehow you're going to need to decide whether a pixel is a leaf pixel or a background pixel. As you scan the image, you could perhaps use local binary pattern like you said first, though that would not be my choice (perhaps just because I'm not that familiar with it) but you can also look at local things like mean colors, standard deviations, etc. You've never yet said what your overall goal is. So sure, you can measure a bunch of parameters/characteristics/attributes at each pixel, or of the image as a whole, but then what? I'm sure you're not done then. What are your plans after that? Determine a "health index" of the leaf? Determine the species of tree it came from? What?
Sorry I didn't answer you before, I didn't know you had one more answer for me (found out it now). Well, In fact I already know what's the leaf's disease (all of my images are from the same kind of plant). My project objectives are: check the similarity between leafs and then, yes, determine how much "sick" a leaf is (I have a leaf group which has been classified according to a "health index"). There is a group in my country that checks this currently, but the process is totally manual, so it takes a lot of time. If I'm able to reduce the manual steps, I'm going to help them a lot. So, now (at least in my mind), I have to find out how to split a leaf in for quadrants (my teacher's suggestion), then get four parts from the image (one from each quadrant), put them togeter, extract LBP vectors for each "new" image and finally run a distance function (euclidean, for example) to check their similarity. After all those steps, I'm gonna try a new proposed re-ranking method to improve the rate of effectiveness of the similarity rank that was created. The last method I'll try aggregates several rankings to get the best one (those methods are part of a recent paper I've been reading). So, yes, I still have a lot to do. But I'm thankful, your files and answers have been helping me a lot.

Sign in to comment.

More Answers (0)

Categories

Find more on Agriculture 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!