Chapter 5: Computer Vision

Camera Integration

import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2

class CameraNode(Node):
    def __init__(self):
        super().__init__('camera_node')
        self.subscription = self.create_subscription(
            Image,
            'camera/image_raw',
            self.image_callback,
            10
        )
        self.bridge = CvBridge()
    
    def image_callback(self, msg):
        # Convert ROS Image to OpenCV
        cv_image = self.bridge.imgmsg_to_cv2(msg, 'bgr8')
        
        # Process image
        gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
        edges = cv2.Canny(gray, 50, 150)
        
        # Display
        cv2.imshow('Edges', edges)
        cv2.waitKey(1)

Object Detection

# Using YOLO or TensorFlow
import cv2
import numpy as np

def detect_objects(image):
    # Load model
    net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
    
    # Detect
    blob = cv2.dnn.blobFromImage(image, 1/255, (416, 416))
    net.setInput(blob)
    outputs = net.forward()
    
    return outputs

Line Following

def detect_line(image):
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    
    # Detect yellow line
    lower = np.array([20, 100, 100])
    upper = np.array([30, 255, 255])
    mask = cv2.inRange(hsv, lower, upper)
    
    # Find center
    M = cv2.moments(mask)
    if M['m00'] > 0:
        cx = int(M['m10'] / M['m00'])
        return cx
    return None

Next: Chapter 6 - Sensor Fusion!