中科因仑“3+1”工程特种兵精英论坛
标题:
基于QT与OpenCV“别踩白块”简易物理外挂 上位机部分
[打印本页]
作者:
贾想
时间:
2018-7-25 08:37
标题:
基于QT与OpenCV“别踩白块”简易物理外挂 上位机部分
本帖最后由 贾想 于 2018-7-25 08:39 编辑
摄像头显示
void MainWindow::cameraopen()
{
Mat input_pic_mat,output_pic_mat;
Mat input_pic_mat_2,output_pic_mat_2;
cam >> input_pic_mat;
cam >> input_pic_mat_2;
//recSerialData();
if(!input_pic_mat.empty())
{
output_pic_mat=demo(input_pic_mat);
//output_pic_mat=test(input_pic_mat);
qImage = QImage(static_cast<uchar *>(output_pic_mat.data),output_pic_mat.cols,output_pic_mat.rows,QImage::Format_RGB888);
//装载图片
//ui->label->resize(output_pic_mat.cols,output_pic_mat.rows);
ui->label->setPixmap(QPixmap::fromImage(qImage));
//显示图片,自动添加滚动条
ui->label->show();
//显示图片
}
//mat2
if(!input_pic_mat_2.empty())
{
//output_pic_mat_2=demo(input_pic_mat);
output_pic_mat_2=test(input_pic_mat);
qImage_2 = QImage(static_cast<uchar *>(output_pic_mat_2.data),output_pic_mat_2.cols,output_pic_mat_2.rows,QImage::Format_RGB888);
//装载图片
//ui->label_2->resize(output_pic_mat.cols,output_pic_mat.rows);
ui->label_2->setPixmap(QPixmap::fromImage(qImage_2));
//显示图片,自动添加滚动条
ui->label_2->show();
//显示图片
}
}
复制代码
作者:
贾想
时间:
2018-7-25 08:41
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);
}
复制代码
作者:
贾想
时间:
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/
include
DEPENDPATH
+=
$$
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/
include
DEPENDPATH
+=
$$
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/
include
DEPENDPATH
+=
$$
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/
include
DEPENDPATH
+=
$$
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/
include
DEPENDPATH
+=
$$
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/
include
DEPENDPATH
+=
$$
PWD
/../../../../usr/local/
include
欢迎光临 中科因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/)
Powered by Discuz! X3.4