樹莓車攝相機程式



我們首先針對 「電腦視覺」領域去做一些相關應用 演算程式設計

1.顏色偵測 、 膚色偵測
2.霍夫找圓 、 霍夫找直線
3.人臉偵測 (Haar偵測)


顏色偵測



第一階段. 開攝影機鏡頭


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/usr/bin/env python
import cv2
# http://docs.opencv.org/2.4.8/modules/highgui/doc/reading_and_writing_images_and_video.html
cap = cv2.VideoCapture(0)
cap.set(3,320)#Sets a property in the VideoCapture---FRAME_WIDTH
cap.set(4,240)#Sets a property in the VideoCapture---FRAME_HEIGHT
while(cap.isOpened()):
    ret , frame = cap.read()
    if ret == True:
        cv2.imshow("preview",frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()
cv2.destroyAllWindows()


第二階段.顏色門檻值遮罩顯示




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
cap.set(3,640)#Sets a property in the VideoCapture---FRAME_WIDTH
cap.set(4,480)#Sets a property in the VideoCapture---FRAME_HEIGHT
while(cap.isOpened()):
    #Step1.Take each frame
    ret , frame = cap.read()
    #Step2.Color Space transform
    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    #Step3.define Range of blue color in HSV
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])
    #Step4.Threshold the HSV image to get only blue color
    imgMask = cv2.inRange(hsv , lower_blue , upper_blue)


  
    cv2.imshow('frame' , frame)
    cv2.imshow('mask' , imgMask)

    k = cv2.waitKey(5) & 0xFF
    if k==27:
        break
cv2.destroyAllWindows()


第三階段.顏色門檻值遮罩圖 去和 原先 彩色圖 做bit運算 位元取AND




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
cap.set(3,320)#Sets a property in the VideoCapture---FRAME_WIDTH
cap.set(4,240)#Sets a property in the VideoCapture---FRAME_HEIGHT
while(cap.isOpened()):
    #Step1.Take each frame
    ret , frame = cap.read()
    #Step2.Color Space transform
    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    #Step3.define Range of blue color in HSV
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])
    #Step4.Threshold the HSV image to get only blue color
    imgMask = cv2.inRange(hsv , lower_blue , upper_blue)
    #Step5.Bitwise AND  mask and original image
    res = cv2.bitwise_and(frame , frame , mask=imgMask)
   
    cv2.imshow('frame' , frame)
    cv2.imshow('mask' , imgMask)
    cv2.imshow('res' , res)

    k = cv2.waitKey(5) & 0xFF
    if k==27:
        break
cv2.destroyAllWindows()








import RPi.GPIO as GPIO
import time
#import readchar   ---> can't not import

Motor_R1_Pin = 16
Motor_R2_Pin = 18
Motor_L1_Pin = 11
Motor_L2_Pin = 13
t = 5


GPIO.setmode(GPIO.BOARD)
GPIO.setup(Motor_R1_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Motor_R2_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Motor_L1_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Motor_L2_Pin, GPIO.OUT, initial=GPIO.LOW)


def stop():
    GPIO.output(Motor_R1_Pin, False)
    GPIO.output(Motor_R2_Pin, False)
    GPIO.output(Motor_L1_Pin, False)
    GPIO.output(Motor_L2_Pin, False)


def forward():
    GPIO.output(Motor_R1_Pin, True)
    GPIO.output(Motor_R2_Pin, False)
    GPIO.output(Motor_L1_Pin, True)
    GPIO.output(Motor_L2_Pin, False)
    time.sleep(t)
    stop()


def backward():
    GPIO.output(Motor_R1_Pin, False)
    GPIO.output(Motor_R2_Pin, True)
    GPIO.output(Motor_L1_Pin, False)
    GPIO.output(Motor_L2_Pin, True)
    time.sleep(t)
    stop()


def turnRight():
    GPIO.output(Motor_R1_Pin, True)
    GPIO.output(Motor_R2_Pin, False)
    GPIO.output(Motor_L1_Pin, False)
    GPIO.output(Motor_L2_Pin, False)
    time.sleep(t)
    stop()

def turnLeft():
    GPIO.output(Motor_R1_Pin, False)
    GPIO.output(Motor_R2_Pin, False)
    GPIO.output(Motor_L1_Pin, True)
    GPIO.output(Motor_L2_Pin, False)
    time.sleep(t)
    stop()


if __name__ == "__main__":

    print ("Press 'q' to quit...")
    print ("Press 'w' to forward...")
    print ("Press 's' to backward...")
    print ("Press 'a' to turnLeft...")
    print ("Press 'd' to turnRight...")

    while True:
        #ch = readchar.readkey()
        ch = raw_input('Press a key and the Enter:')
        #ch = input('Press a key and the Enter:')
       
        if ch == 'w':
            forward()

        elif ch == 's':
            backward()

        elif ch == 'd':
            turnRight()

        elif ch == 'a':
            turnLeft()

        elif ch == 'q':
            print ("\nQuit")
            GPIO.cleanup()
            quit()



留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題