C++ 在OpenCV中将uchar Mat转换为float Mat?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6302171/
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
Convert uchar Mat to float Mat in OpenCV?
提问by small_potato
In OpenCV, if I have a Mat img that contains uchar data, how do I convert the data into float? Is there a function available? Thank you.
在 OpenCV 中,如果我有一个包含 uchar 数据的 Mat img,如何将数据转换为浮点数?有没有可用的功能?谢谢你。
回答by Barney
If you meant c++ then you have
如果你的意思是 c++ 那么你有
#include<opencv2/opencv.hpp>
using namespace cv;
Mat img;
img.create(2,2,CV_8UC1);
Mat img2;
img.convertTo(img2, CV_32FC1); // or CV_32F works (too)
details in opencv2refman.pdf.
opencv2refman.pdf 中的详细信息。
UPDATE:
更新:
CV_32FC1 is for 1-channel (C1, i.e. grey image) float valued (32F) pixels
CV_32FC1 用于 1 通道(C1,即灰度图像)浮点值(32F)像素
CV_8UC1 is for 1-channel (C1, i.e. grey image) unsigned char (8UC) valued ones.
CV_8UC1 用于 1 通道(C1,即灰度图像)无符号字符 (8UC) 值。
UPDATE 2:
According to Arthur Tacca, only CV_32F
is correct (or presumably CV_8U), since convertTo should not change the number of channels. It sounds logical right? Nevertheless, when I have checked opencv reference manual, I could not find any info about this, but I agree with him.
更新 2:
根据 Arthur Tacca 的说法,只有CV_32F
正确(或可能是 CV_8U),因为 convertTo 不应更改通道数。这听起来合乎逻辑吗?尽管如此,当我查看 opencv 参考手册时,我找不到任何关于此的信息,但我同意他的观点。
回答by Andrey Sboev
Use cvConvert
function. In Python:
使用cvConvert
功能。在 Python 中:
import cv
m = cv.CreateMat(2, 2, cv.CV_8UC1)
m1 = cv.CreateMat(2, 2, cv.CV_32FC1)
cv.Convert(m, m1)