Gopichandh, please post it as an answer below, not a comment, so you might get credit for it. And readthis.
And use the 'bbox', 'crop' option of imrotate() to keep the final image the same size.
That method is okay if the background is perfectly uniform gray level and there are no pixels of that gray level inside the leaf. Otherwise it's not 100% robust, since it rotates the background as well as the leaf, and sets black pixels thatmay be inside the leaf to white.
And see my attached demo if you want to find the farthest points so you know the angle you need to rotate by.
Gopichandh Danala, Thank you for your answer. But I have a problem. When the whole image was rotated, that image was rotated and leaf part became small. But here I want to maintain the leaf size as same as the original image. Is this possible in matlab?
Gopichandh Danica, Thank you for your answer. Suppose that was the correct answer. ButI have a small question? output leaf size was same as the original leaf? Actually I am asking about leaf part only.
I don't think it was correct. Did you see the colored artifacts I mentioned? Anyway, the leaf will be the same size. There is no magnification or demagnification when doing a simple rotation on an image with square pixels.
Thank you for your answer. Code was worked perfectly on rotation which I want to do.
But there is a small issue. When all the black pixels turn into white using this coderotateImg(rotateImg == 0) = 255;* then some leaf parts were also changed the colour.
So is there way to convert only background black pixels only?
Setting black to white won't be totally robust either. What if the stem has black in it? What if the leaf has black in it? What if the stem touches the border, so that setting only black touching the border to white is changed? That would also set the stem to white. You need to define parameters. For example, it appears that the background can now be blue instead of white, and it can have a gradient in color across the image. That will have to be taken into account. You could just find the mean color of the background and set the corner triangles to that color, but you'll have noticeable triangles in the corner because the color won't match the edges everywhere. But it's a good start.
You hopefully looked at my other leaf code in other posts. I'd probably convert to HSV color space and segment out the green by thresholding the saturation, and possibly the hue channel. Then you might need to combine it with a threshold on the V channel to get the dark brown stem. Then combine the two binary images to get a mask of the leaf and stem together. You might have to call imfill() to get rid of any internal holes in the binary image. Then get the mean RGB color of the non-leaf mask so you can fill in the triangles.
So there's a bit more to it if you want to be robust for all kinds of leaves with different colors. Ideally you'd have control over the lighting and snap a background picture. Even better, have an x-rite Color Checker chart in each photo to do color correction on it. You'll need to call functions like imfill(), imclearborder(), bwareafilt(), mean(), etc. A robust program is not going to be some simple 20 line program.
One question, it looks like you're removing the background. Do you want to keep the blue/white shaded background, or just mask it out and make it all white?
Perhaps Gopichandh Danala will do all that for you, and I don't want to jump in if he's going to do it since you asked him specifically. If not, you could open up your question to others.
Image Analyst , Sir, I want just mask it out and make it all white the background.This is what I have done up to now (RotateImageForAnyAngle.m) Can you please suggest me the way to avoid that colour changed pixels inside leaf? code for that operation
Gopichandh Danala, Superb!!!It worked :) Ya all the images have same quality and size so this code is perfectly done the job Thanks for your help and wish you all the best for your future studies :)
Also Image Analyst, thank you for given help to me.
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.