首页 > 解决方案 > 如何将 PySpin 与 opencv python 一起使用?

问题描述

我刚买了一台 FLIR BlackFlyS USB3.0 相机。我可以从相机中抓取帧,但如果不先保存它们,我就无法在 opencv 中使用该帧。有谁知道如何将它们转换为在 opencv 中使用?

我在互联网上搜索了包括“PySpin”这个词的所有内容,并找到了这本书。我试过用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'

标签: pythonopencvpyspin

解决方案


一年后,不确定我的回复是否会有所帮助,但我发现您可以使用 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() 返回一个 1-D numpy 数组,然后可以通过 reshape 将其转换为正确的维度。我看过一些关于使用 UMat 类的讨论,但似乎没有必要让它在这种情况下工作。也许它有助于提高性能,但我不确定:)


推荐阅读