C++ OpenCV 视频处理库:使用 QCamera 和 cv::Mat 实现实时视频处理
"#include "video_processing.h" #include "<opencv2/dnn.hpp>" #include "<opencv2/opencv.hpp>" #include "<opencv2/imgproc.hpp>" #include "<opencv2/highgui.hpp>"
VideoProcessor::VideoProcessor(QObject *parent) : QObject(parent) { m_camera = new QCamera(this); m_imageCapture = new QCameraImageCapture(m_camera, this);
connect(m_imageCapture, SIGNAL(imageAvailable(int,QVideoFrame)), this, SLOT(processFrame(int,QVideoFrame)));
m_camera->setCaptureMode(QCamera::CaptureStillImage);
m_camera->start();
}
VideoProcessor::~VideoProcessor(){ delete m_imageCapture; delete m_camera; }
void VideoProcessor::startCamera() { m_imageCapture->capture(); }
void VideoProcessor::processFrame(int requestid, const QImage &Image) { Q_UNUSED(requestid);
// Convert QVideoFrame to cv::Mat
cv::Mat srcImage = QImageToCvMat(Image);
// Process the image using OpenCV functions
// ...
cv::Mat dstImage = srcImage;
// Convert the processed image back to QImage
QImage processedImg(dstImage.data, dstImage.cols, dstImage.rows, QImage::Format_RGB888);
// Emit signal with the processed image
emit frameProcessed(processedImg);
emit frameProcessedCvMat(dstImage);
}
cv::Mat QImageToCvMat(const QImage& image) { cv::Mat mat; switch (image.format()) { case QImage::Format_RGB888: mat = cv::Mat(image.height(), image.width(), CV_8UC3, const_cast<uchar*>(image.bits()), image.bytesPerLine()); cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR); // 如果需要将RGB格式转换为BGR格式 break;
case QImage::Format_Grayscale8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, const_cast<uchar*>(image.bits()), image.bytesPerLine());
break;
// 其他QImage格式的处理方式
default:
qWarning() << \"Unsupported image format\";
break;
}
return mat;
}
// Main.c
#include
void frameProcessedCvMat(const cv::Mat& mat) { // Convert cv::Mat to QImage QImage dstImage(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
// Convert QImage to QPixmap
QPixmap pixmap = QPixmap::fromImage(dstImage);
// Display the QPixmap in a QLabel or other suitable widget
// ...
}
int main(int argc, char** argv) { QApplication a(argc, argv);
VideoProcessor *videoProcessor = new VideoProcessor();
// Connect the signal from VideoProcessor to the callback function
QObject::connect(videoProcessor, &VideoProcessor::frameProcessedCvMat, frameProcessedCvMat);
// ...
return a.exec();
}
// 代码示例:在VideoProcessor类中添加新的信号用于传递cv::Mat格式的图像 signals: void frameProcessedCvMat(const cv::Mat& mat);
// 代码示例:修改processFrame函数,将cv::Mat格式的图像通过新的信号发送出去 void VideoProcessor::processFrame(int requestid, const QImage &Image) { Q_UNUSED(requestid);
// Convert QVideoFrame to cv::Mat
cv::Mat srcImage = QImageToCvMat(Image);
// Process the image using OpenCV functions
// ...
// Emit signal with the processed image in cv::Mat format
emit frameProcessedCvMat(srcImage);
}
// 代码示例:在Main.c中,连接VideoProcessor的新信号,并在回调函数中将cv::Mat格式的图像转换为cv::Mat格式的图像,并在窗口中显示 void frameProcessedCvMat(const cv::Mat& mat) { // Convert cv::Mat to QImage QImage dstImage(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
// Convert QImage to QPixmap
QPixmap pixmap = QPixmap::fromImage(dstImage);
// Display the QPixmap in a QLabel or other suitable widget
// ...
}
int main(int argc, char** argv) { // ...
// Connect the signal from VideoProcessor to the callback function
QObject::connect(videoProcessor, &VideoProcessor::frameProcessedCvMat, frameProcessedCvMat);
// ...
return a.exec();
原文地址: https://www.cveoy.top/t/topic/pPMP 著作权归作者所有。请勿转载和采集!