简体   繁体   English

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

[英]Create image patches, sklearn.feature_extraction.image.extract_patches_2d memory error

I was looking for a way to divide a numpy image into grid like patches. 我正在寻找一种方法将numpy图像划分为网格状补丁。

This task has been answered a couple times. 此任务已回答了几次。 Extracting patches of a certain size from the image in python efficiently 从python中的图像中有效地提取特定大小的补丁

skleans extract_patches_2d looks exactly right. skleans extract_patches_2d看起来完全正确。

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

However, I feel that i'm not understanding the docs. 但是,我觉得我不理解这些文档。

I have a image, its not particularly large, a few Mb on disk. 我有一个图像,它不是特别大,磁盘上有几个Mb。 OpenCV has no trouble with it. OpenCV没有问题。

Its dimensions are 它的尺寸是

self.original_image.shape
(1536, 2048, 3)

So let's extract it into blocks each 100 X 100. Back of the envelope calculation the number of patches should be something like 因此,让我们将它提取到每个100 X 100的块中。在信封的背面计算补丁的数量应该是类似的

(1536 * 2048) / (100*100) = 314 (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.

This is a numpy memory error. 这是一个numpy内存错误。 What is causing that? 是什么造成的?

I get the feeling I don't follow entirely, lets make a tiny image and split that into pretty large sections 我感觉我没有完全遵循,让我们制作一个小图像并将其分成相当大的部分

patches=extract_patches_2d(self.original_image[0:100,0:100],(50,50))

That works, but yields thousands of patches 这有效,但会产生数千个补丁

len(patches)
2601

Not the ~ 4 I'd expect. 不是我期望的~4。 What am I not understanding about this function? 我对这个功能有什么不了解? How do people go about getting patches, it seems like a common thing in computer vision. 人们如何获得补丁,这似乎是计算机视觉中的常见问题。

Python 2.7 on Windows, packages recently installed and up to date 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

The function creates all possible combinations of patches from the given image. 该函数从给定图像创建所有可能的补丁组合。 In your case, the first patch will span 0:49 pixels row wise and 0: 49 pixels column wise. 在您的情况下,第一个补丁将按行排列0:49像素,按列排列0:49像素。 The second patch from 1:50 row wise and column wise and o on. 第二个补丁从1:50行和列方式和o开启。 So if you have an image of size (m,n) and wish to extract patches of size (a,b) , then (ma)+1 x (n-b+1) maximum possible patches can be extracted . 因此,如果您有大小(m,n)的图像并希望提取大小(a,b)的块,则可以提取(ma)+1 x(n-b + 1)个最大可能的块。 In your case that is 51x 51 = 2601 patches.. If you want to limit the number of patches to be extracted, you can do so by the optional parameter by the max_ patches parameter. 在您的情况下,51x 51 = 2601补丁..如果要限制要提取的补丁数,可以通过max_ patches参数的可选参数执行此max_ patches

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM