树莓派3B基于python用opencv进行颜色识别

树莓派opencv读取一帧图像

如果你的树莓派还没有安装opencv,请参考我的这篇文章安装树莓派3B安装opencv
用以下命令查看设备,若存在video0,则说明摄像头设备正常

ls /dev/

opencv读取一帧图像demo:

import cv2
import numpy as np
camera=cv2.VideoCapture(0)
while True:
	ret,frame=camera.read()
	if ret: #如果当前帧有效
		#TODO some image process
	else:
		continue
		
	cv2.imshow("frame",frame)
	cv2.waitKey(10)

opencv颜色识别流程

1.将RGB模型转换成HSV模型

RGB模型适合显示,但却并不适合作为颜色识别的模型。
HSV更加符合人眼的感受,将其作为颜色识别的模型会大大提高识别的鲁棒性,因为HSV模型颜色识别大大减少了对环境光的依赖。
opencv提供了将RGB转换到HSV的函数:

hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

2.opencv中,HSV模型的取值范围

H∈ [0, 180), S ∈ [0, 255], V ∈ [0, 255]

经过实验,蓝色在HSV模型中的取值是:H在100到140之间,S和V都在90到255之间。一些基本的颜色H的取值可以如下设置:

Orange  0-22
Yellow 22- 38
Green 38-75
Blue 75-130
Violet 130-160
Red 160-179

3.对彩色图像进行直方图均衡

#split hsv_img into 3 channels
hsvSplit = cv2.split(hsv_img)
#Hist equalization only on the V channel
cv2.equalizeHist(hsvSplit[2],hsvSplit[2])
#merge hsv_img using the result of V channel hist equalization
cv2.merge(hsvSplit,hsv_img)

4.使用opencvAPI进行颜色阈值检测,得到二值图像

    #yellow parameter  
    iLowH = 10
    iHighH = 55
    iLowS = 40
    iHighS = 255
    iLowV = 20
    iHighV = 255
#using OpenCV API to detect special color we interest
#detect each pixel in hsv_img
#if current pixel HSV value is between the theshold we set before,then set this pixel 255 in the output image
#else,set this pixel 0 in the output image
imgThresholded = cv2.inRange(hsv_img, (iLowH, iLowS, iLowV), (iHighH, iHighS, iHighV))

5.对二值图像进行开操作,删除零星噪点

#perform open opration for imgThresholded, in order to drop some noisy pixels 
element = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
imgThresholded = cv2.morphologyEx(imgThresholded, cv2.MORPH_OPEN, element)

6.再进行闭操作,连接一些连通域

#then perform close opration to connect some separated white small piece
imgThresholded = cv2.morphologyEx(imgThresholded, cv2.MORPH_CLOSE, element)

这样得到的二值图像就是颜色识别后的结果。

opencv颜色识别的完整流程

import cv2
import numpy as np
camera=cv2.VideoCapture(0)

def cam_detection_color_hsv(hsv_img):
    #set H S V threshold
    #red parameter
    '''
    iLowH = 160
    iHighH = 179
    iLowS = 90
    iHighS = 255
    iLowV = 90
    iHighV = 255
    '''
    #yellow parameter  
    iLowH = 10
    iHighH = 55
    iLowS = 40
    iHighS = 255
    iLowV = 20
    iHighV = 255
    '''
    #black parameter
    iLowH = 0
    iHighH = 180
    iLowS = 0
    iHighS = 255
    iLowV = 0
    iHighV = 48
    '''
    
    #对彩色图像做直方图均衡化
    #split hsv_img into 3 channels
    hsvSplit = cv2.split(hsv_img)
    #Hist equalization
    cv2.equalizeHist(hsvSplit[2],hsvSplit[2])
    #merge hsv_img using the result of hist equalization
    cv2.merge(hsvSplit,hsv_img)
    
    #using OpenCV API to detect special color we interest
    #detect each pixel in hsv_img
    #if current pixel HSV value is between the theshold we set before,then set this pixel 255 in the output image
    #else,set this pixel 0 in the output image
    imgThresholded = cv2.inRange(hsv_img, (iLowH, iLowS, iLowV), (iHighH, iHighS, iHighV))
    
    #perform open opration for imgThresholded, in order to drop some noisy pixels 
    element = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    imgThresholded = cv2.morphologyEx(imgThresholded, cv2.MORPH_OPEN, element)
    #then perform close opration to connect some separated white small piece
    imgThresholded = cv2.morphologyEx(imgThresholded, cv2.MORPH_CLOSE, element)
                                      
    return imgThresholded
  


while(True):
    ret,frame=camera.read()
    if ret: #如果当前帧有效
        hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    else:
        continue
    
    threshold_img = cam_detection_color_hsv(hsv_frame)
    
    cv2.imshow("threshold_img",threshold_img)
    cv2.imshow("frame",frame)
    cv2.waitKey(10)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值