Reputation: 982
When rotating an image using
import skimage
result = skimage.transform.rotate(img, angle=some_angle, resize=True)
# the result is the rotated image with black 'margins' that fill the blanks
The algorithm rotates the image but leaves the newly formed background black and there is no way - using the rotate function - to choose the color of the newly formed background.
Do you have any idea how to do choose the color of the background before rotating an image?
Thank you.
Upvotes: 3
Views: 3749
Reputation: 41
tbh I don't know why everyone set the cval value to a strange value.
I think it's intuitive when you look at the documentation of the function
cval : float, optional
Used in conjunction with mode ‘constant’, the value outside the image boundaries.
Therefore depending on what your input image is, in my case, my picture already used cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
, therefore in order to set it to white, I can simply set cval=255
.
If it's ndarray(img more than 1 channel), you can either rotate each channel separately, or set it to strange value then use np.where
to replace afterward. (idk why but I just don't like this method lol)
Upvotes: 0
Reputation: 21
It's a little annoying that the cval
parameter to skimage.transform.warp()
can only be a scalar float
.
You can set it to any value outside of the range of your data (like -1), and use np.where()
to fix it afterwards. Here's an example that sets the background of a 3-channel image
to red:
output = tf.warp(image, tform, mode='constant', cval=-1,
preserve_range=True, output_shape=(256, 256), order=3)
w = np.where(output == -1)
output[w[0], w[1], :] = [255, 0, 0]
Upvotes: 2
Reputation: 61
Just use cval
parameter
img3 = transform.rotate(img20, 45, resize=True, cval=1, mode ='constant')
img4 = img3 * 255
Upvotes: 6
Reputation: 1103
if your pic is a gray picture,use cval
;
if pic is rgb ,no good way,blow code works:
path3=r'C:\\Users\forfa\Downloads\1111.jpg'
img20=data.imread(path3)
import numpy as np
img3=transform.rotate(img20,45,resize=True,cval=0.1020544) #0.1020544 can be any unique float,the pixel outside your pic is [0.1020544]*3
c,r,k=img3.shape
for i in range(c):
for j in range(r):
if np.allclose(img3[i][j],[0.1020544]*3):
img3[i][j]=[1,0,0] #[1,0,0] is red pixel,replace pixel [0.1020544]*3 with red pixel [1,0,0]
Upvotes: -1
Reputation: 38
I don't know how do it, but maybe this help you
http://scikit-image.org/docs/dev/api/skimage.color.html
Good luck! ;)
Upvotes: -2