Zynq-7000学习笔记(十六)——opencv采集USB摄像头图像

PC平台:WINDOWS 10 64位 + 虚拟机Ubuntu 14.04

Xilinx设计开发套件:Xilinx_vivado_sdk_2015.4

开发板:ZedBoard

USB摄像头:罗技 C270(720P)

Linux源码:2016_R1

Linaro文件系统:linaro-vivid-developer-20150618-705.tar.gz

QT库:qt-everywhere-opensource-src-4.7.3.tar.gz

openCV: opencv-3.1.0

一、QT界面上添加一个button和两个label,一个显示原图像,另一个显示处理后的图像

二、代码

mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::image_show(QLabel *label, Mat image)
{
Mat rgb;
QImage img;

if(image.channels()==3)
{
cvtColor(image,rgb,CV_BGR2RGB);
img =QImage((const unsigned char*)(rgb.data),
rgb.cols,rgb.rows,
rgb.cols*rgb.channels(),
QImage::Format_RGB888);
}
else if(image.channels()==1)
{
img =QImage((const unsigned char*)(image.data),
image.cols,image.rows,
image.cols*image.channels(),
QImage::Format_Indexed8);
}
else
{
img =QImage((const unsigned char*)(image.data),
image.cols,image.rows,
image.cols*image.channels(),
QImage::Format_RGB888);
}

label->setPixmap(QPixmap::fromImage(img));
label->resize(label->pixmap()->size());

}

void MainWindow::on_pushButton_clicked()
{
Mat frame,edges, grey;
VideoCapture cap(0);
if(!cap.isOpened())
{
printf("failed to open video device\n");
}

cap>>frame;
image_show(ui->label, frame);
cvtColor(frame, edges, CV_BGR2GRAY);
blur(edges, edges, Size(7, 7));
Canny(edges, edges, 9, 11,3);
image_show(ui->label_2, edges);
}

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include
#include
#include
#include
#include

using namespace cv;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;

void image_show(QLabel *label, Mat image);
};

#endif // MAINWINDOW_H

来源:luotong86的专栏
*本文已由作者授权转发,如需转载请联系作者本人获得授权