问题
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 typeImgproc.COLOR_RGB2BGR
. That's a constant that is used for thecvtColor
function, not an OpenCV matrix type. Since your input image is probably a color image, that would make the correct typeCV_8UC3
.lmsTransformMat
should be a 3x3 matrix of typeCV_64FC1
.actRGBVec
needs to be 3x1 matrix of the same type aslmsTransformMat
. (Again:Imgproc.COLOR_RGB2BGR
is not a matrix type).lmsResVec
needs to be a 3x1 matrix of the same type aslmsTransformMat
. (You can just usenew Mat()
however. OpenCV will take care of it inCore.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
.
来源:https://stackoverflow.com/questions/35671531/rgb-to-lms-color-space-conversion-with-opencv