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

2 Hướng phát triển của đồ án.

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

TÀI LIỆU THAM KHẢO



1. Giáo trình “Nhập mơn xử lý ảnh” Đại học Bách khoa Hà Nội, PGS.TS Nguyễn



Quang Hoan, Năm 2006.

2. Mastering OpenCV with Practical Computer Vision Projects, Daniel Lélis



Baggio, 2012.

3. OpenCV 2 Computer Vision Application Programming Cookbook, Robert



Laganière, 2011

4. Website linh kiện điện tử http://www.htpro.vn

5. Luận văn tốt nghiệp “Nghiên cứu, thiết kế, chế tạo mơ hình đếm cá giống tự



động” Bùi Đắc Phương và Lê Trọng Tấn Phát, 2010



48



PHỤ LỤC

Code xử lý ảnh

importnumpy as np

import cv

import cv2

import time

import os

import RPi.GPIO as GPIO

import time

import sys

sys.path.append('/home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCD')

from Adafruit_CharLCD import Adafruit_CharLCD

lcd=Adafruit_CharLCD(pin_rs=9,pin_e=11,pins_db=[25,8,7,12])

Display

GPIO.setmode(GPIO.BCM)

lcd.begin(16,2)

lcd.clear()

soca = 0

i =0

timel = 0

setup_soca = 0

socacandem = 0

start = time.time()

avg = 0

49



# instantiate LCD



kq = 0

# khai bao bien tang giam so luong can dem

tang = 0

giam = 0

setuptime = 0

trunggian = 1

# khai bao cac bien dung loai 2 gt max min

gt1 = 0

gt2 = 0

gt3 = 0

gt4 = 0

gt5 = 0

gtmax = 0

gtmin = 0



GPIO.setup(21,GPIO.IN,pull_up_down = GPIO.PUD_UP)

lcd.setCursor(0,0)

lcd.message('



WELLCOME')



time.sleep(2)



lcd.setCursor(0,0)

lcd.message("May dem ca giong")

time.sleep(2)

lcd.setCursor(0,0)

lcd.message("Nhap so luong ca")

time.sleep(2)

50



# setup pin mode out

GPIO.setup(5, GPIO.OUT) #dong co quay thuan

GPIO.setup(6, GPIO.OUT) # dong co quay nguoc

def settang():

global setup_soca

globaltrunggian

GPIO.setmode(GPIO.BCM)

GPIO.setup(26,GPIO.IN,pull_up_down = GPIO.PUD_UP)

input_state = GPIO.input(26)

if input_state == False:

lcd.clear()

print('tang')

if trunggian == 1:

setup_soca = setup_soca + 1

elif trunggian == 2:

setup_soca = setup_soca + 10

elif trunggian == 3:

setup_soca = setup_soca + 20

lcd.message('so ca nhap:')

s = str(setup_soca)

lcd.setCursor(13,0)

lcd.message(s)



defsetgiam():

global setup_soca

global trunggian

51



GPIO.setmode(GPIO.BCM)

GPIO.setup(19,GPIO.IN,pull_up_down = GPIO.PUD_UP) # buton setup decrease

input_state = GPIO.input(19)

if input_state == False:

lcd.clear()

print ('giam')

if trunggian == 1:

setup_soca = setup_soca - 1

elif trunggian == 2:

setup_soca = setup_soca - 10

elif trunggian == 3:

setup_soca = setup_soca - 20

lcd.message('so ca nhap')

s = str(setup_soca)

lcd.setCursor(13, 0)

lcd.message(s)

'''--------------------------------------------------chuong trinh dem ca voi so lan lap lai la 5 lan

-------------------------------------------------'''

def demca():

global soca

global i

global OrgImg

global avg

global gt1

global gt2

52



global gt3

global gt4

global gt5

global gtmax

global gtmin

lap = 0

lap1 = 0

avg = 0

cam = cv2.VideoCapture(0)



#cv2.imwrite("anhchup5.jpg", cam)



while (lap <5):



lap = lap + 1

soca = 0

avg =0

err,img = cam.read()

#cv2.imshow("anhgoc", img)

k = cv2.waitKey(10) & 0xff

# Load an color image. 2nd arg: 1 = color, 0 = grayscale, -1 = unchanged)

#OrgImg = cv2.imread("anhca2.jpg",1)

OrgImg = img



53



cv2.imwrite("anhchupca.jpg",OrgImg)

#cv2.imshow('anhmau',OrgImg)

BinImg = cv2.cvtColor(OrgImg, cv2.COLOR_BGR2GRAY)

# BinImg = cv2.Canny(blur)

ret, BinImg = cv2.threshold(~BinImg, 155, 255, cv2.THRESH_BINARY)

OrgImg = cv2.medianBlur(OrgImg, 3)

cv2.imwrite("anhnhiphan.jpg",BinImg)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(1,1))

kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))



# co anh

BinImg = cv2.erode(BinImg,kernel)

#gian anh

BinImg = cv2.dilate(BinImg,kernel2)

cv2.imwrite("anhxuli.jpg",BinImg)

contours, hierarchy = cv2.findContours(BinImg, cv2.RETR_EXTERNAL,

cv2.CHAIN_APPROX_SIMPLE)



cv2.drawContours(OrgImg,contours,-1,(0,255,0),1)

cv2.imwrite("drawcontour.jpg",OrgImg)

#cv2.imshow("anh",OrgImg)

print "tong so phan tu cnt ",(len(contours))

h=0



#for i in contours:

54



for i in range (0, len(contours)):

#for i in contours:

area = cv2.contourArea(contours[i])

h = h+1

unicocnt = contours[i]

print "phan tu thu",h

# tim chu viperimeter = cv2.arcLength(unicocnt, True)

hull = cv2.convexHull(unicocnt)

hullperimeter = cv2.arcLength(hull,True)

if area >15 and area <= 90:

soca = soca + 1

elif area>90and area<=170:

soca = soca + area/90

elif area>170 and area<= 350:

soca = soca + area/100

elif area >350 and area <= 700:

soca = soca + area / 100

elif area >700 and area <= 1300 :

soca = soca + area/100

elif area >1300 and area <= 2000:

soca = soca + area/90

elif area>2000 and area<=4000:

soca = soca + area/120

elif area >4000and area <= 6000 :

soca = soca + area/130

elif area >6000and area <= 8000:

55



soca = soca + area/140

elif area >8000:

soca = soca +area/ 120

print area

#print perimeter

# print hullperimeter

print "so ca ",soca

if lap == 1:

gt1 = soca

if lap == 2:

gt2 = soca

if lap == 3:

gt3 = soca

if lap == 4:

gt4 = soca

if lap == 5:

gt5 = soca



list1 = [gt1,gt2,gt3,gt4,gt5]

gtmax = max(list1)

gtmin = min(list1)

avg = gt1 + gt2 + gt3 + gt4 + gt5 - gtmax - gtmin



print" gia tri trung binh", avg

print " Cac gia tri", gt1,gt2,gt3,gt4,gt5

print " gia tri min", gtmin

56



print " gia tri max", gtmax

print "so ca cuoi cung ",avg//3

print "so lan lap", lap

cam.release()



def demca1():

global soca

global i

global OrgImg

global avg

global gt1

global gt2

global gt3

global gt4

global gt5

global gtmax

global gtmin

lap = 0

lap1 = 0

avg = 0

cam = cv2.VideoCapture(0)



#cv2.imwrite("anhchup5.jpg", cam)



while (lap <1):

57



lap = lap + 1

soca = 0

avg =0

err,img = cam.read()

#cv2.imshow("anhgoc", img)

k = cv2.waitKey(10) &0xff

# Load an color image. 2nd arg: 1 = color, 0 = grayscale, -1 = unchanged)

#OrgImg = cv2.imread("anhca2.jpg",1)

OrgImg = img



cv2.imwrite("anhchupca.jpg",OrgImg)

#cv2.imshow('anhmau',OrgImg)

BinImg = cv2.cvtColor(OrgImg, cv2.COLOR_BGR2GRAY)

# BinImg = cv2.Canny(blur

ret, BinImg = cv2.threshold(~BinImg, 155, 255, cv2.THRESH_BINARY)

OrgImg = cv2.medianBlur(OrgImg, 3)

cv2.imwrite("anhnhiphan.jpg",BinImg)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(1,1))

kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))



# co anh

BinImg = cv2.erode(BinImg,kernel)

#gian anh

BinImg = cv2.dilate(BinImg,kernel2)

58



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

2 Hướng phát triển của đồ án.

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

×