首页 > 解决方案 > 旋转时得到黑白图像?

问题描述

我想将图像旋转 45 度。我已经为它定义了一个函数。当我在我的 RGB 图像上运行它时,它被保存为灰度图像?

def rotateImage(image, angle):
    row,col = image.shape
    center=tuple(np.array([row,col])/2)
    rot_mat = cv2.getRotationMatrix2D(center,angle,1.0)
    new_image = cv2.warpAffine(image, rot_mat, (col,row))
    return new_image


rotate_img_path = '../data/rotate_45_img'
rotate_mask_path = '../data/rotate_45_mask'
real_img_path = '../data/img'
real_mask_path = '../data/mask'


for img in os.listdir(real_img_path):
    edge_img = cv2.imread(os.path.join(real_img_path,img))
    edges = rotateImage(edge_img, 45)
    cv2.imwrite(os.path.join(rotate_img_path, img), edges)
print("Finished Copying images")

for img in os.listdir(real_mask_path):
    edge_img = cv2.imread(os.path.join(real_mask_path,img))
    edges = rotateImage(edge_img, 45)
    cv2.imwrite(os.path.join(rotate_mask_path, img), edges)
#     cv2.imwrite('edge_' + '.jpg', edges)
print("Finished Copying masks")

标签: rgbcv2image-rotation

解决方案


这里的线索可能是

row,col = image.shape

image如果是三维的(即,具有 B、G 和 R 的单独通道的颜色)而不是二维(即灰度),则会引发错误。除非您使用此处未显示的代码读取 RGB 图像,否则这表明您的图像已经是灰度的。


推荐阅读