RGB to LMS color space conversion with OpenCV

2019-09-05 15:19发布

问题:

I have a problem understanding how i correctly use my matrices types for a color conversion from rgb to lms color space with opencv. The paper I've found is here. What I want to do is simply calculate the lms color triples as following:

Mat actRGBVec = new Mat(1,3,Imgproc.COLOR_RGB2BGR);
Mat lmsResVec = new Mat(1,3,CvType.CV_64FC3);

lmsMat = new Mat(inputImg.rows(),inputImg.cols(),CvType.CV_64FC3);

// iterate through all pixels and multiply rgb values with the lms transformation matrix
try {
  for (int x = 0; x < inputImg.rows(); x++) {
    for (int y = 0; y < inputImg.cols(); y++) {
      actRGBVal = inputImg.get(x, y);

      // vector holding rgb info
      actRGBVec.put(0, 0, actRGBVal);
      Core.gemm(lmsTransformMat, actRGBVec, 1, null, 0, lmsResVec, 0);

      lmsMat.put(x, y, lmsResVec.get(0, 0));
    }
  }
}
catch (Exception e) {
  Log.d("ImageHandler","Error rgb to lms conversion! " + e.getMessage());
}

lmsMat is of type CV_64FC3. inputImg is of type Imgproc.COLOR_RGB2BGR. lmsTransformMat is of type CV_64FC1 (since it contains only scalar values this should be the right type?).

The Exception says: Error rgb to lms conversion! null What am I doing wrong here?

回答1:

Make sure all your matrices have the correct dimensions and type:

  • inputImage can't be of type Imgproc.COLOR_RGB2BGR. That's a constant that is used for the cvtColor function, not an OpenCV matrix type. Since your input image is probably a color image, that would make the correct type CV_8UC3.
  • lmsTransformMat should be a 3x3 matrix of type CV_64FC1.
  • actRGBVec needs to be 3x1 matrix of the same type as lmsTransformMat. (Again: Imgproc.COLOR_RGB2BGR is not a matrix type).
  • lmsResVec needs to be a 3x1 matrix of the same type as lmsTransformMat. (You can just use new Mat() however. OpenCV will take care of it in Core.gemm).
  • lmsMat looks fine.

In addition you pass null as the src3 parameter to Core.gemm. That's the cause of your NullPointerException. Since your beta parameter is 0 you can just provide new Mat() instead of null.



标签: java opencv rgb