树莓派摄像头实现 红线循迹

import cv2
import numpy as np

global color_x, color_y, color_radius
color_x = color_y = color_radius = 0

global color_lower
global color_upper

color_hsv = {"red" : ((100, 120, 0), (200, 250, 200))}

color = "red"
color_lower = np.array([color_hsv[color][0][0], color_hsv[color][0][1], color_hsv[color][0][2]])
color_upper = np.array([color_hsv[color][1][0], color_hsv[color][1][1], color_hsv[color][1][2]])

image = cv2.imread("./test.jpg")

def Color_Recongnize():
    global color_lower, color_upper
    global color_x, target_servox,picture
    frame = cv2.GaussianBlur(image,(5,5),0)  # 高斯滤波
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv,color_lower, color_upper)  
    rows, cols = mask.shape[:2] 
    res = cv2.resize(mask, (int(cols / 10), int(rows / 10)), interpolation=cv2.INTER_CUBIC)
    cv2.imshow('Binarization', res)
	 
    mask = cv2.erode(mask,None, iterations=2)
    mask = cv2.dilate(mask,None, iterations=2)
    mask = cv2.GaussianBlur(mask, (5, 5), 0)
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    if 1:
        if len(cnts) > 0:
            cnt = max (cnts, key = cv2.contourArea)
            (color_x, color_y), color_radius = cv2.minEnclosingCircle(cnt)
            print(color_x, color_y, color_radius)
            if color_radius > 5:
				# 将检测到的颜色用原形线圈标记出来
               cv2.circle(frame, (int(color_x), int(color_y)), int(color_radius), (0, 0, 0), 10)  
			   
    rows, cols = frame.shape[:2] 
    res = cv2.resize(frame, (int(cols / 10), int(rows / 10)), interpolation=cv2.INTER_CUBIC)
    cv2.imshow('mark', res)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == '__main__':
    Color_Recongnize()

实现效果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值