<JAVA> 如何在OpenCV中获取整个屏幕的RGB像素值


这是我

到目前为止的代码,但我收到类似OpenCV Error: One of arguments' values is out of range (index is out of range) in cvPtr2D, file ........opencvmodulescoresrcarray.cpp, line 1797 Exception in thread "main" java.lang.RuntimeException: ........opencvmodulescoresrcarray.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可能应该是一个 2D 数组,这样您就不会一直覆盖数据。试试这个:

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);
    }
}

最新更新