arm linux 平台 打开摄像头矫正

安装v4l

v4l 意思为 video for Linux, 是linux驱动链接摄像头的软件应用部分,当然先要安装好v4l-utils

$  sudo  apt  install  -y  v4l-utils
$  v4l2-ctl  --list-devices
$  v4l2-ctl  -d 0 --list-formats-ext

看完以后基础信息以后就可以使用jetson等主板工具打开查看
nvgstcapture

使用gstreamer 工具
gst-launch-1.0 nvarguscamerasrc ! nvegltransform ! nveglglessink

下面是用python基础代码来打开

使用海康大华等的摄像头一般使用rtsp协议,也可以使用gb28181 来被动接收流,如果有mipi,usb摄像头都比较类似,我们写一些测试程序,这些都比较简单,不做阐述,仅仅提供代码就行了

def open_cam_rtsp(uri, width, height, latency):
    gst_str = ("rtspsrc location={} latency={} ! rtph264depay ! h264parse ! omxh264dec ! "
               "nvvidconv ! video/x-raw, width=(int){}, height=(int){}, format=(string)BGRx ! "
               "videoconvert ! appsink").format(uri, latency, width, height)
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
 
def open_cam_usb(dev, width, height):
    # We want to set width and height here, otherwise we could just do:
    #     return cv2.VideoCapture(dev)
    gst_str = ("v4l2src device=/dev/video{} ! "
               "video/x-raw, width=(int){}, height=(int){}, format=(string)RGB ! "
               "videoconvert ! appsink").format(dev, width, height)
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
 
def open_cam_onboard(width, height):
    # On versions of L4T previous to L4T 28.1, flip-method=2
    # Use Jetson onboard camera
    gst_str = ("nvcamerasrc ! "
               "video/x-raw(memory:NVMM), width=(int)2592, height=(int)1458, format=(string)I420, framerate=(fraction)30/1 ! "
               "nvvidconv ! video/x-raw, width=(int){}, height=(int){}, format=(string)BGRx ! "
               "videoconvert ! appsink").format(width, height)
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

def open_cam_onboard(width ,height)
# 上面代码类似使用
nvarguscamerasrc ! nvegltransform ! nveglglessink

打开摄像头矫正

先要做测试, 把图像存到一个文件夹下面,然后再继续,自动识别文件夹下所有jpg和png文件,然后再使用KD参数矫正摄像头,将所有文件矫正看是否大部分都正常, 也就反向看KD参数时候合适,如何不合适,则是要修改参数。如果成功了,再使用c++,编写代码,这样比较快速,所以说,python真是一个优秀的工具

import os
import rejustbase
import argparse
import cv2
import numpy as np
folder0 = 'd:/img/'

def process(folder_path,K,D,DIM,scale):
   if folder_path and len(folder_path)>0:
       folder = folder_path
   else:
       folder = folder0
      
   for filename in os.listdir(folder):
       if filename.endswith('.jpg') or filename.endswith('.png'):
           
          image_path = os.path.join(folder, filename)

          filename = image_path.split('/')[-1]
          filename0 = filename.split(".")[0]
          filename1 = filename.split(".")[1]
          filenameout = filename0+"_rect" + "." + filename1;
         
          image_path_out = os.path.join(folder, filenameout)
          img = cv2.imread(image_path)
          #img01 = cv2.resize(img01,(1920,1080),cv2.INTER_LINEAR)
          img1 = rejustbase.undistort1(img,K,D,DIM,scale)
          print(image_path_out)
          cv2.imwrite(image_path_out,img1)
          




if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument('--id', type=int, default=0)
  parser.add_argument('--folder', type=str, default = None)
  #parser.add_argument('--name2', type=str, default = None)
  args = parser.parse_args()

#   file_path = "c:/abc/abc.png";

#   filename = file_path.split('/')[-1]
#   filename0 = filename.split(".")[0]
#   print(filename)
#   print(filename0)
#   print(args.id)
#   print(args.folder)

  DIM=(1920,1080)
  Kf = np.loadtxt("C:/QiNa/camerax1920_K.txt",delimiter=" ")
  Df = np.loadtxt("C:/QiNa/camerax1920_D.txt",delimiter=" ")
  K4 = np.loadtxt("C:/QiNa/camera4_K.txt",delimiter=" ")
  D4 = np.loadtxt("C:/QiNa/camera4_D.txt",delimiter=" ")
  K8 = np.loadtxt("C:/QiNa/camera2.8_K.txt",delimiter=" ")
  D8 = np.loadtxt("C:/QiNa/camera2.8_D.txt",delimiter=" ")
  process("",Kf,Df,DIM,0.8)

你可能感兴趣的:(音视频和c++,java,物联网,linux,opencv,运维)