基于Qt的OpenCV图像处理1:对比度、亮度、平滑滤波、缩放旋转以及人脸识别;

本系列文章我将为大家介绍一些基于Qt的OpenCV图像处理实现。

Qt得益于信号和槽的抽象概念,使得在此利用OpenCV实现图像处理并不困难。只需要简单的初始化,就可以使用它了。

初始化

首先在Qt项目的.pro文件下添加语句导入OpenCV库:

12345INCLUDEPATH += D:/Work/Code/Learn/OpenCV01/Qt/opencv-mingw-3.4.5/include\              D:/Work/Code/Learn/OpenCV01/Qt/opencv-mingw-3.4.5/include/opencv\              D:/Work/Code/Learn/OpenCV01/Qt/opencv-mingw-3.4.5/include/opencv2LIBS += -L D:/Work/Code/Learn/OpenCV01/Qt/opencv-mingw-3.4.5/lib/libopencv_*.a

接着声明三个基本槽:

123void initMainWindow();  //初始化void imgShow();      //图像显示Mat myImg;        //图像存放变量

最后编写基本代码段:

12345678910111213141516//初始化void MainWindow::initMainWindow(){    QString imgPath="test.jpg";    Mat imgData=imread(imgPath.toLatin1().data());    cvtColor(imgData,imgData,COLOR_BGR2RGB);    myImg=imgData;    myQImg=QImage((const unsigned char*)(imgData.data),imgData.cols,imgData.rows,QImage::Format_RGB888);    imgShow();}myImg=imgData;:赋给myImg全局变量待处理,myImg是点阵类型的以图像形式缓存图片。//将图像通过QPixmap到Label控件上显示void MainWindow::imgShow(){    ui->viewLabel->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->viewLabel->size(),Qt::KeepAspectRatio)));}

至此就可以开始编写你程序的其它功能了。

对比度与亮度

https://github.com/MoeDisk/OpenCV_Qt_Learn/tree/master/Pic_Contrast_Brightness

1234567891011121314151617181920212223void MainWindow::imgProc(float con,int bri){    Mat imgSrc=myImg;    Mat imgDst=Mat::zeros(imgSrc.size(),imgSrc.type());    imgSrc.convertTo(imgDst,-1,con,bri);    myQImg=QImage((const unsigned char*)(imgDst.data),imgDst.cols,imgDst.rows,QImage::Format_RGB888);    imgShow();}void MainWindow::on_contrastVerticalSlider_sliderMoved(int position){    imgProc(position/33.3,0);}void MainWindow::on_contrastVerticalSlider_valueChanged(int value){    imgProc(value/33.3,0);}void MainWindow::on_brightnessVerticalSlider_sliderMoved(int position){    imgProc(1.0,position);}void MainWindow::on_brightnessVerticalSlider_valueChanged(int value){    imgProc(1.0,value);}

imgSrc.convertTo(imgDst,-1,con,bri);:

OpenCV增强图片使用的是点算子,即用常数对每个像素点执行乘法和加法的复合运算:g(i,j)=af(i,j)+b。

式中,f(i,j)代表一个原图的像素点;a是增益参数,控制图片对比度;b是偏值参数,控制图片亮度;而g(i,j)则表示经处理后的对应像素点。这两个参数分别对应程序中的变量con和bri,执行时将它们的值传入OpenCV的convertTo()方法,在其内部就会对图片上的每个点均匀用上式的算法进行处理变换。

平滑滤波

https://github.com/MoeDisk/OpenCV_Qt_Learn/tree/master/Pic_Blur_Gaussian_Median_Bilateral_1

均值滤波:

123Mat imgDst1=imgSrc.clone();    for(int i=1;i<ker;i+=2) blur(imgSrc,imgDst1,Size(i,i),Point(-1,-1));    myBlurQImg=QImage((const unsigned char*)(imgDst1.data),imgDst1.cols,imgDst1.rows,QImage::Format_RGB888);

高斯滤波:

123Mat imgDst2=imgSrc.clone();    for(int i=1;i<ker;i+=2) GaussianBlur(imgSrc,imgDst2,Size(i,i),0,0);    myGaussianQImg=QImage((const unsigned char*)(imgDst2.data),imgDst2.cols,imgDst2.rows,QImage::Format_RGB888);

中值滤波:

123Mat imgDst3=imgSrc.clone();    for(int i=1;i<ker;i+=2) medianBlur(imgSrc,imgDst3,i);    myMedianQImg=QImage((const unsigned char*)(imgDst3.data),imgDst3.cols,imgDst3.rows,QImage::Format_RGB888);

