rgb - 旋转时得到黑白图像?
问题描述
我想将图像旋转 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")
解决方案
这里的线索可能是
row,col = image.shape
image
如果是三维的(即,具有 B、G 和 R 的单独通道的颜色)而不是二维(即灰度),则会引发错误。除非您使用此处未显示的代码读取 RGB 图像,否则这表明您的图像已经是灰度的。
推荐阅读
- java - 读取 java 文件的最简单方法 - 是否有比 JSON 更简单的替代方法
- android - 如何导航回导航抽屉
- excel - 根据部分字符串匹配更改字体颜色到列表
- c - 通过 SPI 使用 STM32F030R8 控制 AMIS-30543
- r - 合并相同的日期
- php - WordPress pre_cache_alloptions 过滤器应该在哪里注册?
- python - Dask - 如何连接到正在运行的集群调度程序并访问“total_occupancy”?
- r - 绘制 RandomFields 包中的对象时出现奇怪的蓝色字幕
- xquery - 如何在 MarkLogic 中使用 XQuery 创建 UTC 时间戳?
- javascript - Discord.js 故障排除 - 特定机器人命令的奇怪行为