QHYCCD

Taking single frame in Python - problem with exposure time/settings

I've made a Python singe frame capture script, it "works" but it seems exposure time/gain/something is not setting properly as the image data looks super under exposed catching only bright light bulbs.

In qhyccdstruct.h there is enum CONTROL_ID with /*8*/  CONTROL_EXPOSURE so I use something like:

Code: [Select]
EXPOSURE_TIME = ctypes.c_uint32(8)
qhyccd.SetQHYCCDParam(camera_handle, EXPOSURE_TIME, ctypes.c_uint32(15200000))
And it does return 0 so "ok" but the image isn't looking as expected (using QHY5-III 174 mono)

Full code:
Code: [Select]
import ctypes

import cv2
import numpy


qhyccd = ctypes.CDLL("/path/to/sdk_linux64_21.03.13/usr/local/lib/libqhyccd.so")
qhyccd.GetQHYCCDParam.restype = ctypes.c_double
qhyccd.OpenQHYCCD.restype = ctypes.POINTER(ctypes.c_uint32)

result = qhyccd.InitQHYCCDResource()
if result == 0:
    print("InitSDK success\n")
else:
    raise Exception('No SDK')

cameras_found = qhyccd.ScanQHYCCD()
if cameras_found > 0:
    print("found camera\n")
else:
    raise Exception('No Camera')

position_id = 0
type_char_array_32 = ctypes.c_char * 32
id_object = type_char_array_32()
result = qhyccd.GetQHYCCDId(position_id, id_object)

camera_handle = qhyccd.OpenQHYCCD(id_object)

qhyccd.SetQHYCCDStreamMode(camera_handle, ctypes.c_uint32(0))
qhyccd.InitQHYCCD(camera_handle)

chipWidthMM = ctypes.c_uint32(0)
chipHeightMM = ctypes.c_uint32(0)
maxImageSizeX = ctypes.c_uint32(0)
maxImageSizeY = ctypes.c_uint32(0)
pixelWidthUM = ctypes.c_uint32(0)
pixelHeightUM = ctypes.c_uint32(0)
bpp = ctypes.c_uint32(0)
camera_info = qhyccd.GetQHYCCDChipInfo(
    camera_handle, ctypes.byref(chipWidthMM), ctypes.byref(chipHeightMM), ctypes.byref(maxImageSizeX),
    ctypes.byref(maxImageSizeY), ctypes.byref(pixelWidthUM), ctypes.byref(pixelHeightUM),
    ctypes.byref(bpp),
)
print([
    chipWidthMM.value, chipHeightMM.value, maxImageSizeX.value, maxImageSizeY.value,
    pixelWidthUM.value, pixelHeightUM.value, bpp.value
])

GAIN = ctypes.c_uint32(8)
EXPOSURE_TIME = ctypes.c_uint32(8)
depth = ctypes.c_uint32(8)

qhyccd.SetQHYCCDBitsMode(camera_handle, depth)
qhyccd.SetQHYCCDParam(camera_handle, GAIN, ctypes.c_uint32(100))
qhyccd.SetQHYCCDParam(camera_handle, EXPOSURE_TIME, ctypes.c_uint32(15200000))
qhyccd.SetQHYCCDResolution(camera_handle, ctypes.c_uint32(0), ctypes.c_uint32(0), maxImageSizeX, maxImageSizeY)
qhyccd.SetQHYCCDBinMode(camera_handle, ctypes.c_uint32(1), ctypes.c_uint32(1))
qhyccd.ExpQHYCCDSingleFrame(camera_handle)

image_data = (ctypes.c_uint8 * maxImageSizeX.value * maxImageSizeY.value)()
channels = ctypes.c_uint32(1)

response = qhyccd.GetQHYCCDSingleFrame(
    camera_handle, ctypes.byref(maxImageSizeX), ctypes.byref(maxImageSizeY),
    ctypes.byref(depth), ctypes.byref(channels), image_data,
)

print('RESPONSE: %s' % response)
bytes_data = bytearray(image_data)
print(bytes_data[0], bytes_data[1])

raw_array = numpy.array(bytes_data)
mono_image = raw_array.reshape(maxImageSizeY.value, maxImageSizeX.value)
cv2.imwrite('frame.tiff', mono_image)


qhyccd.CancelQHYCCDExposingAndReadout(camera_handle)
qhyccd.CloseQHYCCD(camera_handle)
qhyccd.ReleaseQHYCCDResource()

Any ideas what's wrong?
« Last Edit: April 11, 2021, 10:10:28 AM by riklaunim »

Re: Taking single frame in Python - problem with exposure time/settings
« Reply #1 on: April 11, 2021, 08:20:34 PM »
Hi,
    I seem you setup expose time too long,15200000 is 15.2s,you can try decrease this value.
Best Regards,
QinXiaoXu

Re: Taking single frame in Python - problem with exposure time/settings
« Reply #2 on: April 12, 2021, 05:24:40 PM »
Hi,
    I seem you setup expose time too long,15200000 is 15.2s,you can try decrease this value.
Best Regards,
QinXiaoXu
I've started with 10 and went up and no effect. With a C-mount lens only a light bulb manages to show up:


Re: Taking single frame in Python - problem with exposure time/settings
« Reply #3 on: April 12, 2021, 08:41:54 PM »
Hi,
    You can turn debug output on with qhyccd.ini,just put it in same folder with libqhyccd.so.And send the debug info to me.
    And you also can use GetQHYCCDParam function to check if your parameters be setuped successfully.
    And which camera do you use?And if you test it on windows or other software,can it work normally?
Best Regards,
QinXiaoXu

Re: Taking single frame in Python - problem with exposure time/settings
« Reply #4 on: April 12, 2021, 08:46:26 PM »
Also you can try add this code:
Code: [Select]
SetQHYCCDParam.restype = c_uint32
SetQHYCCDParam.argtypes = [c_void_p, c_int, c_double]

Re: Taking single frame in Python - problem with exposure time/settings
« Reply #5 on: April 13, 2021, 05:42:49 PM »
Also you can try add this code:
Code: [Select]
SetQHYCCDParam.restype = c_uint32
SetQHYCCDParam.argtypes = [c_void_p, c_int, c_double]


I used
Code: [Select]
qhyccd.SetQHYCCDParam.restype = ctypes.c_uint32
qhyccd.SetQHYCCDParam.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint32]

As for the debug it seems the exposure time isn't set and it's trying with exposure time 0 ?

I've checked with oacapture (Ubuntu) and the QHY5III174 works without problems there.[/code]

Re: Taking single frame in Python - problem with exposure time/settings
« Reply #6 on: April 14, 2021, 08:20:19 PM »
    Well,maybe you can try use double value to setup.The function need double value.
Best Regards,
QinXiaoXu

Re: Taking single frame in Python - problem with exposure time/settings
« Reply #7 on: April 15, 2021, 02:57:37 PM »
    Well,maybe you can try use double value to setup.The function need double value.
Best Regards,
QinXiaoXu
That did the trick, thanks.

Full code on https://rk.edu.pl/en/scripting-machine-vision-and-astronomical-cameras-python/


Re: Taking single frame in Python - problem with exposure time/settings
« Reply #8 on: April 15, 2021, 08:21:26 PM »
Hi,
    It's a good news,and thanks for your share.
Best Regards,
QinXiaoXu