1

我试图弄清楚如何使用 numpy 和 PIL 对图像进行白平衡。我正在使用 IDS 相机,我们拍摄的所有图像看起来都非常暗,并且带有蓝色色调以及不稳定的图像质量,有时图像会完全创建,而其他时候这些大黑条会水平穿过图片。这是我指的图像

这是我们正在使用的相机: 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)

这是我们用来创建图像的代码。任何帮助都会很棒,谢谢!!!

4

1 回答 1

2

您的图像是蓝色的,因为您将相机设置为 BGR 模式,但您的 PIL.Image() 是 RGB。这意味着您的红色和蓝色通道被交换。即世界上所有蓝色的东西都是红色的,红色的东西都会显示为蓝色。因此,通过将相机设置为 RGB 模式或之后在 PIL Image() 中交换 R 和 B 通道,可以轻松解决此问题。如果相机不支持 RGB,它可能支持 YUV 系统,并且您拥有“YUV”类型的 PIL Image()。那会解决你的问题。如果您需要手动交换通道,则可以使用 Image.split() 将图像拆分为 RGB 通道,然后使用 Image.merge() 创建新的 Image() 并交换通道。

i = <some_Image()_that_is_BGR>
b, g, r = i.split()
i = Image.merge("RGB", (r, g, b))

但是,如果您的图像显示出一些在任何地方都不存在的奇怪线条,我担心您的 CCD IC 已经完成,您需要一个新相机。

于 2020-03-09T00:30:37.890 回答