廢話:
有時候我們是從物品的斜上方拍攝的圖片,看起來不直觀,需要把視角拉正,這樣的一個操作就叫做 梯度矯正,需要用到的技術是 Opencv 的 透視變換。
這個只是一個簡單的演示demo,如果完善一下,比如物品檢測,可以應用更多的場景,比如常見的:檔案、資料上傳,軟管攝像頭的應用等,怎麼說也是一個技術點吧
重要程式碼:
/** * @brief hDLL_gradientAuto 梯度矯正 * @param src 輸入影像 * @param dst 輸出影像 * @param flag 方向,[0(左),1(上),2(右),3(下)] * @param val 矯正度數,畫素,[10 ~ 100] * @return 0(成功),-1(失敗) */ int MainWindow::hDLL_gradientAuto(Mat &src, Mat &dst, int flag, int val) { if(flag != 0 && flag != 1 && flag != 2 && flag != 3) return -1; if(val < 10 || val > 100) return -1; int width = src.cols; int height = src.rows; Mat M; // flag 方向,[0(左),1(上),2(右),3(下)] switch (flag) { case 0: { Point2f pts_src[] = { Point(val,val), Point(width, 0), Point(width, height), Point(val, height-val)}; Point2f pts_dst[] = { Point(0, 0), Point(width, 0), Point(width, height) ,Point(0, height) }; M = cv::getPerspectiveTransform(pts_src, pts_dst); }break; case 1: { Point2f pts_src[] = { Point(val,val), Point(width-val, val), Point(width, height), Point(0, height)}; Point2f pts_dst[] = { Point(0, 0), Point(width, 0), Point(width, height) ,Point(0, height) }; M = cv::getPerspectiveTransform(pts_src, pts_dst); }break; case 2: { Point2f pts_src[] = { Point(0,0), Point(width-val, val), Point(width-val, height-val), Point(0, height)}; Point2f pts_dst[] = { Point(0, 0), Point(width, 0), Point(width, height) ,Point(0, height) }; M = cv::getPerspectiveTransform(pts_src, pts_dst); }break; case 3: { Point2f pts_src[] = { Point(0,0), Point(width, 0), Point(width-val, height-val), Point(val, height-val)}; Point2f pts_dst[] = { Point(0, 0), Point(width, 0), Point(width, height) ,Point(0, height) }; M = cv::getPerspectiveTransform(pts_src, pts_dst); }break; } cv::warpPerspective(src, dst, M, dst.size(), cv::INTER_LINEAR , cv::BORDER_REPLICATE); return 0; }
Demo演示:
完整程式碼:
.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QImage> #include <QDebug> #include <QtMath> #include "opencv2/opencv.hpp" using namespace cv; QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void updateQLabelImage(); Mat QImage2Mat(QImage &img); QImage Mat2QImage(Mat &img); /** * @brief hDLL_gradientAuto 梯度矯正 * @param src 輸入影像 * @param dst 輸出影像 * @param flag 方向,[0(左),1(上),2(右),3(下)] * @param val 矯正度數,畫素,[10 ~ 100] * @return 0(成功),-1(失敗) */ int hDLL_gradientAuto(Mat &src, Mat &dst, int flag, int val); public slots: void horChange(int index); void verChange(int index); private: Ui::MainWindow *ui; QImage m_img; // 原圖 QImage m_img_dst; // 處理過的影像 }; #endif // MAINWINDOW_H
.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(horChange(int))); connect(ui->verticalSlider, SIGNAL(valueChanged(int)), this, SLOT(verChange(int))); m_img = QImage("F:1.jpg"); m_img_dst = m_img; updateQLabelImage(); } MainWindow::~MainWindow() { delete ui; } // 更新QLabel裡面的影像 void MainWindow::updateQLabelImage() { // m_img = m_img.scaled(ui->label->width(), ui->label->height()); QImage img_show = m_img_dst.scaled(ui->label->width(), ui->label->height()); ui->label->setPixmap(QPixmap::fromImage(img_show)); } Mat MainWindow::QImage2Mat(QImage &img) { cv::Mat mat; switch (img.format()) { case QImage::Format_RGB32: //一般Qt讀入彩色圖後為此格式 mat = cv::Mat(img.height(), img.width(), CV_8UC4, (void*)img.constBits(), img.bytesPerLine()); cv::cvtColor(mat,mat,cv::COLOR_BGRA2BGR); //轉3通道 break; case QImage::Format_RGB888: mat = cv::Mat(img.height(), img.width(), CV_8UC3, (void*)img.constBits(), img.bytesPerLine()); cv::cvtColor(mat,mat,cv::COLOR_RGB2BGR); break; case QImage::Format_Indexed8: mat = cv::Mat(img.height(), img.width(), CV_8UC1, (void*)img.constBits(), img.bytesPerLine()); break; } return mat; } QImage MainWindow::Mat2QImage(Mat &img) { if(img.type()==CV_8UC1 || img.type()==CV_8U) { QImage image((const uchar *)img.data, img.cols, img.rows, img.step, QImage::Format_Grayscale8); return image; } else if(img.type()==CV_8UC3) { QImage image((const uchar *)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888); return image.rgbSwapped(); //r與b調換 } } int MainWindow::hDLL_gradientAuto(Mat &src, Mat &dst, int flag, int val) { if(flag != 0 && flag != 1 && flag != 2 && flag != 3) return -1; if(val < 10 || val > 100) return -1; int width = src.cols; int height = src.rows; Mat M; // flag 方向,[0(左),1(上),2(右),3(下)] switch (flag) { case 0: { Point2f pts_src[] = { Point(val,val), Point(width, 0), Point(width, height), Point(val, height-val)}; Point2f pts_dst[] = { Point(0, 0), Point(width, 0), Point(width, height) ,Point(0, height) }; M = cv::getPerspectiveTransform(pts_src, pts_dst); }break; case 1: { Point2f pts_src[] = { Point(val,val), Point(width-val, val), Point(width, height), Point(0, height)}; Point2f pts_dst[] = { Point(0, 0), Point(width, 0), Point(width, height) ,Point(0, height) }; M = cv::getPerspectiveTransform(pts_src, pts_dst); }break; case 2: { Point2f pts_src[] = { Point(0,0), Point(width-val, val), Point(width-val, height-val), Point(0, height)}; Point2f pts_dst[] = { Point(0, 0), Point(width, 0), Point(width, height) ,Point(0, height) }; M = cv::getPerspectiveTransform(pts_src, pts_dst); }break; case 3: { Point2f pts_src[] = { Point(0,0), Point(width, 0), Point(width-val, height-val), Point(val, height-val)}; Point2f pts_dst[] = { Point(0, 0), Point(width, 0), Point(width, height) ,Point(0, height) }; M = cv::getPerspectiveTransform(pts_src, pts_dst); }break; } cv::warpPerspective(src, dst, M, dst.size(), cv::INTER_LINEAR , cv::BORDER_REPLICATE); return 0; } // 橫向改變 void MainWindow::horChange(int index) { qDebug() << "hor:" << index; if(index == 0) { m_img_dst = m_img; } else if(index < 0) { int val = abs(index) * 10; Mat src = QImage2Mat(m_img); Mat dst; hDLL_gradientAuto(src, dst, 0, val); m_img_dst = Mat2QImage(dst); } else if (index > 0) { int val = abs(index) * 10; Mat src = QImage2Mat(m_img); Mat dst; hDLL_gradientAuto(src, dst, 2, val); m_img_dst = Mat2QImage(dst); } updateQLabelImage(); } // 豎向改變 void MainWindow::verChange(int index) { qDebug() << "ver:" << index; if(index == 0) { m_img_dst = m_img; } else if(index < 0) { int val = abs(index) * 10; Mat src = QImage2Mat(m_img); Mat dst; hDLL_gradientAuto(src, dst, 3, val); m_img_dst = Mat2QImage(dst); } else if (index > 0) { int val = abs(index) * 10; Mat src = QImage2Mat(m_img); Mat dst; hDLL_gradientAuto(src, dst, 1, val); m_img_dst = Mat2QImage(dst); } updateQLabelImage(); }
程式碼下載:
我的環境是:Qt 5.15.2 + Opencv V4.8.0,如果需要下載程式碼,自己除錯,自己配置環境即可
程式碼倉庫:https://gitee.com/vvvj/qt-test-gradient-auto