Java OpenCV CV_8UC3 到 CV_8UC1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22787030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:52:20  来源:igfitidea点击:

OpenCV CV_8UC3 to CV_8UC1

javaopencv

提问by glethien

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

我的图像类型CV_8UC3(它是灰度图像),我需要它作为CV_8UC1. 我该如何进行转换?我已经试过了

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.

但它仍然给了我一个 3 通道的图像。

RectLeft is the rectified version of this image:

RectLeft 是这张图片的修正版:

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_8UC1but I've checked and rectLeft.type()gives me a 16. (I guess this is CV_8UC1).

但是有一个例外,它说两个输入图像都应该是类型,CV_8UC1但我已经检查过并rectLeft.type()给了我 16。(我猜这是CV_8UC1)。

采纳答案by berak

you probably want a grayscale conversion for StereoBM :

您可能需要 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)

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)

(您不能使用 Mat.convertTo() 更改/减少通道数,只能更改深度)

回答by nkint

No, 16 is CV_8UC3:

不,16 是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.

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

rtype – 所需的目标矩阵类型,或者更确切地说,深度(因为通道的数量与源的数量相同)。如果 rtype 为负,则目标矩阵将与源具有相同的类型。

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

split通道并只取其中一个,或使用转换为二进制图像threshold

EDIT:

编辑:

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

有关如何在 opencv 中构建类型(如 8UC1 等)的完整答案,请参阅

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

对于转换为灰度.. 谷歌它,它充满了很好的资源。记住参考基本图像容器