glethien
glethien

Reputation: 2471

OpenCV CV_8UC3 to CV_8UC1

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

Upvotes: 2

Views: 17094

Answers (2)

nkint
nkint

Reputation: 11733

No, 16 is CV_8UC3:

~  python
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.CV_8UC1
0
>>> cv2.CV_8UC3
16
>>>

You have to convert it into grayscale:

cvtColor(imageIn, imageOut, COLOR_BGR2GRAY);

From the references:

void Mat::convertTo(Mat& m, int rtype, double alpha=1, double beta=0) const

rtype – The desired destination matrix type, or rather, the depth (since the number of channels will be the same with the source one). If rtype is negative, the destination matrix will have the same type as the source.

or split the channels and take only one of them, or convert to binary images using threshold

EDIT:

For a complete answer on how types (like 8UC1 etc) are build in opencv see this.

For converting to a grayscale.. google it, it's full of good resources. Remember the references The basic image container

Upvotes: 3

berak
berak

Reputation: 39796

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)

Upvotes: 7

Related Questions