利用python打开摄像头及颜色检测

利用python打开摄像头及颜色检测

最近两周由于忙于个人项目,一直未发言了,实在是太荒凉了。。。。,上周由于项目,见到Python的应用极为广泛,用起来也特别顺手,于是小编也开始着手学习Python,…下面我就汇报下今天的学习成果吧
小编运行环境unbuntu 14.0.4
首先我们先安装一下Python呗,我用的2.7,其实特别简单,一行指令就OK

sudo apt-get install python-dev

一般安装系统的时候其实python已经自带了,这步基本可以不用做,OK,我们继续往下走吧,安装python-opencv ,稍后我们需要用到opencv的库,一行指令即可,这也是小编特别喜欢linux的原因:

sudo apt-get install python-opencv

完成之后我们开始操作吧,首先同样的我们打开摄像头露个脸呗,不多说,上代码, vim pythonpractice.py 打开vim,copy以下代码即可(友情提示 python是有严格的缩进的,下面我都是四个空格缩进,各位不要复制错了):lo

lmport cv2
import numpy as np#添加模块和矩阵模块
cap=cv2.VideoCapture(0)
#打开摄像头,若打开本地视频,同opencv一样,只需将0换成("×××.avi")
while(1):    # get a frame   
    ret, frame = cap.read()    # show a frame   
    cv2.imshow("capture", frame)   
    if cv2.waitKey(1) & 0xFF == ord('q'):        
        break
cap.release()
cv2.destroyAllWindows()
#释放并销毁窗口

保存退出
python pythonpractice.py
小脸蛋即可出现在你的屏幕上了,下面稍微添加几行有意思的代码吧,实现蓝色背景检测,我这有瓶蓝色脉动,正好做个小实验。

import cv2
import numpy as np
cap = cv2.VideoCapture(0)# set blue thresh
lower_blue=np.array([78,43,46])
upper_blue=np.array([110,255,255])
while(1):    # get a frame and show   
    ret, frame = cap.read()   
    cv2.imshow('Capture', frame)    # change to hsv model   
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)    # get mask   
    mask = cv2.inRange(hsv, lower_blue, upper_blue)   
    cv2.imshow('Mask', mask)    # detect blue   
    res = cv2.bitwise_and(frame, frame, mask=mask)   
    cv2.imshow('Result', res)   
    if cv2.waitKey(1) & 0xFF == ord('q'):      
        breakcap.release()
cv2.destroyAllWindows()

同样python pythonpractice.py 运行一下,可以把手机换成蓝色背景检测以下,下面时间就交给各位理解了,代码很简单,只有简单的几行程序。
下面有个复杂点颜色识别的代码

#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import time
readlower=np.array([156,179,144])
readupper=np.array([180,255,255])
readlower1 = np.array([0, 128, 146])
readupper2 = np.array([5, 255,  255])
lowerarry=[[readlower,readupper,'red'],[readlower1,readupper2,'red1']]
capture=cv2.VideoCapture('4.mp4')
while True:
    ret,frame=capture.read()
    print frame.shape
    frame=cv2.resize(frame,(640,480))
    if ret==False:
        print("video is erro")
    #cv2.imshow('xiaorun',frame)
    hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    for colormin,colermax,name in lowerarry:
        mask=cv2.inRange(hsv,colormin,colermax)
        #res = cv2.bitwise_and(frame, frame, mask=mask)
    #mask=cv2.erode(mask,None,iterations=1)
    mask=cv2.dilate(mask,None,iterations=25)
    ret, binary = cv2.threshold(mask,15, 255, cv2.THRESH_BINARY)
    cv2.imshow('result',binary)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
    closed = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
    cv2.imshow('closed', closed)
    #erode = cv2.erode(closed, None, iterations=4)
    #cv2.imshow('erode', erode)
    dilate = cv2.dilate(closed, None, iterations=50)
    cv2.imshow('dilate', dilate)
    _,contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    #res=_.copy()
    for con in contours:
        x, y, w, h = cv2.boundingRect(con)  # 将轮廓分解为识别对象的左上角坐标和宽、高
        # 在图像上画上矩形(图片、左上角坐标、右下角坐标、颜色、线条宽度)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0,0), 3)

    cv2.imshow('res',frame)
    key=cv2.waitKey(1)
    if key==ord('q'):
        break

小编只是想说明以下,一定要学以致用,任何一种编程语言都是倒腾两天就直接上手的,按部就班的学习语法,那样不知何时才能出师了,祝各位玩得high在机器视觉上

你可能感兴趣的:(python-opencv)