简体   繁体   中英

Why does gstreamer capture image incorrectly?

Hello,

I have a Jetson Nano device with a Raspberry Pi v2.1-camera and use gstreamer and OpenCV to capture images with it. Still, I thought this question would be better preserved here because I mainly think it is a software issue. The point is: I use following Python-script to capture my image:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imshow

def gstreamer_pipeline (capture_width=3280, capture_height=2464, display_width=1280, display_height=720, framerate=21, flip_method=0) :   
    return ('nvarguscamerasrc ! ' 
    'video/x-raw(memory:NVMM), '
    'width=(int)%d, height=(int)%d, '
    'format=(string)NV12, framerate=(fraction)%d/1 ! '
    'nvvidconv flip-method=%d ! '
    'video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! '
    'videoconvert ! '
    'video/x-raw, format=(string)BGR ! appsink'  % (capture_width,capture_height,framerate,flip_method,display_width,display_height))


if __name__ == '__main__':
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
    if cap.isOpened():
        ret_val, img = cap.read()
        img = np.flipud(img)
        img = np.fliplr(img)
        imshow(img)
        cv2.imwrite("test.jpg", img)
    else:
        print("Unable to open camera.")

My problem is I am not sure if it captures color values correctly. When I use imshow(img) from SciPy to display the image I just took, it looks like this: (I do not have enough reputation to insert it, so please see this link)

That is strange because it is very blue-toned. After I use cv2.imwrite("test.jpg", img) to save it, it looks normal again: (Here the another image)

I do not know if I defined something wrong in the gstreamer-pipeline or what else could cause the error. It is important for my program to capture colors correctly and since I do not want to save an image and then load it just to get the colors right (bad in terms of speed and CPU-power), I would be very glad if someone could help me out.

Thanks for the answers in advance!

As mentioned by HansHirse your color space is wrong.

You could alter your gstreamer pipeline to output RGB

format=(string)RGB    

Or convert from BGR to RGB within oopencv

im_rgb = cv2.cvtColor(im_cv, cv2.COLOR_BGR2RGB)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM