1、概述
案例:使用OpenCV實現(xiàn)圖像的旋轉和鏡像操作
所用函數(shù):這里主要使用到了兩個函數(shù)
1.旋轉:cv::rotate
2.鏡像:cv::flip
rotate(InputArray src, OutputArray dst, int rotateCode);
src:輸入圖像
dst:輸出圖像
rotateCode:
ROTATE_180,順時針180°
ROTATE_90_CLOCKWISE,順時針90°
ROTATE_90_COUNTERCLOCKWISE,逆時針90°
flip(InputArray src, OutputArray dst, int flipCode);
src:輸入圖像
dst:輸出圖像
flipCode:
>0表示y軸翻轉
=0表示x軸翻轉
<0表示xy軸同時翻轉
2、代碼示例
Video_Player_Roate_Flip::Video_Player_Roate_Flip(QWidget *parent)
: QWidget{parent}
{
this->setWindowTitle("圖片旋轉與鏡像");
this->setFixedSize(320,480);
//選擇圖片
QPushButton *chooseImageBtn = new QPushButton(this);
chooseImageBtn->setText("選擇圖片");
connect(chooseImageBtn,&QPushButton::clicked,[=](){//選擇圖片
chooseImage();
});
//圖像旋轉
QRadioButton *rotate1 = new QRadioButton(this);
rotate1->move(0,chooseImageBtn->y()+chooseImageBtn->height()+5);
rotate1->setText("順時針180°");
QRadioButton *rotate2 = new QRadioButton(this);
rotate2->move(rotate1->x()+rotate1->width()+5,chooseImageBtn->y()+chooseImageBtn->height()+5);
rotate2->setText("順時針90°");
QRadioButton *rotate3 = new QRadioButton(this);
rotate3->move(rotate2->x()+rotate2->width()+5,chooseImageBtn->y()+chooseImageBtn->height()+5);
rotate3->setText("逆時針90°");
connect(rotate1,&QRadioButton::clicked,[=](){
showImageRoate(0);
});
connect(rotate2,&QRadioButton::clicked,[=](){
showImageRoate(1);
});
connect(rotate3,&QRadioButton::clicked,[=](){
showImageRoate(2);
});
//圖像鏡像
QRadioButton *rotate4 = new QRadioButton(this);
rotate4->move(0,rotate1->y()+rotate1->height()+5);
rotate4->setText("沿y軸翻轉");
QRadioButton *rotate5 = new QRadioButton(this);
rotate5->move(rotate4->x()+rotate4->width(),rotate1->y()+rotate1->height()+5);
rotate5->setText("沿x軸翻轉");
QRadioButton *rotate6 = new QRadioButton(this);
rotate6->move(rotate5->x()+rotate5->width(),rotate1->y()+rotate1->height()+5);
rotate6->setText("沿xy軸翻轉");
connect(rotate4,&QRadioButton::clicked,[=](){
showImageFlip(0);
});
connect(rotate5,&QRadioButton::clicked,[=](){
showImageFlip(1);
});
connect(rotate6,&QRadioButton::clicked,[=](){
showImageFlip(2);
});
}
void Video_Player_Roate_Flip::chooseImage(){
path = QFileDialog::getOpenFileName(this,"選擇圖像","/Users/yangwei/Downloads/","Image File(*.jpg *.jpeg *.png *.bmp)");
qDebug()<<path;
}
void Video_Player_Roate_Flip::showImageRoate(int type){
Mat src = imread(path.toStdString().c_str());
if(src.empty()){
qDebug()<<"不能為空";
return;
}
imshow("src",src);
Mat dst;
switch(type){
case 0://
cv::rotate(src,dst,ROTATE_180);//順時針180°
break;
case 1:
cv::rotate(src,dst,ROTATE_90_CLOCKWISE);//順時針90°
break;
case 2:
cv::rotate(src,dst,ROTATE_90_COUNTERCLOCKWISE);//逆時針90°
break;
}
imshow("dst",dst);
}
void Video_Player_Roate_Flip::showImageFlip(int type){
Mat src = imread(path.toStdString().c_str());
if(src.empty()){
qDebug()<<"不能為空";
return;
}
imshow("src",src);
Mat dst;
switch(type){
case 0:
cv::flip(src,dst,1);//y軸翻轉
break;
case 1:
cv::flip(src,dst,0);//x軸翻轉
break;
case 2:
cv::flip(src,dst,-1);//xy軸翻轉
break;
}
imshow("dst",dst);
}
3、演示圖像
文章來源:http://www.zghlxwxcb.cn/news/detail-569629.html
?本文福利,莬費領取Qt開發(fā)學習資料包、技術視頻,內(nèi)容包括(C++語言基礎,Qt編程入門,QT信號與槽機制,QT界面開發(fā)-圖像繪制,QT網(wǎng)絡,QT數(shù)據(jù)庫編程,QT項目實戰(zhàn),QSS,OpenCV,Quick模塊,面試題等等)↓↓↓↓↓↓見下面↓↓文章底部點擊莬費領取↓↓文章來源地址http://www.zghlxwxcb.cn/news/detail-569629.html
到了這里,關于OpenCV圖像旋轉(cv::rotate)與鏡像(cv::flip)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!