C/C++,Qt,Python,OpenCV小專案實戰-實時桌面顏色查詢
原始碼連線如下(含Qt,VS,Python)
https://download.csdn.net/download/qq78442761/10723417
程式執行截圖如下:
(原理)邏輯如下:
1.使用VS2012以及OpenCV3,編寫識別顏色的演算法,傳入一個影像(只有一個畫素(滑鼠當前畫素)),識別這個畫素是什麼顏色(識別原理在此不說,原理在這篇連線裡面https://blog.csdn.net/qq78442761/article/details/83056346),把程式做成C介面的dll。
2.使用Python呼叫演算法dll,並且接收返回過來的值
3.使用Qt擷取當前滑鼠的畫素點,呼叫Python進行分析,並且獲取返回值。
程式原始碼如下:
VS2012 OpenCV的重點程式碼:
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
using namespace cv;
extern "C"__declspec(dllexport) char* getColorName(char *FileName){
char *colorName;
Mat matSrc=imread(FileName,IMREAD_UNCHANGED);
Mat hsvSrc;
cvtColor(matSrc,hsvSrc,COLOR_BGR2HSV);
int HValue,SValue,VValue;
HValue=(int)hsvSrc.at<uchar>(0,0);
SValue=(int)hsvSrc.at<uchar>(0,1);
VValue=(int)hsvSrc.at<uchar>(0,2);
if((HValue>=0&&HValue<=180)
&&(SValue>=0&&SValue<=255)
&&(VValue>=0&&VValue<=46)){
colorName="黑";
}
else if((HValue>=0&&HValue<=180)
&&(SValue>=0&&SValue<=43)
&&(VValue>=46&&VValue<=220)){
colorName="灰";
}
else if((HValue>=0&&HValue<=180)
&&(SValue>=0&&SValue<=30)
&&(VValue>=221&&VValue<=255)){
colorName="白";
}
else if(((HValue>=0&&HValue<=10)||(HValue>=156&&HValue<=180))
&&(SValue>=43&&SValue<=255)
&&(VValue>=46&&VValue<=255)){
colorName="紅";
}
else if((HValue>=11&&HValue<=25)
&&(SValue>=43&&SValue<=255)
&&(VValue>=46&&VValue<=255)){
colorName="橙";
}
else if((HValue>=26&&HValue<=34)
&&(SValue>=43&&SValue<=255)
&&(VValue>=46&&VValue<=255)){
colorName="黃";
}
else if((HValue>=35&&HValue<=77)
&&(SValue>=43&&SValue<=255)
&&(VValue>=46&&VValue<=255)){
colorName="綠";
}
else if((HValue>=78&&HValue<=99)
&&(SValue>=43&&SValue<=255)
&&(VValue>=46&&VValue<=255)){
colorName="青";
}
else if((HValue>=100&&HValue<=124)
&&(SValue>=43&&SValue<=255)
&&(VValue>=46&&VValue<=255)){
colorName="藍";
}
else if((HValue>=125&&HValue<=155)
&&(SValue>=43&&SValue<=255)
&&(VValue>=46&&VValue<=255)){
colorName="紫";
}
else{
colorName="未知";
}
return colorName;
}
膠水Python的原始碼:
import ctypes
import sys
if __name__=='__main__':
fileName=str(sys.argv[1])
ll=ctypes.cdll.LoadLibrary
lib =ll("judgeColor.dll")
charPointer=bytes(fileName,"gbk")
result=lib.getColorName(charPointer)
pyResult=ctypes.string_at(result);
result=pyResult.decode("gbk")
print(result)
pass
Qt原始碼如下:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected slots:
void printMousePoint();
protected:
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
private:
Ui::Widget *ui;
bool m_dragging;
bool m_isRunning;
QPoint m_startPosition;
QPoint m_framePosition;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QProcess>
#include <QTimer>
#include <QPixmap>
#include <QWindow>
#include <QMouseEvent>
#include <QEventLoop>
#include <QScreen>
#include <windows.h>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setMouseTracking(true);
QTimer *timer=new QTimer;
connect(timer,SIGNAL(timeout()),this,SLOT(printMousePoint()));
timer->start(20);
setWindowFlags(Qt::WindowStaysOnTopHint|Qt::Window|Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
m_dragging=false;
m_isRunning=false;
}
Widget::~Widget()
{
delete ui;
}
void Widget::printMousePoint()
{
POINT p;
if(GetCursorPos(&p)&&!m_isRunning){
QWindow *window=windowHandle();
QScreen *screen=window->screen();
QPixmap pixmap=screen->grabWindow(0,(int)p.x,(int)p.y,1,1);
QString filePath=qApp->applicationDirPath()+"/1.png";
pixmap.save(filePath);
QProcess p(0);
QString cmdString="python "+qApp->applicationDirPath()+"/demo.py "+qApp->applicationDirPath()+"/1.png";
m_isRunning=true;
p.start("cmd", QStringList()<<"/c"<<cmdString);
//p.waitForStarted();
//p.waitForFinished();
QEventLoop loop;
connect(&p,SIGNAL(finished(int,QProcess::ExitStatus)),&loop,SLOT(quit()));
loop.exec();
QString strTemp=QString::fromLocal8Bit(p.readAllStandardOutput());
strTemp=strTemp.left(1);
ui->label->setText(strTemp);
m_isRunning=false;
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()&Qt::LeftButton){
if(m_dragging){
QPoint delta=event->globalPos()-m_startPosition;
move(m_framePosition+delta);
}
}
QWidget::mouseMoveEvent(event);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton){
m_dragging=true;
m_startPosition=event->globalPos();
m_framePosition=frameGeometry().topLeft();
}
QWidget::mousePressEvent(event);
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
m_dragging=false;
QWidget::mouseReleaseEvent(event);
}
相關文章
- 折半查詢(C++實現)C++
- openCV實戰專案--人臉考勤OpenCV
- OpenCV4影像處理--影像查詢表和顏色表OpenCV
- c++字串查詢函式實現C++字串函式
- 最新《30小時搞定Python網路爬蟲專案實戰》Python爬蟲
- python實戰專案Python
- Python網路爬蟲實戰小專案Python爬蟲
- 【實測】Python 和 C++ 下字串查詢的速度對比PythonC++字串
- python--顏色的RGB轉BGR(opencv)PythonOpenCV
- python-opencv顏色分析小工具PythonOpenCV
- Opencv專案實戰:14 手勢控制音量OpenCV
- C++,Java,Python,Javascript實現二分查詢演算法C++PythonJavaScript演算法
- python爬蟲實操專案_Python爬蟲開發與專案實戰 1.6 小結Python爬蟲
- C++ Qt開發:QHostInfo主機地址查詢元件C++QT元件
- 北京實時公交查詢——Flutter 入坑實戰Flutter
- 專案實戰小問題:
- Jenkins部署Python專案實戰JenkinsPython
- Python專案實戰例項Python
- [Python實戰]Python製作天氣查詢軟體Python
- C/C++ Qt TabWidget 實現多窗體建立C++QT
- Flutter實戰: 如何同時設定Container的圖片和顏色FlutterAI
- OpenCV翻譯專案總結三——對OpenCV效能、查詢表等分析OpenCV
- 小程式雲開發專案實戰
- Qt中用C++呼叫Python檔案的三種方法QTC++Python
- 專案實戰:Qt+OpenCV大家來找茬(Qt抓圖,穿透應用,識別區別,框選區別,微調位置)QTOpenCV穿透
- Kotlin DSL C++專案引入OpenCV異常處理(轉)KotlinC++OpenCV
- C++中一個名字查詢的小知識C++
- C++實現查詢本機資訊並且上報C++
- Hadoop - 實時查詢DrillHadoop
- 最新Python開發專案實戰(完整)Python
- C++實用程式設計——坦克大戰小遊戲C++程式設計遊戲
- linux下qt用c++呼叫pythonLinuxQTC++Python
- 『小幫廚』- React+AntD專案實戰React
- 實現隨機顏色隨機
- 二分查詢(c++)C++
- QT:用QWebSocket實現webchannel,實現C++與HTML通訊QTWebC++HTML
- Go語言專案實戰:基於開源資料的成語查詢Go
- python實現查詢糾錯Python