linux+Qt+Opencv实现采集摄像头采集,截图,边缘检测,霍夫变换
linux+Qt+Opencv实现采集摄像头采集,截图,边缘检测,霍夫变换
最近在做⼀个机器视觉货物分拣系统,⼩⽩⼀个,从头开始学起。希望写博客可以督促⾃⼰加快步伐,完成任务!
系统:ubantu14.04
opencv版本 :3.0.0
Qt版本:5.7
程序功能:实现摄像头视频的采集,截图,边缘检测和霍夫直线检测。
运⾏效果:
原始截图
⽣成的灰度图
边缘检测和霍夫变换直线检测后的图像
⾸先你得先装好软件和库才⾏。⽹上教程很多,这个就不多说了。 话不多说,直接上程序:
前期防御塔保护机制1,新建⼀个qt wiget⼯程;
2,在.pro⽂件中最后加⼊opencv库⽂件
INCLUDEPATH += .
INCLUDEPATH += /usr/local/include
INCLUDEPATH += /usr/local/include/opencv
INCLUDEPATH += /usr/local/include/opencv2
INCLUDEPATH += /usr/local/include/opencv2/core
INCLUDEPATH += /usr/local/include/opencv2/highgui
INCLUDEPATH += /usr/local/include/opencv2/imgproc
INCLUDEPATH += /usr/local/include/opencv2/flann
INCLUDEPATH += /usr/local/include/opencv2/photo
INCLUDEPATH += /usr/local/include/opencv2/video
INCLUDEPATH += /usr/local/include/opencv2/features2d
INCLUDEPATH += /usr/local/include/opencv2/objdetect
INCLUDEPATH += /usr/local/include/opencv2/calib3d
INCLUDEPATH += /usr/local/include/opencv2/ml
INCLUDEPATH += /usr/local/include/opencv2/contrib
LIBS += `pkg-config opencv --cflags --libs`
3,在头⽂件中写⼊如下代码:
其中private slots:下的函数是在Ui界⾯添加按钮的槽函数后⾃动⽣成的
教师奖励制度#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
装修合同范本
#include <opencv2/opencv.hpp>
工业设计专业就业#include <iostream>
#include "highgui.h"
using namespace cv;
using namespace std;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_Cut_clicked();
void on_Open_clicked();
void on_Quit_clicked();
private:
Ui::Widget *ui;
Mat edges;
Mat mid_edges;
Mat frame;
Mat Gray_Frame;
Mat Canny_Frame;
QImage Img;
QImage Scaled_Img;
QImage Cut_Img;
QImage Gray_Img;
QImage  Scaled_Gray_Img;
QImage Canny_Img;
QImage Scaled_Canny_Img;
vector<Vec2f> lines;//定义⼀个⽮量结构lines⽤于存放得到的线段⽮量集合
四川旅游必去十大景点推荐
};
#endif // WIDGET_H
4,在widget.c⽂件下⾯加⼊以下代码:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
delete ui;
}
void Widget::on_Cut_clicked()
{
Cut_Img =Scaled_Img;
QLabel  *Img_label =new QLabel(this);
Img_label->setGeometry(360,20,300,200);
Img_label->setPixmap(QPixmap::fromImage(Cut_Img));
Img_label->resize( Img_label->pixmap()->size());
Img_label->show();
cvtColor(frame,Gray_Frame,CV_BGR2GRAY);
//anny算⼦边缘检测
Canny(Gray_Frame,Canny_Frame,75,80,3);
//霍夫变换直线检测算法---8bit pic,setp,angel,length_limit
HoughLines(Canny_Frame, lines, 3, CV_PI/20, 70, 0, 0 );
//依次在图中绘制出每条线
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 10000*(-b));
pt1.y = cvRound(y0 + 10000*(a));
pt2.x = cvRound(x0 - 10000*(-b));
pt2.y = cvRound(y0 - 10000*(a));
line( Canny_Frame, pt1, pt2, Scalar(55,100,195), 1, CV_AA);
}
/*
//灰度图显⽰
Gray_Img = QImage((const unsigned char*)(Gray_Frame.data), ls, ws, QImage::Format_Grayscale8);
大盘鸡做法
Scaled_Gray_Img=  Gray_Img.scaled(Gray_Img.width()/2,Gray_Img.height()/2,Qt::KeepAspectRatio) ;
QLabel  *Gray_Img_label =new QLabel(this);
Gray_Img_label->setGeometry(20,260,300,200);
Gray_Img_label->setPixmap(QPixmap::fromImage(Scaled_Gray_Img));
Gray_Img_label->resize( Gray_Img_label->pixmap()->size());
Gray_Img_label->show();
*/
Canny_Img = QImage((const unsigned char*)(Canny_Frame.data), ls, ws, QImage::Format_Grayscale8);      Scaled_Canny_Img=  Canny_Img.scaled(Canny_Img.width()/2,Canny_Img.height()/2,Qt::KeepAspectRatio) ;
QLabel  *Gray_Img_label =new QLabel(this);
Gray_Img_label->setGeometry(20,280,300,200);
Gray_Img_label->setPixmap(QPixmap::fromImage( Scaled_Canny_Img));
Gray_Img_label->resize( Gray_Img_label->pixmap()->size());
Gray_Img_label->show();
imwrite( "/home/zy/qt_pro/frame.jpg", frame );
imwrite( "/home/zy/qt_pro/Gray_Image.jpg", Gray_Frame );
imwrite( "/home/zy/qt_pro/Canny_Frame.jpg", Canny_Frame );
}
void Widget::on_Open_clicked()
{
VideoCapture cap(-1);
/
/设置视频格式
cap.set(CV_CAP_PROP_FRAME_WIDTH , 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT , 480);
if(!cap.isOpened())

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。