I am communicating with a USB camera via Python, but can not find a way to control the gain. I have tried pylablib, pyusb, opencv, and a few other libraries with no success (the manufacturer does not provide a working DLL). At this point I think my only option is to apply artificial gain to the image, but I'm not sure if this is even possible. My understanding is that gain is just the amplification of signal on the camera's sensor, so I would expect to be able to just multiply the pixel intensities by some factor to achieve the same affect: shown_img = (shown_img * 2.0).astype("uint8")
...but this results in an image with random colors (attached image "weird.png"). I'm obviously doing something wrong here, but maybe my foundational understanding is incorrect.
Could anyone confirm that this is possible? If so what would be the proper array manipulation to apply gain? This doesn't need to be explained with Python, that is just what I've been using for this project.
Thank you in advance!
Original:
Result:
Expected:
Your code
shown_img = (shown_img * 2.0).astype("uint8")
scales the intensities and casts to 8-bit unsigned integer. This cast, when values exceed 255 (the largest value representable in a uint8) will wrap around. For example, casting 256 to uint 8 produces 0. This wrap around is what causes the weird colors you see.
To prevent the wrap around, you need to saturate the values, make those larger than 255 have a value of 255, before casting:
shown_img =
shown_img[shown_img > 255] = 255
shown_img = shown_img.astype("uint8")
It might be easier to use the np.clip
function:
shown_img = np.clip(shown_img * 2.0, 0, 255).astype("uint8")