java 使用 mat.get() 的像素的 OpenCV 颜色值有时会返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15764176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:47:27  来源:igfitidea点击:

OpenCV color-value of a pixel by using mat.get() returns sometimes null

javaandroidopencv

提问by Paket2001

I try to get the RGB Values of a pixel using mat.get(inx, int y) on Android and OpenCV 2.4.4.

我尝试在 Android 和 OpenCV 2.4.4 上使用 mat.get(inx, int y) 获取像素的 RGB 值。

Mat img = Utils.loadResource(getBaseContext(), R.drawable.ex3);
double[] tmp = img.get(100, 100);
if(printLog) Log.v(tag, "Color: "+ tmp[0] +","+ tmp[1] +","+ tmp[2] +"");

Normaly I got the tmp-Array returned. But at some pixels, i got returned "null". (That points are in range of the picture!)

通常我得到了返回的 tmp-Array。但是在某些像素处,我得到了“null”的返回。(这些点在图片范围内!)

So why I get at some coordinates a array and on some others "null" and how to fix that?

那么为什么我在某些坐标上得到一个数组而在其他一些坐标上得到“null”以及如何解决这个问题?

回答by Paket2001

At OpenCV by getting pixelinformations with Mat.get(row, col) the meaning of X and Y is changed: Use Y for the row and X for the col.

在 OpenCV 中,通过使用 Mat.get(row, col) 获取像素信息,X 和 Y 的含义发生了变化:对行使用 Y,对列使用 X。

Mat.get(Y, X);

So in my case I was out of range but openCV did not return a Exception. It returns "null"

所以在我的情况下,我超出了范围,但 openCV 没有返回异常。它返回“空”

回答by Dan

I'd first check how many channels your Matgot with Mat::channels()and then access to them through:

我会首先检查您使用Mat了多少个频道Mat::channels(),然后通过以下方式访问它们:

double[] tmp = img.at(100,100);

双[] tmp = img.at(100,100);

回答by GPrathap

To access each pixel separately you can do this if you are using https://github.com/bytedeco/javacv

要单独访问每个像素,如果您使用的是https://github.com/bytedeco/javacv,则可以执行此操作

      IplImage image = cvLoadImage("path/to/image/get.jpg");

      public void colorProcess(IplImage image){
        CvMat result = CvMat.create(image.width(),image.height(), CV_32F);
        CvMat ff =image.asCvMat(); 

        for(int a=0;a<result.cols();a++){
             for(int b=0;b<result.rows();b++){

                CvScalar rgb = cvGet2D(ff, a, b);
                System.out.println("blue "+rgb.getVal(0)+"green "+rgb.getVal(1)+"red    "+rgb.getVal(2));

              }
        }

      }