項目實戰:Qt+OpenCV大家來找茬(Qt抓圖,穿透應用,識別左右圖區別,框選區別,微調位置)

前言

  本項目的出現理由只是筆者的一個念頭,於是利用專業Qt和Opencv相關的知識開發一個輔助工具,本文章僅用於Qt和Opencv結合的學習。

 

Demo演示效果

  請添加圖片描述
  請添加圖片描述
  請添加圖片描述
  請添加圖片描述

運行包下載地址(供測試學習)

  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技術點

 

OpenCV技術點

 

項目模塊化部署

  項目的環境爲Qt5.9.3 mingw32版本,使用QtCreator開發,配合mingw32版本的Opencv3.4.10,下圖左側爲項目結構,右側爲實際文件夾部署結構。
  在這裏插入圖片描述

 

Qt代碼:DrawWdget

  該類的主要作用:

  • 覆蓋在遊戲窗口上
  • 全部透明窗口用以當作遊戲界面上的畫布
  • 對鼠標消息穿透(無法點擊中)
  • 識別出後繪製標記處不同的區域

Ui界面

  爲自動生成默認的,沒有任何改動。
  在這裏插入圖片描述
  一共繪製兩類圖形,一類是框出抓取圖的界面,一類是識別出後的區域,定義兩個緩存變量,用以繪製對應的區域矩形。

DrawWidegt.h

#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

DrawWidget.cpp

#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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章