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

时间:2016-09-27 16:43:30

标签: python scikit-image

使用

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

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

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

谢谢。

4 个答案:

答案 0 :(得分:4)

只需使用cval参数

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

答案 1 :(得分:0)

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

http://scikit-image.org/docs/dev/api/skimage.color.html
祝你好运! ;)

答案 2 :(得分:0)

如果您的照片是灰色图片,请使用cval; 如果pic是rgb,没有好办法,打击代码可以工作:

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]

答案 3 :(得分:0)

cval的{​​{1}}参数只能是skimage.transform.warp()有点烦人。

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

image