<JAVA> OpenCV中如何取整屏RGB像素值

<JAVA> How to take the RGB pixel values of the whole screen in OpenCV

到目前为止,这是我的代码,但我收到类似 OpenCV Error: One of arguments' values is out of range (index is out of range) in cvPtr2D, file ..\..\..\..\opencv\modules\core\src\array.cpp, line 1797 Exception in thread "main" java.lang.RuntimeException: ..\..\..\..\opencv\modules\core\src\array.cpp:1797: error: (-211) index is out of range in function cvPtr2D

的错误

关于如何解决这个问题有什么建议吗?感谢您的帮助 ;)

    cvNamedWindow("OpenCV", 0);
    while(true)
    {
        IplImage img = cvQueryFrame(cvCreateCameraCapture(0));
        CvScalar[] s = new CvScalar[img.height()*img.width()+2];
        for(int i = 0;i<=img.width();i++)
        {
            for(int j = 0;j<=img.height();j++)
            {
                s[j] = cvGet2D(img, i, j);
            }
        }
        cvShowImage("OpenCV", img);
        cvWaitKey(33);
    }

cvGet2D() 需要行在列之前。而且索引是从 0 开始的,所以你的循环就在边界之外。 s 应该是一个二维数组,这样你就不会一直覆盖你的数据。试试这个:

CvScalar[][] s = new CvScalar[img.height()][img.width()];
for (int i = 0; i < img.height(); i++) {
    for (int j = 0; j < img.width(); j++) {
        s[i][j] = cvGet2D(img, i, j);
    }
}