中科因仑“3+1”工程特种兵精英论坛

标题: 基于QT与OpenCV“别踩白块”简易物理外挂 上位机部分 [打印本页]

作者: 贾想    时间: 2018-7-25 08:37
标题: 基于QT与OpenCV“别踩白块”简易物理外挂 上位机部分
本帖最后由 贾想 于 2018-7-25 08:39 编辑

摄像头显示
  1. void MainWindow::cameraopen()
  2. {

  3.     Mat input_pic_mat,output_pic_mat;
  4.     Mat input_pic_mat_2,output_pic_mat_2;



  5.     cam >> input_pic_mat;

  6.     cam >> input_pic_mat_2;

  7.     //recSerialData();

  8.     if(!input_pic_mat.empty())
  9.     {


  10.     output_pic_mat=demo(input_pic_mat);

  11.     //output_pic_mat=test(input_pic_mat);


  12.     qImage = QImage(static_cast<uchar *>(output_pic_mat.data),output_pic_mat.cols,output_pic_mat.rows,QImage::Format_RGB888);

  13.     //装载图片
  14.     //ui->label->resize(output_pic_mat.cols,output_pic_mat.rows);

  15.     ui->label->setPixmap(QPixmap::fromImage(qImage));
  16.     //显示图片,自动添加滚动条

  17.     ui->label->show();
  18.     //显示图片

  19.     }
  20.     //mat2
  21.     if(!input_pic_mat_2.empty())
  22.     {


  23.     //output_pic_mat_2=demo(input_pic_mat);

  24.     output_pic_mat_2=test(input_pic_mat);


  25.     qImage_2 = QImage(static_cast<uchar *>(output_pic_mat_2.data),output_pic_mat_2.cols,output_pic_mat_2.rows,QImage::Format_RGB888);

  26.     //装载图片
  27.     //ui->label_2->resize(output_pic_mat.cols,output_pic_mat.rows);

  28.     ui->label_2->setPixmap(QPixmap::fromImage(qImage_2));
  29.     //显示图片,自动添加滚动条

  30.     ui->label_2->show();
  31.     //显示图片

  32.     }


  33. }
复制代码






