新手想用opencv python做颜色识别,然后通过树莓派将信号输出到单片机中

来源:互联网 发布:盐城java软件培训 编辑:程序博客网 时间:2024/04/28 06:48


本人新手一枚,老师给了个任务,让我通过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画出来,如果这加上个删除像素或者面积小于一定值的范围轮廓就好了,所以这代码还有待完善,希望有兄弟可以完善下。谢谢~~~~

原创粉丝点击