Search code examples
pythonnumpyflirpyspinspinnaker-cam

Resizing BayerRG8 image using PySpin(FLIR) changes it to grayscale


I am using a FLIR Blackfly S BFS-PGE-31S4C to collect video and writing the video with python, using the PySpin package. I wanted to add a real time video preview so I could see what was being recorded and wanted to downsize the image so I could easily see the entire frame on my screen. Below is my current code:

im = cam.GetNextImage(PySpin.EVENT_TIMEOUT_INFINITE if wait else PySpin.EVENT_TIMEOUT_NONE)
im = im.GetNDArray()
resize_factor = 2

cv2.imshow("test1",cv2.cvtColor(im, cv2.COLOR_BAYER_RG2RGB))
cv2.waitKey(0)

im = im[::resize_factor,::resize_factor]

cv2.imshow("test2",cv2.cvtColor(im, cv2.COLOR_BAYER_RG2RGB))
cv2.waitKey(0)

The cameras record in Bayer RG8 format. PySpin has a .GetNDArray() method which converts the image to numpy array (still in the bayer RG8 format I believe because the values are uint8). When I look at the type (type(im),type(im[0]),type(im[0][0])) of the numpy array prior to resizing it is a 2D numpy.ndarray containing numpy.uint8 values, and this is the same after I resize the image. When I show the first image (before conversion), it is in color. When I show the second, resized image, it is grayscale and just ignores the cv2.cvtColor() call.

I think it may have something to do with the BayerRG8 format of the original image but not sure why resizing it affects it in this way as I do not have a good knowledge of that image format. Any advice on my problem or suggestions to improve the question are appreciated!


Solution

  • It is very tricky to resize a Bayer image. Remember that the color components are interleaved:

    R G R G R G ...
    G B G B G B ...
    R G R G R G ...
    

    Your simple decimation is not going to honor the pixel values. If you decimate by 2, you will end up just with the R plane. You should use an image library to do the resize, rather than just decimation, and probably should convert to RGB first.