作者: 贾想    时间: 2018-7-25 08:41
ROI区域判断钢琴块、串口发送数据
  1. int MainWindow::black_sum(Mat input_mat)
  2. {
  3.     int counter = 0;

  4.     Mat_<uchar>::iterator it = input_mat.begin<uchar>();

  5.     Mat_<uchar>::iterator itend = input_mat.end<uchar>();

  6.     for(; it!=itend; ++it)
  7.     {
  8.         if(( *it) == 0) counter +=1;
  9.     }

  10.     return counter;

  11. }

  12. Mat MainWindow::demo(Mat input_mat)
  13. {
  14.     Mat output_mat,output_mat_1,gray_mat,blur_mat,median_mat,thresh_mat;

  15.     Mat input_mat_show = input_mat;

  16.     int wight_edge_0,wight_edge_1,wight_edge_2,wight_edge_3,
  17.             wight_edge_0_e,wight_edge_1_e,wight_edge_2_e,wight_edge_3_e;

  18.     int wight_edge_s = 190;
  19.     int wight_edge_e = 450;

  20.     int high_edge_s = 200;
  21.     int high_edge_e = 300;

  22.     wight_edge_0 = wight_edge_s + 5; //10
  23.     wight_edge_0_e = wight_edge_0 + ((wight_edge_e - wight_edge_s)/4 - 10 ); //10+150

  24.     wight_edge_1 = wight_edge_0_e + 10;
  25.     wight_edge_1_e = wight_edge_1 + ((wight_edge_e - wight_edge_s)/4 - 10 );

  26.     wight_edge_2 = wight_edge_1_e + 10;
  27.     wight_edge_2_e = wight_edge_2 + ((wight_edge_e - wight_edge_s)/4 - 10) ;

  28.     wight_edge_3 = wight_edge_2_e + 10;
  29.     wight_edge_3_e = wight_edge_3 + ((wight_edge_e - wight_edge_s)/4 - 10) ;



  30.     GaussianBlur(input_mat, blur_mat, Size(3, 3), 1);

  31.     cvtColor(blur_mat,gray_mat,CV_RGB2GRAY);

  32.     medianBlur(gray_mat,median_mat,5);

  33.     threshold(gray_mat, thresh_mat, 125, 255, THRESH_BINARY);

  34.     Mat channel_0 = thresh_mat(Rect(wight_edge_0 + ((wight_edge_0_e - wight_edge_0)/4), high_edge_s + 35, (wight_edge_0_e - wight_edge_0)/4, 25));
  35.     Mat channel_1 = thresh_mat(Rect(wight_edge_1 + (wight_edge_0_e - wight_edge_0)/4, high_edge_s + 35, (wight_edge_0_e - wight_edge_0)/4,  25));
  36.     Mat channel_2 = thresh_mat(Rect(wight_edge_2 + (wight_edge_0_e - wight_edge_0)/4, high_edge_s + 35,  (wight_edge_0_e - wight_edge_0)/4,  25));
  37.     Mat channel_3 = thresh_mat(Rect(wight_edge_3 + (wight_edge_0_e - wight_edge_0)/4, high_edge_s + 35,  (wight_edge_0_e - wight_edge_0)/4,  25));




  38.     cvtColor(input_mat_show,output_mat,CV_RGB2BGR);

  39.     cvtColor(thresh_mat,output_mat_1,CV_GRAY2BGR);

  40.     cv::rectangle(output_mat,cvPoint( 190, 20),cvPoint( 450, 460),cvScalar( 0, 255, 0),2,1,0);

  41.     cv::rectangle(output_mat,cvPoint( wight_edge_0, high_edge_s),cvPoint( wight_edge_0_e, high_edge_e),cvScalar( 0, 0, 255),2,1,0);
  42.     cv::rectangle(output_mat,cvPoint( wight_edge_1, high_edge_s),cvPoint( wight_edge_1_e, high_edge_e),cvScalar( 0, 0, 255),2,1,0);
  43.     cv::rectangle(output_mat,cvPoint( wight_edge_2, high_edge_s),cvPoint( wight_edge_2_e, high_edge_e),cvScalar( 0, 0, 255),2,1,0);
  44.     cv::rectangle(output_mat,cvPoint( wight_edge_3, high_edge_s),cvPoint( wight_edge_3_e, high_edge_e),cvScalar( 0, 0, 255),2,1,0);

  45. //channel_0
  46.     if(black_sum(channel_0) >= 5)
  47.     {
  48.         UART_Send(0x01);
  49.         putText(output_mat, "have", Point(wight_edge_0, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 255, 0, 0), 2);
  50.     }
  51.     else
  52.     {
  53.         UART_Send(0x11);
  54.         putText(output_mat, "none", Point(wight_edge_0, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar(0, 255, 0), 2);
  55.     }
  56. //channel_1
  57.     if(black_sum(channel_1) >= 5)
  58.     {
  59.         UART_Send(0x02);
  60.         putText(output_mat, "have", Point(wight_edge_1, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 255, 0, 0 ), 2);
  61.     }
  62.     else
  63.     {
  64.         UART_Send(0x12);
  65.         putText(output_mat, "none", Point(wight_edge_1, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 0, 255, 0), 2);
  66.     }
  67. //channel_2
  68.     if(black_sum(channel_2) >= 5)
  69.     {
  70.         UART_Send(0x03);
  71.         putText(output_mat, "have", Point(wight_edge_2, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 255, 0,  0), 2);
  72.     }
  73.     else
  74.     {
  75.         UART_Send(0x13);
  76.         putText(output_mat, "none", Point(wight_edge_2, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 0, 255, 0), 2);
  77.     }
  78. //channel_3
  79.     if(black_sum(channel_3) >= 5)
  80.     {
  81.         UART_Send(0x04);
  82.         putText(output_mat, "have", Point(wight_edge_3, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 255, 0, 0), 2);
  83.     }
  84.     else
  85.     {
  86.         UART_Send(0x14);
  87.         putText(output_mat, "none", Point(wight_edge_3, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 0, 255, 0), 2);
  88.     }




  89. return  output_mat;
  90. }






  91. Mat MainWindow::test(Mat input_mat)
  92. {
  93.     Mat output_mat,imageSobel,gray_mat,blur_mat,median_mat,thresh_mat;




  94.     cvtColor(input_mat,output_mat,CV_RGB2BGR);

  95.     cvtColor(output_mat,gray_mat,CV_RGB2GRAY);

  96.     //Laplacian(gray_mat, imageSobel, CV_16U);
  97.     Sobel(gray_mat, imageSobel, CV_16U, 1, 1);

  98.     double meanValue = 0.0;
  99.     meanValue = mean(imageSobel)[0];
  100.     stringstream meanValueStream;
  101.     string meanValueString;
  102.     meanValueStream << meanValue;
  103.     meanValueStream >> meanValueString;
  104.     meanValueString = "Focus definition: " + meanValueString;


  105.     GaussianBlur(input_mat, blur_mat, Size(3, 3), 1);

  106.     cvtColor(blur_mat,gray_mat,CV_RGB2GRAY);

  107.     medianBlur(gray_mat,median_mat,5);

  108.     threshold(gray_mat, thresh_mat, 125, 255, THRESH_BINARY);

  109.     cvtColor(thresh_mat,output_mat,CV_GRAY2BGR);

  110.     putText(output_mat, meanValueString, Point(20, 50), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar(255, 0, 0), 2);
  111.     return output_mat;

  112. }


  113. void MainWindow::UART_Init()
  114. {
  115.     foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
  116.         {
  117.             qDebug() << "Name : " << info.portName();
  118.             qDebug() << "Description : " << info.description();
  119.             qDebug() << "Manufacturer: " << info.manufacturer();
  120.             qDebug() << "Serial Number: " << info.serialNumber();
  121.             qDebug() << "System Location: " << info.systemLocation();
  122.         }

  123.     //myCom = new QSerialPort();
  124.     myCom->setPortName("/dev/ttyUSB0");
  125.     myCom->open(QIODevice::ReadWrite);
  126.     myCom->setBaudRate(QSerialPort::Baud115200);
  127.     myCom->setBaudRate(QSerialPort::Data8);
  128.     myCom->setParity(QSerialPort::NoParity);
  129.     myCom->setStopBits(QSerialPort::OneStop);
  130.     myCom->setFlowControl(QSerialPort::NoFlowControl);
  131. }


复制代码



作者: 贾想    时间: 2018-7-25 08:45
在pro文件中添加
INCLUDEPATH += /usr/local/include \                      /usr/local/include/opencv \                /usr/local/include/opencv2 \                /usr/local/include/opencv2/video\                /usr/local/include/opencv2/imgproc\                /usr/local/include/opencv2/videoio


win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/release/ -lopencv_videoioelse:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/debug/ -lopencv_videoioelse:unix: LIBS += -L$$PWD/../../../../usr/local/lib/ -lopencv_videoio
INCLUDEPATH += $$PWD/../../../../usr/local/includeDEPENDPATH += $$PWD/../../../../usr/local/include

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/release/ -lopencv_imgcodecselse:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/debug/ -lopencv_imgcodecselse:unix: LIBS += -L$$PWD/../../../../usr/local/lib/ -lopencv_imgcodecs
INCLUDEPATH += $$PWD/../../../../usr/local/includeDEPENDPATH += $$PWD/../../../../usr/local/include

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/release/ -lopencv_imgprocelse:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/debug/ -lopencv_imgprocelse:unix: LIBS += -L$$PWD/../../../../usr/local/lib/ -lopencv_imgproc
INCLUDEPATH += $$PWD/../../../../usr/local/includeDEPENDPATH += $$PWD/../../../../usr/local/include

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/release/ -lopencv_coreelse:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/debug/ -lopencv_coreelse:unix: LIBS += -L$$PWD/../../../../usr/local/lib/ -lopencv_core
INCLUDEPATH += $$PWD/../../../../usr/local/includeDEPENDPATH += $$PWD/../../../../usr/local/include

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/release/ -lopencv_highguielse:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/debug/ -lopencv_highguielse:unix: LIBS += -L$$PWD/../../../../usr/local/lib/ -lopencv_highgui
INCLUDEPATH += $$PWD/../../../../usr/local/includeDEPENDPATH += $$PWD/../../../../usr/local/include

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/release/ -ltesseractelse:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/local/lib/debug/ -ltesseractelse:unix: LIBS += -L$$PWD/../../../../usr/local/lib/ -ltesseract
INCLUDEPATH += $$PWD/../../../../usr/local/includeDEPENDPATH += $$PWD/../../../../usr/local/include







欢迎光临 中科因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/) Powered by Discuz! X3.4