ROS-release camera node and write opencv image processing node (python)

Software and hardware environment

hardware

  • Lower computer: Raspberry Pi 4B (4G)
  • Host computer: PC
  • USB camera

software

  • Both upper and lower computers are Ubuntu 18.04 systems
  • ROS melodic

Opencv and ROS

Conversion between OpenCV format pictures (or video frames) and ROS data format pictures (or video frames). Or be straightforward and send a message of the image data type through ROS.

We only need to define the following data to publish the image data.
Insert picture description here

Publish camera data node

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

import cv2
import numpy as np
import rospy
from std_msgs.msg import Header
from sensor_msgs.msg import Image
from cv_bridge import CvBridge , CvBridgeError
import time

if __name__=="__main__":
    capture = cv2.VideoCapture(0) # 定义摄像头
    rospy.init_node('camera_node', anonymous=True) #定义节点
    image_pub=rospy.Publisher('/image_view/image_raw', Image, queue_size = 1) #定义话题

    while not rospy.is_shutdown():    # Ctrl C正常退出,如果异常退出会报错device busy!
        start = time.time()
        ret, frame = capture.read()
        if ret: # 如果有画面再执行
            # frame = cv2.flip(frame,0)   #垂直镜像操作
            frame = cv2.flip(frame,1)   #水平镜像操作   
    
            ros_frame = Image()
            header = Header(stamp = rospy.Time.now())
            header.frame_id = "Camera"
            ros_frame.header=header
            ros_frame.width = 640
            ros_frame.height = 480
            ros_frame.encoding = "bgr8"
            ros_frame.step = 1920
            ros_frame.data = np.array(frame).tostring() #图片格式转换
            image_pub.publish(ros_frame) #发布消息
            end = time.time()  
            print("cost time:", end-start ) # 看一下每一帧的执行时间,从而确定合适的rate
            rate = rospy.Rate(25) # 10hz 

    capture.release()
    cv2.destroyAllWindows() 
    print("quit successfully!")

Image receiving and processing node

I only wrote a simple code for receiving images and displaying it, which can be expanded on the basis of the basic code.

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

import rospy
import numpy as np
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2
 
def callback(data):
    global bridge
    cv_img = bridge.imgmsg_to_cv2(data, "bgr8")
    cv2.imshow("frame" , cv_img)
    cv2.waitKey(1)

if __name__ == '__main__':
    rospy.init_node('img_process_node', anonymous=True)
    bridge = CvBridge()
    rospy.Subscriber('/image_view/image_raw', Image, callback)
    rospy.spin()

Show results:
Insert picture description here

Reference article:

Guess you like

Origin blog.csdn.net/qq_45779334/article/details/114044103