OpenCV数据类型转换:cnvertTo函数的使用
生活随笔
收集整理的這篇文章主要介紹了
OpenCV数据类型转换:cnvertTo函数的使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
功能描述
使用可選的類型和變換因子將轉(zhuǎn)換稀疏矩陣到密集n維矩陣。
函數(shù)原型
void convertTo( Mat& m, int rtype, double alpha=1, double beta=0 ) const;參數(shù)釋義
-
參數(shù)[out] m -輸出矩陣,如果他在操作前沒(méi)有合適的大小或類型它將要被重新分配 。
-
參數(shù) [in] rtype - 所需的輸出矩陣的類型,或者更確切的說(shuō)是由于通道數(shù)與輸入相同而產(chǎn)生的深度,如果類型是負(fù)值,輸出矩陣將和輸入矩陣具有相同的類型。
-
參數(shù) [in] alpha - 可選,尺度變換因子。
-
參數(shù) [in] beta - 可選,附加到尺度變換后的值上的偏移量。
代碼示例
#include <iostream> #include <opencv2/opencv.hpp> ? std::string getImageType(int type) {int typeCounts= 35; // 7個(gè)基礎(chǔ)類型,加5個(gè)通道選項(xiàng) ?int enum_ints[] = { CV_8U, ?CV_8UC1, ?CV_8UC2, ?CV_8UC3, ?CV_8UC4,CV_8S, ?CV_8SC1, ?CV_8SC2, ?CV_8SC3, ?CV_8SC4,CV_16U, CV_16UC1, CV_16UC2, CV_16UC3, CV_16UC4,CV_16S, CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4,CV_32S, CV_32SC1, CV_32SC2, CV_32SC3, CV_32SC4,CV_32F, CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4,CV_64F, CV_64FC1, CV_64FC2, CV_64FC3, CV_64FC4 }; ?std::string enum_strings[] = { "CV_8U", ?"CV_8UC1", ?"CV_8UC2", ?"CV_8UC3", ?"CV_8UC4","CV_8S", ?"CV_8SC1", ?"CV_8SC2", ?"CV_8SC3", ?"CV_8SC4","CV_16U", "CV_16UC1", "CV_16UC2", "CV_16UC3", "CV_16UC4","CV_16S", "CV_16SC1", "CV_16SC2", "CV_16SC3", "CV_16SC4","CV_32S", "CV_32SC1", "CV_32SC2", "CV_32SC3", "CV_32SC4","CV_32F", "CV_32FC1", "CV_32FC2", "CV_32FC3", "CV_32FC4","CV_64F", "CV_64FC1", "CV_64FC2", "CV_64FC3", "CV_64FC4" }; ?for (int i = 0; i < typeCounts; i++){if (type == enum_ints[i]) return enum_strings[i];} ?return "unknown image type"; } ? ? int main() {cv::Mat imageColor = cv::imread("D:\\OpenCVtest\\images\\juice.png", cv::IMREAD_COLOR);cv::Mat imageGray = cv::imread("D:\\OpenCVtest\\images\\juice.png", cv::IMREAD_GRAYSCALE);std::cout << "image color type:" << getImageType(imageColor.type()) << std::endl;std::cout << "image Gray type:" << getImageType(imageGray.type()) << std::endl; ? ?// 深度16位 3通道轉(zhuǎn)換cv::Mat imageColor2;cv::Mat imageGray2;imageColor.convertTo(imageColor2, CV_16UC3);imageGray.convertTo(imageGray2, CV_16UC3); ?std::cout << "image color convertTo CV_16UC3 image color type:" << getImageType(imageColor2.type()) << std::endl;std::cout << "image Gray convertTo CV_16UC3 image Gray type:" << getImageType(imageGray2.type()) << std::endl; ?// 深度16位 1通道轉(zhuǎn)換cv::Mat imageColor3;cv::Mat imageGray3;imageColor.convertTo(imageColor3, CV_16UC1);imageGray.convertTo(imageGray3, CV_16UC1); ?std::cout << "image color convertTo CV_16UC1 image color type:" << getImageType(imageColor3.type()) << std::endl;std::cout << "image Gray convertTo CV_16UC1 image Gray type:" << getImageType(imageGray3.type()) << std::endl; ? ?// 就地操作,深度16 3通道轉(zhuǎn)換imageColor.convertTo(imageColor, CV_16UC3);imageGray.convertTo(imageGray, CV_16UC3); ?std::cout << "image color convertTo CV_16UC3 image color type:" << getImageType(imageColor.type()) << std::endl;std::cout << "image Gray convertTo CV_16UC3 image Gray type:" << getImageType(imageGray.type()) << std::endl; }輸出結(jié)果
結(jié)論
-
convertTo函數(shù)支持轉(zhuǎn)換深度,比如k可以把CV_8UC3的轉(zhuǎn)換為深度CV_16UC3
-
convertTo函數(shù)不支持轉(zhuǎn)換通道數(shù),從上面運(yùn)行結(jié)果看,轉(zhuǎn)換后都保留了原來(lái)的通道。比如把CV_8U轉(zhuǎn)換為CV_16UC3的結(jié)果是CV_16U。
-
convertTo函數(shù)支持就地操作(in-place)
總結(jié)
以上是生活随笔為你收集整理的OpenCV数据类型转换:cnvertTo函数的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenCV图像翻转:flip()函数的
- 下一篇: Anaconda 中no module