ROI区域判断钢琴块、串口发送数据
- int MainWindow::black_sum(Mat input_mat)
- {
- int counter = 0;
- Mat_<uchar>::iterator it = input_mat.begin<uchar>();
- Mat_<uchar>::iterator itend = input_mat.end<uchar>();
- for(; it!=itend; ++it)
- {
- if(( *it) == 0) counter +=1;
- }
- return counter;
- }
- Mat MainWindow::demo(Mat input_mat)
- {
- Mat output_mat,output_mat_1,gray_mat,blur_mat,median_mat,thresh_mat;
- Mat input_mat_show = input_mat;
- int wight_edge_0,wight_edge_1,wight_edge_2,wight_edge_3,
- wight_edge_0_e,wight_edge_1_e,wight_edge_2_e,wight_edge_3_e;
- int wight_edge_s = 190;
- int wight_edge_e = 450;
- int high_edge_s = 200;
- int high_edge_e = 300;
- wight_edge_0 = wight_edge_s + 5; //10
- wight_edge_0_e = wight_edge_0 + ((wight_edge_e - wight_edge_s)/4 - 10 ); //10+150
- wight_edge_1 = wight_edge_0_e + 10;
- wight_edge_1_e = wight_edge_1 + ((wight_edge_e - wight_edge_s)/4 - 10 );
- wight_edge_2 = wight_edge_1_e + 10;
- wight_edge_2_e = wight_edge_2 + ((wight_edge_e - wight_edge_s)/4 - 10) ;
- wight_edge_3 = wight_edge_2_e + 10;
- wight_edge_3_e = wight_edge_3 + ((wight_edge_e - wight_edge_s)/4 - 10) ;
- GaussianBlur(input_mat, blur_mat, Size(3, 3), 1);
- cvtColor(blur_mat,gray_mat,CV_RGB2GRAY);
- medianBlur(gray_mat,median_mat,5);
- threshold(gray_mat, thresh_mat, 125, 255, THRESH_BINARY);
- 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));
- 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));
- 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));
- 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));
- cvtColor(input_mat_show,output_mat,CV_RGB2BGR);
- cvtColor(thresh_mat,output_mat_1,CV_GRAY2BGR);
- cv::rectangle(output_mat,cvPoint( 190, 20),cvPoint( 450, 460),cvScalar( 0, 255, 0),2,1,0);
- 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);
- 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);
- 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);
- 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);
- //channel_0
- if(black_sum(channel_0) >= 5)
- {
- UART_Send(0x01);
- putText(output_mat, "have", Point(wight_edge_0, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 255, 0, 0), 2);
- }
- else
- {
- UART_Send(0x11);
- putText(output_mat, "none", Point(wight_edge_0, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar(0, 255, 0), 2);
- }
- //channel_1
- if(black_sum(channel_1) >= 5)
- {
- UART_Send(0x02);
- putText(output_mat, "have", Point(wight_edge_1, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 255, 0, 0 ), 2);
- }
- else
- {
- UART_Send(0x12);
- putText(output_mat, "none", Point(wight_edge_1, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 0, 255, 0), 2);
- }
- //channel_2
- if(black_sum(channel_2) >= 5)
- {
- UART_Send(0x03);
- putText(output_mat, "have", Point(wight_edge_2, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 255, 0, 0), 2);
- }
- else
- {
- UART_Send(0x13);
- putText(output_mat, "none", Point(wight_edge_2, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 0, 255, 0), 2);
- }
- //channel_3
- if(black_sum(channel_3) >= 5)
- {
- UART_Send(0x04);
- putText(output_mat, "have", Point(wight_edge_3, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 255, 0, 0), 2);
- }
- else
- {
- UART_Send(0x14);
- putText(output_mat, "none", Point(wight_edge_3, 180), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar( 0, 255, 0), 2);
- }
- return output_mat;
- }
- Mat MainWindow::test(Mat input_mat)
- {
- Mat output_mat,imageSobel,gray_mat,blur_mat,median_mat,thresh_mat;
- cvtColor(input_mat,output_mat,CV_RGB2BGR);
- cvtColor(output_mat,gray_mat,CV_RGB2GRAY);
- //Laplacian(gray_mat, imageSobel, CV_16U);
- Sobel(gray_mat, imageSobel, CV_16U, 1, 1);
- double meanValue = 0.0;
- meanValue = mean(imageSobel)[0];
- stringstream meanValueStream;
- string meanValueString;
- meanValueStream << meanValue;
- meanValueStream >> meanValueString;
- meanValueString = "Focus definition: " + meanValueString;
- GaussianBlur(input_mat, blur_mat, Size(3, 3), 1);
- cvtColor(blur_mat,gray_mat,CV_RGB2GRAY);
- medianBlur(gray_mat,median_mat,5);
- threshold(gray_mat, thresh_mat, 125, 255, THRESH_BINARY);
- cvtColor(thresh_mat,output_mat,CV_GRAY2BGR);
- putText(output_mat, meanValueString, Point(20, 50), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar(255, 0, 0), 2);
- return output_mat;
- }
- void MainWindow::UART_Init()
- {
- foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
- {
- qDebug() << "Name : " << info.portName();
- qDebug() << "Description : " << info.description();
- qDebug() << "Manufacturer: " << info.manufacturer();
- qDebug() << "Serial Number: " << info.serialNumber();
- qDebug() << "System Location: " << info.systemLocation();
- }
- //myCom = new QSerialPort();
- myCom->setPortName("/dev/ttyUSB0");
- myCom->open(QIODevice::ReadWrite);
- myCom->setBaudRate(QSerialPort::Baud115200);
- myCom->setBaudRate(QSerialPort::Data8);
- myCom->setParity(QSerialPort::NoParity);
- myCom->setStopBits(QSerialPort::OneStop);
- myCom->setFlowControl(QSerialPort::NoFlowControl);
- }
复制代码
|