简体   繁体   English

如何解决 java opencv memory 不足错误?

[英]How to resolve java opencv insufficient memory error?

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import java.util.Arrays;

public class Main {

private static double getRGB(int i, int j) {
    Imgcodecs imageCodecs = new Imgcodecs();
    Mat matrix = imageCodecs.imread("/Users/brand/Downloads/SPACE.JPG");
    double rgbVal;
    double rgb[] = matrix.get(i, j);
    rgbVal = rgb[0] + rgb[1] + rgb[2];
    rgbVal = rgbVal / 3;

    return rgbVal;
}
public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Imgcodecs imageCodecs = new Imgcodecs();
    Mat matrix = imageCodecs.imread("/Users/brand/Downloads/SPACE.JPG");
    System.out.println("Image loaded");
    System.out.println("Image size : " + matrix.width() + " x " + matrix.height());
    double[][] rgb = new double[matrix.width()][matrix.height()];

    for (int i = 0; i < matrix.width(); i++) {
        for (int j = 0; j < matrix.height(); j++) {
            rgb[i][j] = getRGB(i, j);
        }
    }
    System.out.println(Arrays.deepToString(rgb));
}
}

When I run my program it takes a very long time to finish, and at the end returns this error:当我运行我的程序时,它需要很长时间才能完成,最后返回此错误:

"Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.5.0) C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core\src\alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 921600 bytes in function 'cv::OutOfMemoryError' ]" “线程“主”中的异常 CvException [org.opencv.core.CvException:cv::Exception: OpenCV(4.5.0) C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core\s \alloc.cpp:73: 错误: (-4:Insufficient memory) 无法在 function 'cv::OutOfMemoryError' 中分配 921600 字节]"

The goal of the code is to retrieve the rgb values of each pixel, add them together, and then divide them by 3 in order to get the average value at each pixel.代码的目标是检索每个像素的 rgb 值,将它们加在一起,然后将它们除以 3 以获得每个像素的平均值。 How can I avoid this error that I am receiving.我怎样才能避免我收到的这个错误。 I appreciate any help.我很感激任何帮助。

The reason is because at each call to getRGB you load the image again... It allocates those 921600 bytes (maybe 640x480x3) again and again for each pixel in the loop in main.原因是因为在每次调用 getRGB 时,您都会再次加载图像......它会一次又一次地为 main 循环中的每个像素分配那些 921600 字节(可能是 640x480x3)。 You have to send the image to the function as a parameter, a reference to the Mat, created in main, not to load the image each time.您必须将图像作为参数发送到 function,这是对在 main 中创建的 Mat 的引用,而不是每次都加载图像。

Apparently these instances created in getRGB are not released and the memory load grows until you run out of system memory.显然,在 getRGB 中创建的这些实例并未发布,并且 memory 负载会增长,直到您用完系统 memory。 (Watch it using Task Manager, htop etc. depending on your OS) (根据您的操作系统使用任务管理器、htop 等观看)

Not the main problem, but using double is an overkill, the values would be <=255, integer division and bytes would also suffice IMO.不是主要问题,但使用 double 是一种矫枉过正,值将是 <=255,integer 除法和字节也足够 IMO。

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

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