python-2.7 - 如何在结果中删除旋转和蒙版图像周围的黑色边框?OpenCv Python
问题描述
我的任务是为深度学习生成数据。我拍摄种子图像,旋转它们并在背景上随机绘制它们。问题是旋转导致图像周围出现虚线边界,我无法弄清楚它出现的原因或如何摆脱它。
def rotateSeed(img):
rotated = imutils.rotate_bound(img, randint(0,360))
for row in range(rotated.shape[0]):
for col in range(rotated.shape[1]):
if (rotated[row,col,0] == 0) and (rotated[row,col,1] == 0) and (rotated[row,col,2] == 0):
rotated[row,col] = default[0,0]
return rotated
代码说明:默认为种子图片中的背景色。旋转产生一个我用默认值覆盖的黑色区域。
只有一个人有这个问题,解决方案并没有解释太多。它甚至没有旋转:OpenCV
解决方案
您可以使用此代码进行轮换:
import cv2
import numpy as np
img = cv2.imread('C:\\Code\\1.jpeg')
num_rows, num_cols = img.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), 30, 1)
img_rotation = cv2.warpAffine(img, rotation_matrix, (num_cols, num_rows))
cv2.imshow('Rotation', img_rotation)
cv2.waitKey()
推荐阅读
- javascript - 如何为我用 JavaScript 编写的这个 HTML 生成器添加适当的缩进
- regex - Perl - 如何从文本文件中省略行?
- elasticsearch - Elastic Search 多个 AND + OR 查询
- python - Python - 如何在 Excel 中过滤掉数据
- angular - 在以角度运行 compodoc 期间如何修复错误?
- javascript - 在 Plot.ly for JavaScript 中为条形图添加链接
- progressive-web-apps - 尝试加载时 Service Worker 获取错误 --- 我的网站?
- c - 有无限循环,因为 scanf() 不会停止程序从用户那里获取条目
- c++ - 为什么我在一个变量声明行上出现这么多错误
- wordpress - WooCommerce:如何更改产品的特色(主要)图像,(仅)从产品库图像中选择?