日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

立体标定

發(fā)布時(shí)間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 立体标定 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
  • 立體標(biāo)定
  • 應(yīng)用標(biāo)定數(shù)據(jù)
  • 轉(zhuǎn)換成深度圖

標(biāo)定

由于攝像頭目前是我們手動(dòng)進(jìn)行定位的,我們現(xiàn)在還不知道兩張圖像與世界坐標(biāo)之間的耦合關(guān)系,所以下一步要進(jìn)行的是標(biāo)定,用來確定分別獲取兩個(gè)攝像頭的內(nèi)部參數(shù),并且根據(jù)兩個(gè)攝像頭在同一個(gè)世界坐標(biāo)下的標(biāo)定參數(shù)來獲取立體參數(shù)。注:不要使用OpenCV自帶的自動(dòng)calbration,其對(duì)棋盤的識(shí)別率極低,使用Matlab的Camera Calibration Toolbox更為有效,具體細(xì)節(jié)請(qǐng)看:攝像機(jī)標(biāo)定和立體標(biāo)定

同時(shí)從兩個(gè)攝像頭獲取圖片

import cv2 import timeAUTO = True # 自動(dòng)拍照,或手動(dòng)按s鍵拍照 INTERVAL = 2 # 自動(dòng)拍照間隔cv2.namedWindow("left") cv2.namedWindow("right") cv2.moveWindow("left", 0, 0) cv2.moveWindow("right", 400, 0) left_camera = cv2.VideoCapture(0) right_camera = cv2.VideoCapture(1)counter = 0 utc = time.time() pattern = (12, 8) # 棋盤格尺寸 folder = "./snapshot/" # 拍照文件目錄def shot(pos, frame):global counterpath = folder + pos + "_" + str(counter) + ".jpg"cv2.imwrite(path, frame)print("snapshot saved into: " + path)while True:ret, left_frame = left_camera.read()ret, right_frame = right_camera.read()cv2.imshow("left", left_frame)cv2.imshow("right", right_frame)now = time.time()if AUTO and now - utc >= INTERVAL:shot("left", left_frame)shot("right", right_frame)counter += 1utc = nowkey = cv2.waitKey(1)if key == ord("q"):breakelif key == ord("s"):shot("left", left_frame)shot("right", right_frame)counter += 1left_camera.release() right_camera.release() cv2.destroyWindow("left") cv2.destroyWindow("right")

?

在進(jìn)行標(biāo)定的過程中,要注意的是在上面標(biāo)定方法中沒有提到的是,單個(gè)標(biāo)定后,要對(duì)標(biāo)定的數(shù)據(jù)進(jìn)行錯(cuò)誤分析(Analyse Error),如左圖,是我對(duì)左攝像頭的標(biāo)定結(jié)果分析。圖中天藍(lán)色點(diǎn)明顯與大部分點(diǎn)不聚斂,所以有可能是標(biāo)定時(shí)對(duì)這個(gè)圖片標(biāo)定出現(xiàn)的錯(cuò)誤,要重新標(biāo)定,在該點(diǎn)上點(diǎn)擊并獲取其圖片名稱索引,對(duì)其重新標(biāo)定后,右圖的結(jié)果看起來還是比較滿意的

?

在進(jìn)行完立體標(biāo)定后,我們將得到如下的數(shù)據(jù):

Stereo calibration parameters after optimization:Intrinsic parameters of left camera:Focal Length: fc_left = [ 824.93564 825.93598 ] [ 8.21112 8.53492 ] Principal point: cc_left = [ 251.64723 286.58058 ] [ 13.92642 9.11583 ] Skew: alpha_c_left = [ 0.00000 ] [ 0.00000 ] => angle of pixel axes = 90.00000 0.00000 degrees Distortion: kc_left = [ 0.23233 -0.99375 0.00160 0.00145 0.00000 ] [ 0.05659 0.30408 0.00472 0.00925 0.00000 ]Intrinsic parameters of right camera:Focal Length: fc_right = [ 853.66485 852.95574 ] [ 8.76773 9.19051 ] Principal point: cc_right = [ 217.00856 269.37140 ] [ 10.40940 9.47786 ] Skew: alpha_c_right = [ 0.00000 ] [ 0.00000 ] => angle of pixel axes = 90.00000 0.00000 degrees Distortion: kc_right = [ 0.30829 -1.61541 0.01495 -0.00758 0.00000 ] [ 0.06567 0.55294 0.00547 0.00641 0.00000 ]Extrinsic parameters (position of right camera wrt left camera):Rotation vector: om = [ 0.01911 0.03125 -0.00960 ] [ 0.01261 0.01739 0.00112 ] Translation vector: T = [ -70.59612 -2.60704 18.87635 ] [ 0.95533 0.79030 5.25024 ]

