OpenCV, MATLAB or AForge ??
OpenCV, MATLAB or AForge ??
Image processing is the process of manipulating image data in order to make it suitable for Image processing is the process of manipulating image data in order to make it suitable for computer vision applications or to make it suitable to present it to humans. For example, computer vision applications or to make it suitable to present it to humans. For example, changing brightness or contrast
changing brightness or contrast is a image processing tais a image processing task which make the imsk which make the image visuallyage visually ple
pleasiasing ng for for humhumans ans or or suisuitabtable le for for furfurthether r proprocescessinsing g for for a a cercertaitain n comcomputputer er visvisionion application.
application.
Computer vision which go beyond image processing, helps to obtain relevant information Computer vision which go beyond image processing, helps to obtain relevant information from images and make decisions based
from images and make decisions based on that information. In other words, on that information. In other words, computer visioncomputer vision is making the computer see as humans do. Basic steps for a typical computer vision is making the computer see as humans do. Basic steps for a typical computer vision application as follows. application as follows. 1 1.. IImmaagge e aaccuuiissiittiioonn ! !.. IImmaagge e mmaanniippuullaattiioonn " ".. ##bbttaaiinniinng rg reelleevvaannt it innffoorrmmaattiioonn $ $.. %%eecciissiioon n mmaakkiinngg
If you are new to computer vision, you may be wondering where to start. First If you are new to computer vision, you may be wondering where to start. First you have to understand the basic principles of image processing and computer you have to understand the basic principles of image processing and computer visio
vision. Then n. Then you have you have to choose a to choose a suitasuitable language to ble language to develdevelop your op your compucomputerter visio
vision n appliapplicatiocation. n. Some of Some of the most the most populpopular methods are ar methods are usingusingOpenCV withOpenCV with C/C++
C/C++,, MATLABMATLAB and and AForgeAForge. If you don't really know why you would choose. If you don't really know why you would choose one over the other,
one over the other, here is my explanation.here is my explanation.
!T"!# is the most easiest and the ine$cient way to process images and !T"!# is the most easiest and the ine$cient way to process images and %pen& is the most e$cient and hardest way to process images. !Forge has %pen& is the most e$cient and hardest way to process images. !Forge has (ualities in between %pen&
(ualities in between %pen& and !Tand !T"!#."!#.
%pen& has become hardest only because there is no proper documentation %pen& has become hardest only because there is no proper documentation and error ha
and error handling codes. ndling codes. #ut %pen& has lots of basic in#ut %pen& has lots of basic inbuilt image prbuilt image processingocessing functions so that those who want to learn computer vision can develop their functions so that those who want to learn computer vision can develop their applications through prop
applications through proper understanding about what they er understanding about what they do.do.
So, I think that it is worthy to learn computer vision with %pen&. Therefore in So, I think that it is worthy to learn computer vision with %pen&. Therefore in this blog, it is presented basic image processing functions and computer vision this blog, it is presented basic image processing functions and computer vision applications with line by line
Read & Display Image
Read & Display Image
Read Image from File and Displa
Read Image from File and Displa
)ere I am going to explain how to read an image from a *le and display the )ere I am going to explain how to read an image from a *le and display the content using %pen& library functions. First of all, open your &++ I- and content using %pen& library functions. First of all, open your &++ I- and crea
create a te a new pronew proect. /ect. /ou have to con*gurou have to con*gure your new proect e your new proect in order to usein order to use %pen& library functions. If you have not con*gured the proect for %pen& yet, %pen& library functions. If you have not con*gured the proect for %pen& yet, please refer to
please refer to Installing 0 &on*guring with isual Studio.Installing 0 &on*guring with isual Studio.
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111
11111111111111111111111 !in"l#de
!in"l#de $open"%&/highg#i/highg#i'hpp$$open"%&/highg#i/highg#i'hpp$ !in"l#de
!in"l#de (iostream)(iostream)
using namespace
using namespacec!c! using namespace
using namespaces"d!s"d! in"
in" main#main# in" in" argcargc, cons" c$ar%%, cons" c$ar%% arg arg '
'
Ma"
Ma" img img ( ( imread#imread#)My*ic+*-))My*ic+*-), CV.LOAD.IMA-/.01C2A1-/D!, CV.LOAD.IMA-/.01C2A1-/D! 33read "$e 33read "$e image da"a in "$e 4ile )My*ic+*-) and s"ore
image da"a in "$e 4ile )My*ic+*-) and s"ore i" in 5img5i" in 5img5 i4
i4 #img+emp"y# #img+emp"y# 33c$ec6 7$e"$er "$e image is 33c$ec6 7$e"$er "$e image is loaded or no"loaded or no" '
'
cou"
cou" 8888 )/rror 9 Image canno" :e loaded++;;))/rror 9 Image canno" :e loaded++;;) 88 endl! 88 endl! //
//sys"em#sys"em#)pause))pause)!! 337ai" 4or a 6ey press 337ai" 4or a 6ey press re"urn
re"urn <=!<=! >
>
namedindo7#
namedindo7#)Myindo7))Myindo7), CV.I1DO.A0TO@I/!, CV.I1DO.A0TO@I/! 33crea"e a 7indo7 7i"$ "$ 33crea"e a 7indo7 7i"$ "$ee name )Myindo7)
name )Myindo7) ims$o7#
ims$o7#)Myindo7))Myindo7), img!, img! 33display "$e image 7$ 33display "$e image 7$ic$ is s"ored in "$e 5imic$ is s"ored in "$e 5img5 in "$eg5 in "$e )Myindo7) 7indo7
)Myindo7) 7indo7 7ai"ey#!
7ai"ey#! 337ai" in4ini"e "ime 4or a 6eypress 337ai" in4ini"e "ime 4or a 6eypress des"royindo7#
des"royindo7#)Myindo7))Myindo7)!! 33des"roy "$e 7indo7 7i"$ 33des"roy "$e 7indo7 7i"$ "$e name, )Myindo7)"$e name, )Myindo7) re"urn re"urn !! > > 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1111111111111111111111 1111111111111111111111
#efore you run this program, put any image *le
#efore you run this program, put any image *le 2y3ic.4356 into the folder where2y3ic.4356 into the folder where your c++ *le is.%therwise you have
your c++ *le is.%therwise you have to change the to change the *rst argument of imread26*rst argument of imread26 function and give the absolute path to your image *le.
function and give the absolute path to your image *le. /
/ou can download this %pen&ou can download this %pen& visual c++ pro visual c++ proect fromect from herehere. 2The downloaded. 2The downloaded *le is a compressed .rar folder
*le is a compressed .rar folder. So, you . So, you have to extract it usinghave to extract it using 7inrar7inrar or other or other suitable software6
suitable software6
Load & Diplay Image from File Load & Diplay Image from File *planation
*planation
"et's review the above %pen& code line by "et's review the above %pen& code line by lineline..
•
• !in"l#de!in"l#de $stdaf'h$$stdaf'h$
If you are developing your application in isual Studio, don't forget to put this If you are developing your application in isual Studio, don't forget to put this line above the code.
line above the code.
•
• !in"l#de!in"l#de $open"%&/highg#i/highg#i'hpp$$open"%&/highg#i/highg#i'hpp$
imread#
imread#,, namedindo7#namedindo7#,, ims$o7#ims$o7# andand 7ai"ey#7ai"ey# functions are declared in the abovefunctions are declared in the above header file. &o you must include it.
header file. &o you must include it. '
'e e aarre e uussiinnggMa"Ma" ddaatta a ssttrruuccttuurre e iin n tthhe e aabboovve e pprrooggrraamm. . IIt t iis s ddeeccllaarreedd in
because $open"%&/highg#i/highg#i'hpp$ header *le include that header *le inside it. So, we don't need to include it again in our program.
• using namespace c!
(ll the data structures and functions
in )openc3core3core+$pp) and )openc3$ig$gui3$ig$gui+$pp) are declared inside c namespace. &o, we have to add the above line in the top of our program. #therwise we have to append )c995 specifier before each #penC* functions and data structures. +e.g cv--at, cv--imread+/ , etc/. If you are not familiar with namespaces, please refer this article.
,!in"l#de (iostream), and )using namespace s"d5 are added because we are using )cou"5 to display some strings in the console. 0his is basic C and you should be familiar with this.
• Ma" img ( imread#const string0 -lename,
int .ags9C*23#(%2I(452C#3#6
Mat is a data structure to store images in a matrix. It is declared in )openc3core3core+$pp) header file.
imread0 is a function declared in $open"%&/highg#i/highg#i'hpp$ header *le. It loads an image from a *le and stores it in at data structure.
(rguments of imread+/ function
• -lename location of the file. If you 7ust give the filename only, that image should
be in the same folder as your C file. #therwise you have to give the full path to your image.
• 4lags 0here are four possible inputs
o &:"%!:I!5-:;<&)!<5- = image=depth9> bits per pixel in
each channel, no. of channels9unchanged
o &:"%!:I!5-:5?!/S&!"- = image depth9> bits, no. of
channels9@
o &:"%!:I!5-:&%"%? = image=depth98, no. of channels9A
o &:"%!:I!5-:!</-3T) = image=depth9unchanged , no. of
channels98
o &:"%!:I!5-:!</&%"%? = image=depth98, no. of
channels9unchanged
/ou can combine these aboveparameters to get desired image output.
e.g
C*23#(%2I(452(89%5:0; < C*23#(%2I(452(89C#3#6 image depth=unchanged, no. of channels=unchanged
C*23#(%2I(452C#3#6 < C*23#(%2I(452(89%5:0; image depth=unchanged, no. of channels="
If you are not sure what to do, use C*23#(%2I(452C#3#6 as the !nd parameter of
imread+/ function.
0o understand imagedepth and concept of channels, you should be familiar with theory of image processing. &o, let)s discuss little bit of theory of image processing.
(ny digital image consists of pixels. (ny pixel should have some value. 0he minimum value for a pixel is > and it represents black.'hen the value of the pixel is increased, the intensity of that pixel is also increased. In a computer memory, /. In decimal, it is !??. fixed number of bits are allocated for every pixel. &ay the number of allocated bits per pixel is @. 0hen the maximum number that a pixel can have is !?? +11111111 in binary/
8ow what is imagedepthA 0he image<dep"$ means the number of bits allocated for each pixel. If it is @, each pixel can have a value between > and !??. If it is $, each pixel can have a value between > to 1? +1111 in binary/.
;ere is a simple model of a image with imagedepth of @ bits. 5ach small box represents a pixel. &o, each box may contain a value bet ween > to !??.
;ere is some properties of the following image.
o Imagedepth @ bit
o 1 channel + &o, this is a grayscale image / o 0he height is $ pixel
o 0he width is ? pixels
o 0he resolution of this image is $x?.
0his is a grayscale image +black and white image/ because this image has no color content. If the value of this pixel is higher, it will be shown more brighter. If the value is low, it will be shown more darker.
Grayscale Image with image depth of 8
planes 6ed, 4reen and Blue. (ny color can be created using a particular combination of these " colors. (ny pixel is a combination of three " values. +!??, >, >/ represent pure red. +>, !??, >/ represent pure green. +!??, >, !??/ represents pure violate. In the same way, you can create many color. Imagedepth is !$ because each pixel is represented with @ x " bits +@ bits from each channel/.
;ere is some properties of the following image.
o Imagedepth !$ bit
o " channels + &o, this is a color image / o 0he height is $ pixel
o 0he width is ? pixels
o 0he resolution of this image is $x?.
R, G, B planes of a color image
In the above model, top left pixel is +!", !"1, $/. It will be shown as a greenish color because the green value+!"1/ of that pixel is larger than the red+!"/ and blue+$/ value.
• i4 #img+emp"y#
If imread+/ function fails to load the image, )img5 will not be loaded any data. 0herefore 5img+emp"y#) should return true. It)s a good practice to check whether the image is loaded successfully and if not exit the program. #therwise your program will crash when executing ims$o7#function.
• :ool Ma"99emp"y#
This function returns true, if atBBdata99<;"" or atBBtotal26 99 C
• //sys"em#)pause)!
If you are using *isual &tudio, it)s better to uncomment this line because it will pause the program until user press any key. If we don)t uncomment it, the program will exit immediately so that user will not see the er ror message.
• oid namedindo7#cons" s"ring& 7inname, in" 4lags ( I1DO.A0TO@I/!
This function creates a window. 3arameters =
o 7inname 0itle of the window. 0hat name will display in the title bar of the
newly created window
o 4lags determine the siDe of the window. 0here are two options
I1DO.A0TO@I/ < Eser cannot resiDe the image. Image will be displayed in its original siDe
CV.I1DO.1ORMAL < Image will resiDed if you resiDe the the window
• oid ims$o7#cons" s"ring& 7inname, Inpu"Array ma"!
This function shows the image which is stored in the 'mat, in a window speci*ed by winname. If the window is created with1I2DO13A4TO5I6* Dag, image will be displayed in its original siEe. %therwise image may be scaled to the siEe of the window.
3arameters =
o 7inname 0itle of the window. 0his name is used to identify the window
created by named'indow+/ function.
o ma" hold the image data
• in" 7ai"ey#in" delay (
waitey26 function wait for keypress for certain time, speci*ed by dela 2in milliseconds6. If dela is Eero or negative, it will wait for in*nite time. If any key is pressed, this function returns the !S&II value of the key and your program will continue. If there is no key press for the specified time, it will return 1 and program will continue.
• oid des"royindo7#cons" s"ring& 7inname
This function closes the opened window, with the title of winname and deallocate any associated memory usage. This function is not essential for this application because when the program exits, operating system usually close all the opened windows and deallocate any associated memory usage.
5#mmar
7hen running this program, the image of 'M7i"'879' is loaded into the variable, 'img, of type at. Then a window named 'M1indow' is opened. !fter that 'img' is loaded to that window. The window with the image will be displayed until any key is pressed.
This program is also very much similar to the previous application. The only diGerence is that this program creates a blank image instead of loading an existing image from a *le.
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111 !in"l#de $open"%&/highg#i/highg#i'hpp$ !in"l#de (iostream) using namespace c! using namespace s"d!
in" main# in" argc, cons" c$ar%% arg '
Ma" img#E, =, CV.0CG, @calar#,, =! 33crea"e an image # G c$annels, :i" image dep"$, E $ig$, = 7ide, #, , = assigned 4or Blue, -reen and Red plane respec"iely+
i4 #img+emp"y# 33c$ec6 7$e"$er "$e image is loaded or no" '
cou" 88 )/rror 9 Image canno" :e loaded++;;) 88 endl!
//sys"em#)pause)! 337ai" 4or a 6ey press re"urn <=!
>
namedindo7#)Myindo7), CV.I1DO.A0TO@I/! 33crea"e a 7indo7 7i"$ "$e name )Myindo7)
ims$o7#)Myindo7), img! 33display "$e image 7$ic$ is s"ored in "$e 5img5 in "$e )Myindo7) 7indo7
7ai"ey#! 337ai" in4ini"e "ime 4or a 6eypress
des"royindo7#)Myindo7)! 33des"roy "$e 7indo7 7i"$ "$e name, )Myindo7) re"urn !
>
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1111111111111111111111
#efore you run this program, put any image *le 2y3ic.4356 into the folder where your c++ *le is.%therwise you have to change the *rst argument of imread26 function and give the absolute path to your image *le.
/ou can download this %pen& visual c++ proect from here. 2The downloaded *le is a compressed .rar folder. So, you have to extract it using 7inrar or other suitable software6
Create & Display Image
2ew OpenCV f#n"tions
• Mat<<Matint rows= int "ols= int tpe= "onst 5"alar; s0>
This is the one of the many constructor available in Mat class. It initialiEe the at obect with the value given by the Scalar obect
3arameters B
o rows = <umber of rows in the H array 2 height of the image in
pixels6
o "ols = <umber of columns in the H array 2 width of the image in
pixels6
o tpe = specify the bit depth, data type and number of channels of
the image. I gave CV3?4C@ and it specify > bit unsigned integers with A channels. )ere are some of possible inputs for this parameter
&:>;&@ = > bit unsigned integers with single channel &:>;&A = > bit unsigned integers with A channels &:JF&@ = J bit Doating point value with @ channels
If you want more details about this, please refer to Data Tpes for Arras in the #asics of %pen& !3I
o s InitialiEe each array element with the value given by s. In the
above application, I gave Scalar2C,C,@CC6. So, it initialiEe my *rst channel 2#lue plane6 with C, Hnd channel 25reen plane6 with C and Ard channel 2?ed 3lane6 with @CC. So, my *nal image is red. /ou can try diGerent combinations of these three and see the output image.
5#mmar
In this program, I created a A channel image with KCC height and @CCC width. > bit unsigned integer is allocated for each pixel in each channel. 2>xA 9 HJ bits per each pixel6 !nd each pixel is assigned with 2C,C,@CC6 scalar value. That means @st channel is all Eero, Hnd channel is also all Eero and the Ard channel is all @CC. Therefore we can see a red image as the output of the program.
Cap"ure Video 4rom File or Camera
Capt#re Video From File
In this lesson, I am going to show you how to read a video *le. It's fairly simple. /ou ust need to initialiEe a ideo&apture obect which will open a video *le and
read frame by frame from that opened video. )ere is the sample %pen& code. If you are using isual Studio, you will need to include Lstdafx.h header file.
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
Hinclude )openc3$ig$gui3$ig$gui+$pp) Hinclude 8ios"ream
using namespace c! using namespace s"d!
in" main#in" argc, c$ar % argJK '
VideoCap"ure cap#)C930sers3@2/RMAL3Des6"op3@ampleVideo+ai)! 33 open "$e ideo 4ile 4or reading
i4 # ;cap+isOpened# 33 i4 no" success, ei" program '
cou" 88 )Canno" open "$e ideo 4ile) 88 endl! re"urn <=!
>
33cap+se"#CV.CA*.*RO*.*[email protected]@/C, G! 33s"ar" "$e ideo a" Gms
dou:le 4ps ( cap+ge"#CV.CA*.*RO*.F*@! 33ge" "$e 4rames per seconds o4 "$e ideo
cou" 88 )Frame per seconds 9 ) 88 4ps 88 endl!
namedindo7#)MyVideo),CV.I1DO.A0TO@I/! 33crea"e a 7indo7 called )MyVideo)
7$ile#= '
Ma" 4rame!
:ool :@uccess ( cap+read#4rame! 33 read a ne7 4rame 4rom ideo i4 #;:@uccess 33i4 no" success, :rea6 loop
'
:rea6! >
ims$o7#)MyVideo), 4rame! 33s$o7 "$e 4rame in )MyVideo) 7indo7
i4 #7ai"ey#G (( 337ai" 4or 5esc5 6ey press 4or G ms+ I4 5esc5 6ey is pressed, :rea6 loop
'
cou" 88 )esc 6ey is pressed :y user) 88 endl! :rea6! > > re"urn ! > 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
/ou can download this %pen& visual c++ proect from here. 2The downloaded *le is a compressed .rar folder. So, you have to extract it using 7inrar or other suitable software6
This is how I pla a %ideo #sing m OpenCV program
2ew OpenCV f#n"tions whi"h are not fo#nd earlier are eplained here
• VideoCap"ure99VideoCap"ure#cons" s"ring& 4ilename
This is one of few constructors available in ideo&apture class. This constructor open the video *le and initialiEes the ideo&apture obect for reading the video stream from the speci*ed *le.
The destructor of this class will deallocated any associated memory with this obect. Therefore you don't need to deallocate memory explicitly in your program.
• :ool VideoCap"ure99IsOpened#
If the previous call to *ideoCapture constructor is successful, this method will return true. #therwise it will return false.
It is essential to check that whether the *ideoCapture initialiDe successfully. If it is
unsuccessful, program should be exited. #therwise when you try to read a frame from the *ideoCapture ob7ect, your program will crash.
• :ool VideoCap"ure99se"#in" propId, dou:le alue
9ou can change some properties of *ideoCapture ob7ect. If it is successful, this method will return true. #therwise it will return false. 9ou should try to change some properties of the video stream in your code. In my code, I have shown you how to change the position of the video, by changing the CV3CA737RO737O53M5*C property.
:arameters
-o in" propID < 0his argument specify the property you are going to change. 0here are many
options for this argument. &ome of them are listed here.
CV3CA737RO737O53M5*C current position of the video in milliseconds CV3CA737RO737O53FRAM*5 current position of the video in frames CV3CA737RO73FRAM*31IDT width of the frame of the video stream CV3CA737RO73FRAM*3*I9T height of the frame of the video stream CV3CA737RO73F75 frame rate 2frames per second6
CV3CA737RO73FO4RCC = four character code of codec
o dou:le alue < 0his is the new value you are going to assign to the property, specified by
the propI%
• dou:leVideoCap"ure99ge"#in" propId
0his function returns the value of the property which is specified by propId+ 9ou should try to obtain some properties of the video stream in your code. In my code, I have shown you how to obtain the frame rate +frames per second/ of the video, by using
the CV.CA*.*RO*.F*@ argument.
:arameters
o in" propID < 0his argument specify the property you are going to obtain. 0here are many
options for this argument. &ome of them are listed here.
CV3CA737RO737O53M5*C current position of the video in milliseconds CV3CA737RO737O53FRAM*5 current position of the video in frames CV3CA737RO73FRAM*31IDT width of the frame of the video stream CV3CA737RO73FRAM*3*I9T height of the frame of the video stream CV3CA737RO73F75 frame rate +frames per second/
CV3CA737RO73FO4RCC = four character code of codec
• :ool VideoCap"ure99read#Ma"& image!
The function grabs the next frame from the video, decodes it and stores it in the 'image, variable. Inside this function, ideo&aptureBBgrap26 and ideo&aptureBBretrieve26 will be called. If you want, you can use these two functions instead of ideo&aptureBBread26 function.
If the operation successful, it will return true. %therwise it will return false.
• 7ai"ey#@
The function waits for AC milliseconds. If a key was pressed before the speci*ed time, it returns the !S&II value of the pressed key. If that value is HM 2!S&II value of 'esc' key is HM6, the program will execute inside the if block. If no key is pressed during that ACms, the function returns =@ program will continue the while loop.
• VideoCap"ure99NVideoCap"ure#
estructor of ideo&apture obect will destroy any associated memory of that particular obect. This destructor will be called implicitly on exit of the main method of the above program.
5#mmar
!t *rst, this program captures a video from a *le. Then the program enters into a in*nite loop. In that loop, it grabs frames from the captured video se(uentially, decodes it, shows it in a window and waits for @ milliseconds. If the video *le has no more frames or if the user presses the 'esc' key, the program will break the in*nite loop.
2ote<
;sing waiteint0 function is very important because imshowstring;= MAT0 function need time to paint the image in the window and 7ai"ey#in"will give that necessary time.
Capt#re Video From Camera
The main diGerence of the following program from the above program is the argument to the ideo&apture constructor. )ere you ust need to give the index of your camera to the constructor of the ideo&apture obect instead of the *lename in the above program. )ere is the sample %pen& code.
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 !in"l#de $open"%&/highg#i/highg#i'hpp$
!in"l#de (iostream) #sing namespa"e "%> #sing namespa"e std>
int mainint arg"= "harE arg%G0 H
VideoCapt#re "ap0> // open the %ideo "amera no' if "ap'isOpened00 // if not s#""ess= eit program H
"o#t (( $Cannot open the %ideo "am$ (( endl> ret#rn J>
K
do#le d1idth "ap'getCV3CA737RO73FRAM*31IDT0> //get the width of frames of the %ideo
do#le deight "ap'getCV3CA737RO73FRAM*3*I9T0> //get the height of frames of the %ideo
"o#t (( $Frame siNe < $ (( d1idth (( $ $ (( deight (( endl> named1indow$MVideo$=CV31I2DO13A4TO5I6*0> //"reate a window "alled $MVideo$
while J0 H
Mat frame>
ool 5#""ess "ap'readframe0> // read a new frame from %ideo if 5#""ess0 //if not s#""ess= rea: loop
H
"o#t (( $Cannot read a frame from %ideo stream$ (( endl> rea: >
K
imshow$MVideo$= frame0> //show the frame in $MVideo$ window if waite@0 &0 //wait for ,es", :e press for @ms' If ,es", :e is pressed= rea: loop
H
"o#t (( $es" :e is pressed #ser$ (( endl> rea: >
K K
ret#rn > K
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
/ou can download this %pen& visual c++ proect from here. 2The downloaded *le is a compressed .rar folder. So, you have to extract it using 7inrar or other
suitable software6
T$is is $o7 I cap"ure mysel4 7i"$ my 7e:cam using "$e OpenCV program
2ew OpenCV f#n"tions whi"h are not fo#nd earlier are eplained here
• VideoCapt#re<<VideoCapt#reint de%i"e0
0his is one of constructors available in *ideoCapture class. 0his constructor open the camera indexed by the argument of this constructor and initialiDes the *ideoCapture ob7ect for reading the video stream from the specified camera.
)ere the 'C' means the index of the camera to be used. /ou can use @,H,A.. instead of C, if your computer is attached to more than @ camera. The destructor of this class will deallocated any associated memory with this
obect. Therefore you don't need to deallocate memory explicitly in your program.
If the *ideoCapture ob7ect initialiDation unsuccessful, the expression inside the )if )parentheses will evaluate to true and the statements inside the )if) block will be executed. It is a good practice to check this scenario and handle it accordingly because otherwise it may cause the program to crash.
• dou:le did"$ ( cap+ge"#CV.CA*.*RO*.FRAM/.IDT2
0his function obtain the width +in pixels/ of frames of the camera output.
• dou:le d2eig$" ( cap+ge"#CV.CA*.*RO*.FRAM/.2/I-2T
0his function obtain the height +in pixels/ of frames of the camera output.
ri"e Image & Video "o File
In this lesson, I am going to show you how to write images and videos to a *le using %pen&.
If you have not install and con*gure %pen& yet, please refer to Installing 0 &on*guring with isual Studio.
1rite Image to File
In the following example, a yellow image is created and written into a file. 3et)s see how to
do it with #penC*. 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 111111111111111 !in"l#de $open"%&/highg#i/highg#i'hpp$ !in"l#de (iostream) using namespace c! #sing namespa"e std>
in" main# in" argc, cons" c$ar%% arg
H
Mat imgPQ= P= CV3JP4C@= 5"alar=Q= Q00> //"reate an image @ "hannels= JP it image depth= PQ high= P wide= = Q= Q0 assigned for Bl#e= 9reen and Red plane respe"ti%el' 0
i4 #img+emp"y# 33c$ec6 7$e"$er "$e image is loaded or no"
H "o#t (( $*RROR < Image "annot e loaded''$ (( endl>
//sstem$pa#se$0> //wait for a :e press
ret#rn J> K
%e"tor(int) "ompression3params> //%e"tor that stores the "ompression parameters of the image
"ompression3params'p#sh3a":CV3IM1RIT*387*934ALITS0> //spe"if the "ompression te"hni#e
"ompression3params'p#sh3a":U?0> //spe"if the compression #alit
ool 5#""ess imwrite$D</TestImage'pg$= img= "ompression3params0> //write the image to -le
if 5#""ess 0 H
"o#t (( $*RROR < Failed to sa%e the image$ (( endl> //sstem$pa#se$0> //wait for a :e press
K
namedindo7#)Myindo7), CV.I1DO.A0TO@I/! 33crea"e a 7indo7 7i"$ "$e
name )Myindo7)
ims$o7#)Myindo7), img! 33display "$e image 7$ic$ is s"ored in "$e 5img5 in "$e
)Myindo7) 7indo7
7ai"ey#! 337ai" 4or a 6eypress
des"royindo7#)Myindo7)! 33des"roy "$e 7indo7 7i"$ "$e name, )Myindo7) re"urn !
K
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 111111111111111
/ou can download this %pen& visual c++ proect from here. 2The downloaded *le is a compressed .rar folder. So, you have to extract it using 7inrar or other suitable software6
The above program is very much similar to the program under '&reate a #lank Image 0 isplay' section in the lesson of ?ead 0 isplay Image. If you need further clari*cations of any %pen& functions which are not explained here, please refer to 6ead G %isplay Image lesson.
2ew OpenCV f#n"tions whi"h are not fo#nd earlier
• :ool im7ri"e# cons" s"ring& 4ilename, Inpu"Array img, cons" ec"or8in"&
params(ec"or8in"#
The function saves the image in the variable 'img, to a *le, speci*ed by ,-lename, . If this function fails to save the image, it will return false. %n success of writing the *le to the harddisk, it will return true. 3arameters =
o 4ilename < specify the location and name of the file to be saved o img < hold the image which is going to save
o params < 0his is a int vector to which you have to insert some int parameters
specifying the format of the image
H:54 format 9ou have to
puch2backCV3IM1RIT*387*934ALITS *rst and then a number between C and @CC 2higher is the better6. If you want the best (uality output, use @CC. I have used N> in the above sample program. #ut higher the value, it will take longer time to write the image
:84 format 9ou have to
puch2backCV3IM1RIT*37293COM7R*55IO2 first and then a number between > and +higher is the better compression, but slower/.
The image format is chosen depending on the *le name extension. %nly images with > bit or @ bit unsigned single channel or A channel+ C*2@EC1, C*2@EC", C*2@&C1, C*2@&C",&:@;&@, &:@;&A6 with 'B9R' channel order, can be saved. If the depth or channel order of the image is diGerent, use ,Mat<<"on%ertTo0, or ,"%tColor, functions to convert the image to
supporting format before using imwritefunction.
The above program is very much similar to the program under '&reate a #lank Image 0 isplay' section in the lesson of ?ead 0 isplay Image. If you need further clari*cations of any %pen& functions which are not explained here,
please refer to6ead G %isplay Image lesson.
@ummary
0his program creates an yellowish image 2 A channels, @ bit image depth, KC high, CC wide, 2C, KC,CCC, KC,CCC6 assigned for #5? channels6. #ecause the image has @ bit depth, you can use values between C and KKAK for each element in each channel. I have used KC,CCC for each element in the green and red planes which gives the yellowish color. /ou can try diGerent values. Then it speci*es the compressing techni(ue. I have used 43-5 as the
compression techni(ue in the above example. Then it saves the image in the %-J0estImage.7pg location. 0hen it displays the newly created image in a window and wait indefinitely until an user presses a key.
1rite Video to File
In the following example, a video is captured from the webcam and written into a *le. "et's see how to do it with %pen&.
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1111111
!in"l#de $open"%&/highg#i/highg#i'hpp$ !in"l#de (iostream)
#sing namespa"e std>
int mainint arg"= "harE arg%G0 H
VideoCapt#re "ap0> // open the %ideo "amera no' if "ap'isOpened00 // if not s#""ess= eit program H
"o#t (( $*RROR< Cannot open the %ideo -le$ (( endl> ret#rn J>
K
named1indow$MVideo$=CV31I2DO13A4TO5I6*0> //"reate a window "alled $MVideo$
do#le d1idth "ap'getCV3CA737RO73FRAM*31IDT0> //get the width of frames of the %ideo
do#le deight "ap'getCV3CA737RO73FRAM*3*I9T0> //get the height of frames of the %ideo
"o#t (( $Frame 5iNe $ (( d1idth (( $$ (( deight (( endl>
5iNe frame5iNestati"3"ast(int)d1idth0= stati"3"ast(int)deight00> Video1riter oVideo1riter $D</MVideo'a%i$ = CV3FO4RCC,7,=,I,=,M,=,J,0= &= frame5iNe= tr#e0> //initialiNe the Video1riter oe"t
if oVideo1riter'isOpened0 0 //if not ini"ialie the Video1riter s#""essf#ll= eit the program
H
"o#t (( $*RROR< Failed to write the %ideo$ (( endl> ret#rn J>
K
while J0 H
Mat frame>
ool 5#""ess "ap'readframe0> // read a new frame from %ideo if 5#""ess0 //if not s#""ess= rea: loop
H
"o#t (( $*RROR< Cannot read a frame from %ideo -le$ (( endl> rea: >
K
imshow$MVideo$= frame0> //show the frame in $MVideo$ window if waiteJ0 &0 //wait for ,es", :e press for @ms' If ,es", :e is pressed= rea: loop
H
"o#t (( $es" :e is pressed #ser$ (( endl> rea: > K K ret#rn > K 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1111111
/ou can download this %pen& visual c++ proect from here. 2The downloaded *le is a compressed .rar folder. So, you have to extract it using 7inrar or other suitable software6
ere is how I re"ord m %ideo from m we"am and sa%e to a -le in D dri%e
2ew OpenCV f#n"tions whi"h are not fo#nd earlier are eplained here
• 5iNe
&reate a 5iNe obect with a given width and height. )ere I have cast the width and height to integers because they are originally double values and the 5iNe constructor does not accept double values as its parameters.
• Video1riter<<Video1riter "onst string;
-lename= int fo#r""= do#le fps= 5iNe frame5iNe= ool isColortr#e0
This is the constructor of the ideo7riter class. It initialiEes the obect with following parameters
o "onst string; -lename = Specify the name and the location of the
output *le. The video stream is written into this *le
o int fo#r"" = specify the J character code for the codec which is
used to compress the video. /our computer may not be supported some codecs. So, if you fail to save the video, please try other codecs. )ere are some popular codecs.
&:F%;?&&2'', 'I', '', 'A'6 for ivO 3-5=J codec &:F%;?&&2'', '3', 'J', 'H'6 for 3-5=J codec &:F%;?&&2'', 'I', '', 'O'6 for ivO codec &:F%;?&&2'3','I','','@'6 for 3-5=@ codec &:F%;?&&2'I', 'H', '', 'A'6 for IT; ).HA codec &:F%;?&&2'', '3', '-', '5'6 for 3-5=@ codec
&omplete list of codec can be found here
)ere I have used CV3FO4RCC,7,= ,I,= ,M,= ,J,0 as the four character code of codec to compress the video. If the output *le cannot be opened, you can try diGerent four character code of codecs.
For 1indows #sers, it is possible to use =@ instead of the above codecs in order to choose compression method and additional compression parameters from a dialog box. It is a best method for icrosoft 7indows users.
o do#le fps frames per seconds of the video stream. I have used
HC. /ou can try diGerent values. #ut the codec should support the fps value. So, use an appropriate value.
o 5iNe frame5iNe = SiEe obect which specify the width and the
height of each frame of the video stream.
o ool isColor = If you want to save a color video, pass the value as
true. %therwise false. ?emember codec should support whatever value, you pass. In the above example, you have to give true as the Kth argument. %therwise it will not work.
• if oVideo1riter'isOpened0 0
&heck whether the ideo7riter obect initialiEe successfully. If not exit the program immediately.
7rite a frame to the video stream. The siEe of the frame should be same as the siEe you speci*ed when initialiEing the ideo7riter obect.
!ll other %pen& functions have been discussed in earlier lessons. So, if you are not familiar with those %pen& functions yet, please go through Capt#re Video from File or Camera
Fil"ering Images
Image *ltering is an important part of computer vision. For most of computer vision applications, *ltering should be done before anything else. %pen& supports lots of in=build *ltering methods for images. )ere is the list of *ltering methods that I am going to discuss with you in the following posts 2with %pen& H.J.K and &++ 6
•
Change Brightness of Image or Video
•Change Contrast of Image or Video
•
istogram *#aliNation of 9ras"ale or
Color Image
•
5mooth / Bl#r Images
)ere is the list of image *ltering methods which are explained using examples with %pen& H.@ in & style 2not &++6
• -roding
• ilating
• Inverting
Original Image
If you have not install and con*gure %pen& yet, please refer to Installing 0 &on*guring with isual Studio.
*roding
-roding is a simple way of *ltering images. )ere is how it can be done with %pen&. 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 !in"l#de $stdaf'h$ !in"l#de ("%'h) !in"l#de (highg#i'h) int main0 H
//displa the original image
IplImageE img "%LoadImage$C</M7i"'pg$0> "%2amed1indow$M1indow$0>
"%5howImage$M1indow$= img0>
//erode and displa the eroded image
"%*rodeimg= img= = &0>
"%2amed1indow$*roded$0> "%5howImage$*roded$= img0> "%1aite0> //"leaning #p "%Destro1indow$M1indow$0> "%Destro1indow$*roded$0> "%ReleaseImage;img0> ret#rn > K 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 /ou can download this %pen& visual c++ proect from here.
roded Image
2ew OpenCV f#n"tions whi"h are not fo#nd earlier are eplained here
• "%*rodeimg= img= = &0
The @st parameter is the source image.
The Hnd parameter is the destination image which is to be the eroded image. )ere the Ard parameter is the structuring element used for erosion. If it is 0,
a APA rectangular structuring element is used.
The Jth parameter is the number of times, erosion is applied.
This function can process images in place. That means same variable can be used for the @st and Hnd parameters.
If you want more explanation about various methods in the above computer application , please refer to &apturing Images 0 ideos.
Dilating
ilating is something like opposite of the eroding an image. )ere is the %pen& code.
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
!in"l#de ("%'h)
!in"l#de (highg#i'h) int main0
H
//displa the original image
IplImageE img "%LoadImage$C</M7i"'pg$0> "%2amed1indow$M1indow$0>
"%5howImage$M1indow$= img0> //dilate and displa the dilated image "%Dilateimg= img= = &0>
"%2amed1indow$Dilated$0> "%5howImage$Dilated$= img0> "%1aite0> //"leaning #p "%Destro1indow$M1indow$0> "%Destro1indow$Dilated$0> "%ReleaseImage;img0> ret#rn > K 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
Dilated Image
2ew OpenCV f#n"tions whi"h are not fo#nd earlier are eplained here
• "%Dilateimg= img= = &0
The @st parameter is the source image.
The Hnd parameter is the destination image which is to be the dilated image. )ere the Ard parameter is the structuring element used for dilation. If it is 0,
a APA rectangular structuring element is used.
The Jth parameter is the number of times, dilation is applied.
This function can process images in place. That means same variable can be used for the @st and Hnd parameters.
In%erting
Inverting an image is like taking the negative of an image. 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 !in"l#de $stdaf'h$
!in"l#de ("%'h)
!in"l#de (highg#i'h) int main0
H
//displa the original image
IplImageE img "%LoadImage$C</M7i"'pg$0> "%2amed1indow$M1indow$0>
"%5howImage$M1indow$= img0>
//in%ert and displa the in%erted image "%2otimg= img0> "%2amed1indow$In%erted$0> "%5howImage$In%erted$= img0> "%1aite0> //"leaning #p "%Destro1indow$M1indow$0> "%Destro1indow$In%erted$0> "%ReleaseImage;img0> ret#rn > K 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 /ou can download this %pen& visual c++ proect from here.