欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Raspberry Pi 4B 颜色检测

发布时间:2025/5/22 编程问答 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Raspberry Pi 4B 颜色检测 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

环境:

  • Python:3.7.3
  • opencv-python:3.4.6.27
main.py文件
#!/usr/bin/env python # coding: UTF-8 #显示摄像头组件 import cv2 import sys, os HSV_Configwidgets_path = os.getcwd() sys.path.append(HSV_Configwidgets_path) from time import time from IPython.display import display import HSV_Configimage = cv2.VideoCapture(0)image.set(3, 320) image.set(4, 240) image.set(5, 30) #设置帧率 image.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G')) image.set(cv2.CAP_PROP_BRIGHTNESS, 62) #设置亮度 -64 - 64 0.0 image.set(cv2.CAP_PROP_CONTRAST, 63) #设置对比度 -64 - 64 2.0 image.set(cv2.CAP_PROP_EXPOSURE, 4800) #设置曝光值 1.0 - 5000 156.0update_hsv = HSV_Config.update_hsv()color_hsv = {"red" : ((0, 70, 72), (7, 255, 255)),"green" : ((54, 109, 78), (77, 255, 255)),"blue" : ((92, 100, 62), (121, 251, 255)),"yellow": ((26, 100, 91), (32, 255, 255))}print("开始检测...") while True:start_time = time()ret, frame = image.read()frame, binary = update_hsv.get_contours(frame, color_hsv)parsing_time = time() - start_timecv2.putText(frame, "infer time(ms): %.3f, FPS: %.2f" % (parsing_time * 1000, 1 / (parsing_time + 0.0001)), (15, 55), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255), 1)# 显示检测结果cv2.imshow('capture', frame)# 检测按键k = cv2.waitKey(1)if k==27:breakimage.release() cv2.destroyAllWindows()
HSV_Config.py 文件
# !/usr/bin/env python # coding: utf-8 import random import cv2 as cv import numpy as np import tkinter as tkclass update_hsv:def __init__(self):'''初始化一些参数'''self.image = Noneself.binary = Nonedef Image_Processing(self, hsv_range):'''形态学变换去出细小的干扰因素:param img: 输入初始图像:return: 检测的轮廓点集(坐标)'''(lowerb, upperb) = hsv_range# 复制原始图像,避免处理过程中干扰color_mask = self.image.copy()# 将图像转换为HSV。hsv_img = cv.cvtColor(self.image, cv.COLOR_BGR2HSV)# 筛选出位于两个数组之间的元素。color = cv.inRange(hsv_img, lowerb, upperb)# 设置非掩码检测部分全为黑色color_mask[color == 0] = [0, 0, 0]# 将图像转为灰度图gray_img = cv.cvtColor(color_mask, cv.COLOR_RGB2GRAY)# 获取不同形状的结构元素kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))# 形态学闭操作dst_img = cv.morphologyEx(gray_img, cv.MORPH_CLOSE, kernel)# 图像二值化操作ret, binary = cv.threshold(dst_img, 10, 255, cv.THRESH_BINARY)# 获取轮廓点集(坐标) python2和python3在此处略有不同# _, contours, heriachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) #python2contours, heriachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) # python3return contours, binarydef draw_contours(self, hsv_name, contours):'''采用多边形逼近的方法绘制轮廓'''for i, cnt in enumerate(contours):# 计算多边形的矩mm = cv.moments(cnt)if mm['m00'] == 0:continuecx = mm['m10'] / mm['m00']cy = mm['m01'] / mm['m00']# 获取多边形的中心(x, y) = (np.int(cx), np.int(cy))# 计算轮廓的⾯积area = cv.contourArea(cnt)# ⾯积⼤于10000if area > 800:# 绘制中⼼cv.circle(self.image, (x, y), 5, (0, 0, 255), -1)# 计算最小矩形区域rect = cv.minAreaRect(cnt)# 获取盒⼦顶点box = cv.boxPoints(rect)# 转成long类型box = np.int0(box)# 绘制最小矩形cv.drawContours(self.image, [box], 0, (255, 0, 0), 2)cv.putText(self.image, hsv_name, (int(x - 15), int(y - 15)),cv.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 255), 2)def get_contours(self, img, color_hsv):binary = None# 规范输入图像大小self.image = cv.resize(img, (320, 240), )for key, value in color_hsv.items():# 检测轮廓点集color_contours, binary = self.Image_Processing(color_hsv[key])# 绘制检测图像,并控制跟随self.draw_contours(key, color_contours)return self.image, binary

识别效果:

总结

以上是生活随笔为你收集整理的Raspberry Pi 4B 颜色检测的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。