创建图像补丁,sklearn.feature_extraction.image.extract_patches_2d内存错误

bw4*_*4sz 4 python numpy scikit-learn

我一直在寻找一种方法,可以将numpy图片划分为类似补丁的网格。

这个任务已经回答了几次。 有效地从python中的图像中提取特定大小的补丁

skleans extract_patches_2d看起来完全正确。

http://scikit-learn.org/stable/modules/generation/sklearn.feature_extraction.image.extract_patches_2d.html

但是,我觉得我不了解这些文档。

我有一个映像,它不是特别大,磁盘上只有几个Mb。OpenCV没问题。

它的尺寸是

self.original_image.shape
(1536, 2048, 3)
Run Code Online (Sandbox Code Playgroud)

因此,让我们将其提取到每个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.
Run Code Online (Sandbox Code Playgroud)

这是一个numpy内存错误。是什么原因造成的?

我有种我不完全遵循的感觉,让我们制作一个很小的图像并将其分成相当大的部分

patches=extract_patches_2d(self.original_image[0:100,0:100],(50,50))
Run Code Online (Sandbox Code Playgroud)

可行,但会产生数千个补丁

len(patches)
2601
Run Code Online (Sandbox Code Playgroud)

不是我期望的〜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
Run Code Online (Sandbox Code Playgroud)

use*_*128 6

该函数根据给定图像创建补丁的所有可能组合。在您的情况下,第一个色块将在行方向上跨越0:49像素,在列方向上跨越0:49像素。从1:50行和列的第二个补丁,然后继续。因此,如果您具有大小为(m,n)的图像并希望提取大小为(a,b)的色块,则可以提取(ma)+1 x(n-b + 1)个最大可能的色块。在您的情况下为51x 51 = 2601补丁程序。如果您想限制要提取的补丁程序数量,则可以通过参数的可选参数来实现max_ patches。