I want to rotate image like this
I have rotated Image but I have got image like this in matlab
How to fix this problem in matlab Actually I want only rotate leaf part only not whole image. Is it posibble

4 commentaires

Gopichandh Danala
Gopichandh Danala le 16 Sep 2016
Modifié(e) : Gopichandh Danala le 16 Sep 2016
A simple trick is to change all the pixels other than the segment to a white background in your case.
I cropped your main image as u didn't post original image
and sample code is here:
leaf = imread('leaf.jpg');
figure, imshow(leaf, []);
rotateImg = imrotate(leaf,45); % rotate by 45 or 315 degrees
figure,
subplot(1,2,1)
imshow(rotateImg, [min(min(rotateImg(:))) max(max(rotateImg(:)))]);
newrotateImg = rotateImg;
newrotateImg(newrotateImg == 0) = 255; % make all 0 pixels to 255..
subplot(1,2,2)
imshow(newrotateImg, [min(min(newrotateImg(:))) max(max(newrotateImg(:)))]);
Hope it helps,
Image Analyst
Image Analyst le 16 Sep 2016
Modifié(e) : Image Analyst le 16 Sep 2016
Gopichandh, please post it as an answer below, not a comment, so you might get credit for it. And read this.
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 that may 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
Gopichandh Danala le 16 Sep 2016
Thanks image analyst i will do as u suggested from now..
Malan Jayanka
Malan Jayanka le 18 Sep 2016
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?

Connectez-vous pour commenter.

 Réponse acceptée

A simple trick is to change all the pixels other than the segment to a white background in your case.
I cropped your main image as u didn't post original image
and sample code is here:
leaf = imread('leaf.jpg');
figure, imshow(leaf, []);
rotateImg = imrotate(leaf,45,'crop'); % rotate by 45 or 315 degrees
figure, %imshow(rotateImg);
subplot(1,2,1)
imshow(rotateImg, [min(min(rotateImg(:))) max(max(rotateImg(:)))]);
newrotateImg = rotateImg;
newrotateImg(newrotateImg == 0) = 255; % make all 0 pixels to 255..
subplot(1,2,2)
imshow(newrotateImg, [min(min(newrotateImg(:))) max(max(newrotateImg(:)))]);
Hope it helps.

12 commentaires

Image Analyst
Image Analyst le 17 Sep 2016
Modifié(e) : Image Analyst le 17 Sep 2016
imshow(rotateImg, [min(min(rotateImg(:))) max(max(rotateImg(:)))]);
is more simply done as
imshow(rotateImg, []);
And like I said in my first comment, you can see some artifacts with this approach due to the line
newrotateImg(newrotateImg == 0) = 255;
which gives rise to funny colored spots in the output image. Let me know if you want to avoid that.
Malan Jayanka
Malan Jayanka le 18 Sep 2016
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.
Image Analyst
Image Analyst le 18 Sep 2016
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.
Malan Jayanka
Malan Jayanka le 18 Sep 2016
Modifié(e) : Image Analyst le 18 Sep 2016
Gopichandh Danala,
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 code rotateImg(rotateImg == 0) = 255;* then some leaf parts were also changed the colour.
So is there way to convert only background black pixels only?
The before image
The after image:
This is the original image:
Image Analyst
Image Analyst le 18 Sep 2016
Modifié(e) : Image Analyst le 18 Sep 2016
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.
Malan Jayanka
Malan Jayanka le 18 Sep 2016
Modifié(e) : Malan Jayanka le 18 Sep 2016
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
Gopichandh Danala le 19 Sep 2016
Hi Malan,
I am also a student and i would love to try what Image Analyst suggested above and will post answer when i figure it out
Gopichandh Danala
Gopichandh Danala le 19 Sep 2016
Modifié(e) : Gopichandh Danala le 19 Sep 2016
Here is the code i wrote for this It works for your requirement without changing the inside pixels
cd 'D:\MATLAB ANSWERS';
leaf = imread('leaf.jpg');
rotateImg = imrotate(leaf,45,'crop'); % rotate by 45 or 315 degrees
figure,imshow(rotateImg);
r = rotateImg(:,:,1);
g = rotateImg(:,:,2);
b = rotateImg(:,:,3);
newrotateImg = rotateImg;
[rows cols] = size(leaf(:,:,1));
for i = 1:rows
for j = 1:cols
if( r(i,j) == 0 && g(i,j) == 0 && b(i,j) == 0)
newrotateImg(i,j,1) = 255;
newrotateImg(i,j,2) = 255;
newrotateImg(i,j,3) = 255;
end
end
end
figure, imshow(newrotateImg,[]);
The only suggestion i give to you is the quality and size of the images if they are all not constant the imrotate function angle cannot be fixed..
Let me know if this is ok for your requirement
Malan Jayanka
Malan Jayanka le 21 Sep 2016
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.
Gopichandh Danala
Gopichandh Danala le 21 Sep 2016
Happy to hear that it works for your requirement..
Thanks and you too
Busy Bee
Busy Bee le 10 Fév 2018
what if the image segment that I want to rotate is itself made of black pixels?
Image Analyst
Image Analyst le 10 Fév 2018
What if you showed it to us?

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by