You are on page 1of 14

RASPBERRY PI WITH CAMERA TUTORIAL

Sinh vin tm hiu: 1/ Nguyn c Trung - 10520353 2/ Trn Thanh Lim - 10520364

Mc lc:
I. II. III. IV. 1) 2) V. 1) 2) VI. VII. Gii thiu tng quan. ............................................................................................................................ 2 c im v tnh cht. .......................................................................................................................... 4 Lp t. ............................................................................................................................................. 6 Lm vic vi camera........................................................................................................................ 10 Cc lnh thng s hnh nh:............................................................................................................ 10 Cc lnh thng s xem trc: ......................................................................................................... 11 Quay video v chp nh. ..................................................................................................................... 11 Quay phim: ...................................................................................................................................... 11 Chp nh: ........................................................................................................................................ 12 X l nh c bn. ............................................................................................................................ 12 Ti liu tham kho:.......................................................................................................................... 14

Raspberry Pi with Camera Tutorial

I.

Gii thiu tng quan.

Trc khi xut hin camera, iu duy nht bn c th lm thm kh nng nhn bit hnh nh, quay phim, chp hnh cho RPi l s dng 1 webcam cm vo cng USB. Vi cc webcam Logotech tch hp sn nh dng xut mjpeg s gip Raspberry x l nhanh hn. Nhng cc webcam Logitech li c gi thnh kh cao, nht l cc webcam c phn gii ln. Raspberry Pi camera c tch hp camera 5 Megapixel c nhy sng cao, c th chp tt nhiu iu kin nh sng khc nhau, c trong nh v ngoi tri. im c bit m camera mang li l chp hnh nt cao trong lc quay phim. Bn khng tn thm cng USB no cho camera v camera c gn chc chn vo socket CSI (gn cng LAN viRaspberry Pi model B). iu ny gip hn ch tnh trng nghn bng thng cho chip x l USB trn mch
2

Raspberry Pi with Camera Tutorial

Raspberry. Chiu di cp ni camera c tnh ton cn thn khi va t c di cn thit trong khi vn m bo tc truyn hnh nh t module v RPi.

Raspberry Pi camera module gn vo Vi Raspberry pi camera board bn s lm c g? Bn c th dng camera v RPi thit lp h thng pht hin chuyn ng. H thng ny hot ng bng cch s dng hnh nh t camera kt hp vi chng trnh motion x l hnh nh v a ra lnh iu khin nu pht hin thy c chuyn ng. Camera cng c bit hu ch vi cc bn yu thch lm phim, bn c th s dng quay nhng gc quay kh hoc nhng cnh quay c m ch vi my quay gn nh nht mi lm c. Ngoi ra, bn c th s dng camera quay cc on phim time-lapse (ghp nhiu hnh li vi nhau) ang c rt nhiu ngi dng trn th gii thc hin.

Raspberry Pi with Camera Tutorial

Camera module c gn hon chnh vi RPi Trc khi c th s dng c raspberry pi camera board, bn cn phi thit lp cu hnh h thng v kch hot chc nng camera trn RPi.
II. c im v tnh cht.

y l mt module camera CMOS 5MP vi ng knh c kh nng chp nh cng nhng quay video vi nt cao. N c th chp nh vi phn gii 2592x1944, trong khi video c h tr c th xem c 1080p vi 30FPS, 720P vi 60 FPS, v 640x480 vi 60FPS hoc 90 FPS.

Raspberry Pi with Camera Tutorial

Kch thc ca module camera ch c 25mm x 20mm x 9mm v ch nng c 3grams. iu ny gip n tr thnh thit b l tng cho cc projec nh camera an ninh b mt, v thm ch n cn l camera trn board cho cc xe thm him RC. Module ny c h tr phin bn mi nht ca Raspbian, h iu hnh chnh ca Raspberry Pi.

1.4 m X 1.4 m pixel with OmniBSI technology for high performance (high sensitivity, low crosstalk, low noise) Optical size of 1/4" Automatic image control functions: Automatic exposure control (AEC) Automatic white balance (AWB) Automatic band filter (ABF) Automatic 50/60 Hz luminace detection Automatic black level calibration (ABLC)
5

Raspberry Pi with Camera Tutorial

