這個是官方的例子,我把頭檔案,庫都配好后,可以運行。
但是它直接呼叫的是pylon的視窗,現在我想用opencv的視窗來顯示他
#include <pylon/PylonIncludes.h>
#include <pylon/PylonGUI.h>
// Namespace for using pylon objects.
using namespace Pylon;
// Namespace for using cout.
using namespace std;
// Number of images to be grabbed.
static const uint32_t c_countOfImagesToGrab = 100;
int main(int argc, char* argv[])
{
// The exit code of the sample application.
int exitCode = 0;
// Automagically call PylonInitialize and PylonTerminate to ensure the pylon runtime system
// is initialized during the lifetime of this object.
Pylon::PylonAutoInitTerm autoInitTerm;
try
{
// Create an instant camera object with the camera device found first.
CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());
// Print the model name of the camera.
cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
// The parameter MaxNumBuffer can be used to control the count of buffers
// allocated for grabbing. The default value of this parameter is 10.
camera.MaxNumBuffer = 5;
// Start the grabbing of c_countOfImagesToGrab images.
// The camera device is parameterized with a default configuration which
// sets up free-running continuous acquisition.
camera.StartGrabbing( c_countOfImagesToGrab);
// This smart pointer will receive the grab result data.
CGrabResultPtr ptrGrabResult;
// Camera.StopGrabbing() is called automatically by the RetrieveResult() method
// when c_countOfImagesToGrab images have been retrieved.
while ( camera.IsGrabbing())
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);
// Image grabbed successfully?
if (ptrGrabResult->GrabSucceeded())
{
// Access the image data.
cout << "SizeX: " << ptrGrabResult->GetWidth() << endl;
cout << "SizeY: " << ptrGrabResult->GetHeight() << endl;
const uint8_t *pImageBuffer = (uint8_t *) ptrGrabResult->GetBuffer();
cout << "Gray value of first pixel: " << (uint32_t) pImageBuffer[0] << endl << endl;
// Display the grabbed image.
Pylon::DisplayImage(1, ptrGrabResult);
}
else
{
cout << "Error: " << ptrGrabResult->GetErrorCode() << " " << ptrGrabResult->GetErrorDescription();
}
}
}
catch (GenICam::GenericException &e)
{
// Error handling.
cerr << "An exception occurred." << endl
<< e.GetDescription() << endl;
exitCode = 1;
}
// Comment the following two lines to disable waiting on exit.
cerr << endl << "Press Enter to exit." << endl;
while( cin.get() != '\n');
return exitCode;
}
opencv顯示的代碼很簡潔如下
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
int main( )
{
//宣告IplImage指標
IplImage* pFrame = NULL;
//獲取攝像頭
CvCapture* pCapture = cvCreateCameraCapture(0);
//CvCapture* pCapture2 = cvCreateCameraCapture(0);
//創建視窗
cvNamedWindow("攝像頭", 1);
//顯示視屏
while(1)
{
pFrame=cvQueryFrame( pCapture );
if(!pFrame)
break;
cvShowImage("攝像頭",pFrame);
char c=cvWaitKey(33);
if(c==27)
break;
}
cvReleaseCapture(&pCapture);
cvDestroyWindow("攝像頭");
return 0;
}
uj5u.com熱心網友回復:
#include <cv.h>#include <cxcore.h>
#include <highgui.h>
opencv的頭檔案是這幾個。剛剛 的是粘的。請大神們不吝賜教。
我對pylon剛剛接觸,公司趕得急,還只有我一個人搞這個,所以,遇到問題很艱難,網上也沒有別的例子可以參考,只能 坐等大神出現了
uj5u.com熱心網友回復:
#include <cv.h>#include <cxcore.h>
#include <highgui.h>
opencv的頭檔案是這幾個
uj5u.com熱心網友回復:
獲取的是電腦攝像頭,而不是basler相機啊uj5u.com熱心網友回復:
樓主,我最近也需要做這個,能不能加個QQ,我想請教一下您。uj5u.com熱心網友回復:
使用CvvImage類的DrawToHDC函式,將相機采集回來的圖片顯示在指定的picture控制元件上。。
uj5u.com熱心網友回復:
你好樓主,請問如何配置pylon頭檔案,庫,自己研究了半天不回,好著急uj5u.com熱心網友回復:
既然ptrGrabResult->GetBuffer()可以獲得捕獲的影像緩沖區,那你自己把它畫到任何表面都可以。uj5u.com熱心網友回復:
樓主請問你解決了嗎?能告訴我怎么弄得嗎?uj5u.com熱心網友回復:
我也是這個問題,等待大神uj5u.com熱心網友回復:
非大神,初學者,做了很久才做好了。在此給出我的程式,是直接提取出basler的sample里邊存放的圖片,通過opencv來顯示在對話框上的。[code=c
void CMFCOpencvDlg::OnBnClickedReadPic()
{
// TODO: 在此添加控制元件通知處理程式代碼
PylonInitialize();
try
{
CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
CString str;
str = camera.GetDeviceInfo().GetModelName();
GetDlgItem(IDC_STATIC)->SetWindowText(str);//這句話是顯示相機名字的,在statictext文本框顯示的
camera.Open();
camera.MaxNumBuffer = 5;
CImageFormatConverter formatCoverter;
formatCoverter.OutputPixelFormat = PixelType_BGR8packed;
CPylonBitmapImage pylonImage;
camera.StartGrabbing(c_countOfImagesToGrab, GrabStrategy_LatestImageOnly);
// This smart pointer will receive the grab result data.
CGrabResultPtr ptrGrabResult;
// Camera.StopGrabbing() is called automatically by the RetrieveResult() method
// when c_countOfImagesToGrab images have been retrieved.
while (camera.IsGrabbing())
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
camera.RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
//IplImage* pFrame;
// Image grabbed successfully?
if (ptrGrabResult->GrabSucceeded())
{
formatCoverter.Convert(pylonImage, ptrGrabResult);//影像轉換,怎么個轉換,應該是轉換成bitmap的型別
//Creat an OpenCV image from a pylon image.
img = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t*)pylonImage.GetBuffer());
IplImage imgTmp = img;
m_cvImage = cvCloneImage(&imgTmp);
if (m_cvImage == 0)
MessageBox(_T("Grab Picture Failed"));
else
DrawPicToHDC(m_cvImage, IDC_pic);
}
cvReleaseImage(&m_cvImage);
}
}
catch (cv::Exception& e)
{
const char * s_ERROR = e.what();
AfxMessageBox(_T("s_ERROR"));
}
}
然后里邊是有個DrawPicToHDC吧,這個是呼叫的函式,所以要先寫函式。
?void CMFCOpencvDlg::DrawPicToHDC(Mat img, UINT ID)
{
IplImage iplImg = IplImage(img);
CDC *pDC = GetDlgItem(ID)->GetDC();
HDC hDC = pDC->GetSafeHdc();
CRect rect;
GetDlgItem(ID)->GetClientRect(&rect);
CvvImage cimg;
cimg.CopyOf(&iplImg); // 復制圖片
cimg.DrawToHDC(hDC, &rect); // 將圖片繪制到顯示控制元件的指定區域內
ReleaseDC(pDC);
}
][/code]記住,添加這種函式需要去添加一些東西,什么包含庫目錄這種,不懂的再問我。
uj5u.com熱心網友回復:
非大神,初學者,做了很久才做好了。在此給出我的程式,是直接提取出basler的sample里邊存放的圖片,通過opencv來顯示在對話框上的。[code=c
void CMFCOpencvDlg::OnBnClickedReadPic()
{
// TODO: 在此添加控制元件通知處理程式代碼
PylonInitialize();
try
{
CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());
CString str;
str = camera.GetDeviceInfo().GetModelName();
GetDlgItem(IDC_STATIC)->SetWindowText(str);//這句話是顯示相機名字的,在statictext文本框顯示的
camera.Open();
camera.MaxNumBuffer = 5;
CImageFormatConverter formatCoverter;
formatCoverter.OutputPixelFormat = PixelType_BGR8packed;
CPylonBitmapImage pylonImage;
camera.StartGrabbing(c_countOfImagesToGrab, GrabStrategy_LatestImageOnly);
// This smart pointer will receive the grab result data.
CGrabResultPtr ptrGrabResult;
// Camera.StopGrabbing() is called automatically by the RetrieveResult() method
// when c_countOfImagesToGrab images have been retrieved.
while (camera.IsGrabbing())
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
camera.RetrieveResult(5000, ptrGrabResult, TimeoutHandling_ThrowException);
//IplImage* pFrame;
// Image grabbed successfully?
if (ptrGrabResult->GrabSucceeded())
{
formatCoverter.Convert(pylonImage, ptrGrabResult);//影像轉換,怎么個轉換,應該是轉換成bitmap的型別
//Creat an OpenCV image from a pylon image.
img = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t*)pylonImage.GetBuffer());
IplImage imgTmp = img;
m_cvImage = cvCloneImage(&imgTmp);
if (m_cvImage == 0)
MessageBox(_T("Grab Picture Failed"));
else
DrawPicToHDC(m_cvImage, IDC_pic);
}
cvReleaseImage(&m_cvImage);
}
}
catch (cv::Exception& e)
{
const char * s_ERROR = e.what();
AfxMessageBox(_T("s_ERROR"));
}
}
然后里邊是有個DrawPicToHDC吧,這個是呼叫的函式,所以要先寫函式。
?void CMFCOpencvDlg::DrawPicToHDC(Mat img, UINT ID)
{
IplImage iplImg = IplImage(img);
CDC *pDC = GetDlgItem(ID)->GetDC();
HDC hDC = pDC->GetSafeHdc();
CRect rect;
GetDlgItem(ID)->GetClientRect(&rect);
CvvImage cimg;
cimg.CopyOf(&iplImg); // 復制圖片
cimg.DrawToHDC(hDC, &rect); // 將圖片繪制到顯示控制元件的指定區域內
ReleaseDC(pDC);
}
][/code]記住,添加這種函式需要去添加一些東西,什么包含庫目錄zhezho不懂的再問我。
uj5u.com熱心網友回復:
樓主,求幫忙,我也是新手轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/60152.html
