先说下不用GUI的程序
// UEyeOpenCV.hpp 摄像头SDK类
#ifndef UEYEOPENCV_HPP
#define UEYEOPENCV_HPP
#pragma once
#include <ueye.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
class UeyeOpencvCam {
public:
UeyeOpencvCam(int wdth, int heigh);
cv::Mat getFrame();
private:
HIDS hCam; //摄像机设备号
cv::Mat mattie;
int width;
int height;
};
#endif // UEYEOPENCV_HPP
// UEyeOpenCV.cpp
#include <UEyeOpenCV.hpp>
#include <iostream>
#include <ueye.h>
UeyeOpencvCam::UeyeOpencvCam(int wdth, int heigh) {
hCam = 1;
.....初始化省略.....
}
cv::Mat UeyeOpencvCam::getFrame() {
VOID* pMem;
is_GetImageMem(hCam, &pMem); //读取图像得函数,pMen就是这个图像
memcpy(mattie.ptr(), pMem, width * height * 3); //转化成opencv的mat矩阵,mattie
return mattie;
}
// main.cpp
#include <string>
#include <iostream>
#include <ueye.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include "UEyeOpenCV.hpp"
int main(){
cv::Mat image;
UeyeOpencvCam cam = UeyeOpencvCam(752,480);
while (true) {
cv::namedWindow("cam", CV_WINDOW_AUTOSIZE);
image= cam.getFrame();
cv::imshow("cam", image);
if (cv::waitKey(1) >= 0) {break;}
}
return 0;
}
现在我需要把上面的程序改写成QT的GUI程序,工程名camaraget,尝试写了下面得程序,,其中包含上面的UEyeOpenCV.hpp和UEyeOpenCV.cpp
// main.cpp 保持默认,只添加了UEyeOpenCV.hpp
#include "camaraget.h"
#include <QApplication>
#include "UEyeOpenCV.hpp"
int main(int argc, char *argv[]){
QApplication a(argc, argv);
camaraget w;
w.show();
return a.exec();
}
// camaraget.h
#ifndef CAMARAGET_H
#define CAMARAGET_H
#include <QWidget>
#include <QImage>
#include <QTimer>
#include<QPixmap>
#include"UEyeOpenCV.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
namespace Ui {
class camaraget;
}
class UeyeOpencvCam;
class camaraget : public QWidget
{
Q_OBJECT
public:
explicit camaraget(QWidget *parent = 0);
~camaraget();
private slots:
void openCamara();
void readFarme();
QImage Mat2QImage(const cv::Mat &mat);
private:
Ui::camaraget *ui;
QTimer *timer;
QImage image;
UeyeOpencvCam cam;
cv::Mat frame;
};
#endif // CAMARAGET_H
// camaraget.cpp
#include "camaraget.h"
#include "ui_camaraget.h"
#include "UEyeOpenCV.hpp"
camaraget::camaraget(QWidget *parent) :
QWidget(parent),
ui(new Ui::camaraget)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(readFarme())); // if time=33ms, read a frame
connect(ui->open, SIGNAL(clicked()), this, SLOT(openCamara()));
}
/********* open camara **********/
void camaraget::openCamara(){
cam = UeyeOpencvCam(752,480);
timer->start(33); // if time=33ms, go to readFarme())
}
/********* OpenCV MatImage to QImage **********/
QImage camaraget::Mat2QImage(const cv::Mat &mat){
QImage img;
cv::Mat rgb;
if(mat.channels()==3) {
//cvt Mat BGR 2 QImage RGB
cv::cvtColor(mat,rgb,CV_BGR2RGB);
img =QImage((const unsigned char*)(rgb.data),
rgb.cols,rgb.rows,
rgb.cols*rgb.channels(),
QImage::Format_RGB888);
}
else{
img =QImage((const unsigned char*)(mat.data),
mat.cols,mat.rows,
mat.cols*mat.channels(),
QImage::Format_RGB888);
}
return img;
}
/********* reaad frame ***********/
void camaraget::readFarme(){
frame = cam.getFrame();
QImage image = camaraget::Mat2QImage(frame);
ui->label->setPixmap(QPixmap::fromImage(image));
ui->label->resize(ui->label->pixmap()->size());
ui->label->show();
}
camaraget::~camaraget(){ delete ui;}
现在主要的问题就是在camaraget.h中,使用UeyeOpencvCam cam还是UeyeOpencvCam *cam,,或是这2种都不对???
1.如果使用UeyeOpencvCam cam
camaraget.cpp中的ui(new Ui::camaraget)处出现错误:error: no matching function for call to 'UeyeOpencvCam::UeyeOpencvCam()'
但是我都在开头添加摄像头类的头文件了啊,,不懂
2.如果使用UeyeOpencvCam *cam
frame = cam.getFrame()需要改成改成frame->cam.getFrame()
camaraget.cpp中的cam = UeyeOpencvCam(752,480)处有错误cannot convert 'UeyeOpencvCam' to 'UeyeOpencvCam*' in assignment
应该怎么解决,,求指教啊
------解决思路----------------------
第二种:cam = new UeyeOpenCvCam(752,480);