Вы находитесь на странице: 1из 14

Lab 0:

Lm quen cc thao tc c bn v x l
nh s v video s
bng ngn ng C++/C#
Ni dung
[C#] ............................................................................................................................................1
nh .............................................................................................................................................1
Load nh v hin th ................................................................................................................1
Truy xut n cc im nh .....................................................................................................1
Gii phng d liu nh khi khng s dng na ........................................................................3
Lu nh ...................................................................................................................................3
Video ..........................................................................................................................................3
Cng c lp trnh .....................................................................................................................3
Khi to ..................................................................................................................................3
Thit lp thng tin ...................................................................................................................3
Trch frame .............................................................................................................................3
Ghi file video ..........................................................................................................................4
[C++ & OpenCV] .......................................................................................................................5
Phin bn ....................................................................................................................................5
Cu hnh .....................................................................................................................................5
Chng trnh minh ha............................................................................................................5
nh .............................................................................................................................................5
To nh mi ............................................................................................................................5
Load nh .................................................................................................................................6
To ca s hin th nh ............................................................................................................6
Gii phng vng nh ...............................................................................................................6
Truy xut n cc pixel ca nh...............................................................................................6
Cch 1: ................................................................................................................................6

BI TP THC HNH

X L NH S V VIDEO S

Cch 2: ................................................................................................................................7
Lu nh ...................................................................................................................................7
Video ..........................................................................................................................................8
Load d liu video t file hoc t webcam ..............................................................................8
Trch tng khung hnh (frame) v hin th ln mn hnh ..........................................................8
Gii phng vng nh ...............................................................................................................8
Ghi ni dung file video xung file ...........................................................................................8
Cch t chc chng trnh ..........................................................................................................9
Bi tp tho lun trn din n .................................................................................................. 11
Bi tp lp trnh......................................................................................................................... 11
Ti liu tham kho..................................................................................................................... 12

ddkhoa@fit.hcmus.edu.vn

X L NH S V VIDEO S

BI TP THC HNH

[C#]
nh
Tool: C#

Load nh v hin th
Gi s c mt PictureBox t tn pb_img.
Bitmap bm = new Bitmap([ng dn]);
pb_img.Image = bm;

Truy xut n cc im nh
Color c = bm.GetPixel(10, 10);

Mt im nh hin th trn mn hnh thng c 3 thnh phn mu.


for (int i = 0 ; i < bm.Width; i++)
for (int j = 0; j < bm.Height; j++)
{
//Ly tng im nh ti v tr i, j, im (0,0) l gc tri trn ca nh
Color c = bm.GetPixel(i, j);
//Ly
//vd:
int r
int g
int b

tng thnh phn mu v x l,


tng thnh phn mu ln 30 n v
= Math.Min(c.R + 30, 255);
= c.G;
= c.B;

//Cp nht gi tr pixel trong nh


Color c1 = Color.FromArgb(r, g, b);
bm.SetPixel(i, j, c1);
}

Khi chy chng trnh s pht hin mt vn l chng trnh chy kh chm. l do ta lin
tc gi cc phng thc GetPixel v SetPixel truy xut n cc im nh. tng tc x
l, ta dng phng thc Lockbit lu tm d liu nh vo b nh v truy truy xut trc tip
n vng nh ny. Xem thm trong [1].
using System.Drawing.Imaging;

public BitmapData LockBits(


Rectangle rect,
ImageLockMode flags,
PixelFormat format
VD:
BitmapData bmd = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite, bm.PixelFormat);

ddkhoa@fit.hcmus.edu.vn

BI TP THC HNH

X L NH S V VIDEO S

Lp BitmapData c cc thnh phn quan trng cn lu nh sau:


Scan0 Con tr lu a ch vng nh cha d liu nh.
Stride Kch thc ca 1 dng d liu nh (tnh bng byte), lun chia ht cho 4, nu thiu th
chng trnh s thm vi byte trng vo cui mi dng (Padding).
PixelFormat nh dng im nh:
o Format32BppArgb: Mi im nh chim 4 byte gm Red-Green-Blue-Alpha. Truy xut d
liu ca im nh ton (x,y) theo cng thc:
Scan0+(y * stride)+(x*4)
o Format24BppRgb: Mi im nh chim 3 byte gm Red-Green-Blue
Truy xut d liu ca im nh ton (x,y) theo cng thc:
Scan0+(y*Stride)+(x*3)
o Format8BppIndexed: Mi im nh biu din bi 1 byte tng ng vi v tr trong bng
mu.
Truy xut d liu ca im nh ton (x,y) theo cng thc:
Scan0+(y*Stride)+x
o Cc nh dng Format4BppIndexed, Format1BppIndexed sinh vin t tm hiu thm.
Width Chiu rng ca vng nh c lock.
Height Chiu di ca vng nh c lock.

V d:
BitmapData bmd = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite, bm.PixelFormat);
int iPixelSize=3;
switch (bm.PixelFormat)
{
case PixelFormat.Format32bppArgb: iPixelSize = 4; break;
case PixelFormat.Format24bppRgb: iPixelSize = 3; break;
case PixelFormat.Format8bppIndexed: iPixelSize = 1; break;

ddkhoa@fit.hcmus.edu.vn

X L NH S V VIDEO S

BI TP THC HNH
}

unsafe //Nhp phi vo tn Project -> Property -> Unsafe code


{
for (int y = 0; y < bmd.Height; y++)
{
byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);
for (int x = 0; x < bmd.Width; x++)
{
row[x * iPixelSize] = 255;
}
}
}
bm.UnlockBits(bmd);

