【发布时间】:2017-12-07 08:58:18
【问题描述】:
我正在寻找一种将 numpy 图像划分为网格状补丁的方法。
这个任务已经回答了好几次了。 Extracting patches of a certain size from the image in python efficiently
skleans extract_patches_2d 看起来完全正确。
但是,我觉得我不理解文档。
我有一个图像,它不是特别大,磁盘上只有几 Mb。 OpenCV 没有问题。
它的尺寸是
self.original_image.shape
(1536, 2048, 3)
所以让我们将它提取成每 100 X 100 的块。在信封计算的后面,补丁的数量应该是这样的
(1536 * 2048) / (100*100) = 314
patches=extract_patches_2d(self.original_image,(100,100))
Traceback (most recent call last):
Debug Probe, prompt 46, line 1
File "c:\Python27\Lib\site-packages\sklearn\feature_extraction\image.py", line 374, in extract_patches_2d
extraction_step=1)
File "c:\Python27\Lib\site-packages\sklearn\feature_extraction\image.py", line 296, in extract_patches
patches = as_strided(arr, shape=shape, strides=strides)
File "c:\Python27\Lib\site-packages\numpy\lib\stride_tricks.py", line 48, in as_strided
array = np.asarray(DummyArray(interface, base=x))
File "c:\Python27\Lib\site-packages\numpy\core\numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger than the maximum possible size.
这是一个 numpy 内存错误。是什么原因造成的?
我觉得我没有完全遵循,让我们制作一个小图像并将其分成相当大的部分
patches=extract_patches_2d(self.original_image[0:100,0:100],(50,50))
这可行,但会产生数千个补丁
len(patches)
2601
不是我期望的 ~ 4。我对此功能有什么不了解的地方?人们如何获得补丁,这在计算机视觉中似乎很常见。
Windows 上的 Python 2.7,最近安装且最新的软件包
Ben@Laptop MINGW64 ~/Desktop
$ pip install -U scikit-learn
Requirement already up-to-date: scikit-learn in c:\python27\lib\site-packages
【问题讨论】:
-
sklearn 也有
extract_patches(注意没有_2d)。在这个函数中,你可以给出补丁之间的步骤,比如extract_patches(img, (100,100,1), (100,100,1)).squeeze()
标签: python numpy scikit-learn