簡體   English   中英

如何在 opencv python 中使用 PySpin?

[英]How to use PySpin with opencv python?

我剛買了一台 FLIR BlackFlyS USB3.0 相機。 我可以從相機捕捉幀,但如果不先保存它們,我就無法在 opencv 中使用該幀。 有沒有人知道如何將它們轉換為在 opencv 中使用?

我在互聯網上搜索了包含“PySpin”一詞的所有內容,並找到了這本書 我曾嘗試使用PySpinCapture中提到的PySpinCapture ,但無論如何我還是想不通。

capture = PySpinCapture.PySpinCapture(0, roi=(0, 0, 960, 600),binningRadius=2,isMonochrome=True)

ret, frame = capture.read()

cv2.imshow("image",frame)

cv2.waitKey(0)

我希望看到圖像,但它會引發錯誤

_PySpin.SpinnakerException: Spinnaker: GenICam::AccessException= Node is not writable. : AccessException thrown in node 'PixelFormat' while calling 'PixelFormat.SetIntValue()' (file 'EnumerationT.h', line 83) [-2006]
terminate called after throwing an instance of 'Spinnaker::Exception'

一年后,我不確定我的回答是否會有所幫助,但我發現您可以使用 GetData() 函數從 PySpin 圖像中獲取 RGB numpy 數組。

因此,您可以在沒有 PySpinCapture 模塊的情況下執行以下操作。

import PySpin
import cv2

serial = '18475994' #Probably different for you although I also use a BlackFly USB3.0

system = PySpin.System.GetInstance()

blackFly_list = system.GetCameras()

blackFly = blackFly_list.GetBySerial(serial)

height = blackFly.Height()
width = blackFly.Width()
channels = 1

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('test_vid.avi',fourcc, blackFly.AcquisitionFrameRate(), (blackFly.Width(), blackFly.Height()), False) #The last argument should be True if you are recording in color.


blackFly.Init()
blackFly.AcquisitionMode.SetValue(PySpin.AcquisitionMode_Continuous)
blackFly.BeginAcquisition()

nFrames = 1000

for _ in range(nFrames):
    im = blackFly.GetNextImage()    

    im_cv2_format = im.GetData().reshape(height,width,channels)

    # Here I am writing the image to a Video, but once you could save the image as something and just do whatever you want with it.
    out.write(im_cv2_format)
    im.release()

out.release()    

在此代碼示例中,我想創建一個包含 1000 個抓取幀的 AVI 視頻文件。 im.GetData() 返回一個一維 numpy 數組,然后可以通過 reshape 將其轉換為正確的維度。 我看過一些關於使用 UMat 類的討論,但在這種情況下似乎沒有必要讓它工作。 也許它有助於性能,但我不確定:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM