关键词搜索

源码搜索 ×
×

C/C++,Qt,Python,OpenCV小项目实战-实时桌面颜色查询

发布2018-10-16浏览4776次

详情内容

源码连接如下(含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的重点代码:

  1. #include <opencv2/core.hpp>
  2. #include <opencv2/highgui.hpp>
  3. #include <opencv2/imgproc.hpp>
  4. #include <opencv2/imgproc/imgproc_c.h>
  5. using namespace cv;
  6. extern "C"__declspec(dllexport) char* getColorName(char *FileName){
  7. char *colorName;
  8. Mat matSrc=imread(FileName,IMREAD_UNCHANGED);
  9. Mat hsvSrc;
  10. cvtColor(matSrc,hsvSrc,COLOR_BGR2HSV);
  11. int HValue,SValue,VValue;
  12. HValue=(int)hsvSrc.at<uchar>(0,0);
  13. SValue=(int)hsvSrc.at<uchar>(0,1);
  14. VValue=(int)hsvSrc.at<uchar>(0,2);
  15. if((HValue>=0&&HValue<=180)
  16. &&(SValue>=0&&SValue<=255)
  17. &&(VValue>=0&&VValue<=46)){
  18. colorName="黑";
  19. }
  20. else if((HValue>=0&&HValue<=180)
  21. &&(SValue>=0&&SValue<=43)
  22. &&(VValue>=46&&VValue<=220)){
  23. colorName="灰";
  24. }
  25. else if((HValue>=0&&HValue<=180)
  26. &&(SValue>=0&&SValue<=30)
  27. &&(VValue>=221&&VValue<=255)){
  28. colorName="白";
  29. }
  30. else if(((HValue>=0&&HValue<=10)||(HValue>=156&&HValue<=180))
  31. &&(SValue>=43&&SValue<=255)
  32. &&(VValue>=46&&VValue<=255)){
  33. colorName="红";
  34. }
  35. else if((HValue>=11&&HValue<=25)
  36. &&(SValue>=43&&SValue<=255)
  37. &&(VValue>=46&&VValue<=255)){
  38. colorName="橙";
  39. }
  40. else if((HValue>=26&&HValue<=34)
  41. &&(SValue>=43&&SValue<=255)
  42. &&(VValue>=46&&VValue<=255)){
  43. colorName="黄";
  44. }
  45. else if((HValue>=35&&HValue<=77)
  46. &&(SValue>=43&&SValue<=255)
  47. &&(VValue>=46&&VValue<=255)){
  48. colorName="绿";
  49. }
  50. else if((HValue>=78&&HValue<=99)
  51. &&(SValue>=43&&SValue<=255)
  52. &&(VValue>=46&&VValue<=255)){
  53. colorName="青";
  54. }
  55. else if((HValue>=100&&HValue<=124)
  56. &&(SValue>=43&&SValue<=255)
  57. &&(VValue>=46&&VValue<=255)){
  58. colorName="蓝";
  59. }
  60. else if((HValue>=125&&HValue<=155)
  61. &&(SValue>=43&&SValue<=255)
  62. &&(VValue>=46&&VValue<=255)){
  63. colorName="紫";
  64. }
  65. else{
  66. colorName="未知";
  67. }
  68. return colorName;
  69. }

 

胶水Python的源码:

  1. import ctypes
  2. import sys
  3. if __name__=='__main__':
  4. fileName=str(sys.argv[1])
  5. ll=ctypes.cdll.LoadLibrary
  6. lib =ll("judgeColor.dll")
  7. charPointer=bytes(fileName,"gbk")
  8. result=lib.getColorName(charPointer)
  9. pyResult=ctypes.string_at(result);
  10. result=pyResult.decode("gbk")
  11. print(result)
  12. pass

 

Qt源码如下:

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. namespace Ui {
  5. class Widget;
  6. }
  7. class Widget : public QWidget
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit Widget(QWidget *parent = 0);
  12. ~Widget();
  13. protected slots:
  14. void printMousePoint();
  15. protected:
  16. void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  17. void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  18. void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
  19. private:
  20. Ui::Widget *ui;
  21. bool m_dragging;
  22. bool m_isRunning;
  23. QPoint m_startPosition;
  24. QPoint m_framePosition;
  25. };
  26. #endif // WIDGET_H

main.cpp

  1. #include "widget.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Widget w;
  7. w.show();
  8. return a.exec();
  9. }

widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QDebug>
  4. #include <QProcess>
  5. #include <QTimer>
  6. #include <QPixmap>
  7. #include <QWindow>
  8. #include <QMouseEvent>
  9. #include <QEventLoop>
  10. #include <QScreen>
  11. #include <windows.h>
  12. Widget::Widget(QWidget *parent) :
  13. QWidget(parent),
  14. ui(new Ui::Widget)
  15. {
  16. ui->setupUi(this);
  17. setMouseTracking(true);
  18. QTimer *timer=new QTimer;
  19. connect(timer,SIGNAL(timeout()),this,SLOT(printMousePoint()));
  20. timer->start(20);
  21. setWindowFlags(Qt::WindowStaysOnTopHint|Qt::Window|Qt::FramelessWindowHint);
  22. setAttribute(Qt::WA_TranslucentBackground);
  23. m_dragging=false;
  24. m_isRunning=false;
  25. }
  26. Widget::~Widget()
  27. {
  28. delete ui;
  29. }
  30. void Widget::printMousePoint()
  31. {
  32. POINT p;
  33. if(GetCursorPos(&p)&&!m_isRunning){
  34. QWindow *window=windowHandle();
  35. QScreen *screen=window->screen();
  36. QPixmap pixmap=screen->grabWindow(0,(int)p.x,(int)p.y,1,1);
  37. QString filePath=qApp->applicationDirPath()+"/1.png";
  38. pixmap.save(filePath);
  39. QProcess p(0);
  40. QString cmdString="python "+qApp->applicationDirPath()+"/demo.py "+qApp->applicationDirPath()+"/1.png";
  41. m_isRunning=true;
  42. p.start("cmd", QStringList()<<"/c"<<cmdString);
  43. //p.waitForStarted();
  44. //p.waitForFinished();
  45. QEventLoop loop;
  46. connect(&p,SIGNAL(finished(int,QProcess::ExitStatus)),&loop,SLOT(quit()));
  47. loop.exec();
  48. QString strTemp=QString::fromLocal8Bit(p.readAllStandardOutput());
  49. strTemp=strTemp.left(1);
  50. ui->label->setText(strTemp);
  51. m_isRunning=false;
  52. }
  53. }
  54. void Widget::mouseMoveEvent(QMouseEvent *event)
  55. {
  56. if(event->buttons()&Qt::LeftButton){
  57. if(m_dragging){
  58. QPoint delta=event->globalPos()-m_startPosition;
  59. move(m_framePosition+delta);
  60. }
  61. }
  62. QWidget::mouseMoveEvent(event);
  63. }
  64. void Widget::mousePressEvent(QMouseEvent *event)
  65. {
  66. if(event->button()==Qt::LeftButton){
  67. m_dragging=true;
  68. m_startPosition=event->globalPos();
  69. m_framePosition=frameGeometry().topLeft();
  70. }
  71. QWidget::mousePressEvent(event);
  72. }
  73. void Widget::mouseReleaseEvent(QMouseEvent *event)
  74. {
  75. m_dragging=false;
  76. QWidget::mouseReleaseEvent(event);
  77. }

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载