前言
本項目的出現理由只是筆者的一個念頭,於是利用專業Qt和Opencv相關的知識開發一個輔助工具,本文章僅用於Qt和Opencv結合的學習。
CSDN粉絲0積分下載地址:https://download.csdn.net/download/qq21497936/85372782
QQ羣下載地址:1047134658(點擊“文件”搜索“findTheDifference”,羣內與博文同步更新)
CSDN下載地址:https://download.csdn.net/download/qq21497936/85372767
(注意:源碼本博客後面都有,若是想一步到位,下載這個,源碼編譯版本爲Qt5.9.x mingw32 + openCV3.4.10)
- 應用程序可將某Q遊戲界面套入內部區域,遊戲方便操作;
- 抓圖區域調整,可通過右上角區域,調整區域1和區域2的位置;
- 位置微調功能,點擊按鈕可像對應方向微調一個像素;
- 識別不同,調用opencv算法,識別不同處在遊戲圖上繪製顯示區域;
- 遊戲界面區域操作焦點爲遊戲界面;
- 可清空已經繪製的區域;
- Qt窗口部分透明
《Qt實用技巧:實現不規則窗口的鼠標消息穿透,包括穿透到桌面和穿透到父窗口》; - Qt窗口透明區域鼠標穿透
《Qt實用技巧:使用非透明窗體鼠標穿透到桌面的設置方法》; - Qt窗口置頂
《Qt實用技巧:Qt窗口置頂》; - Qt繪圖
《Qt開發技術:Qt繪圖系統(一)繪圖系統介紹》
《Qt開發技術:Qt繪圖系統(二)QPainter詳解》; - Qt雙窗口疊加(底窗口+透明窗口層)
《Qt實用技巧:實現窗口透明的五種方法》中的方法五; - QLabel,QGroupBox,QSpinBox,QPushButton控件的使用
- Qt截取屏幕
《Qt實用技巧:截屏功能的實現》
- OpenCV開發環境的搭建
《OpenCV開發筆記(〇):使用mingw530_32編譯openCV3.4.1源碼,搭建Qt5.9.3的openCV開發環境》 - OpenCV中的cv::Mat
《OpenCV開發筆記(三):OpenCV圖像的概念和基本操作》 - OpenCV中QImage轉換爲cv::Mat
- OpenCV灰度化
《OpenCV開發筆記(六):OpenCV基礎數據結構、顏色轉換函數和顏色空間》 - OpenCV閾值化
《OpenCV開發筆記(二十八):帶你學習圖像識別之閾值化》 - OpenCV的中值濾波
《OpenCV開發筆記(十八):算法基礎之非線性濾波-中值濾波》 - OpenCV的閉運算
《OpenCV開發筆記(二十四):算法基礎之形態學濾波-閉運算》 - OpenCV的尋找邊界
《OpenCV開發筆記(二十八):帶你學習圖像識別之閾值化》 - OpenCv的最小矩形
《OpenCV開發筆記(五十六):紅胖子8分鐘帶你深入瞭解多種圖形擬合逼近輪廓(圖文並茂+淺顯易懂+程序源碼)》
項目的環境爲Qt5.9.3 mingw32版本,使用QtCreator開發,配合mingw32版本的Opencv3.4.10,下圖左側爲項目結構,右側爲實際文件夾部署結構。
該類的主要作用:
- 覆蓋在遊戲窗口上
- 全部透明窗口用以當作遊戲界面上的畫布
- 對鼠標消息穿透(無法點擊中)
- 識別出後繪製標記處不同的區域
爲自動生成默認的,沒有任何改動。
一共繪製兩類圖形,一類是框出抓取圖的界面,一類是識別出後的區域,定義兩個緩存變量,用以繪製對應的區域矩形。
#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H
#include <QWidget>
namespace Ui {
class DrawWidget;
}
class DrawWidget : public QWidget
{
Q_OBJECT
public:
explicit DrawWidget(QWidget *parent = 0);
~DrawWidget();
public:
void setRect(int x, int y, int width, int height);
void setRect2(int x, int y, int width, int height);
void clearListRect();
void setListRect(QList<QRect> listRect);
protected:
void initControl();
protected:
void paintEvent(QPaintEvent *event);
protected:
void drawSelectRect(QPainter *painter);
void drawListRect(QPainter *painter);
private:
Ui::DrawWidget *ui;
private:
QColor _colorRect;
QRect _rect;
QRect _rect2;
QList<QRect> _listRect;
};
#endif // DRAWWIDGET_H
#include "DrawWidget.h"
#include "ui_DrawWidget.h"
#include <QPainter>
#include <windows.h>
#include <QDebug>
#include <QDateTime>
//#define LOG qDebug()<<__FILE__<<__LINE__
//#define LOG qDebug()<<__FILE__<<__LINE__<<__FUNCTION__
//#define LOG qDebug()<<__FILE__<<__LINE__<<QThread()::currentThread()
//#define LOG qDebug()<<__FILE__<<__LINE__<<QDateTime::currentDateTime().toString("yyyy-MM-dd")
#define LOG qDebug()<<__FILE__<<__LINE__<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")
DrawWidget::DrawWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::DrawWidget),
_colorRect(Qt::red)
{
ui->setupUi(this);
setWindowFlag(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_TransparentForMouseEvents);
initControl();
}
DrawWidget::~DrawWidget()
{
delete ui;
}
void DrawWidget::setRect(int x, int y, int width, int height)
{
_rect.setRect(x, y, width, height);
}
void DrawWidget::setRect2(int x, int y, int width, int height)
{
_rect2.setRect(x, y, width, height);
LOG << _rect << _rect2;
}
void DrawWidget::clearListRect()
{
_listRect.clear();
update();
}
void DrawWidget::setListRect(QList<QRect> listRect)
{
_listRect = listRect;
update();
}
void DrawWidget::initControl()
{
// 置頂
::SetWindowPos(HWND(this->winId()), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
void DrawWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
drawSelectRect(&painter)