1. 寻找匹配物体
1.1 mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void initMainWindow();
void imgproc();
void imgShow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
cv::Mat myImg;
QImage myQImg;
};
#endif
1.2 mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
initMainWindow();
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::initMainWindow() {
cv::Mat imgData = cv::imread("D:\\BaiduNetdiskDownload\\search_pic\\search_pic\\mermaid.jpg");
cv::cvtColor(imgData, imgData, cv::COLOR_BGR2RGB);
myImg = imgData;
myQImg = QImage((const unsigned char*)(imgData.data), imgData.cols, imgData.rows, QImage::Format_RGB888);
imgShow();
}
void MainWindow::imgproc() {
int METHOD = CV_TM_CCOEFF;
cv::Mat imgSrc = myImg;
cv::Mat imgTmp = cv::imread("D:\\BaiduNetdiskDownload\\search_pic\\search_pic\\fish.jpg");
cv::cvtColor(imgTmp, imgTmp, cv::COLOR_BGR2RGB);
cv::Mat imgRes;
cv::Mat imgDisplay;
imgSrc.copyTo(imgDisplay);
int rescols = imgSrc.cols - imgTmp.cols + 1;
int resrows = imgSrc.rows - imgTmp.rows + 1;
imgRes.create(rescols, resrows, CV_32FC1);
cv::matchTemplate(imgSrc, imgTmp, imgRes, METHOD);
cv::normalize(imgRes, imgRes, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
double minVal;
double maxVal;
cv::Point minLoc;
cv::Point maxLoc;
cv::Point matchLoc;
cv::minMaxLoc(imgRes, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());
if (METHOD == CV_TM_SQDIFF || METHOD == CV_TM_SQDIFF_NORMED) {
matchLoc = minLoc;
} else {
matchLoc = maxLoc;
}
cv::rectangle(imgDisplay, matchLoc, cv::Point(matchLoc.x + imgTmp.cols, matchLoc.y + imgTmp.rows), cv::Scalar::all(0), 2, 8, 0);
cv::rectangle(imgRes, matchLoc, cv::Point(matchLoc.x + imgTmp.cols, matchLoc.y + imgTmp.rows), cv::Scalar::all(0), 2, 8, 0);
myQImg = QImage((const unsigned char*)(imgDisplay.data), imgDisplay.cols, imgDisplay.rows, QImage::Format_RGB888);
imgShow();
}
void MainWindow::imgShow() {
ui->label->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->label->size(), Qt::KeepAspectRatio)));
}
void MainWindow::on_pushButton_clicked() {
imgproc();
}
2. 人脸识别实例
2.1 mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void initMainWindow();
void imgProc();
void imgShow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
cv::Mat myImg;
QImage myQImg;
};
#endif
2.2 mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
initMainWindow();
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::initMainWindow() {
cv::Mat imgData = cv::imread("D:\\download\\qt_test\\OpencvFace\\model.jpg");
cv::cvtColor(imgData, imgData, cv::COLOR_BGR2RGB);
myImg = imgData;
myQImg = QImage((const unsigned char*)(imgData.data), imgData.cols, imgData.rows, QImage::Format_RGB888);
imgShow();
}
void MainWindow::imgProc() {
cv::CascadeClassifier face_detector;
cv::CascadeClassifier eyes_detector;
face_detector.load("D:\\download\\qt_test\\OpencvFace\\haarcascade_frontalface_alt.xml");
eyes_detector.load("D:\\download\\qt_test\\OpencvFace\\haarcascade_eye_tree_eyeglasses.xml");
std::vector<cv::Rect> faces;
cv::Mat imgSrc = myImg;
cv::Mat imgGray;
cv::cvtColor(imgSrc, imgGray, cv::COLOR_BGR2GRAY);
cv::equalizeHist(imgGray, imgGray);
face_detector.detectMultiScale(imgGray, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
for (uint64 i = 0; i < faces.size(); i++) {
cv::Point center(faces[i].x + faces[i].width * 0.5, faces[i].y + faces[i].height * 0.5);
cv::ellipse(imgSrc, center, cv::Size(faces[i].width * 0.5, faces[i].height * 0.5), 0, 0, 360, cv::Scalar(255, 0, 255), 4, 8, 0);
cv::Mat faceROI = imgGray(faces[i]);
std::vector<cv::Rect> eyes;
eyes_detector.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
for (uint64 j = 0; j < eyes.size(); j++) {
cv::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[j].height) * 0.25);
cv::circle(imgSrc, center, radius, cv::Scalar(255, 0, 0), 4, 8, 0);
}
}
cv::Mat imgDst = imgSrc;
myQImg = QImage((const unsigned char*)(imgDst.data), imgDst.cols, imgDst.rows, QImage::Format_RGB888);
imgShow();
}
void MainWindow::imgShow() {
ui->label->setScaledContents(true);
ui->label->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->label->size(), Qt::KeepAspectRatio)));
}
void MainWindow::on_pushButton_clicked() {
imgProc();
}