Tải bản đầy đủ - 0 (trang)
3 Hướng phát triển của đề tài

3 Hướng phát triển của đề tài

Tải bản đầy đủ - 0trang

2. OpenCV_2_Computer_Vision_Application_Programming_Cookbook của tác giả

Robert Laganière

Website

2. https://xivila.com/talks/detail/32/huong-dan-lap-trinh-raspberry-pi-phan-1-gioi-

thieu/

3. http://docs.opencv.org/master/d9/df8/tutorial_root.html

4. http://opencv.org/

52

PHỤ LỤC

#!/usr/bin/python

# import the necessary packages

from picamera.array import PiRGBArray

from picamera import PiCamera

import time

import cv2

import numpy as np

import io

import serial

arduinoSerialData = serial.Serial('/dev/ttyACM0',9600)

rangeMin = np.array([0, 100, 100], np.uint8)

rangeMax = np.array([14, 255, 255], np.uint8)

rangeMin1 = np.array([170,80, 80], np.uint8)

rangeMax1 = np.array([190, 255, 255], np.uint8)

#mauvang

rangeMiny = np.array([13, 180, 180], np.uint8)

rangeMaxy = np.array([30, 255, 255], np.uint8)

#mauxanh

rangeMing = np.array([30, 100, 100], np.uint8)

53

rangeMaxg = np.array([75, 255, 255], np.uint8)

i=0

a=0

b=0

c=0

m=120

m1=121

n=120

n1=121

#moto

u1=22

d1=12

u2=35

d2=25

kernel = np.ones((5,5),np.uint8)

# initialize the camera and grab a reference to the raw camera capture

camera = PiCamera()

camera.resolution = (640, 480)

camera.framerate = 32

rawCapture = PiRGBArray(camera, size=(640, 480))

54

# allow the camera to warmup

time.sleep(0.1)

frame=io.BytesIO()

for frame in camera.capture_continuous(rawCapture,

format="bgr",use_video_port=True,splitter_port = 0):

frame.truncate()

frame.seek(0)

camera.capture_continuous(rawCapture, format="bgr")

image = rawCapture.array

image = cv2.flip(image,0)

cv2.imshow("original Image", image)

imgHSV = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)

#cv2.getStructuringElement(shape, ksize[, anchor])

#mau do

imgThresh2 = cv2.inRange(imgHSV, rangeMin, rangeMax, 0)

imgThresh1 = cv2.inRange(imgHSV, rangeMin1, rangeMax1, 0)

imgThresh = imgThresh1 + imgThresh2

opening = cv2.morphologyEx(imgThresh, cv2.MORPH_OPEN,kernel)

opening = cv2.morphologyEx(opening, cv2.MORPH_OPEN,kernel)

closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)

closing = cv2.morphologyEx(closing, cv2.MORPH_CLOSE, kernel)

55

dilation = cv2.dilate(closing,kernel,iterations = 2)

cv2.imshow("binari red",dilation)

im2, contours, hierarchy = cv2.findContours(dilation,cv2.RETR_TREE,

cv2.CHAIN_APPROX_NONE)

#mau vang

imgThreshy = cv2.inRange(imgHSV, rangeMiny, rangeMaxy, 0)

openingy = cv2.morphologyEx(imgThreshy, cv2.MORPH_OPEN,kernel)

openingy = cv2.morphologyEx(imgThreshy, cv2.MORPH_OPEN,kernel)

closingy = cv2.morphologyEx(openingy, cv2.MORPH_CLOSE, kernel)

closingy = cv2.morphologyEx(closingy, cv2.MORPH_CLOSE, kernel)

dilationy = cv2.dilate(closingy,kernel,iterations = 2)

cv2.imshow("binari yellow",dilationy)

im1, contours1, hierarchy= cv2.findContours(dilationy,cv2.RETR_TREE,

cv2.CHAIN_APPROX_NONE)

#mauxanh

imgThreshg = cv2.inRange(imgHSV, rangeMing, rangeMaxg, 0)

openingg = cv2.morphologyEx(imgThreshg, cv2.MORPH_OPEN,kernel)