双边滤波:

123Mat imgDst4=imgSrc.clone();    for(int i=1;i<ker;i+=2) bilateralFilter(imgSrc,imgDst4,i,i*2,i/2);    myBilateralQImg=QImage((const unsigned char*)(imgDst4.data),imgDst4.cols,imgDst4.rows,QImage::Format_RGB888);

旋转与缩放

https://github.com/MoeDisk/OpenCV_Qt_Learn/tree/master/Pic_Zoom_Rotate_1

123456789101112void MainWindow::imgProc(float ang, float sca){    Point2f srcMatrix[3];    Point2f dstMatrix[3];    Mat imgRot(2,3,CV_32FC1);    Mat imgSrc=myImg;    Mat imgDst;    Point centerPoint=Point(imgSrc.cols/2,imgSrc.rows/2);    imgRot=getRotationMatrix2D(centerPoint,ang,sca);    warpAffine(imgSrc,imgDst,imgRot,imgSrc.size());    myQImg=QImage((const unsigned char*)(imgDst.data),imgDst.cols,imgDst.rows,QImage::Format_RGB888);    imgShow();}

该方法接收两个参数,皆为单精度实型,ang表示旋转角度(正为顺时针、负为逆时针),sca表示缩放率(大于1为放大、小于1为缩小)。

imgRot=getRotationMatrix2D(centerPoint,ang,sca);:

OpenCV内部用仿射变换算法来实现图片的旋转缩放。它需要三个参数:

(1)旋转图片所要围绕的中心;

(2)旋转的角度,在OpenCV中逆时针角度为正值,反之为负值;

(3)缩放因子(可选),在本例中分别对应centerPoint、ang和sca参数值。

任何一个仿射变换都能表示为向量乘以一个矩阵(线性变换)再加上另一个向量(平移),研究表明,不论是对图片的旋转还是缩放操作,本质上都是对其每个像素施加了某种线性变换,如果不考虑平移,实际上也就是一个仿射变换。因此,变换的关键在于求出变换矩阵,这个矩阵实际上代表了变换前后两张图片之间的关系。这里用OpenCV的getRotationMatrix2D()方法来获得旋转矩阵,然后通过warpAffine()方法将获得的矩阵用到对图片的旋转缩放操作中。

人脸识别

https://github.com/MoeDisk/OpenCV_Qt_Learn/tree/master/Pic_FaceRecognition_1

123456789101112131415161718192021222324252627282930void MainWindow::imgProc(){    CascadeClassifier face_detector;    CascadeClassifier eyes_detector;    string fDetectorPath="D:/Work/Code/Learn/OpenCV01/Qt/face_recognition/haarcascade_frontalface_alt.xml";    face_detector.load(fDetectorPath);    string eDetectorPath="D:/Work/Code/Learn/OpenCV01/Qt/face_recognition/haarcascade_eye_tree_eyeglasses.xml";    eyes_detector.load(eDetectorPath);    vector<Rect> faces;    Mat imgSrc=myImg;    Mat imgGray;    cvtColor(imgSrc,imgGray,CV_RGB2GRAY);    equalizeHist(imgGray,imgGray);    face_detector.detectMultiScale(imgGray,faces,1.1,2,0|CV_HAAR_SCALE_IMAGE,Size(30,30));    for(int i=0;i<faces.size();i++){        Point center(faces[i].x+faces[i].width*0.5,faces[i].y+faces[i].height*0.5);        ellipse(imgSrc,center,Size(faces[i].width*0.5,faces[i].height*0.5),0,0,360,Scalar(255,0,255),4,8,0);        Mat faceROI=imgGray(faces[i]);        vector<Rect> eyes;        eyes_detector.detectMultiScale(faceROI,eyes,1.1,2,0|CV_HAAR_SCALE_IMAGE,Size(30,30));        for(int j=0;j<eyes.size();j++){            Point center(faces[i].x+eyes[j].x+eyes[j].width*0.5,faces[i].y+eyes[j].y+eyes[j].height*0.5);            int radius=cvRound((eyes[j].width+eyes[i].height)*0.25);            circle(imgSrc,center,radius,Scalar(255,0,0),4,8,0);        }    }    Mat imgDst=imgSrc;    myQImg=QImage((const unsigned char*)(imgDst.data),imgDst.cols,imgDst.rows,QImage::Format_RGB888);    QMessageBox::about(NULL, " ", "Done");    imgShow();}

eyes_detector.load(eDetectorPath);:

load()方法用于加载一个XML分类器文件,OpenCV既支持Haar特征算法也支持LBP特征算法的分类器。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容