Skimage :旋转图像并填充新形成的背景

Skimage : rotate image and fill the new formed background

使用

旋转图像时
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

该算法旋转了图像,但使新形成的背景保持黑色,并且无法使用旋转功能来选择新形成的背景的颜色。

您知道如何在旋转图像之前选择背景颜色吗?

谢谢。

我不知道怎么做,但也许这对你有帮助

http://scikit-image.org/docs/dev/api/skimage.color.html

祝你好运! ;)

如果你的图片是灰色图片,使用cval; 如果图片是 rgb,没有好办法,吹代码有效:

path3=r'C:\Users\forfa\Downloads11.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]

只需使用cval参数

img3 = transform.rotate(img20, 45, resize=True, cval=1, mode ='constant')
img4 = img3 * 255

skimage.transform.warp()cval参数只能是scalar float.

有点烦人

您可以将其设置为数据范围之外的任何值(例如 -1),然后使用 np.where() 对其进行修复。这是将 3 通道 image 的背景设置为红色的示例:

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]