openingg = cv2.morphologyEx(openingg, cv2.MORPH_OPEN,kernel)

closingg = cv2.morphologyEx(openingg, cv2.MORPH_CLOSE, kernel)

closingg = cv2.morphologyEx(closingg, cv2.MORPH_CLOSE, kernel)

dilationg = cv2.dilate(closingg,kernel,iterations = 2)

56

cv2.imshow("binari green",dilationg)

im2, contours2, hierarchy= cv2.findContours(dilationg,cv2.RETR_TREE,

cv2.CHAIN_APPROX_NONE)

if ( a<35000 and b<17000 and c<10000): # dieu kien de tinh dien tich

for pic,contour in enumerate(contours):

area = cv2.contourArea(contour)

if (area>1):

areas = [cv2.contourArea(k) for k in contours]

max_index = np.argmax(areas)

cnt=contours[max_index]

a=cv2.contourArea(cnt)

print ("dien tich mau do ",a)

break

for pic1,contour1 in enumerate(contours1):

area1 = cv2.contourArea(contour1)

if (area1>1):

areas1=[cv2.contourArea(d) for d in contours1]

max_index1=np.argmax(areas1)

cnt1=contours1[max_index1]

b=cv2.contourArea(cnt1)

print ("dien tich mau vang ", b)

57

break

#mau xanh

for pic2,contour2 in enumerate(contours2):

area2 = cv2.contourArea(contour2)

if (area2>1):

areas2=[cv2.contourArea(e) for e in contours2]

max_index2=np.argmax(areas2)

cnt2=contours2[max_index2]

c=cv2.contourArea(cnt2)

print ("dien tich mau xanh ",c)

break

if ( a>35000 ):

arduinoSerialData.write('1')

a=0

if m>u1:

m=0

else:

m1=0

elif( b>17000 ):

arduinoSerialData.write('2')

58

b=0

if n>u2:

n=0

else:

n1=0

elif (c>10000) :

arduinoSerialData.write('3')

c=0

# chon thoi diem cho dong co chay

# mau do

if m1>u1:

if (m==d1):

arduinoSerialData.write('4')

if(m==u1):

arduinoSerialData.write('6')

else:

if m>d1:

if m1>d1:

m=m1

59

if(m==u1):

arduinoSerialData.write('6')

else:

m=d1+1

if(m==u1):

arduinoSerialData.write('6')

else:

if (m==d1):

arduinoSerialData.write('4')

#mau vang

if n1>u2:

if(n==d2):

arduinoSerialData.write('5')

if(n==u2):

arduinoSerialData.write('7')

else:

if n>d2:

if n1>d2:

n=n1

if(n==u2):

60

arduinoSerialData.write('7')

else:

n=d2 + 1

if(n==u2):

arduinoSerialData.write('7')

else:

if(n==d2):

arduinoSerialData.write('5')

m=m+1

m1=m1+1

n=n+1

n1=n1+1

for pic,contour in enumerate(contours):

area = cv2.contourArea(contour)

if (area>1):

areas = [cv2.contourArea(k) for k in contours]

max_index = np.argmax(areas)

cnt=contours[max_index]

a=cv2.contourArea(cnt)

break

61

for pic1,contour1 in enumerate(contours1):

area1 = cv2.contourArea(contour1)

if (area1>1):

areas1=[cv2.contourArea(d) for d in contours1]

max_index1=np.argmax(areas1)

cnt1=contours1[max_index1]

b=cv2.contourArea(cnt1)

break

#mau xanh

for pic2,contour2 in enumerate(contours2):

area2 = cv2.contourArea(contour2)

if (area2>1):

areas2=[cv2.contourArea(e) for e in contours2]

max_index2=np.argmax(areas2)

cnt2=contours2[max_index2]

c=cv2.contourArea(cnt2)

break

cv2.drawContours(image, contours, -1, (0,0,255),2)

cv2.drawContours(image,contours1,-1,(255,0,0),2)

cv2.drawContours(image,contours2,-1,(0,255,0),2)

62

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

3 Hướng phát triển của đề tài

Tải bản đầy đủ ngay(0 tr)

×