Taline
Taline

Reputation: 59

How can i rotate an image on matlab without getting the black background

Checking https://www.mathworks.com/help/matlab/matlab_prog/run-sections-of-programs.html i can use imrotate but i will get an image with a black background like this:

enter image description here

and i want an image more like this:

enter image description here

basically, i don't want to add any black or white pixels or any pixels at all i just want my image to rotate without any additional background.

Kindly note that when i display the second image on matlab it also shows with black background and a bunch of 0 pixels

Upvotes: 0

Views: 584

Answers (1)

enrico_ay
enrico_ay

Reputation: 76

I think you could use

J = imrotate(I,angle,method,'loose')

To save in J the full image (containing the 0 pixels at the border). Then using rgb2ind (https://it.mathworks.com/help/matlab/ref/rgb2ind.html) or bwconncomp (https://www.mathworks.com/help/images/ref/bwconncomp.html) you can identify the connected components which enclose the image (there should be 4): in this way you can change the color of those pixels in white To avoid having 4 components you could firstly add one black rectangle yourself, so that the image J will be completely enclosed by black pixels

Upvotes: 1

Related Questions