简体   繁体   English

如何使用OpenCV在Python中管理大型图像?

[英]How can I manage large images in Python using OpenCV?

I am trying to deal with a massive numpy array that I then end up writing into a JPEG image using cv2.imwrite(numpy.array) . 我正在尝试处理一个巨大的numpy数组,然后我使用cv2.imwrite(numpy.array)写入JPEG图像。 Unfortunately, what I am working with does not fit into my RAM even though the final JPG image should come up to about 200 MB only. 不幸的是,我正在使用的东西不适合我的RAM,即使最终的JPG图像只能达到200 MB左右。

How can I manage such loads without overloading my RAM? 如何在不超载RAM的情况下管理此类负载?

Are there other ways to write an image without storing the entire array in my RAM at once? 是否有其他方法可以在不将整个阵列同时存储在RAM中的情况下编写图像? It's possible for me to load small bits of the array at a time, but I don't know which module/function to use to write to an image without storing the whole thing on my RAM at once. 我可以一次加载一小部分数组,但我不知道用哪个模块/函数写入图像而不将整个内容一次存储在我的RAM中。

As of right now, I have saved the whole image in 4 smaller images (quarters) because that's the best I could do with my limited RAM. 到目前为止,我已将整个图像保存在4个较小的图像(四分之一)中,因为这是我用有限的RAM做的最好的。 But I still want to be able to stitch them together into one complete image. 但我仍然希望能够将它们拼接成一个完整的图像。 The target image is a 3-channel 26112 x 20480 image. 目标图像是3通道26112 x 20480图像。

If the image is size 26112 x 20480 with three channels, at one byte per channel, the uncompressed data takes 3 x 26112 x 20480 bytes, which works out to about 1.5 gigabytes. 如果图像大小为26112 x 20480,有三个通道,每个通道一个字节,未压缩数据需要3 x 26112 x 20480字节,大约1.5千兆字节。 The JPEG file can be much smaller than that because it uses lossy compression, but the OpenCV representation of the image does not. JPEG文件可以比这小得多,因为它使用有损压缩,但图像的OpenCV表示不会。

Some operations (such as cropping on block boundaries) are possible on the compressed representation as evidenced by Jpegtran but for most things you'd do in OpenCV you will have to decompress the data. 一些操作(例如块边界上的裁剪)可以在压缩表示中进行,如Jpegtran所证明的那样,但是对于你在OpenCV中做的大多数事情,你将不得不解压缩数据。 If your algorithm happens to be something that can be written directly in terms of the DCT coefficients of 8x8 blocks, you won't have to decompress, but OpenCV won't help you there. 如果您的算法碰巧是可以根据8x8块的DCT系数直接编写的,那么您不必解压缩,但OpenCV将无法帮助您。

I don't know if it is feasible with OpenCV, but numpy supports memory-mapped I/O of raw array data. 我不知道OpenCV是否可行,但numpy支持原始数组数据的内存映射I / O. When you read a page of data that is not in memory, the operating system reads it from the mapped file, and if it's running out of memory, it will evict some pages. 当您读取一个不在内存中的数据页面时,操作系统会从映射文件中读取它,如果内存不足,它将逐出某些页面。 It will depend on your data access patterns if this is going to be performant. 它将取决于您的数据访问模式,如果这将是高性能。 In principle you should be able to simply call mmap on a file into which you have decompressed all the image data in the correct memory layout and wrap the pointer in an OpenCV array , but I haven't tried this myself. 原则上你应该能够简单地在一个文件上调用mmap,你已经在正确的内存布局中解压缩了所有的图像数据, 并将指针包装在OpenCV数组中 ,但我自己没有尝试过。

You may be able to use a sparse matrix for large image manipulation. 您可以使用稀疏矩阵进行大图像处理。 One library that might work for you is Eigen. 一个可能适合您的库是Eigen。 Here is a tutorial on using sparse matrices with that library. 这是一个关于在该库中使用稀疏矩阵的教程

OpenCV also supports sparse matrices. OpenCV还支持稀疏矩阵。

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

相关问题 使用opencv在python中读取大量图像 - Reading large number of images in python using opencv 如何使用 OpenCV Python 一次从大量视频中提取和保存图像帧? - How can I extract and save image frames from a large number of videos all at once using OpenCV Python? 如何使用Python和openCV从卫星图像创建地形的3D模型? - How can I create 3D model of terrain from satellite images using Python and openCV? 如何在 Python 中使用 opencv 从文件夹中读取图像序列(im1、im2、...)? - How can I read a sequence of images (im1, im2, ...) from a folder using opencv in Python? 如何使用Python在OpenCV中创建两个图像的组合? - How can I make the composite of two images in OpenCV with Python? 如何使用多处理并行处理OpenCV的图像? - How can I process images with OpenCV in parallel using multiprocessing? 如何使用 python 在 opencv 中加载图像? - How to load images in opencv using python? 为什么我不能使用 opencv python 在视频中获得所有不同大小的图像? - why can't I get all different sized images in the video using opencv python? 如何使用 opencv 和 Python 在 ROI 内找到轮廓? - How can I find contours inside ROI using opencv and Python? 如何使用opencv python在运行时在图像上添加文本 - How can I add text on image at runtime using opencv python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM