可见光相机与红外相机标定
生活随笔
收集整理的這篇文章主要介紹了
可见光相机与红外相机标定
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
1.打開相機
2.可見光相機畸變矯正
安裝相關庫
查看相機參數
修改launch文件
啟動標定程序
啟動矯正圖像節點
3.查看相機圖像
4.計算H舉矩陣
手動標注獲取每一組的H矩陣(h.py)
計算出平均H矩陣并查看標定效果(MeanH_Test.py)
5.接受、處理、發送圖像(biaoding.py)
6.標定效果
1.打開相機
cd camera_ws source devel/setup.bash roslaunch usb_cam usb_cam-dual_model_rectified.launch2.可見光相機畸變矯正
安裝相關庫
catkin_make -DCATKIN_WHITELIST_PACKAGES="aruco;aruco_ros;aruco_msgs" catkin_make -DCATKIN_WHITELIST_PACKAGES="aruco_mapping;lidar_camera_calibration" catkin_make -DCATKIN_WHITELIST_PACKAGES=""查看相機參數
ls /dev/video* #查看video個數和名稱 v4l2-ctl -d 0 --all #查看video0的參數修改launch文件
<param name="video_device" value="/dev/video1" /> <param name="image_width" value="640" /> <param name="image_height" value="480" /> <param name="pixel_format" value="mjpeg" />啟動標定程序
rosrun camera_calibration cameracalibrator.py --size 8x6 --square 0.06 image:=/usb_cam/image_raw camera:=/usb_cam- 移動標定板時,標定窗口中的各指標增加。
- 當CALIBRATE按鈕亮起時,單擊計算相機內參矩陣及矯正參數,并輸出至終端,此過程可能需要1-2分鐘。
- 當SAVE按鈕亮起時,單擊將采集的樣本圖片和計算結果保存
- 當COMMIT按鈕亮起時,單擊將標定結果寫入~/.ros/camera_info/head_camera.yaml。注:此標定文件將被原始圖像節點usb_cam讀取,標定參數會被發布至話題/usb_cam/camera_info以供矯正圖像節點image_proc使用。
啟動矯正圖像節點
ROS_NAMESPACE=/cam_rgb/usb_cam rosrun image_proc image_proc3.查看相機圖像
rqt_image_view4.計算H舉矩陣
手動標注獲取每一組的H矩陣(h.py)
#!/usr/bin/python # -*- coding: UTF-8 -*- from PIL import Image import cv2 import numpy as np import pylab as pl import matplotlib.pyplot as plt import scipy.io as sio from PIL import Imagedef re_img(path):img = cv2.imread(path)a=int(20) #x startb=int(700) #x endc=int(0) #y startd=int(510) #y endcropimg=img[c:d,a:b]imgresize=cv2.resize(cropimg,(640,480))return imgresizeif __name__ == '__main__' :num = 9h=np.zeros((3,3))# 讀取紅外圖.inf_Fig = "./rgb1/" + str(num) +"_dual.png"img_dst = re_img(inf_Fig)pl.figure(), pl.imshow(img_dst[:, :, ::-1]), pl.title('dual')# 紅外圖中的4個點dst_point = plt.ginput(4) dst_point = np.float32(dst_point)# 讀取RGB圖像.RGB_Fig_1 = "./rgb1/"+ str(num) +".png"im_src_1 = cv2.imread(RGB_Fig_1)pl.figure(), pl.imshow(im_src_1[:, :, ::-1]), pl.title('rgb1')# RGB圖中的4個點src_point_1 = plt.ginput(4) src_point_1 = np.float32(src_point_1)# Calculate Homographyh, status = cv2.findHomography(src_point_1, dst_point)# Warp source image to destination based on homographyim_out_1 = cv2.warpPerspective(im_src_1,h,(640,480),borderValue=(255,255,255))pl.imshow(im_out_1[:, :, ::-1]), pl.title('out')pl.show() #show dst#保存H矩陣H1_name = 'h'+str(num)+'.mat'print('H1:',h)sio.savemat(H1_name, {'Homography_Mat_1':h})計算出平均H矩陣并查看標定效果(MeanH_Test.py)
#!/usr/bin/python # -*- coding: UTF-8 -*-from PIL import Image import cv2 import numpy as np #import pylab as pl #import matplotlib.pyplot as plt import scipy.io as sio import timeclass bd():def get_time_stamp(self):ct = time.time()local_time = time.localtime(ct)data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)data_secs = (ct - int(ct)) * 1000time_stamp = "%s.%03d" % (data_head, data_secs)#print(time_stamp)stamp = ("".join(time_stamp.split()[0].split("-"))+"".join(time_stamp.split()[1].split(":"))).replace('.', '')#print(stamp)return stampdef re_img(self,img):#img = cv2.imread(path)a=int(20) #x startb=int(700) #x endc=int(0) #y startd=int(510) #y endcropimg=img[c:d,a:b]imgresize=cv2.resize(cropimg,(640,480))return imgresizedef H_use(self,fig1):name_1 = 'H11.mat'data = sio.loadmat(name_1)h1 = data['Homography_Mat_1']h1[0][2]=h1[0][2]+30 #根據實際效果的修正h1[1][2]=h1[1][2]+10#print(h1[0][2])#print(h1)im_out_1 = cv2.warpPerspective(fig1,h1,(640,480))return im_out_1def Htest(self,path1,path2):im_src_1 = cv2.imread(path1)img_dst = self.re_img(path2)im_out_1= self.H_use(im_src_1)#pl.figure(),pl.imshow(img_dst[:, :, ::-1]), pl.title('dst'),#pl.figure(),pl.imshow(im_out_1[:, :, ::-1]), pl.title('out1'),#pl.show()return im_out_1, img_dstdef Htest_t(self,img_t):img_dst = self.re_img(img_t)return img_dstdef Htest_rgb(self,img_rgb):im_out_1= self.H_use(img_rgb)return im_out_1def cal_H(self):num=0H=np.zeros((3,3))while int(num) < 10:print(num)name = 'h' + str(num) +'.mat'data=sio.loadmat(name)h = data['Homography_Mat_1']print("h:",h)num = num + 1H=H+hH=H/10H1_name = 'H.mat'print('H1:',H)sio.savemat(H1_name, {'Homography_Mat_1':H})if __name__ == '__main__' :B=bd()num=0#B.cal_H()while int(num)<10:rgb_path="./rgb1/"+str(num)+".png" t_path="./rgb1/"+str(num)+"_t.png"img_rgb,img_t = B.Htest(rgb_path,t_path)cv2.imwrite('./out/'+str(num)+'.png',img_rgb)cv2.imwrite('./out/'+str(num)+'_t.png',img_t)num=num+1time=B.get_time_stamp()print(time)5.接受、處理、發送圖像(biaoding.py)
#! /usr/bin/env python2from __future__ import print_function#python2 import os import roslib import rospy from std_msgs.msg import Header from std_msgs.msg import String from sensor_msgs.msg import Image # from ros_numpy import msgify from cv_bridge import CvBridge import sys sys.path.remove('/opt/ros/melodic/lib/python2.7/dist-packages') import threading import time import numpy as np import MeanH_Test as biao import cv2 import threading #python3class SubscribeAndPublish:def __init__(self):self.all_obstacle_str=''self.sub1_name="/cam_rgb/usb_cam/image_rect_color"#self.sub1_name="/cam_rgb/usb_cam/image_raw"self.sub1= rospy.Subscriber(self.sub1_name, Image,self.callback_rgb)self.sub2_name="/cam_t/usb_cam/image_raw"self.sub2= rospy.Subscriber(self.sub2_name, Image,self.callback_t)self.rate = 10self.timer = rospy.Timer(rospy.Duration(1.0 / self.rate), self.callback_timer)self.lock_rgb = threading.Lock()self.lock_t = threading.Lock()self.img_rgb_global = []self.img_t_global = []self.pub1_name="pub_rgb"self.pub1= rospy.Publisher(self.pub1_name, Image,queue_size=1)self.pub2_name="pub_t"self.pub2= rospy.Publisher(self.pub2_name, Image,queue_size=1)self.mode=0self.path_rgb='./img/img_rgb'self.path_t='./img/img_t'# self.bridge = CvBridge()def callback_rgb(self,data):print('callback1')self.lock_rgb.acquire()img = np.frombuffer(data.data, dtype=np.uint8).reshape(data.height, data.width, -1)if len(self.img_rgb_global) > 0:self.img_rgb_global.pop(0)self.img_rgb_global.append(img)else:self.img_rgb_global.append(img)self.lock_rgb.release()#print('rgb')#img = CvBridge().imgmsg_to_cv2(data, "bgr8")#img = np.frombuffer(data.data, dtype=np.uint8).reshape(data.height, data.width, -1)#print(img.shape)#B=biao.bd()#time=B.get_time_stamp()#name='rgb_'+str(time)+'.jpg'#img_rgb=B.Htest_rgb(img)#cv2.imwrite('./img/img_rgb/'+str(name),img_rgb)#self.pub1.publish(CvBridge().cv2_to_imgmsg(img_rgb,"bgr8"))def callback_t(self,data):print('callback2')self.lock_t.acquire()img = np.frombuffer(data.data, dtype=np.uint8).reshape(data.height, data.width, -1)if len(self.img_t_global) > 0:self.img_t_global.pop(0)self.img_t_global.append(img)else:self.img_t_global.append(img)self.lock_t.release()#print('t')#img = CvBridge().imgmsg_to_cv2(data, "bgr8")#img = np.frombuffer(data.data, dtype=np.uint8).reshape(data.height, data.width, -1)#print(img.shape)#B=biao.bd()#time=B.get_time_stamp()#name='t_'+str(time)+'.jpg'#img_t=B.Htest_t(img)#cv2.imwrite('./img/img_t/'+str(name),img_t)# self.pub2.publish(CvBridge().cv2_to_imgmsg(img_t,"bgr8"))def callback_timer(self, event):print('callback')self.lock_rgb.acquire()if len(self.img_rgb_global) > 0:img_rgb = self.img_rgb_global[0].copy()else:returnself.lock_rgb.release()self.lock_t.acquire()if len(self.img_t_global) > 0:img_t = self.img_t_global[0].copy()else:returnself.lock_t.release()B=biao.bd()time=B.get_time_stamp()img_rgb=B.Htest_rgb(img_rgb)img_t=B.Htest_t(img_t)img_rgb=img_rgb[:,:,::-1]img_t=img_t[:,:,::-1]self.mode=modeif self.mode==0:self.path_rgb=path_rgbself.path_t=path_tcv2.imwrite(self.path_rgb+'/rgb_'+str(time)+'.jpg',img_rgb)cv2.imwrite(self.path_t+'/t_'+str(time)+'.jpg',img_t)print('save image successful!')self.pub1.publish(CvBridge().cv2_to_imgmsg(img_rgb,"rgb8"))self.pub2.publish(CvBridge().cv2_to_imgmsg(img_t,"rgb8"))def main(mode,path_rgb,path_t):rospy.init_node('biaoding_ws', anonymous=True)#####################t=SubscribeAndPublish()#####################rospy.spin() if __name__ == "__main__":mode=0if len(sys.argv)>1:mode=int(sys.argv[1])if mode>1:mode=1else:mode=0print(mode)if mode==0:print("save image")else:print("no save image")#create dirsb=biao.bd()ti=b.get_time_stamp()path='./img'+str(ti)folder=os.path.exists(path)if not folder:os.makedirs(path)path_rgb=path+'/img_rgb'os.makedirs(path_rgb)path_t=path+'/img_t'os.makedirs(path_t)main(mode,path_rgb,path_t)6.標定效果
總結
以上是生活随笔為你收集整理的可见光相机与红外相机标定的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java把字符转化为整型_Java开发笔
- 下一篇: 像疯了一样