C/C++,Qt,Python,OpenCV小项目实战-实时桌面颜色查询
发布日期:2021-06-30 10:57:13
浏览次数:2
分类:技术文章
本文共 4505 字,大约阅读时间需要 15 分钟。
源码连接如下(含Qt,VS,Python)
程序运行截图如下:
(原理)逻辑如下:
1.使用VS2012以及OpenCV3,编写识别颜色的算法,传入一个图像(只有一个像素(鼠标当前像素)),识别这个像素是什么颜色(识别原理在此不说,原理在这篇连接里面),把程序做成C接口的dll。
2.使用Python调用算法dll,并且接收返回过来的值
3.使用Qt截取当前鼠标的像素点,调用Python进行分析,并且获取返回值。
程序源码如下:
VS2012 OpenCV的重点代码:
#include#include #include #include 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 (0,0); SValue=(int)hsvSrc.at (0,1); VValue=(int)hsvSrc.at (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 ctypesimport sysif __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#includenamespace Ui {class Widget;}class Widget : public QWidget{ Q_OBJECTpublic: 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"#includeint 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#include #include #include #include #include #include #include #include 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"< 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);}
转载地址:https://it1995.blog.csdn.net/article/details/83070537 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年05月01日 06时04分33秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
JAVA中两个不同对象的equals方法使用注意点
2019-04-30
JAVA判断奇数的正确方法
2019-04-30
linux安装jdk
2019-04-30
百度移动搜索转码
2019-04-30
小程序app.json配置
2019-04-30
微信小程序上线发布流程
2019-04-30
你不知道的微信命令行
2019-04-30
Echarts饼图代码
2019-04-30
小本生意,请各位博友多多支持
2019-04-30
奇葩现象
2019-04-30
二维码工具类生成
2019-04-30
JSON的构建
2019-04-30
json-lib——JsonConfig详细使用说明
2019-04-30
JSONConfig处理日期
2019-04-30
Gson处理json数据,转换javaBean的时候,替换输出字段名,解析日期的坑
2019-04-30
给json对象添加属性
2019-04-30
中国移动DNS大全
2019-04-30
ORA-04031: 无法分配 4064 字节的共享内存
2019-04-30
mysql 单节点数据库压力测试
2019-04-30