genicam / harvesters

Image Acquisition Library for GenICam-based Machine Vision System
Apache License 2.0
514 stars 88 forks source link

Image aquired only in black and white #278

Closed MasIgor closed 2 years ago

MasIgor commented 2 years ago

Hello all!

First off thank you for this!

I'm trying to read a genie nano color c1920, and to use the image with yoloV5.

The script loads a jpg file (first rows) and I am trying to replace that code with the frame taken from the camera. Problem is that it only reads one channel and the size of the array is wrong. the array that cv2.imread(path) generates is 3 dimensional: {640,640,3} with a total size of 1228800. the array that the GigE code generates is 1 dimensional {409600} so just one third of the expected size. Also if I use Harvester GUI, the image is black and white. Why? What am I doing wrong?

Thanks, any help appreciated.

    self.count += 1
            img0 = cv2.imread(path)  # BGR
            print(img0.size)
            # GigE
            h = Harvester()
            h.add_file('C:\\Program Files\\MATRIX VISION\\mvIMPACT Acquire\\bin\\x64\\mvGenTLProducer.cti')
            h.update()
            print(h.device_info_list)
            ia = h.create_image_acquirer(0)
            ia.remote_device.node_map.Width.value = 640
            ia.remote_device.node_map.Height.value = 640
            print(ia.remote_device.node_map.PixelFormat.symbolics)
            ia.remote_device.node_map.PixelFormat.value = 'BayerRG8'
            ia.start_acquisition()
            with ia.fetch_buffer() as buffer:
                component = buffer.payload.components[0]
                data_format = component.data_format
                print("data format: ", data_format)
                _1d = component.data
                print('1D: {0}'.format(_1d))
                print(_1d.size)
            ia.stop_acquisition()
            ia.destroy()
            h.reset()
            img0 = _1d
Gornoka commented 2 years ago

The image you gather is in the BayerRG8 colorspace, you need to convert it to BGR first in order to use it. I recommend using opencv, but the color conversions for bayer codecs in there are not 100% in line with all camera producers/manufacturers, you may need to play around a little bit to find the one for your camera.

https://docs.opencv.org/4.x/de/d25/imgproc_color_conversions.html

I would try it with: retrieved_image = np.ndarray(buffer=component.data.copy(), dtype=np.uint8, shape=(component.height, component.width, 1)) retrieved_image = cv2.cvtColor(retrieved_image, cv2.COLOR_BayerBG2BGR)

The other option would be to use a different color space for your image acquisition, but that entirely depends on your camera, which I am not familliar with.

kazunarikudo commented 2 years ago

@Gornoka Thank you for your kind help! I appreciate that. Regards, Kazunari.

MasIgor commented 2 years ago

@Gornoka Sorry for the late reply, I have to admit that I tested it and forgot to answer. It worked like a charm. Thank you!!