process.h
//引入必要的头文件
#include "process.h"
#include "Test01.h"
Mat Process(Mat image) {
Mat m;
cvtColor(image, m, COLOR_RGB2GRAY);//转为灰度图
threshold(m, m, 120, 255, CV_THRESH_BINARY);//二值化图片
return m;
}
process.h
#pragma once
#include "Test01.h"
using namespace std;
using namespace cv;
//检测刀片
Mat Process(Mat image);
main.cpp
#include "Test01.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test01 w;
w.show();
return a.exec();
}
Test01.h
#pragma once
//引入qt相关头文件
#ifndef MYCLASS_H
#define MYCLASS_H
#include
#include
#include "ui_Test01.h"//.ui文件编译生成的文件
#include
#include
#include
#include //警告窗口
#include
#include
//引入pylon相关头文件
#include
//引入opencv相关头文件
#include
#include
#include
using namespace Pylon;
using namespace cv;
using namespace std;
class Test01 : public QMainWindow
{
Q_OBJECT
public:
Test01(QWidget *parent = Q_NULLPTR);
// ~Test01();//析构函数
static const uint32_t c_countOfImagesToGrab = 100;
private slots:
void on_open_clicked();//打开图片
void on_process_clicked();//处理图片
void on_OpenCameraBtn_clicked();//打开摄像头
// void getFrame();//打开图片
//void on_TakePicBtn_clicked();//拍照
//void on_CloseCameraBtn_clicked();//关闭摄像头
private:
Ui::Test01Class ui;
Mat image;//yua
// Mat m;//结果图片
QLabel *label;
QLabel *label2;
// void open();
// QAction *openAction();
// QTimer *timer;//定时器,用于定时取帧
// QImage *img;
Mat showimage;//摄像头每次抓取的图像为一帧,使用该指针指向一帧图像的内存空间
QImage Test01::MatToQImage(const cv::Mat& mat);//实现图像的转换
Mat Test01::QImage2cvMat(QImage image);
};
#endif
Test01.cpp
#include "Test01.h"
#include "ui_Test01.h"
#include "process.h"
Test01::Test01(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//添加信号与槽
connect(ui.OpenCameraBtn, SIGNAL(clicked()), this, SLOT(on_OpenCameraBtn_clicked()));
}
//打开图片
void Test01::on_open_clicked() {
QString filename;
filename = QFileDialog::getOpenFileName(this,
tr("选择图像"),
"",
tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));
if (filename.isEmpty())
{
return;
}
else
{
QImage* img2 = new QImage;
if (!(img2->load(filename))) //加载图像
{
QMessageBox::information(this,
tr("打开图像失败"),
tr("打开图像失败!"));
delete img2;
return;
}
//QImage i = *img2;
//image = QImage2cvMat(i);
string str = filename.toStdString(); // 将filename转变为string类型;
image = imread(str);//读取图片
QString qstr = QString::fromStdString(str);
if (image.empty()) {
ui.label_4->setText("open error");
ui.label_3->setText(qstr);
}
*img2 = img2->scaled(img2->width() / 3, img2->height() / 3, Qt::KeepAspectRatio);
QGraphicsScene *scene = new QGraphicsScene;
scene->addPixmap(QPixmap::fromImage(*img2));
ui.graphicsView->setScene(scene);
ui.graphicsView->resize(img2->width() + 10, img2->height() + 10);
ui.graphicsView->show();
//Mat image2=image.clone();
//QImage img = MatToQImage(image2);//转化为QImage
// QGraphicsScene *scene = new QGraphicsScene;
// scene->addPixmap(QPixmap::fromImage(img));
//label = new QLabel();
//label->setPixmap(QPixmap::fromImage(img));
//label->resize(QSize(img.width(), img.height()));
//ui.scrollArea->setWidget(label);
}
}
//处理图片
void Test01::on_process_clicked() {
Mat m=Process(image);//进行图像处理
QImage img = MatToQImage(m);//转化为~~
img = img.scaled(img.width() / 3, img.height() / 3, Qt::KeepAspectRatio);
QGraphicsScene *scene = new QGraphicsScene;
scene->addPixmap(QPixmap::fromImage(img));
ui.graphicsView_2->setScene(scene);
ui.graphicsView_2->resize(img.width() + 10, img.height() + 10);
ui.graphicsView_2->show();
}
//打开摄像头
void Test01::on_OpenCameraBtn_clicked() {
Mat src;
// The exit code of the sample application.
int exitCode = 0;
CPylonImage pylonImage;
CImageFormatConverter formatConverter;
// Before using any pylon methods, the pylon runtime must be initialized.
PylonInitialize();
try
{
CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
GenApi::INodeMap& nodemap = camera.GetNodeMap();
GenApi::CIntegerPtr width = nodemap.GetNode("Width");
GenApi::CIntegerPtr height = nodemap.GetNode("Height");
cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
camera.MaxNumBuffer = 5;
camera.StartGrabbing(c_countOfImagesToGrab);
CGrabResultPtr ptrGrabResult;
if (camera.IsGrabbing())
{
camera.RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
if (ptrGrabResult->GrabSucceeded())
{
const uint8_t *pImageBuffer = (uint8_t *)ptrGrabResult->GetBuffer();
#ifdef PYLON_WIN_BUILD
// Display the grabbed image.
//Pylon::DisplayImage(1, ptrGrabResult);
//system("pause");
#endif
formatConverter.Convert(pylonImage, ptrGrabResult);
//openCvImage = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *)pylonImage.GetBuffer());
src = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC1, (uint8_t *)pylonImage.GetBuffer());
QImage img = MatToQImage(src);
QPixmap pixmap = QPixmap::fromImage(img);
pixmap.scaled(ui.label_3->size(), Qt::KeepAspectRatio);//图片实现自适应大小
ui.label_3->setScaledContents(true);
ui.label_3->setPixmap(pixmap);
}
else
{
ui.label_3->setText("fail");
cout << "Error: " << ptrGrabResult->GetErrorCode() << " " << ptrGrabResult->GetErrorDescription() << endl;
}
}
}
catch (const GenericException &e)
{
exitCode = 1;
}
PylonTerminate();
}
////拍照
//void Test01::on_TakePicBtn_clicked() {
//
//}
////关闭摄像头
//void Test01::on_CloseCameraBtn_clicked() {
//
//}
//实现Mat向QImage图像的转换
QImage Test01::MatToQImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if (mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for (int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for (int row = 0; row < mat.rows; row++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if (mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if (mat.type() == CV_8UC4)
{
const uchar *pSrc = (const uchar*)mat.data;
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
return QImage();
}
}
//实现
Mat Test01::QImage2cvMat(QImage image)
{
ui.label_3->setText("打开1");
cv::Mat mat;
switch (image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.bits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.bits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.bits(), image.bytesPerLine());
break;
}
return mat;
}