Reputation: 11
I am trying to figure out how to whitebalance images using numpy and PIL. I am using an IDS camera and all the images that we take seem to come out really dark and have a blue tint to them along with an inconstant picture quality, sometimes images are created fully and other times these large black bars will run horizontally across the image. Here is an image i am referring to
Here is the camera we are using: https://en.ids-imaging.com/manuals/uEye_SDK/EN/uEye_Manual_4.93/index.html
from pyueye import ueye
import numpy as np
import sys
from PIL import Image
import cv2
hCam = ueye.HIDS(0)
sInfo = ueye.SENSORINFO()
cInfo = ueye.CAMINFO()
pcImageMemory = ueye.c_mem_p()
MemID = ueye.int()
rectAOI = ueye.IS_RECT()
pitch = ueye.INT()
nBitsPerPixel = ueye.INT(24)
channels = 3
m_nColorMode = ueye.IS_CM_RGBA12_UNPACKED #IS_CM_BGRA8_PACKED
bytes_per_pixel = int(nBitsPerPixel / 8)
#Camera Init
nRet = ueye.is_InitCamera(hCam, None)
if nRet != ueye.IS_SUCCESS:
print("is_InitCamera ERROR")
#Get Sensor Info
nRet = ueye.is_GetSensorInfo(hCam, sInfo)
if nRet != ueye.IS_SUCCESS:
print("is_GetSensorInfo ERROR")
#Set Display Mode
nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)
#Set Color mode
nRet = ueye.is_SetColorMode(hCam, ueye.IS_CM_BGR8_PACKED)
#Area of Interest
nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
if nRet != ueye.IS_SUCCESS:
print("is_AOI ERROR")
#Define Width and Height
width = rectAOI.s32Width
height = rectAOI.s32Height
# Prints out some information about the camera and the sensor
print("Camera model:\t\t", sInfo.strSensorName.decode('utf-8'))
print("Camera serial no.:\t", cInfo.SerNo.decode('utf-8'))
print("Maximum image width:\t", width)
print("Maximum image height:\t", height)
print()
#Allocate Image Memory
nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
if nRet != ueye.IS_SUCCESS:
print("is_AllocImageMem ERROR")
#Add to Sequence
nRet = ueye.is_AddToSequence(hCam , pcImageMemory , MemID)
if nRet != ueye.IS_SUCCESS:
print("is_AddToSequence ERROR")
#Capture Video
nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
if nRet != ueye.IS_SUCCESS:
print("is_CaptureVideo ERROR")
#Inquire Image Memory
nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
if nRet != ueye.IS_SUCCESS:
print("is_InquireImageMem ERROR")
#Image Display
array = [0]
# Continuous image display
while(nRet == ueye.IS_SUCCESS):
nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
if nRet != ueye.IS_SUCCESS:
print("is_InquireImageMem ERROR")
while array[0] == 0:
array = ueye.get_data(pcImageMemory, width.value, height.value, nBitsPerPixel.value, pitch.value, copy=False)
print(array)
frame = np.reshape(array,(height.value, width.value, bytes_per_pixel))
#print(frame)
img = Image.fromarray(frame,'RGB')
img.show()
break
ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)
ueye.is_ExitCamera(hCam)
here is the code we use to create the image. Any help would be great, thanks!!!
Upvotes: 1
Views: 614
Reputation: 4236
Your image is blue-tinted because you set your camera to BGR mode, but your PIL.Image() is RGB. This means that your red and blue channels are swapped. I.e. everything that is in the world blue will be red, and everything red will show as blue. So the problem is easily fixable either by setting the camera to RGB mode or swapping R and B channels in the PIL Image() afterwards. If the camera doesn't support RGB it may support YUV system and you have the PIL Image()s of type "YUV". That would solve your problem. If you need to swap channels manually then you can use Image.split() to split the image to R-G-B channels and then use Image.merge() to create new Image() with channels swapped.
i = <some_Image()_that_is_BGR>
b, g, r = i.split()
i = Image.merge("RGB", (r, g, b))
But, if your image is showing some odd lines that do not exist anywhere I am afraid that your CCD IC is finished and you need a new camera.
Upvotes: 2