jenston nano上CSI相机读取

一、正常读取的程序(有畸变)

# -*- coding: utf-8 -*-
#!/usr/bin/env python

import cv2
 
 
def gstreamer_pipeline(
    capture_width=640,
    capture_height=480,
    display_width=640,
    display_height=480,
    framerate=60,
    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,
        )
    )
 
 
def show_camera():
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
 
    i = 0
    while cap.isOpened():
        flag, img = cap.read()
        cv2.imshow("CSI Camera", img)
        kk = cv2.waitKey(1)
 
        # do other things
 
        if kk == ord('q'):  # 按下 q 键,退出
            break
        elif kk == ord('p'):
            cv2.imwrite('./picture/%02d.png'%i,img)
            print(i)
            i += 1
 
    cap.release()
    cv2.destroyAllWindows()
    
 
if __name__ == "__main__":
    show_camera()

二、矫正畸变的程序

1.相机内参标定(使用matlab进行标定,具体方法百度)

2.畸变参数的使用(基于python)

 在代码中需要修改的参数有两对,一个内参,一个畸变系数

(1)内参,在MATLAB提取,为3X3的一个矩阵,从中去除0,,对应位置填到代码中(应该是可以转置)。

(2)畸变系数,总共有五个,径向畸变3个(k1,k2,k3)和切向畸变2个(p1,p2)。对比填到代码中,别填错顺序!!!没有的填0就行

# -*- coding: utf-8 -*-
#!/usr/bin/env python

#######运行之前需要修改为自己CSI相机的参数值,具体修改以下fx、cx、 fy 、cy、 k1, k2, p1, p2, k3的值

import cv2
import numpy as np
 
def gstreamer_pipeline(
    capture_width=640,
    capture_height=480,
    display_width=640,
    display_height=480,
    framerate=60,
    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,
        )
    )
 
def undistort(frame):
    fx = 403.722124149864  
    cx = 309.512521984214
    fy = 540.51469497719
    cy = 268.164727162155
    k1, k2, p1, p2, k3 = -0.30058090333203, 0.0836081153924359, 0.0, 0.0, 0.0
 
    # 相机坐标系到像素坐标系的转换矩阵
    k = np.array([
        [fx, 0, cx],
        [0, fy, cy],
        [0, 0, 1]
    ])
    # 畸变系数
    d = np.array([
        k1, k2, p1, p2, k3
    ])
    h, w = frame.shape[:2]
    mapx, mapy = cv2.initUndistortRectifyMap(k, d, None, k, (w, h), 5)
    return cv2.remap(frame, mapx, mapy, cv2.INTER_LINEAR)
def show_camera():
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
 
    i = 0
    while cap.isOpened():
        flag, img = cap.read()
        cv2.imshow("CSI Camera", undistort(img))
        kk = cv2.waitKey(1)
 
        # do other things
 
        if kk == ord('q'):  # 按下 q 键,退出
            break
        elif kk == ord('p'):
            cv2.imwrite('./picture/%02d.png'%i,undistort(img))
            print(i)
            i += 1
 
    cap.release()
    cv2.destroyAllWindows()
    
 
if __name__ == "__main__":
    show_camera()

参考了(127条消息) Python+OpenCV相机摄像头标定,矫正畸变,实时输出正常视频流_曾某人啊的博客-CSDN博客

你可能感兴趣的:(nano深度学习,python,开发语言)