Gii phng d liu nh khi khng s dng na


Gi phng thc bm.Dispose(); nhanh chng gii phng vng nh khng s dng na.

Lu nh
S dng phng thc bm.Save([ng

dn]);

lu nh.

Video
Cng c lp trnh
Emgu 2.2. Xem hng dn v ci t v cu hnh trong file hng dn trn din n.

Khi to
Thm vo cc th vin cn thit
using
using
using
using
using

Emgu.CV;
Emgu.Util;
Emgu.CV.UI;
Emgu.CV.CvEnum;
Emgu.CV.Structure;

Load file video


Image<Bgr, Byte> img;
Capture c = new Emgu.CV.Capture("D:\\test.avi");

Thit lp thng tin


Mun ly mt s thng tin v file video, dng phng thc
SetCaptureProperty, v d mun ly s lng frame trong file video

GetCaptureProperty

int numFrame = (int)c.GetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_COUNT);

Trch frame
Cch 1:
CvInvoke.cvNamedWindow("1");
for (int i = 0; i < numFrame; i += 10)

ddkhoa@fit.hcmus.edu.vn

BI TP THC HNH

X L NH S V VIDEO S

{
//Di chuyn ti frame th i
c.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_POS_FRAMES, i);
img = c.QueryFrame();
//Show the image
CvInvoke.cvShowImage("1", img.Ptr);
//Wait for the key pressing event
CvInvoke.cvWaitKey(40);
}
c.Dispose();
CvInvoke.cvDestroyWindow("1");

Cch 2: Gi hm ca OpenCV thng qua phng thc CvInvoke


public static void GramFrame2()
{
IntPtr c = CvInvoke.cvCreateFileCapture("D:\\test.avi");
IntPtr Frame = new IntPtr();
int FrmNum = 0;
while ((Frame = CvInvoke.cvQueryFrame(c)) != (IntPtr)null)
{
FrmNum++;
if (FrmNum == 1)
{
Size size = CvInvoke.cvGetSize(Frame);
}
else
{
CvInvoke.cvNamedWindow("1");
CvInvoke.cvShowImage("1", Frame);
CvInvoke.cvWaitKey(40);
}
}
CvInvoke.cvReleaseCapture(ref c);
CvInvoke.cvReleaseCapture(ref Frame);
}

Ghi file video


public static void WriteVideo()
{
Image<Bgr, byte> src;
Capture c = new Emgu.CV.Capture("test.avi");
int numFrame = (int)c.GetCaptureProperty(CAP_PROP.CV_CAP_PROP_FRAME_COUNT);
VideoWriter vw = new VideoWriter("out.avi", 30, c.Width, c.Height, true);
for (int i = 0; i < numFrame; i += 10)
{
c.SetCaptureProperty(CAP_PROP.CV_CAP_PROP_POS_FRAMES, i);
src = c.QueryFrame();
vw.WriteFrame(src);
}
src.Dispose();
vw.Dispose();
c.Dispose();
}

ddkhoa@fit.hcmus.edu.vn

X L NH S V VIDEO S

BI TP THC HNH

[C++ & OpenCV]


Phin bn
S dng phin bn OpenCV 2.2.

