如何使用Sci-kit学习reconstruct_from_patches_2d

时间:2019-11-03 23:19:51

标签: python image image-processing scikit-learn image-segmentation

我正在处理一个成像项目,该项目需要读取图像,将其拆分为重叠的补丁,对补丁进行一些操作,然后将它们重新组合为单个图像。为此,我决定使用sci-kit学习方法extract_patches_2d和reconstruct_from_patches_2d。

https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.image.extract_patches_2d.html https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.image.reconstruct_from_patches_2d.html

import numpy as np
import cv2
from sklearn.feature_extraction import image as extraction

img = cv2.imread("cat_small.jpg", cv2.IMREAD_COLOR)
grid_size = 500
images = extraction.extract_patches_2d(img, (grid_size, grid_size), max_patches=100)
image = extraction.reconstruct_from_patches_2d(images, img.shape)
cv2.imwrite("stack_overflow_test.jpg", image)

由于每个补丁都可以保存为单独的图像,因此我可以说提取操作正确。重建无效。

图片: Picture of a cat

成为: Black image with white at the top

在白色背景上观看时看起来完全是黑色的,但在左上角确实有一些白色像素(在单独的标签中打开时可以看到)。灰度也会发生同样的问题。

我已经尝试添加astype(np.uint8),如 How to convert array to image colour channel in python? 无济于事。如何正确使用此方法?

0 个答案:

没有答案