本文主要是介绍Ubuntu16.04 通过opencv的方式打开realsenseD435i相机中的彩色摄像头,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
此博客主要记录如何在不使用realsenseD435i相机自带的SDK情况下,仅通过opencv的方式打开相机的彩色摄像头
操作环境:Ubuntu16.04 64 位 python2.7 opencv3.3 ROS(kinetic)
import sys
import cv2
import rospy
from camera_info_manager import CameraInfoManager
from cv_bridge import CvBridge
from sensor_msgs.msg import Image, CameraInfodef publish_images(freq=100):cam_index = 2 # index of camera to capture### initialize ROS publishers etc.rospy.init_node('dope')camera_ns = rospy.get_param('camera', 'camera/color')img_topic = '{}/image_raw'.format(camera_ns)info_topic = '{}/camera_info'.format(camera_ns)image_pub = rospy.Publisher(img_topic, Image, queue_size=10)info_pub = rospy.Publisher(info_topic, CameraInfo, queue_size=10)info_manager = CameraInfoManager(cname='dope_{}'.format(cam_index),namespace=camera_ns)try:camera_info_url = rospy.get_param('~camera_info_url')if not info_manager.setURL(camera_info_url):rospy.logwarn('Camera info URL invalid: %s', camera_info_url)except KeyError:# we don't have a camera_info_url, so we'll keep the# default ('file://${ROS_HOME}/camera_info/${NAME}.yaml')passinfo_manager.loadCameraInfo()if not info_manager.isCalibrated():rospy.logwarn('Camera is not calibrated, please supply a valid camera_info_url parameter!')### open cameracap = cv2.VideoCapture(cam_index)if not cap.isOpened():rospy.logfatal("ERROR: Unable to open camera for capture. Is camera plugged in?")sys.exit(1)rospy.loginfo("Publishing images from camera %s to topic '%s'...", cam_index, img_topic)rospy.loginfo("Ctrl-C to stop")### publish imagesrate = rospy.Rate(freq)while not rospy.is_shutdown():ret, frame = cap.read()if ret:image = CvBridge().cv2_to_imgmsg(frame, "bgr8")image.header.frame_id = 'camera_color_frame'image.header.stamp = rospy.Time.now()image_pub.publish(image)# we need to call getCameraInfo() every time in case it was updatedcamera_info = info_manager.getCameraInfo()camera_info.header = image.headerinfo_pub.publish(camera_info)rate.sleep()if __name__ == "__main__":try:publish_images()except rospy.ROSInterruptException:pass
这篇关于Ubuntu16.04 通过opencv的方式打开realsenseD435i相机中的彩色摄像头的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!