Cu hnh
Xem trong file hng dn cu hnh trn din n.

Chng trnh minh ha


G th on chng trnh sau v xem kt qu
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
void main()
{
IplImage *img = cvLoadImage([ng dn n file nh]);
cvNamedWindow("MyImageWindow",1);
cvShowImage("MyImageWindow",img);
cvWaitKey();
cvDestroyWindow("MyImageWindow");
cvReleaseImage(&img);
}

nh
To nh mi
//To nh c kch thc nh nh,
//mi pixel biu din bng 1 byte c gi tr unsigned int
//ch c 1 knh mu
IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
//To nh c 3 knh (thnh phn) mu: Blue-Green-Red
//Gi tr ca mi thnh phn mu ti mi pixel l s thc
IplImage* img2=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
//Sao chp nh ban u sang nh mi
IplImage* img3;
img3=cvCloneImage(img1);

P0

P1

P2

...

nh grayscale

padding
B0
B2

G0
G2

R0
R2

B1
...

G1

R1

nh mu c s dng

ddkhoa@fit.hcmus.edu.vn

X L NH S V VIDEO S

BI TP THC HNH

padding mode (c th c hoc khng)

Load nh
IplImage *img = cvLoadImage([ng dn n file nh]);

To ca s hin th nh
cvNamedWindow("MyImageWindow",1);
cvShowImage("MyImageWindow",img);

Gii phng vng nh


Sau khi ngi dng bm phm bt k tt ca s nh, nu khng s dng na th ta gii phng
vng nh.
cvDestroyWindow("MyImageWindow"); //Gii phng vng nh ca ca s nh
cvReleaseImage(&img); // Gii phng vng nh cp cho nh

Truy xut n cc pixel ca nh


Gi s ta cn truy xut n knh (thnh phn) mu th k ca pixel dng i v ct j trong nh.
Cch 1:
D hiu, nhng chm do khng truy xut trc tip m thng qua cc hm x l.
//i vi nh n sc (ch c 1 knh mu)
//v gi tr mu ca mi pixel l s nguyn: img=cvCreateImage(...,IPL_DEPTH_8U,1)
CvScalar s;
// Ly gi tr im nh ta (i,j)
s=cvGet2D(img,i,j);
printf("Gi tr mu ca im nh ny = %d",s.val[0]);
//Gn gi tr im nh ti ta (i,j)
s.val[0]=111; //thay i gi tr im nh
cvSet2D(img,i,j,s);
//i vi nh mu (c 3 knh mu)
//v gi tr mu ca mi thnh phn mu ti mi im nh l s thc
//img=cvCreateImage(...,IPL_DEPTH_32F,3)
//Mi im nh biu din bi 3 byte tng ng vi 3 thnh phn mu Blue-Green-Red ca
im nh
//Cc thnh phn ca cc im nh c lu theo th t nh sau: B1 G1 R1 B2 G2 R2 ...
CvScalar s;
// Ly gi tr im nh ta (i,j)
s=cvGet2D(img,i,j);
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
// Gn gi tr cho im nh ti ta (i,j)
s.val[0]=111; // gi tr mu cho thnh phn Blue
s.val[1]=111; // gi tr mu cho thnh phn Green

ddkhoa@fit.hcmus.edu.vn

BI TP THC HNH

X L NH S V VIDEO S

s.val[2]=111; // gi tr mu cho thnh phn Red


cvSet2D(img,i,j,s);

