我有两张图像A和B,图像A是原始图像,图像B是深度神经网络的结果作为预测图像,像素值在0和1之间。因此,在这方面,我必须叠加这两张图像即,图像 A(背景)上的图像 B(前景)和原始图像 A(背景)的像素值应从覆盖图像中提取为新数组,并将新数组的视觉表示形式作为图像 C(即覆盖图像)。
最后给出了尝试过的代码。我正在尝试通过定义两个函数来通过在 Google Colab 中叠加原始图像 A(背景)和分割图像 B(前景)来提取原始像素值来使用代码,但在 Google Colab 中它会抛出错误,因为没有包粘贴功能和代码也有一些问题。有人可以在这方面帮助纠正代码或建议吗,谢谢
def overlap_images(background_path, foreground_path, output_path, x, y):
background=np.array(Image.fromarray(background_path)).reshape(512,512)
foreground=np.array(Image.fromarray(foreground_path)).reshape(512,512,3)
merged_image = foreground.copy()
merged_image.paste(background, (x, y), background)
return merged_image
def extract_original_pixels(background_path, merged_image, x, y, width, height):
region_of_interest = background_path.crop((merged_image, x, y, x + width, y + height))
return region_of_interest
background_path = X_train[img_num] #original image i.e. img_num =any
foreground_path = img_pred #the resultant segmented image from DL model pixels values in b/w [0 and 1]
output_image_path = "path/to/output_image.jpg"
overlay_x = 512
overlay_y = 512
merged_image = overlap_images(background_path, foreground_path, output_image_path, overlay_x, overlay_y)
region_width = background_path.width
region_height = background_path.height
extracted_pixels = extract_original_pixels(merged_image, overlay_x,
overlay_y, region_width, region_height)