概述
不是所有格式的Mat型數(shù)據(jù)都能被使用。
目前OpenCV主要只支持單通道和3通道的圖像,并且此時(shí)要求其深度為8bit和16bit無符號(hào)(即CV_16U),所以其他一些數(shù)據(jù)類型是不支持的,比如說float型等。
如果Mat類型數(shù)據(jù)的深度和通道數(shù)不滿足上面的要求,則需要使用convertTo()函數(shù)和cvtColor()函數(shù)來進(jìn)行轉(zhuǎn)換。
convertTo()函數(shù)負(fù)責(zé)轉(zhuǎn)換數(shù)據(jù)類型不同的Mat,即可以將類似float型的Mat轉(zhuǎn)換到imwrite()函數(shù)能夠接受的類型。
而cvtColor()函數(shù)是負(fù)責(zé)轉(zhuǎn)換不同通道的Mat,因?yàn)樵摵瘮?shù)的第4個(gè)參數(shù)就可以設(shè)置目的Mat數(shù)據(jù)的通道數(shù)(只是我們一般沒有用到它,一般情況下這個(gè)函數(shù)是用來進(jìn)行色彩空間轉(zhuǎn)換的)。
函數(shù)
void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;
- m ?目標(biāo)矩陣。如果m在運(yùn)算前沒有合適的尺寸或類型,將被重新分配。
- rtype ?目標(biāo)矩陣的類型。因?yàn)槟繕?biāo)矩陣的通道數(shù)與源矩陣一樣,所以rtype也可以看做是目標(biāo)矩陣的位深度。如果rtype為負(fù)值,目標(biāo)矩陣和源矩陣將使用同樣的類型。
- alpha ?尺度變換因子(可選)。
- beta ?附加到尺度變換后的值上的偏移量(可選)。
測(cè)試代碼
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//載入圖片
cv::Mat mat1 = imread("c:/opencv/111.jpg");
//輸出對(duì)象
cv::Mat mat2;
//運(yùn)算
mat1.convertTo(mat2,CV_32F,1.0/255,0);
//顯示
imshow("mat1",mat1);
imshow("mat2",mat2);
//輸出圖像信息
qDebug()<<"mat1的類型是:"<<mat1.type();
qDebug()<<"mat2的類型是:"<<mat2.type();
}
Widget::~Widget()
{
delete ui;
}
?測(cè)試結(jié)果
參考
OpenCV—矩陣數(shù)據(jù)類型轉(zhuǎn)換cv::convertTo?
openCV中convertTo的用法文章來源:http://www.zghlxwxcb.cn/news/detail-441467.html
對(duì)比度亮度圖像增強(qiáng)及convertTo詳解
openCV中convertTo的用法
OpenCV 中的 convertTo 函數(shù)OpenCV之顏色空間轉(zhuǎn)換:cvtColor()和convertTo()函數(shù)文章來源地址http://www.zghlxwxcb.cn/news/detail-441467.html
到了這里,關(guān)于Qt-OpenCV學(xué)習(xí)筆記--基本函數(shù)操作--cv::convertTo(圖像類型轉(zhuǎn)換)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!