[C++] opencv - Mat::convertTo函数介绍和使用场景

Mat::convertTo()函数

Converts an array to another data type with optional scaling.   

该函数主要用于数据类型的相互转换。

The method converts source pixel values to the target data type. saturate_cast<> is applied at the end to avoid possible overflows:

m(x,y)=saturate_cast<rtype>(α(∗this)(x,y)+β)   

这是函数底层算法实现公式,了解算法方便我们熟练运用该函数。

函数原型:

void cv::Mat::convertTo(OutputArray m, int rtype) const;

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

参数说明:
m:目标矩阵。如果m在运算前没有合适的尺寸或类型,将被重新分配。

rtype: 标矩阵的类型。因为目标矩阵的通道数与源矩阵一样,所以rtype也可以看做是目标矩阵的位深度。如果rtype为负值,目标矩阵和源矩阵将使用同样的类型。

alpha: 尺度变换因子(可选)。默认值是1。即把原矩阵中的每一个元素都乘以alpha。

beta: 附加到尺度变换后的值上的偏移量(可选)。默认值是0。即把原矩阵中的每一个元素都乘以alpha,再加上beta。

函数使用案例

降低图像的亮度

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

using namespace cv;
using namespace std;

int main()
{
    // 读取一张灰色图片
    Mat img = imread("D:/LocalTest/images/beauty_01.jpg", IMREAD_GRAYSCALE);
    imshow("原始灰度图片", img);
    
    // 降低图像的亮度
    Mat target_img;
    img.convertTo(target_img, -1, 1.0, -50);
    
    // 打印第一行第一个像素的原始值
    int img_value = img.at<uchar>(0, 0);
    int target_img_value = target_img.at<uchar>(0, 0);
    cout << "The original value of the first pixel is: " << img_value << endl;
    cout << "The target img value of the first pixel is: " << target_img_value << endl;

    imshow("减少图像亮度", target_img);

    // 等待用户按下任意键退出程序
    waitKey(0);

    return 0;
}

输出结果:

The original value of the first pixel is: 243

The target img value of the first pixel is: 193

对图像进行归一化处理

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

using namespace cv;
using namespace std;

int main()
{
    // 读取一张灰色图片
    Mat img = imread("D:/LocalTest/images/beauty_01.jpg", IMREAD_GRAYSCALE);
    imshow("原始灰度图片", img);
    
    // 将灰色图片转换为进行归一化
    Mat norm_img;
    img.convertTo(norm_img, CV_32FC1, 1.0/255.0);
    
    // 打印第一行第一个像素的原始值
    int img_value = img.at<uchar>(0, 0);
    float norm_img_value = norm_img.at<float>(0, 0);
    cout << "The original value of the first pixel is: " << img_value << endl;
    cout << "The norm value of the first pixel is: " << norm_img_value << endl;

    imshow("归一化图片", norm_img);

    // 等待用户按下任意键退出程序
    waitKey(0);

    return 0;
}

输出结果:

The original value of the first pixel is: 243

The norm value of the first pixel is: 0.952941

相关推荐

  1. Redis基本介绍使用场景

    2024-01-18 23:00:02       32 阅读
  2. 介绍 TensorFlow 的基本概念使用场景

    2024-01-18 23:00:02       38 阅读
  3. 介绍 TensorFlow 的基本概念使用场景

    2024-01-18 23:00:02       41 阅读
  4. 介绍 TensorFlow 的基本概念使用场景

    2024-01-18 23:00:02       40 阅读
  5. 介绍 TensorFlow 的基本概念使用场景

    2024-01-18 23:00:02       40 阅读
  6. 介绍 TensorFlow 的基本概念使用场景

    2024-01-18 23:00:02       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-18 23:00:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-18 23:00:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-18 23:00:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-18 23:00:02       18 阅读

热门阅读

  1. [go] 抽象工厂模式

    2024-01-18 23:00:02       31 阅读
  2. QT基础篇(10)QT5网络与通信

    2024-01-18 23:00:02       32 阅读
  3. element plus自定义组件表单校验

    2024-01-18 23:00:02       40 阅读
  4. C# Math.Floor与Math.Round

    2024-01-18 23:00:02       36 阅读
  5. html中flex的使用

    2024-01-18 23:00:02       37 阅读
  6. C#: richTextBox 富文本编辑控件使用

    2024-01-18 23:00:02       38 阅读
  7. InnoDB底层的一些主要数据结构

    2024-01-18 23:00:02       29 阅读
  8. Docker守护进程是什么

    2024-01-18 23:00:02       39 阅读
  9. pyhton合并多个Excel文件

    2024-01-18 23:00:02       31 阅读
  10. uniapp如何实现跨端适配

    2024-01-18 23:00:02       39 阅读