Paket2001
Paket2001

Reputation: 450

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

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

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!)

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

Upvotes: 9

Views: 17933

Answers (3)

GPrathap
GPrathap

Reputation: 7820

To access each pixel separately you can do this if you are using 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));

              }
        }

      }

Upvotes: -1

Paket2001
Paket2001

Reputation: 450

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.

Mat.get(Y, X);

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

Upvotes: 13

Dan
Dan

Reputation: 1486

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

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

Upvotes: 0

Related Questions