應(yīng)用標(biāo)定數(shù)據(jù)

我們使用如下的代碼來將其配置到python中,上面的參數(shù)都是手動(dòng)填寫至下面的內(nèi)容中的,這樣免去保存成文件再去讀取,在托運(yùn)填寫的時(shí)候要注意數(shù)據(jù)的對(duì)應(yīng)位置

# filename: camera_configs.py import cv2 import numpy as npleft_camera_matrix = np.array([[824.93564, 0., 251.64723],[0., 825.93598, 286.58058],[0., 0., 1.]]) left_distortion = np.array([[0.23233, -0.99375, 0.00160, 0.00145, 0.00000]])right_camera_matrix = np.array([[853.66485, 0., 217.00856],[0., 852.95574, 269.37140],[0., 0., 1.]]) right_distortion = np.array([[0.30829, -1.61541, 0.01495, -0.00758, 0.00000]])om = np.array([0.01911, 0.03125, -0.00960]) # 旋轉(zhuǎn)關(guān)系向量 R = cv2.Rodrigues(om)[0] # 使用Rodrigues變換將om變換為R T = np.array([-70.59612, -2.60704, 18.87635]) # 平移關(guān)系向量size = (640, 480) # 圖像尺寸# 進(jìn)行立體更正 R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv2.stereoRectify(left_camera_matrix, left_distortion,right_camera_matrix, right_distortion, size, R,T) # 計(jì)算更正map left_map1, left_map2 = cv2.initUndistortRectifyMap(left_camera_matrix, left_distortion, R1, P1, size, cv2.CV_16SC2) right_map1, right_map2 = cv2.initUndistortRectifyMap(right_camera_matrix, right_distortion, R2, P2, size, cv2.CV_16SC2)

這樣,我們得到了左右攝像頭的兩個(gè)map,并得到了立體的Q,這些參數(shù)都將應(yīng)用于下面的轉(zhuǎn)換成深度圖中

轉(zhuǎn)換成深度圖

import numpy as np import cv2 import camera_configscv2.namedWindow("left") cv2.namedWindow("right") cv2.namedWindow("depth") cv2.moveWindow("left", 0, 0) cv2.moveWindow("right", 600, 0) cv2.createTrackbar("num", "depth", 0, 10, lambda x: None) cv2.createTrackbar("blockSize", "depth", 5, 255, lambda x: None) camera1 = cv2.VideoCapture(0) camera2 = cv2.VideoCapture(1)# 添加點(diǎn)擊事件,打印當(dāng)前點(diǎn)的距離 def callbackFunc(e, x, y, f, p):if e == cv2.EVENT_LBUTTONDOWN: print threeD[y][x]cv2.setMouseCallback("depth", callbackFunc, None)while True:ret1, frame1 = camera1.read()ret2, frame2 = camera2.read()if not ret1 or not ret2:break# 根據(jù)更正map對(duì)圖片進(jìn)行重構(gòu)img1_rectified = cv2.remap(frame1, camera_configs.left_map1, camera_configs.left_map2, cv2.INTER_LINEAR)img2_rectified = cv2.remap(frame2, camera_configs.right_map1, camera_configs.right_map2, cv2.INTER_LINEAR)# 將圖片置為灰度圖,為StereoBM作準(zhǔn)備imgL = cv2.cvtColor(img1_rectified, cv2.COLOR_BGR2GRAY)imgR = cv2.cvtColor(img2_rectified, cv2.COLOR_BGR2GRAY)# 兩個(gè)trackbar用來調(diào)節(jié)不同的參數(shù)查看效果num = cv2.getTrackbarPos("num", "depth")blockSize = cv2.getTrackbarPos("blockSize", "depth")if blockSize % 2 == 0:blockSize += 1if blockSize < 5:blockSize = 5# 根據(jù)Block Maching方法生成差異圖(opencv里也提供了SGBM/Semi-Global Block Matching算法,有興趣可以試試)stereo = cv2.StereoBM_create(numDisparities=16*num, blockSize=blockSize)disparity = stereo.compute(imgL, imgR)disp = cv2.normalize(disparity, disparity, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)# 將圖片擴(kuò)展至3d空間中,其z方向的值則為當(dāng)前的距離threeD = cv2.reprojectImageTo3D(disparity.astype(np.float32)/16., camera_configs.Q)cv2.imshow("left", img1_rectified)cv2.imshow("right", img2_rectified)cv2.imshow("depth", disp)key = cv2.waitKey(1)if key == ord("q"):breakelif key == ord("s"):cv2.imwrite("./snapshot/BM_left.jpg", imgL)cv2.imwrite("./snapshot/BM_right.jpg", imgR)cv2.imwrite("./snapshot/BM_depth.jpg", disp)camera1.release() camera2.release() cv2.destroyAllWindows() 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的立体标定的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。