如何从任何给定大小的图像中仅获取可能的非重叠块并将它们显示在 Python 中
How to get only possible non overlapping blocks from images of any given size and show them in Python
我有不同分辨率的图像,我想从这些图像中提取非重叠块。
但是,因为图像没有固定大小,而且我的块大小很大 (64x64),我只想得到 可以找到的非重叠块图片。如果块超出图像边界,我不想得到它们。
我尝试了 scikit-image 中的 view_as_blocks 函数,如下所示:
from skimage.util import view_as_blocks
for elem in listOfFiles:
# Reading image
print("Reading image "+elem)
img = cv2.imread(elem)
print(img.shape) #for example, one image is (2059, 2059, 3)
Blocks = view_as_blocks(img, block_shape=(64, 64, 3))
代码returns出现如下错误:
ValueError: 'block_shape' is not compatible with 'arr_in'
我也尝试了scikit-learn的Patch Extractor,如下:
from sklearn.feature_extraction import image
import cv2
import numpy
for elem in listOfFiles:
# Reading image
print("Reading image "+elem)
img = cv2.imread(elem)
print(img.shape)
pe = image.PatchExtractor(patch_size=(64,64))
pe_fit = pe.fit(img)
pe_trans = pe.transform(img)
print('Patches shape: {}'.format(pe_trans.shape))
对我来说 returns 的错误如下:
ValueError: negative dimensions are not allowed
来自 sklearns 的函数 image.extract_patches_2d 运行完美,但不幸的是它仅适用于重叠块,如您所见 here.
这些功能对我也没有帮助,因为我还想显示图像选择了这些块,所以我还需要另一个具有这些块坐标的矩阵[=34] =]并显示选定的块。
在Python中有可能吗?
由于您不关心边缘处的不完整块,您可以手动检查每个维度上的块数,并将图像裁剪为该形状:
from skimage.util import view_as_blocks
for elem in listOfFiles:
# Reading image
print("Reading image "+elem)
img = cv2.imread(elem)
print(img.shape) #for example, one image is (2059, 2059, 3)
block_shape = np.array((64, 64, 3))
nblocks = np.array(img.shape) // block_shape # integer division
crop_r, crop_c, crop_ch = nblocks * block_shape
cropped_img = img[:crop_r, :crop_c, :crop_ch]
Blocks = view_as_blocks(cropped_img, block_shape=(64, 64, 3))
我有不同分辨率的图像,我想从这些图像中提取非重叠块。
但是,因为图像没有固定大小,而且我的块大小很大 (64x64),我只想得到 可以找到的非重叠块图片。如果块超出图像边界,我不想得到它们。
我尝试了 scikit-image 中的 view_as_blocks 函数,如下所示:
from skimage.util import view_as_blocks
for elem in listOfFiles:
# Reading image
print("Reading image "+elem)
img = cv2.imread(elem)
print(img.shape) #for example, one image is (2059, 2059, 3)
Blocks = view_as_blocks(img, block_shape=(64, 64, 3))
代码returns出现如下错误:
ValueError: 'block_shape' is not compatible with 'arr_in'
我也尝试了scikit-learn的Patch Extractor,如下:
from sklearn.feature_extraction import image
import cv2
import numpy
for elem in listOfFiles:
# Reading image
print("Reading image "+elem)
img = cv2.imread(elem)
print(img.shape)
pe = image.PatchExtractor(patch_size=(64,64))
pe_fit = pe.fit(img)
pe_trans = pe.transform(img)
print('Patches shape: {}'.format(pe_trans.shape))
对我来说 returns 的错误如下:
ValueError: negative dimensions are not allowed
来自 sklearns 的函数 image.extract_patches_2d 运行完美,但不幸的是它仅适用于重叠块,如您所见 here.
这些功能对我也没有帮助,因为我还想显示图像选择了这些块,所以我还需要另一个具有这些块坐标的矩阵[=34] =]并显示选定的块。
在Python中有可能吗?
由于您不关心边缘处的不完整块,您可以手动检查每个维度上的块数,并将图像裁剪为该形状:
from skimage.util import view_as_blocks
for elem in listOfFiles:
# Reading image
print("Reading image "+elem)
img = cv2.imread(elem)
print(img.shape) #for example, one image is (2059, 2059, 3)
block_shape = np.array((64, 64, 3))
nblocks = np.array(img.shape) // block_shape # integer division
crop_r, crop_c, crop_ch = nblocks * block_shape
cropped_img = img[:crop_r, :crop_c, :crop_ch]
Blocks = view_as_blocks(cropped_img, block_shape=(64, 64, 3))