error: (-215:Assertion failed) !_map1.empty() in function 'remap'

error: (-215:Assertion failed) !_map1.empty() in function 'remap'

回溯错误:

Traceback (most recent call last):
  File "/home/ali/Dev/deepfake/train.py", line 66, in <module>
    warp_A, tar_A = train_util.training_data(train_setA, batch_size)
  File "/home/ali/Dev/deepfake/image.py", line 165, in training_data
    warped_img, target_img = self.warping_image(image)
  File "/home/ali/Dev/deepfake/image.py", line 82, in warping_image
    warped_image = cv2.remap(images, inter_mapx, inter_mapy, cv2.INTER_LINEAR)
cv2.error: OpenCV(4.5.2) ../modules/imgproc/src/imgwarp.cpp:1703:
error: (-215:Assertion failed) !_map1.empty() in function 'remap'

classImage_manipulation中的两个函数在下面的函数training_data中调用。 rotation_matrix 创建一个旋转矩阵并将其应用于已作为参数传递给函数的图像。然后将旋转后的图像作为参数传递给 warping_image 函数,以创建 warped_imagetarget_image,后者将用于在自动编码器上获得 train_on_batch。函数 warping_image 试图对两个图像进行插值。但是我遇到了问题,因为它在 warping_image 函数的这一特定行 warped_image = cv2.remap(images, inter_mapx, inter_mapy, cv2.INTER_LINEAR) 上给我一个错误。我该如何解决这个问题?

class Image_manipulation:

    def rotation_matrix(self, image, rotation_range=10, zoom_range=0.05, shift_range=0.05):
        h,w = image.shape[0:2]
        print(h, w)
        rotation = np.random.uniform( -rotation_range, rotation_range )
        scale = np.random.uniform( 1 - zoom_range, 1 + zoom_range )
        tx = np.random.uniform( -shift_range, shift_range ) * w
        ty = np.random.uniform( -shift_range, shift_range ) * h
        mat = cv2.getRotationMatrix2D( (w//2,h//2), rotation, scale )
        print(mat)
        mat[:,2] += (tx,ty)
        print(f"new mat: {mat}")
        result = cv2.warpAffine( image, mat, (w,h), borderMode=cv2.BORDER_REPLICATE )
        if np.random.random() < 0.4:
            result = result[:,::-1]
        print(f"Rotation matrix value of result: {np.array(result).shape}\n")
        return result


    def warping_image(self, images):
        #assert images.shape == (256, 256, 3)
        range = np.linspace(128 - 80, 128 + 80, 5)
        map_x = np.broadcast_to(range, (5, 5))
        map_y = map_x.T
        map_x = map_x + np.random.normal(size=(5, 5), scale=5)
        map_y = map_y + np.random.normal(size=(5, 5), scale=5)
        inter_mapx = cv2.resize(map_x, (80, 80))[80:72][80:72].astype('float32')
        inter_mapy = cv2.resize(map_y, (80, 80))[80:72][80:72].astype('float32')
        warped_image = cv2.remap(images, inter_mapx, inter_mapy, cv2.INTER_LINEAR)
        src_points = np.stack([map_x.ravel(), map_y.ravel()], axis=-1)
        dst_points = np.mgrid[0:65:16, 0:65:16].T.reshape(-1, 2)
        mat = umeyama(src_points, dst_points, True)[0:2]
        target_image = cv2.warpAffine(images, mat, (64, 64))
        print(f"Warping image: {type(target_image)}")
        print(f"type of warped_image: {type(warped_image)}, type of target image: {type(target_image)}")
        return warped_image, target_image

调用'rotation_matrix'和'warping_image'函数的函数

def training_data(self, imgs, batch_size):
    indices = np.random.randint( len(imgs), size=batch_size )
        for i,index in enumerate(indices):
            image = imgs[index]
            image = self.rotation_matrix(image)
            warped_img, target_img = self.warping_image(image)

            if i == 0:
                warped_images = np.empty( (batch_size,) + warped_img.shape, warped_img.dtype )
                target_images = np.empty( (batch_size,) + target_img.shape, warped_img.dtype )

            warped_images[i] = warped_img
            target_images[i] = target_img

        return warped_images, target_images

问题是这些 resize 调用

inter_mapx = cv2.resize(map_x, (80, 80))[80:72][80:72].astype('float32')
inter_mapy = cv2.resize(map_y, (80, 80))[80:72][80:72].astype('float32')

return 空数组 (array([], shape=(0, 80), dtype=float32)),因为索引 [80:72]。要解决此问题,您必须提供有效的索引切片,因为调整大小的输出形状为 (80, 80).

仅供参考:错误消息中的 _map1 指的是 inter_mapx 变量。