Programmable controls for frame rate , AEC/AGC 16-zone size/position/weight control, mirror and flip, cropping, windowing, and panning Digital video port (DVP) parallel output interface 32 bytes of embedded one-time programmable (OTP) memory
Lp t.

III.

Trc khi lp t module camera, bn phi cn lm trc vi th vi Pi, bt u vi vic phi m bo rng h iu hnh trn Pi ca bn c cp nht phin bn mi nht, vic ny kh n gin, bn ch cn m bo Pi ca mnh ang kt ni vi internet, sau vo console v chy cc lnh sau y: sudo apt-get update

vic ny s mt vi pht hon thnh. Khi hon thnh chng ta cn phi chy update bng cch dng lnh sau: sudo apt-get upgrade y

Raspberry Pi with Camera Tutorial

bc ny bn cn phi in password ca ngi dng root v vic cp nht s mt mt khon thi gian hon thnh. Mt khi n hon tt chng ta chuyn sang vic chun b chy module camera lp t vo Pi. Lnh u tin chng ta cn chy s ci t gi git-core. sudo apt-get install git-core -y

Raspberry Pi with Camera Tutorial

Sau chng ta cn download bn rpi-update, vic ny s c thc thi bng lnh sau: sudo wget https://raw.github.com/Hexxeh/rpi-update/master/rpiupdate -O /usr/bin/rpi-update

Raspberry Pi with Camera Tutorial

Khi vic ny hon tt chng ta phi thay i cc quyn trong bn rpiupdate, thc hin u ny chng ta ch cn chy mt lnh n gin nh sau: sudo chmod +x /usr/bin/rpi-update

Mi vic gn nh hon tt. Gi ta cn phi update cu hnh file boot bng cc thm cc dng sau pha di ca file bng lnh: sudo nano /boot/config.txt thm vo cc dng sau: start_file=start_x.elf fixup_file=fixup_x.dat Bn cng s phi chc rng b nh GPU c cp pht 128MP bng dng lnh: gpu_mem=128 Lu li v vn gi nguyn tn file nh ban u.

Raspberry Pi with Camera Tutorial

Sau dng lnh bn di v chn la chn th 5 kch hot camera module. sudo raspi-config.
IV. Lm vic vi camera.

Sau y l cc lnh iu khin cc chc nng c bn ca module camera. Usage: raspivid [options]
1) Cc lnh thng s hnh nh:

-?, --help : This help information -w, --width : Set image width . Default 1920 -h, --height : Set image height . Default 1080 -b, --bitrate : Set bitrate. Use bits per second (e.g. 10MBits/s would be -b 10000000) -o, --output : Output filename (to write to stdout, use '-o -') -v, --verbose : Output verbose information during run -t, --timeout : Time (in ms) to capture for. If not specified, set to 5s. Zero to disable -d, --demo : Run a demo mode (cycle through range of camera options, no capture)
10

Raspberry Pi with Camera Tutorial

-fps, --framerate : Specify the frames per second to record -e, --penc : Display preview image *after* encoding (shows compression artifacts) -g, --intra : Specify the intra refresh period (key frame rate/GoP size) -sh, --sharpness : Set image sharpness (-100 to 100) -co, --contrast : Set image contrast (-100 to 100) -br, --brightness : Set image brightness (0 to 100) -sa, --saturation : Set image saturation (-100 to 100) -ISO, --ISO : Set capture ISO -vs, --vstab : Turn on video stablisation -ev, --ev : Set EV compensation -ex, --exposure : Set exposure mode (see Notes) -awb, --awb : Set AWB mode (see Notes) -ifx, --imxfx : Set image effect (see Notes) -cfx, --colfx : Set colour effect (U:V) -mm, --metering : Set metering mode (see Notes) -rot, --rotation : Set image rotation (0-359) -hf, --hflip : Set horizontal flip -vf, --vflip : Set vertical flip
2) Cc lnh thng s xem trc:

-p, --preview : Preview window settings -f, --fullscreen : Fullscreen preview mode -op, --opacity : Preview window opacity (0-255) -n, --nopreview : Do not display a preview window
V. Quay video v chp nh. 1) Quay phim:

