I'm trying to take data captured off the camera API and do color postprocessing. My issue is that while I can convert a byte[] object to a Mat object, I cannot get the color channels without receiving an error. I started with the following post: How to get the Mat object from the Byte[] in openCV android? but that implementation does not work.
Here is the relevant code:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// The camera preview was automatically stopped. Start it again.
mCamera.startPreview();
mCamera.setPreviewCallback(this);
// Write the image in a file (in jpeg format)
File pictureFile_interp = getOutputMediaFile(MEDIA_TYPE_IMAGE);
try {
//Now try to convert and save.
Parameters params = camera.getParameters();
Size sz = params.getPictureSize();
//ERROR IN THE LINE BELOW!!!!
Mat raw = new Mat(sz.height,sz.width,CvType.CV_8UC4);
raw.put(0, 0, data);
Mat targ = Highgui.imdecode(raw, 0);
orig = new Mat();
Imgproc.cvtColor(targ, orig, Imgproc.COLOR_GRAY2RGB);
//Imgproc.cvtColor(targ, fixed, Imgproc.COLOR_BGR2RGB);
//now we have the target we want...let's interpolate.
interp = interpExperiment(orig,interpBy);
Highgui.imwrite(pictureFile_interp.toString(), interp);
} finally{
}
}
When I try that code, I get an exception:
Provided data element number should be multiple of the Mat channels count (3)
Instead I replace the problematic line with
Mat raw = new Mat(sz.height,sz.width,CvType.CV_8U);
raw.put(0, 0, data);
Mat targ = Highgui.imdecode(raw, 0);
I can get a grayscale image. What am I doing wrong with
Mat raw = new Mat(sz.height,sz.width,CvType.CV_8UC3);
? I've looked at quite a few stackoverflow posts addressing this and none work for the color matrix. Any help is much appreciated.