Cch 2:
Phc tp hn, nhng nhanh hn.
//i vi nh n sc (ch c 1 knh mu)
//v gi tr mu ca mi pixel l s nguyn: img=cvCreateImage(...,IPL_DEPTH_8U,1)
int height
= img->height;
int width
= img->width;
//Tnh s pixel trn mi dng,
//bi v mt s nh s dng padding mode nn step c th khc width
int step
= img->widthStep/sizeof(uchar);
//Ly a ch ca vng nh cha d liu nh
uchar* data
= (uchar *)img->imageData;
//Thay i gi tr im nh ti ta (i,j)
data[i*step+j] = 111;
//i vi nh mu (c 3 knh mu)
//v gi tr mu ca mi knh ti mi im nh l s nguyn:
//img = cvCreateImage(cvSize(...),IPL_DEPTH_8U,3)
int height
= img->height;
int width
= img->width;
//Tnh s pixel trn mi dng,
//bi v mt s nh s dng padding mode nn step c th khc width
int step
= img->widthStep/sizeof(uchar);
//S lng knh (thnh phn) mu
int channels
= img->nChannels;
uchar* data
= (uchar *)img->imageData;
//Thay i gi tr thnh phn mu th k (k=0 l blue, k=1 l green, k=2 l red)
// ca im nh ti ton (i,j)
data[i*step+j*channels+k] = 111;
//i vi nh mu (c 3 knh mu)
//v gi tr mu ca mi knh ti mi im nh l s thc
//img=cvCreateImage(...,IPL_DEPTH_32F,3)
int height
= img->height;
int width
= img->width;
//Tnh s pixel trn mi dng,
//bi v mt s nh s dng padding mode nn step c th khc width
int step
= img->widthStep/sizeof(float);
int channels
= img->nChannels;
//Ch , con tr s thc
float * data
= (float *)img->imageData;
data[i*step+j*channels+k] = 111;

Ngoi ra cn c cch truy xut khc hiu qu hn, sinh vin t tham kho trong ti liu ...

Lu nh
cvSaveImage(C:\\Test.jpg,pImg);

ddkhoa@fit.hcmus.edu.vn

X L NH S V VIDEO S

BI TP THC HNH

Video
Load d liu video t file hoc t webcam
CvCapture *cap = cvCaptureFromCAM(0); //t webcam
CvCapture *cap = cvCaptureFromAVI([ng dn n file video]);

Trch tng khung hnh (frame) v hin th ln mn hnh


IplImage *img = 0;
cvNamedWindow("Image:",1);
while (1)
{
/*
CCH 1
if (!cvGrabFrame(cap))
{
printf("cannot grab\n");
exit(0);
}
img = cvRetrieveFrame(cap);
*/
//
CCH 2
img = cvQueryFrame(cap); //Bin img s c ti s dng trch frame khc
//nn cha release trong y c.
if (!img)
{
printf("cannot grab\n");
exit(0);
}
// Gi hm x l g y, vd: kh nhiu, chuyn thnh nh trng en...
// Nn copy nh gc sang mt nh khc ri mi x l.
// Sau hin th frame x l ln mn hnh
// 1 giy 24 khung hnh ==> 1 khung hnh ~ 40 mili giy
cvShowImage("Image:",img);
cvWaitKey(40); //Th b dng ny coi chuyn g xy ra!!
}

Gii phng vng nh


Khi no khng s dng na th nh gii phng vng nh
cvDestroyWindow("Image:");
cvReleaseImage(&img);
cvReleaseCapture(&cap);

Ghi ni dung file video xung file


//M file
CvCapture
//s dng
IplImage*

cn x l ra
*input = cvCaptureFromAVI("E:\\Entertainment\\Clips\\test.avi");
1 bin ly thng tin v kch thc ca mi frame
image = cvRetrieveFrame(input);

if (!image) {

ddkhoa@fit.hcmus.edu.vn

BI TP THC HNH

X L NH S V VIDEO S

printf("Unable to read input");


return 0;
}
//Ly kch thc ca frame
CvSize imgSize;
imgSize.width = image->width;
imgSize.height = image->height;
//Thy thng tin v s lng khung hnh/giy (frame per second)
double fps = cvGetCaptureProperty(
input,
CV_CAP_PROP_FPS
);
//Khai bo 1 bin ghi xung file video c nh dng tng t file gc
//s dng b m ha motion-jpeg nn cc frame
CvVideoWriter *writer = cvCreateVideoWriter(
"E:\\Entertainment\\Clips\\out.avi",
CV_FOURCC('M', 'J', 'P', 'G'),
fps,
imgSize
);
IplImage* colourImage;
for (;;)
{
//Trch tng khung hnh ra
colourImage = cvQueryFrame(input);
if (!colourImage)
{
printf("cannot grab\n");
break;
}
//Gi hm x l cho colourImage y
//Nn copy sang mt bin khc x l
//Sau ghi vo file
cvWriteFrame(writer, colourImage);
}

Cch t chc chng trnh


