简易版Qt + Opencv4 颜色检测(滑块改变lowerb和upperb)

简易版Qt + Opencv4 颜色检测(滑块改变lowerb和upperb)

简易版Qt + Opencv4 颜色检测(滑块改变lowerb和upperb)

 1 #include<opencv2/opencv.hpp>    //  opencv头文件
 2 
 3 QT_BEGIN_NAMESPACE
 4 namespace Ui { class MainWindow; }
 5 QT_END_NAMESPACE
 6 
 7 class MainWindow : public QMainWindow
 8 {
 9     Q_OBJECT
10 
11 public:
12     MainWindow(QWidget *parent = nullptr);
13     ~MainWindow();
14 private:
15     void init();
16     void setQImage();
17 
18 private slots:
19     void moveScrollBar();
20 
21 private:
22     Ui::MainWindow *ui;
23 
24     QImage img;
25     int hmin = 0, smin = 0, vmin = 0;
26     int hmax = 180, smax = 255, vmax = 255;
27     cv::Mat imgMat;    //  原图
28     cv::Mat hsvImg;    //  hsv图
29 };
 1 using namespace cv;
 2 
 3 MainWindow::MainWindow(QWidget *parent)
 4     : QMainWindow(parent)
 5     , ui(new Ui::MainWindow)
 6 {
 7     ui->setupUi(this);
 8 
 9     imgMat = imread("car1.jpg");      //  原图
10     cvtColor(imgMat, hsvImg, COLOR_BGR2HSV);    //  hsv相对rgb比较容易处理
11 
12     init();
13     setQImage();
14 
15     ui->label->setPixmap(QPixmap::fromImage(img));
16 }
17 
18 MainWindow::~MainWindow()
19 {
20     delete ui;
21 }
22 
23 void MainWindow::init()
24 {
25     this->resize(1200, 500);
26     ui->hminScorllBar->setRange(0, 180);
27     ui->hmaxScorllBar->setRange(0,180);     //  H是180°
28     ui->sminScorllBar->setRange(0, 255);
29     ui->smaxScorllBar->setRange(0, 255);
30     ui->vminScorllBar->setRange(0, 255);
31     ui->vmaxScorllBar->setRange(0, 255);
32 
33     ui->hminScorllBar->setValue(0);
34     ui->hmaxScorllBar->setValue(180);     //  H是180°
35     ui->sminScorllBar->setValue(0);
36     ui->smaxScorllBar->setValue(255);
37     ui->vminScorllBar->setValue(0);
38     ui->vmaxScorllBar->setValue(255);
39 
40     QVector<QScrollBar*> scrollBarList;
41     scrollBarList.append(ui->hminScorllBar);
42     scrollBarList.append(ui->hmaxScorllBar);
43     scrollBarList.append(ui->sminScorllBar);
44     scrollBarList.append(ui->smaxScorllBar);
45     scrollBarList.append(ui->vminScorllBar);
46     scrollBarList.append(ui->vmaxScorllBar);
47 
48     QVector<QLabel*> labelList;
49     labelList.append(ui->hminVal);
50     labelList.append(ui->hmaxVal);
51     labelList.append(ui->sminVal);
52     labelList.append(ui->smaxVal);
53     labelList.append(ui->vminVal);
54     labelList.append(ui->vmaxVal);
55 
56 
57     for (int i = 0; i < scrollBarList.size(); i++)
58     {
59         connect(scrollBarList.at(i), &QScrollBar::valueChanged,
60                 this, &MainWindow::moveScrollBar);
61         connect(scrollBarList.at(i), &QScrollBar::valueChanged,
62                 labelList.at(i), static_cast<void(QLabel::*)(int)>(&QLabel::setNum));
63     }
64 }
65 
66 void MainWindow::setQImage()
67 {
68     if(hsvImg.channels() == 3)
69     {
70         img = QImage((const uchar*)hsvImg.data,
71                      hsvImg.cols, hsvImg.rows,
72                      hsvImg.cols*hsvImg.channels(),
73                      QImage::Format_BGR888);    //  Opencv是BGR通道
74     }
75     else
76     {
77         img = QImage((const uchar*)hsvImg.data,
78                      hsvImg.cols, hsvImg.rows,
79                      hsvImg.cols*hsvImg.channels(),
80                      QImage::Format_Grayscale8);
81     }
82 }
83 
84 void MainWindow::moveScrollBar()
85 {
86     Scalar lower(ui->hminScorllBar->value(),
87                  ui->sminScorllBar->value(),
88                  ui->vminScorllBar->value());
89     Scalar upper(ui->hmaxScorllBar->value(),
90                  ui->smaxScorllBar->value(),
91                  ui->vmaxScorllBar->value());
92     inRange(imgMat, lower, upper, hsvImg);
93     setQImage();
94     ui->label->setPixmap(QPixmap::fromImage(img));
95 }

 

简易版Qt + Opencv4 颜色检测(滑块改变lowerb和upperb)

上一篇:docker 数据卷


下一篇:linux安装php环境,yum安装php环境