OpenCV中矩阵深度类型转换

来源:互联网 发布:至尊宝官网软件下载 编辑:程序博客网 时间:2024/05/22 10:51

      在OpenCV中,cv::Mat类提供了一个名为converTo的方法来进行矩阵类型转换。当然使用时需要注意的是,该函数只能进行矩阵深度类型的转换,而不能进行矩阵通道数的转换。若需要进行矩阵通道数的转换,可能需要采取进行其他的方法来变通实现。下面对该函数进行测试。

      程序源代码

/** 根据自己机器上的OpenCV版本定义宏              * 例如使用OpenCV 2.1,则定义宏RC_OPENCV_2_1_0;  * 使用OpenCV 2.2,则定义宏RC_OPENCV_2_2_0;  * 以此类推  */#define RC_OPENCV_2_3_0#ifdef _DEBUG#ifdef RC_OPENCV_2_1_0#pragma comment( lib, "cxcore210d.lib" )#pragma comment( lib, "cv210d.lib" )#pragma comment( lib, "highgui210d.lib" )#endif#ifdef RC_OPENCV_2_2_0#pragma comment( lib, "opencv_core220d.lib" )#pragma comment( lib, "opencv_highgui220d.lib" )#pragma comment( lib, "opencv_imgproc220d.lib" )#endif #ifdef RC_OPENCV_2_3_0#pragma comment( lib, "opencv_core230d.lib" )#pragma comment( lib, "opencv_highgui230d.lib" )#pragma comment( lib, "opencv_imgproc230d.lib" )#endif #else#ifdef RC_OPENCV_2_1_0#pragma comment( lib, "cxcore210.lib" )#pragma comment( lib, "cv210.lib" )#pragma comment( lib, "highgui210.lib" )#endif#ifdef RC_OPENCV_2_2_0#pragma comment( lib, "opencv_core220.lib" )#pragma comment( lib, "opencv_highgui220.lib" )#pragma comment( lib, "opencv_imgproc220.lib" )#endif#ifdef RC_OPENCV_2_3_0#pragma comment( lib, "opencv_core230.lib" )#pragma comment( lib, "opencv_highgui230.lib" )#pragma comment( lib, "opencv_imgproc230.lib" )#endif#endif#include <cv.h>#include <highgui.h>#include <iostream>int main( int argc, char** argv ){    /** 构造一个10行10列、深度为32位、3通道的矩阵,矩阵元素初始化为10 */    cv::Mat matOrigin( 10, 10, CV_32FC3, cv::Scalar(10) );    /** 转换为深度为8位、3通道的矩阵 */    cv::Mat mat8UC3Converted;    matOrigin.convertTo( mat8UC3Converted, CV_8UC3, 1, 0 );    if ( CV_8UC3 == mat8UC3Converted.type() )    {        std::cout << "mat8UC3Converted 转换成功。" << std::endl;    }    else    {        std::cout << "mat8UC3Converted 转换失败。" << std::endl;    }    /** 转换为深度为8位、1通道的矩阵 */    cv::Mat mat8UC1Converted;    matOrigin.convertTo( mat8UC1Converted, CV_8UC1, 1, 0 );    if ( CV_8UC1 == mat8UC1Converted.type() )    {        std::cout << "mat8UC1Converted 转换成功。" << std::endl;    }    else    {        std::cout << "mat8UC1Converted 转换失败。" << std::endl;    }    system( "pause" );    return 0;}
      测试结果


原创粉丝点击