c++ - 使用 convertTo 从 CV_16SC1 转换为 CV_32FC1

标签 c++ opencv computer-vision

<分区>


关闭 7 年前

我正在尝试从 CV_16SC1 转换为 CV_32SC1

我的代码很简单:

//print value of data.type() = 16
data.convertTo(data, CV_32FC1); 
//print new value of data.type() = 21

转换没有发生是否有原因?也许由于某种原因,无法在这两种类型之间进行转换?

因为根据这篇文章:Getting enum names (e.g. CV_32FC1) of OpenCV image types?

我的结果是 CV_32SC1,这是非常不受欢迎的,因为我试图在我的矩阵上执行矩阵乘法,这就是我希望它具有浮点类型的原因。

最佳答案

我没有看到任何转换不工作的证据(也没有任何不应该的原因)。

#include <opencv2/opencv.hpp>
#include <iostream>


int main()
{
    cv::Mat data = cv::Mat::ones(2, 2, CV_16SC1);
    std::cout << "Initial data=" << data << "\n";
    std::cout << "Initial type= " << data.type() << "\n";
    std::cout << "Type is CV_16SC1: " << ((data.type() == CV_16SC1) ? "true" : "false") << "\n";

    cv::Mat converted;
    data.convertTo(converted, CV_32FC1);

    std::cout << "Converted" << converted << "\n";
    std::cout << "Converted type=" << converted.type() << "\n";
    std::cout << "Type is CV_32FC1: " << ((converted.type() == CV_32FC1) ? "true" : "false") << "\n";

    return 0;
}

以及来自控制台的文字记录:

Initial data=[1, 1;
  1, 1]
Initial type= 3
Type is CV_16SC1: true
Converted[1, 1;
  1, 1]
Converted type=5
Type is CV_32FC1: true

看看你的代码片段,我什至不确定你是如何得到这些数字的:

  • 16 = 2*8+0 -- 3 channel ,8位,无符号
  • 21 = 2*8+5 -- 3 channel ,32位, float

您在解释结果数据类型时犯了一个差一错误:

#define CV_32F  5

关于c++ - 使用 convertTo 从 CV_16SC1 转换为 CV_32FC1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36271987/

上一篇:c++ - 将不同颜色的二维数组最佳地转换为单一颜色

下一篇:c++ - 返回 boost::smatch 并获取子串 "\000"

相关文章:

java - 如何根据感兴趣区域周围的设计去除图像的背景?

machine-learning - 在在线机器学习算法线性回归随机梯度中,当新的训练数据到来时,我们是否必须将其与以前的数据混合?

c++ - “提示”使用未定义的结构 - winSock

c++ - 将 float ( double )转换为字符串

python - cv2.findContours 无法检测轮廓

python - 导入错误:没有名为 'torchvision.datasets.mnist' 的模块

c++ - 实时模板匹配 - OpenCV,C++

C++ 试图从 std::function 获取函数地址

c++ - 有没有办法在头文件中声明一个指针并在.cpp 中实例化它?

c++ - opencv kmeans聚类多个Mats