Java – OpenCV CV_8UC3 to CV_8UC1

javaopencv

My image is of type CV_8UC3 (it is a grayscale image) and I need it as CV_8UC1.
How can I do the transformation? I already tried

Mat right = new Mat(rectRight.width(), rectRight.height(), CvType.CV_8UC1);
rectRight.convertTo(right, CvType.CV_8UC1,255,0);

But it still gives me a 3-channel image.

RectLeft is the rectified version of this image:

Imgproc.undistort(Highgui.imread(images[0], Imgproc.COLOR_BGR2GRAY), undist_left, cameraMatrix, distCoeff);

The rectification is done using this part of code:

Mat rectLeft = new Mat();
Imgproc.initUndistortRectifyMap(cameraMatrix, distCoeff, R1, newCameraMatrix_left, left.size(), CvType. CV_32FC1, map1_left, map2_left);        
Imgproc.remap(left, rectLeft, map1_left, map2_left, Imgproc.INTER_LANCZOS4);        

The rectified image (and its partner of the right camera) should be used in

StereoBM stereoAlgo = new StereoBM();
stereoAlgo.compute(left, right, disparity);

But there is an exception which says that both input images should be of type CV_8UC1 but I've checked and rectLeft.type() gives me a 16. (I guess this is CV_8UC1).

Best Answer

you probably want a grayscale conversion for StereoBM :

Imgproc.cvtConvert(src,dst,Imgproc.COLOR_BGR2GRAY);

http://docs.opencv.org/java/org/opencv/imgproc/Imgproc.html#cvtColor(org.opencv.core.Mat,%20org.opencv.core.Mat,%20int,%20int)

(you can't change/reduce the number of channels with Mat.convertTo(), only the depth)

Related Topic