简体   繁体   English

通过 Python 控制 USB Thorlabs 相机 - OpenCV

[英]Controlling USB Thorlabs camera via Python - OpenCV

There are several topics on this but many of them are very very old and no real solutions have been offered (or none that work for me for sure).有几个关于此的主题,但其中许多主题非常古老,并且没有提供真正的解决方案(或者没有对我有用的肯定)。

I am trying various libraries to get Python to read the frames of my USB camera (DCC1545M) and they all have various module or DLL import errors.我正在尝试各种库来让 Python 读取我的 USB 相机 (DCC1545M) 的帧,它们都有各种模块或 DLL 导入错误。 I'm trying Instrumental, Thorcam API, py-harware, micromanager..我正在尝试 Instrumental、Thorcam API、py-harware、micromanager..

Specifically I would ideally love to get it to work with OpenCV, because of all the useful computer vision features that you can later use on the image, which I am not sure you can do with the other libraries.具体来说,我非常希望让它与 OpenCV 一起使用,因为您以后可以在图像上使用所有有用的计算机视觉功能,我不确定您是否可以使用其他库。

However, I encounter the same issue as everyone else, in that openCV can not read the USB camera in the first place.但是,我遇到了和其他人一样的问题,因为 openCV 首先无法读取 USB 摄像头。

cap = cv2.VideoCapture(1)              ## tried difference indices
  
cap.isOpened()                        ## returns FALSE
img_counter = 0
   
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

while True: 
    ret,frame = cap.read()                     ## returned frame is empty 
    cv2.imshow('preview',frame)
    k = cv2.waitKey(1)
    if k%256==32: # if SPACE is pressed, take image 
        img_name = 'frame_number_{}.png'.format(img_counter)
        cv2.imwrite(img_name,frame)
        print('frame taken ')
        img_counter += 1

cap.release()
cv2.destroyAllWindows()

I have installed the driver from Thorlabs website and I have the uc480_64.dll.我已经从 Thorlabs 网站安装了驱动程序,并且我有 uc480_64.dll。 The camera is successfully located using the Instrumental() library:使用 Instrumental() 库成功定位相机:

from instrumental import list_instruments, instrument
from ctypes import *

paramsets = list_instruments() ## camera found
print(paramsets)

which returns返回

[<ParamSet[UC480_Camera] serial=b'4102675270' model=b'C1285R12M' id=1>] [<ParamSet[UC480_Camera] 序列号=b'4102675270' 型号=b'C1285R12M' id=1>]

I was wondering if anyone knows if in the last couple of years openCV has managed to find a way to read USB cameras and if so, what is the way?我想知道是否有人知道在过去几年中 openCV 是否设法找到了一种读取 USB 摄像头的方法,如果是这样,那方法是什么? Or of any other reliable method, which allows further image processing on the captured frames.或者任何其他可靠的方法,它允许对捕获的帧进行进一步的图像处理。

PS: I posted this on superuser because apparently hardware questions are not allowed on stackoverflow, but suoeruser migrated it back here .. So apologies if it is off-topic here as well. PS:我在超级用户上发布了这个,因为显然 stackoverflow 上不允许出现硬件问题,但是 suoeruser 将它迁移回这里 .. 如果它在这里也跑题了,我深表歉意。

Can you communicate with the camera it its native software?您可以通过其本机软件与相机通信吗? https://www.thorlabs.com/software_pages/ViewSoftwarePage.cfm?Code=ThorCam https://www.thorlabs.com/software_pages/ViewSoftwarePage.cfm?Code=ThorCam

Our lab is using "pylablib cam-control" to communicate with a variety of cameras (including Thorlabs USB ones): https://pylablib-cam-control.readthedocs.io/en/latest/我们的实验室正在使用“pylablib cam-control”与各种相机(包括Thorlabs USB相机)进行通信: https ://pylablib-cam-control.readthedocs.io/en/latest/

Or if you would prefer writing your own code, pylablib includes a class for Thorlabs USB cameras (actually has been tested with your specific camera).或者,如果您更喜欢编写自己的代码,pylablib 包含一个用于 Thorlabs USB 相机的类(实际上已经用您的特定相机进行了测试)。 https://pylablib.readthedocs.io/en/latest/devices/uc480.html#cameras-uc480 https://pylablib.readthedocs.io/en/latest/devices/uc480.html#cameras-uc480

Try the following code.试试下面的代码。 It works with my Thorlab DCx camera:它适用于我的 Thorlab DCx 相机:

import cv2
import numpy as np
from instrumental.drivers.cameras import uc480

# init camera
instruments = uc480.list_instruments()
cam = uc480.UC480_Camera(instruments[0])

# params
cam.start_live_video(framerate = "10Hz")

while cam.is_open:
     
     frame = cam.grab_image(timeout='100s', copy=True, exposure_time='10ms')
     
     frame1 = np.stack((frame,) * 3,-1) #make frame as 1 channel image
     frame1 = frame1.astype(np.uint8)

     gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)

     #now u can apply opencv features

     cv2.imshow('Camera', gray)
     
     if cv2.waitKey(30) & 0xFF == ord('q'):
        break

cam.close()
cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM