错误:(-215:断言失败)_函数'中的map1.empty();重新映射'



回溯错误:

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'

Image_manipulation内的两个函数在下面的函数training_data中被调用。rotation_matrix创建旋转矩阵,并将其应用于已作为参数传递到函数中的图像。然后,将旋转后的图像作为参数传递到warping_image函数中,以创建将用于在自动编码器上获得train_on_batchwarped_imagetarget_image。函数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"one_answers"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')

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

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

相关内容

最新更新