首页 > 解决方案 > 如何在结果中删除旋转和蒙版图像周围的黑色边框?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

原始种子图像 旋转种子图像

标签: python-2.7opencvimage-processingimage-rotationimutils

解决方案


您可以使用此代码进行轮换:

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()

推荐阅读