显示图片代码:
void MainWindow::OpenCameraVideo()
{
//thread1->start();
QImage ig = cameraM->VideoCamera();
//QImage imgGray_imgShow = ig.scaled(300, 300, Qt::KeepAspectRatio, Qt::FastTransformation);
//ig.load("testCamera.jpg");
SceneM->clear();
SceneM->addPixmap(QPixmap::fromImage(ig));
ui->graphicsView->setScene(SceneM);
ui->graphicsView->show();
//recvShowPicSignal(ig);
}
在ui->graphicsView->setScene(SceneM);这里就出现错误
图片返回函数代码:
QImage Camera::VideoCamera(){
Mat openCvImage;
try {
// 开始抓取c_countOfImagesToGrab images.
//相机默认设置连续抓取模式
cameraM.StartGrabbing(c_countOfImagesToGrab, GrabStrategy_LatestImageOnly);
//抓取结果数据指针
Pylon::CGrabResultPtr ptrGrabResult;
Pylon::CPylonImage pylonImage;
// 等待接收和恢复图像,超时时间设0置为5000 ms.
cameraM.RetrieveResult(5000, ptrGrabResult, Pylon::TimeoutHandling_ThrowException);
if (cameraM.IsGrabbing())
{
//如果图像抓取成功
if (ptrGrabResult->GrabSucceeded())
{
//将抓取的缓冲数据转化成pylon image.
formatConverterM.Convert(pylonImage, ptrGrabResult);
// 将 pylon image转成OpenCV image.
openCvImage = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *)pylonImage.GetBuffer());
}
}
const uchar*qImageBuffer = (const uchar*)openCvImage.data;
//CreateQImagewithsamedimensionsasinputMat
//Mat img;
//cvtColor(openCvImage, openCvImage, CV_BGR2RGB);
//QImage img=QImage(qImageBuffer, openCvImage.cols, openCvImage.rows, openCvImage.step, QImage::Format_Indexed8);
//QImage(openCvImage.data, openCvImage.cols, openCvImage.rows, openCvImage.step, QImage::Format_RGB32)
//imwrite("testCamera.jpg", openCvImage);
cameraM.StopGrabbing();
cv::cvtColor(openCvImage, openCvImage, CV_BGR2RGB);
QImage img= QImage((const unsigned char*)(openCvImage.data), openCvImage.cols, openCvImage.rows, openCvImage.cols*openCvImage.channels(), QImage::Format_RGB888);
return img;
}
catch (GenICam::GenericException &e)
{
// Error handling.
std::cout << "An exception occurred." << std::endl
<< e.GetDescription() << std::endl;
cameraM.StopGrabbing();
}
}