我试图重建一幅分解成小块的图像。我用sklearn.feature_extraction.image.reconstruct_from_patches_2d
或自定义函数进行了尝试,但结果是一样的
以下是我的尝试:
image_path=r'C:\Users\User\Desktop\Thesis-Diabetic Retinopathy\Datasets\DIARETDB1\SeeFar Project\Images\Processed images\image001.png'
os.mkdir(r'C:\Users\User\Desktop\Patched Image')
def create_patches_from_lesions(image_path,patch_widht,patch_height):
im1=cv2.imread(image_path)
im2 = cv2.cvtColor(im1, cv2.COLOR_BGR2RGB)
x1,y1=im2.shape[0]//50,im2.shape[1]//50
x2,y2=x1*50,y1*50
im3 = cv2.resize(im2, (x2,y2))
print(im3.shape)
patches=[]
for row in range(0,im3.shape[0],50):
for column in range(0,im3.shape[1],50):
patch=im3[row:(row+patch_widht),column:(column+patch_height)]
patches.append(patch)
for i in tqdm(range(len(patches))): #### keep only the patches with non zero elememts
cv2.imwrite(os.path.join(r'C:\Users\User\Desktop\Patched Image','{}.png'.format(i)),patches[i])
return patches,x2,y2
patches,x2,y2=create_patches_from_lesions(image_path,50,50)
def reconstruct_image(patches,image_height,image_width):
patches=np.asarray(patches)
print(patches.shape)
reshaped_patches=np.reshape(patches,(image_height,image_width,3))
print([reshaped_patches.shape])
cv2.imwrite(r'C:\Users\User\Desktop\Reconstructed Image.png',reshaped_patches)
reconstruct_image(patches,x2,y2)
^{} 将2D图像重塑为重叠的面片集合。要将图像分解为非重叠块,可以使用^{}
以下代码段是自定义函数的可能实现。请注意,如果图像大小不是面片大小的整数倍,则
create_patches
会去掉图像的底部和右侧边框。我采用这个解决方案是为了避免调整图像的大小(这一操作可能会引入伪影)。原始图像可以从补丁文件(未实现)或直接从create_patches
返回的4D数组重建演示
上述示例中使用的图像可以从here下载
相关问题 更多 >
编程相关推荐