Chng trnh phi c t chc thnh cc lp i tng. Di y l mt v d mu vit bng
C#.
public class ImageShowing
{
public Bitmap myBitmap;
// Cc thuc tnh khc (nu c)
public ImageShowing()
{//Dng khi to gi tr ban u;
myBitmap = null;
}
public ImageShowing(Bitmap bm)
{//Hm dng sao chp
Bitmap myBitmap = bm;

ddkhoa@fit.hcmus.edu.vn

BI TP THC HNH

X L NH S V VIDEO S

}
//Cc hm dng khc
//Cc phng thc truy cp n cc thuc tnh
public Bitmap MyBitmap
{
get { return myBitmap; }
set
{
if (value != null)
myBitmap = value;
}
}
//Cc hm x l nh
public int LoadBitmap(string strFileName)
{
if (strFileName.CompareTo("") != 0)
{
myBitmap = new Bitmap(strFileName);
return 1;
}
return 0;
}
//V cc hm bin i nh khc (nu c)
~ImageShowing()
{//T gii phng vng nh khi ko xi na
if (myBitmap != null)
myBitmap.Dispose();
}
}
public partial class Image_Processing : Form
{
ImageShowing imShow;
public Image_Processing()
{
InitializeComponent();
imShow = new ImageShowing();
}
private void bt_LoadImg_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "..";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (imShow.LoadBitmap(openFileDialog1.FileName) != 0)
MessageBox.Show("ng dn sai");
}
}
...
}

10

ddkhoa@fit.hcmus.edu.vn

BI TP THC HNH

X L NH S V VIDEO S

Bi tp tho lun trn din n


1. Trong phn X l video bng OpenCV c 2 cch trch khung hnh. Tm hiu v tho lun
xem 2 cch ny khc nhau ch no?
2. Dng hm cvCreateImage, ln lt to ra cc nh c kch thc bng nhau theo nh dng:
IPL_DEPTH_8U (unsigned byte)
IPL_DEPTH_8S (byte)
IPL_DEPTH_16U
IPL_DEPTH_16S
IPL_DEPTH_32S
IPL_DEPTH_32F (float)
IPL_DEPTH_64F
Kho st gi tr width v step ca mi loi.

Bi tp lp trnh
Sinh vin c php s dng cc hm chuyn i c sn.
1. Vit chng trnh hin th nh c kch thc bt k. Thay i kch thc nh sao cho hin th
va trong vng c kch thc 200x200.
2. Vit chng trnh ti ln 1 file avi mu, chuyn frame thnh nh xm v hin th ra mn hnh.
Cc file avi c th download ti a ch http://www.jhepple.com/support/sample_movies1.htm

11

ddkhoa@fit.hcmus.edu.vn

BI TP THC HNH

X L NH S V VIDEO S

Ti liu tham kho


[1] Bob Powell, Cch LockBits truy xut d liu nh trong C#,
http://www.bobpowell.net/lockingbits.htm, 2011.
[2] Microsoft, Microsoft Developer Network, http://msdn.microsoft.com, 2011.
[3] Din n lp 51TH1, trng H Nha Trang, Setup v config OpenCV 2.2,
http://51th1.com/read.php?5, 2011.
[4] Gady Agam, Introduction to programming with OpenCV: Accessing image elements,
http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html, 2006
[5] Weis, OpenCV: capture the frame, http://blog.weisu.org/2007/12/opencv-capture-frame.html,
2007.
[6] Stackoverflow,
Writing
avi
files
in
OpenCV,
http://stackoverflow.com/questions/368967/writing-avi-files-in-opencv, 2011.
[7] Saida Bouakaz, GUI and Video Acquisition Reference, http://www710.univlyon1.fr/~bouakaz/OpenCV-0.9.5/docs/ref/OpenCVRef_Highgui.htm#decl_cvWriteToAVI,
?.
[8] Leeds
Guide
to
OpenCV
,
Saving
Movies,
http://www.comp.leeds.ac.uk/vision/opencv/movies.html, 2006.
[9] Robert Laganire, Programming computer vision applications: A step by step guide to the
use
of
Microsoft
Visual
C++
and the
Intel OpenCV library,
http://www.site.uottawa.ca/~laganier/tutorial/opencv+directshow/cvision.htm, 2009.
[10] EmguCV, EmguCV Tutorial, http://www.emgu.com/wiki/index.php/Tutorial, 2010.
[11] Programmers
united
develop
net,
Capture
video
using
Emgu,
http://read.pudn.com/downloads190/sourcecode/graph/texture_mapping/892527/Capture.cs
__.htm, 2011.

12

ddkhoa@fit.hcmus.edu.vn

Вам также может понравиться