Image Stack verarbeiten

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
Kriccemis
User
Beiträge: 15
Registriert: Freitag 8. Mai 2020, 20:10

Hallo Liebe Community,

ich habe ein Problem bezüglich Bild- bzw. Frameverarbeitung. Ich verwende ein IDS Kamera und habe vom Hersteller auch ein kleines Python Tutorial Programm zur Verfügung gestellt bekommen. Dieses habe ich so verändert, dass 3 Frames ausgenommen werden sollen und darach ein Mittelwertframe gebildet werden soll. Merkwürdigerweise ist bei der Datenverarbeitung alles "Null". Siehe Bilder. Gebe ich aber die Befehle in der Konsole ein, funktioniert die Verarbeitung.

Hier die Bilder und der Code:

Code: Alles auswählen

#Libraries
from pyueye import ueye
import numpy as np
import tifffile

#---------------------------------------------------------------------------------------------------------------------------------------

#Variables
hCam = ueye.HIDS(0)             #0: first available camera;  1-254: The camera with the specified camera ID
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)    #24: bits per pixel for color mode; take 8 bits per pixel for monochrome
channels = 1                    #3: channels for color mode(RGB); take 1 channel for monochrome
m_nColorMode = ueye.INT()		# Y8/RGB16/RGB24/REG32
bytes_per_pixel = int(nBitsPerPixel / 8)
#---------------------------------------------------------------------------------------------------------------------------------------
print("START")
print()

# Starts the driver and establishes the connection to the camera
nRet = ueye.is_InitCamera(hCam, None)
if nRet != ueye.IS_SUCCESS:
    print("is_InitCamera ERROR")

# Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
nRet = ueye.is_GetCameraInfo(hCam, cInfo)
if nRet != ueye.IS_SUCCESS:
    print("is_GetCameraInfo ERROR")

# You can query additional information about the sensor type used in the camera
nRet = ueye.is_GetSensorInfo(hCam, sInfo)
if nRet != ueye.IS_SUCCESS:
    print("is_GetSensorInfo ERROR")

nRet = ueye.is_ResetToDefault( hCam)
if nRet != ueye.IS_SUCCESS:
    print("is_ResetToDefault ERROR")

# Set display mode to DIB
nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)

# Set the right color mode
if int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_BAYER:
    # setup the color depth to the current windows setting
    ueye.is_GetColorDepth(hCam, nBitsPerPixel, m_nColorMode)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    print("IS_COLORMODE_BAYER: ", )
    print("\tm_nColorMode: \t\t", m_nColorMode)
    print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
    print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
    print()

elif int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_CBYCRY:
    # for color camera models use RGB32 mode
    m_nColorMode = ueye.IS_CM_BGRA8_PACKED
    nBitsPerPixel = ueye.INT(32)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    print("IS_COLORMODE_CBYCRY: ", )
    print("\tm_nColorMode: \t\t", m_nColorMode)
    print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
    print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
    print()

elif int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME:
    # for color camera models use RGB32 mode
    m_nColorMode = ueye.IS_CM_MONO12
    nBitsPerPixel = ueye.INT(16)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    print("IS_COLORMODE_MONOCHROME: ", )
    print("\tm_nColorMode: \t\t", m_nColorMode)
    print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
    print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
    print()

else:
    # for monochrome camera models use Y8 mode
    m_nColorMode = ueye.IS_CM_MONO8
    nBitsPerPixel = ueye.INT(8)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    print("else")

# Can be used to set the size and position of an "area of interest"(AOI) within an image
nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
if nRet != ueye.IS_SUCCESS:
    print("is_AOI ERROR")

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()

#---------------------------------------------------------------------------------------------------------------------------------------

# Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
if nRet != ueye.IS_SUCCESS:
    print("is_AllocImageMem ERROR")
else:
    # Makes the specified image memory the active memory
    nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID)
    if nRet != ueye.IS_SUCCESS:
        print("is_SetImageMem ERROR")
    else:
        # Set the desired color mode
        nRet = ueye.is_SetColorMode(hCam, m_nColorMode)

# Activates the camera's live video mode (free run mode)
nRet = ueye.is_CaptureVideo(hCam, ueye.IS_DONT_WAIT)
if nRet != ueye.IS_SUCCESS:
    print("is_CaptureVideo ERROR")

# Enables the queue mode for existing image memory sequences

# Downloads the raw byte buffer from the camera.
# writes the buffer content into pcImageMemory

nRet = ueye.is_InquireImageMem(hCam, pcImageMemory, MemID, width, height, nBitsPerPixel, pitch)
if nRet != ueye.IS_SUCCESS:
    print("is_InquireImageMem ERROR")
else:
    print("Press q to leave the programm")
    

"""
Eigener Code zum Testen der Bildmanipulation
"""

    
# Setzt Belichtungszeit auf 4ms
ueye.is_Exposure(hCam, ueye.IS_EXPOSURE_CMD_SET_EXPOSURE, ueye.c_double(4) , 8)

# Aufnahme eines Einzelbildes
array1 = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
frame1 = np.reshape(array1,(height.value, width.value, bytes_per_pixel))
frame1 = frame1.view(np.uint16)

array2 = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
frame2 = np.reshape(array2,(height.value, width.value, bytes_per_pixel))
frame2 = frame2.view(np.uint16)

array3 = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
frame3 = np.reshape(array3,(height.value, width.value, bytes_per_pixel))
frame3 = frame3.view(np.uint16)

# Erzeuge einen Z-Stack der Einzelbilder. Der reshape ist erforderlich 
# um von einem 4D Array (z,y,x,1) zurück zum 3D Array (z,y,x) zu kommen.
#zum Verständnis: 
#    np.stack((frame1,frame2,frame3,)).shape
# ausführen
a = np.stack((frame1, frame2, frame3,))
print(a.shape)
a = np.stack((frame1,frame2,frame3,)).reshape(a.shape[0:3])

# bildet den Mittelwert über die Achse mit Index 0. Das ist die Z-Achse des §D Arrays 
# Reihenfolge der Achsen: (z,y,x).
b = np.mean(a, axis=0).astype("uint16")

tifffile.imshow(b, cmap = "gray")

Bild 1: Ergebnis nach dem Durchlauf des Progamms

Bild

Bild 2: nach konsoleneingabe
Bild
Antworten