樹莓派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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章