quay phim v chp nh bnh thng vi module camera, chng ta phi dng terminal. Trc tin chng ta phi m bo rng mi th ang hot ng, vi module camera c kt ni, ta s dng lnh: raspivid d lnh ny s load mt bn demo m bn c th thy trong ca s terminal. Nu khng c g xy ra v bn b bo li hy dng lnh raspi-config nh
11

Raspberry Pi with Camera Tutorial

theo hng dn cc phn trc v phi chc chn rng camera c bt. raspivid -w 1920 -h 1080 -fps 30 -t 30000 -o video.h264 raspivid kh to camera v thit lp ch video. -w 1920 - h 1080 phn gii ca video. -fps 30 khung hnh trn giy l 30. -t 30000 thit lp thi gian ti a cho video. -o video.h264 tn file xut ra.
2) Chp nh:

raspistill -o picture,jpg raspistill khi to camera v thit lp ch chp nh. -o picture.jpg file xut ra. raspicam -tl 1000 -o file%d.jpg c mt giy s chp 1 nh, ch dng li khi ta nhn Ctrl-C. raspicam -ifx negative -o negative_image.jpg lnh ny s chp hnh ch negative. Raspicam c y cc hiu ng hnh nh khi chp nh. Raspivid C y cc hiu ng hnh nh khi quay video.
VI. X l nh c bn.

y chng ta s dng ngn ng python. Trc tin ta phi thm vo th vin x l nh:
from imgproc import *

Module imgproc cung cp cho chng ta mt s chc nng c bn s dng webcam, truy cp d liu l hnh nh, v hin th hnh nh ln mng hnh. Tip theo chng ta s bt u bng vic m camera . Thm vo ngay di lnh import:
my_camera = Camera(320, 240)

Lnh trn s m camera v thit lp kch thc ca nh l 320 x 240.


my_image = my_camera.grabImage()

Ly mt nh t camera v lu tr n.
# open a view setting the view to the size of the captured image my_view = Viewer(my_image.width, my_image.height, "Basic image processing")
12

Raspberry Pi with Camera Tutorial

# display the image on the screen my_view.displayImage(my_image) # wait for 5 seconds, so we can see the changes waitTime(5000)

Hin th hnh nh ln mng hnh. Code hon chnh s l:


from imgproc import * # open the webcam my_camera = Camera(320, 240) # grab an image from the camera my_image = my_camera.grabImage() # open a view, setting the view to the size of the captured image my_view = Viewer(my_image.width, my_image.height, "Basic image processing") # display the image on the screen my_view.displayImage(my_image) # wait for 5 seconds, so we can see the image waitTime(5000)

Lu li thnh mt file c tn Basic.py. Sau chy cc lnh trn bng lnh sau trn terminal:
python basic.py

D liu hnh nh l mt mng hai chiu cc im nh, mt im nh l tuple ca 3 gi tr: lin quan n 3 my c bn l , xanh l v xanh dng. Bn c th truy cp vo cc im nh bng cch x l hnh nh nh mt mng,
# get the value of the pixel at x position 120 and y position 64 in the image pixel = my_image[120, 64] # pixel is now a tuple of the red, green and blue of the requested pixel # An alternative method red, green, blue = my_image[120, 64] # red, green and blue now contain the intensity of the red, green and blue # channels respectively

thit lp mt im nh, bn phi ch nh v tr ca mt b 3 tuple, hay cn thn v bn ch c th thit lp gi t 0 n 255.


# Set the value of pixel at x position 120 and y position 64 # to an orange colour my_image[120, 64] = 255, 128, 0

Gi chng ta s ly mu l mu ch o trong nh v chuyn n sang mu xanh:


# iterate over ever pixel in the image by iterating over each row and each column
13

Raspberry Pi with Camera Tutorial


for x in range(0, my_image.width): for y in range(0, my_image.height): # get the value of the current pixel red, green, blue = my_image[x, y] # check if the red intensity is greater than the green if red > green: # check if red is also more intense than blue if red > blue: # this pixel is predominantly red # let's set it to blue my_image[x, y] = 0, 0, 255

Lu cc on code ny vo file bn trn v chy chng cng lc.


VII. Ti liu tham kho:

http://www.tweaktown.com/guides/5617/raspberry-pi-cameramodule-review-and-tutorial-guide/index5.html https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/image_p rocessing/

14

You might also like