2014-06-08 42 views
3

我目前正在開發使用Basler相機acA1300-30gc的機器視覺應用程序。爲此,我正在使用Basler Pylon 4和OPENCV 2.4.9版,並且出現了一些問題。我正在嘗試使用Pylon SDK捕捉圖像並將其轉換爲Mat格式以供進一步分析。由於處理時間限制,我的目標是避免將圖像保存到硬盤驅動器,但要實時分析它。Basler Pylon 4 SDK和OPENCV 2.4.9,CPylonImage到Mat

在下面的代碼中,我嘗試捕獲圖像,將其轉換爲Mat格式並將其顯示在新窗口中,但我得到的窗口是空白的。如果有人能幫助我,請確認我的錯誤或解釋我怎樣才能以不同的方式達到我的目標,我將不勝感激。 (我可能應該補充說,相機正常工作,我已經能夠保存圖像到硬盤驅動器)。

謝謝。

這裏是我的代碼:

PylonAutoInitTerm autoInitTerm; 

    try 
    { 
     CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice()); 
     cout << "Dispositivo usado:"<<camera.GetDeviceInfo().GetModelName()<<endl; 

     CGrabResultPtr ptrGrabResult; 

     camera.GrabOne(500,ptrGrabResult,TimeoutHandling_ThrowException); 

     if(ptrGrabResult->GrabSucceeded()) 
     { 
      CPylonImage target; 

      CImageFormatConverter converter; 
      converter.OutputPixelFormat=PixelType_RGB8packed; 
      converter.OutputBitAlignment=OutputBitAlignment_MsbAligned; 

      converter.Convert(target,ptrGrabResult); 

      Mat image(target.GetWidth(),target.GetHeight(),CV_8UC1,target.GetBuffer(),Mat::AUTO_STEP); 

      if(image.empty()) 
      { 
       cout << "No se pudo cargar la imagen Mat" << endl; 
       return -1; 
      } 
      cout << "La imagen se presenta en la ventana *Captura*" << endl; 

      namedWindow("Captura",WINDOW_AUTOSIZE); 
      imshow("Captura", image); 

     } 
     else 
     { 
      cout<<"Error: "<<ptrGrabResult->GetErrorCode()<<" "<<ptrGrabResult->GetErrorDescription()<<endl; 
     } 
    } 

回答

5

你必須確保在像素類型匹配。在您的代碼示例中,您使用PixelType_RGB8packed作爲相機圖像,CV_8UC1作爲Mat像素類型。您應該改用CV_8UC3。另外,我會使用PixelType_BGR8packed而不是PixelType_RGB8packed,因爲BGR與Windows位圖兼容。我假設你使用Windows。

+0

這工作,因爲我現在能夠轉換圖像,因爲我需要它,但我有一個額外的問題。我得到的圖像是它應該是什麼的一個重複片段,出了什麼問題? – Eric

+0

我不知道我明白你的意思。也許你可以發佈圖片? –

+0

這是我從程序 https://www.dropbox.com/s/dyeeyv0cnj4ovq1/CapturaError.JPG 獲得的圖像這是我想捕捉的圖像(掛架觀衆獲得) HTTPS:/ /www.dropbox.com/s/vnb9wcpee73ko8z/testImage.tiff – Eric

0

我似乎無法能夠發表評論,但要完成上面的答案:

它更換後的工作對我來說:

Mat image(target.GetWidth(),target.GetHeight(),CV_16UC3,target.GetBuffer(),Mat::AUTO_STEP); 

通過

Mat image(target.GetHeight(),target.GetWidth(),CV_16UC3,target.GetBuffer(),Mat::AUTO_STEP); 

高度的註釋寬度

0

爲了修復轉換爲OpenCV數據(your image)y ou可以用這種方法複製緩衝區

Mat image(target.GetHeight(), target.GetWidth(), CV_8UC3); 
memcpy(image.ptr(),target.GetBuffer(),3*target.GetWidth()*target.GetHeight()); 

它對我有用。

1

這裏是一個更好的解決方案:

CImageFormatConverter fc; 

fc.OutputPixelFormat = PixelType_BGR8packed; 

CPylonImage image; 

if (ptrGrabResult->GrabSucceeded()) 
{ 
fc.Convert(image, ptrGrabResult); 
Mat cv_img = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3,(uint8_t*)image.GetBuffer()); 
imshow(src_window,cv_img); // display the image in OpenCV image window 
waitKey(1); 
} 
+1

你能解釋爲什麼它更好嗎? –

+0

可能是我用錯了詞。用pylon sdk做這件事比較容易 – sma