1. 程式人生 > >新手想用opencv python做顏色識別,然後通過樹莓派將訊號輸出到微控制器中

新手想用opencv python做顏色識別,然後通過樹莓派將訊號輸出到微控制器中

本人新手一枚,老師給了個任務,讓我通過opencv識別出紅,藍綠,白,黑,5種顏色,之前是用的VS2010和opencv做的,但發現不僅配置麻煩,可能我比較菜,效果做的不好 ,就跑到Python裡做了這。希望有類似的目的的人可以一起學習下,

#!/usr/bin/python

# -*- coding: UTF-8 -*-
import numpy as np
import cv2


cap=cv2.VideoCapture(0)

#藍色HSV範圍

lower_blue=np.array([156,43,46])
upper_blue=np.array([180,255,255])


while True:
    ret,frame=cap.read()
    hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    mask=cv2.inRange(hsv,lower_blue,upper_blue)
    res=cv2.bitwise_and(frame,frame,mask=mask)
    #cv2.imshow("res",res)
    cv2.imwrite("2.jpg", res)

#前面是為了得到一張二值圖
    img = cv2.imread("2.jpg")
    h, w = img.shape[:2]
#    cv2.imshow("Origin", img)


    blured = cv2.blur(img,(5,5))
#cv2.imshow("Blur", blured)


    mask = np.zeros((h+2, w+2), np.uint8)
    cv2.floodFill(blured, mask, (w-1,h-1), (255,255,255), (2,2,2),(3,3,3),8)
    cv2.imshow("floodfill", blured)


    gray = cv2.cvtColor(blured,cv2.COLOR_BGR2GRAY)




    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(50, 50))
    opened = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel) 
    closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, kernel) 
#cv2.imshow("closed", closed)


    ret, binary = cv2.threshold(closed,100,255,cv2.THRESH_BINARY_INV)
    #cv2.imshow("binary", binary)




    contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE) 

    cv2.drawContours(img,contours,-1,(0,0,255),3)

   #輸出輪廓個數

    print(len(contours)

    if len(contours)==1:
            print"blue"

            break

#執行到這發現很容易跳出程式原因是 因為 會有面積很小的輪廓也會drawContours畫出來,如果這加上個刪除畫素或者面積小於一定值的範圍輪廓就好了,所以這程式碼還有待完善,希望有兄弟可以完善下。謝謝~~~~



lower_red=np.array([156,43,46])
upper_red=np.array([180,255,255])
while True:
    ret,frame=cap.read()
    hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    mask=cv2.inRange(hsv,lower_red,upper_red)
    res=cv2.bitwise_and(frame,frame,mask=mask)
    
    cv2.imwrite("3.jpg", res)


    img = cv2.imread("3.jpg")
    h, w = img.shape[:2]
#    cv2.imshow("Origin", img)


    blured = cv2.blur(img,(5,5))
#cv2.imshow("Blur", blured)


    mask = np.zeros((h+2, w+2), np.uint8)
    cv2.floodFill(blured, mask, (w-1,h-1), (255,255,255), (2,2,2),(3,3,3),8)
    cv2.imshow("floodfill", blured)


    gray = cv2.cvtColor(blured,cv2.COLOR_BGR2GRAY)




    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(50, 50))
    opened = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel) 
    closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, kernel) 
#cv2.imshow("closed", closed)


    ret, binary = cv2.threshold(closed,100,255,cv2.THRESH_BINARY_INV)
    #cv2.imshow("binary", binary)




    contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE) 
    cv2.drawContours(img,contours,-1,(0,0,255),3)
    print(len(contours))
    print"顏色為綠色"


    if len(contours)==1:
            print"gree"
            break


    
cap.release()

cv2.destroyAllWindows()

程式容易跳出,沒達到我想要的結果,原因是 因為 會有面積很小的輪廓也會drawContours畫出來,如果這加上個刪除畫素或者面積小於一定值的範圍輪廓就好了,所以這程式碼還有待完善,希望有兄弟可以完善下。謝謝~~~~