如何在掩蔽图像时避免伪影?

2024-04-25 06:36:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个自动编码器来检测图像中的异常。在预处理步骤中,我首先调整图像的大小,然后在图像上应用黑色遮罩,最后形成圆形。示例代码

import cv2
import numpy as np

# center_coords - x & y coordinate of the circle
# RADIUS - radius of the circle

img = cv2.imread(img_path)
(height, width, channels) = img.shape
circle_mask = np.zeros((height, width, channels), dtype=np.uint8)
circle_mask = cv2.circle(circle_mask,center_coord, RADIUS,(255, 255, 255),-1)
       
circle_mask = cv2.bitwise_and(img, circle_mask)
cropped = circle_mask[center_coord[1]-RADIUS: center_coord[1]+RADIUS,
                      center_coord[0]-RADIUS: center_coord[0]+RADIUS]

cv2.imwrite(os.path.join(dst_image), cropped)

现在我使用torchvisions随机旋转360度来增强

from torchvision import transforms

img_transforms= transforms.Compose([
                    transforms.RandomRotation(360),
                    transforms.ToTensor(), 
                    ])

在检查图像的重建误差后,发现拐角和遮罩区域导致重建误差最大的像素

带重建误差的热图

enter image description here

使用的颜色映射:red = high intensity!

enter image description here

高重建误差可能是由于训练图像中的伪影造成的。是否有更好的方法掩盖图像以避免这些伪影,或者这些伪影可能是来自火炬视觉的随机旋转增强的结果