Transcripts
1. Class Intro: Hello. Welcome to our class Python Open City Mastering
Computer Vision. My name is John the Shortcut. I am FOSTEC web developer, Python programmer at
Online Instructor, and I'm going to instruct
you through the whole class. I am thrilled to be your
guide on this journey into the world of computer vision
and image processing. In this course, we will
deep dive into the Open C, the industry standard
library that bring the magic of
computer vision to Python. By the end of this course, you will not only understand
the fundamental of OpenCV, but you will also gain
practical skill that you can immediately apply thanks to
our ten hands on project. Now, let's see what exactly we are going
to learn from here. We will start with the basics. First, we are going
to learn how we can install OpenCV in
our local machine. Then we are going to read image, resizing and rescaling frames. We are going to draw
different shape and put text on image. Then we are going to learn
important functions. Next, we are going
to learn transition, rotation, translation,
conduit detection. Color spaces, color channels,
blurry, Btis operation, masking, gray scale histogram, color image histogram
and thresholding. After learn all of
the basic concept, we are going to jump into
the project section. We are going to conduct
multiple real world projects, such as text detection
and extraction using OpenCV and draw rectangle
shape and extract object, black dot white dot detection. Tis pin coordinates
when we click on image. Then we are going to jump
into the most important part, which is invisible
T shirt project, Background substriction,
phase detection using OpenCV. Also, we're going to
contact another project detecting objects of
similar color in Python. And this is not the
end. Time to time, we are going to
update this class, and we are going to add
new project in this class, such as finger detection,
eye detection, control brightness using
fingers and many more. Every week, I'm going to
add new concept on it. By the end of this class, you will have the skill to build your own computer vision
projects and automate your task. After this course,
you can explore area like AI and argument reality. So what are you waiting
for? Let's start together.
2. Installing openCV in our local machine: Hey, this is the first tutorial related Python OpenCV course. Hell. My name is John Shoker. I host a web developer
and online instruct. We are going to start
out tutorial by installing OpenCV in
our local computer. So let's jump into
the computer screen. So as you can see, we are
in my computer screen, and I open Windows Power Shel. Basically, OpenCV is a
computer vision library. It is available in Python, C plus plus, and Java language. A computer vision is a
application of deep learn. And it primarily
focusing on media files. It could be images,
otherwise, video files. I am assumed that
you are already installed Python
in your computer. And to check, is it installed in your
local computer or not, just type Python space Virgin. Then hit Enter. After
hit Enter AsimCC it return Python version 3.11 0.4. Just make sure you have the
latest version of Python, otherwise, greater than 3.7. So I hope you are already installed Python
in your computer. But now I'm going to install the packages that is
required for this course. So I'm going to install
our main package OpenCV to install
OpenCV in your system, just to type PP command. P, install OpenCV Ctrip. Dash Python. Sometimes people
may tell you just download OpenCV dash Python. Basically, Opens dash
Python is the main package. It is the main module, but Opensi Ctrip contained main module with that it
contain contribute modules, which is provided
by the community. So I'm going to hit Enter
and waiting for download. As you can see, requirement
already satisfied. I already download this module, but you need to complete
your download process. With that, also, I'm going
to update my P version. So I'm going to copy this
command and paste it here. Then I'm going to hit Enter. It's going to update my Pin. So I successfully
update my P version. But if you notice, you can see, when I try to download OpenCV, it also download Numpie warning. Numbi is kind of scientific
calculation packaging Python. It is extensively using matrix,
manipulation, et cetera. Maybe we need to use Numpy
in some of this tutorial, but don't worry about
it because it is simple and relatively
get easy to start. So this is it for this tutorial. In this tutorial, I just
show you how we can download Open C. In
the next tutorial, we are going to learn
how we can read images and video using OpenC. So thanks for
watching this video, stay tuned for this tutorial.
3. Reading images and video using openCV: Hey, everybody. Welcome
back to another field. In this video, we are going
to learn how we can read images and videos
using Python Open Cit. So let's jump into the
isa Studio code editor. As you can see, here I open
my visa Studio code editor. And at the website, you can see this is my
current working ding. At first, in my current
working directory, I'm going to create
a Python five, and I'm going to name it
main dot py main dot py. At the first half of video, we are going to talk about
how we can read images. And next we're
going to talk about how we can read videos. So at first in our Python file, we need to import
OpenCV, so type, import CV two as C. And to read an image in OpenCV, we need to use imaged
meth we need to type cd dot Image R. IMR. Then inside the rounds
inside the quotation, we need to provide
the file path. As you can see in my
working directory, there is another
folder named images. And if I open it,
here you can see, we have to tap two image,
cat one dot JPG cat dot JPG. So at first, I'm going
to read cat dot JPG. So inside the quotation, first, I'm going to tag the
folder name Images. Images slash I'm going to target the picture, cat dot JPG. Also, I want to capture
this image in a variable, and our aria is ING. ING equal to Cv dot
image root function. Now, once we read this image, we need to show this image. To show this image, we
need to use a method, which is image show, some type cv dot image show. IN show. Then inset
the round presses, need to pass total
two parameter. This method going to display
the image in a new window. So first, here we need to provide the name
of the new window, and the Hemotype can. Next, we need to pass the
matrix, the image we read. So I'm going to type IMG, this variable, and I'm
going to set this spot. Now I want to run a keyboard binding function
before I run this scope because here we need a specific d so I'm going
to call another method, which is known as wet key. Swing dot wet key. This one. And instead
the round presses, I'm going to pass Z. It is a keyboard
binding function. It is wait for specific. Until I press any
key of mil keyboard. If I pass zero, it's going
to wait for infinite time. Basically, I want to say until I press any key from our keyboard, it's not going to
remove the image. Let's run the code
in our terminal. So I'm going to set the file
and after set the file, I'm going to hit the run button. After press the Run button, as you can see, it
return the image. Here it open window and display
the image in this window. And if you want to
kill this window, you can praise any key
from your keyboard. Otherwise, you can click
on this close icon. So I'm going to
praise space key. So as you can see,
it kill the window. This is cool. Now, the image I open from my current
working directory, it's a small resolution image. Let me show you.
As you can see in my current working directory,
there is two image. Get one dot JPG and Go dot JPG. Both the images are same, but their resolution
is different. If I show you G dot JPG, now you can see its
dimension is 604 13. But if I Ober my cursor,
get one dot JPG, now you can see its dimension is much more bigger
than previous one. It is 5,026 y 3,458. Now I'm going to open
the same cat image, but this time, I'm going to
open the larger version. So let's get to the
salt studio code. I'm going to change
the image name. I'm going to make
it cat one dot GPG, and I'm going to step this file, and I'm going to run this code. And I'm going to
hit the Run button. After hit the run button, as you can see, the image
goes way off screen. So I'm going to close this image by pressing
any key of my keyboard. I'm going to press B. After press B, it
kill the window. This image is much larger
than my screen resolution. Currently, obviously
we don't have any in build resizing techniques that work according to the
computer resolution, but we can resize the image. In the next tutorial, I'm
going to talk about it. So if you have large image, possibly, it can go off screen. In this tutorial, just focus on reading images and videos. So this is for reading images. Now, let's focus on how
we can read videos. But before I'm going to select all the code and I copy it. Then I'm going to
create another fine in my working directory, which is main two dot Pine. And I'm going to
paste all the code from my previous fine. This time, we are going
to read video five. So at first, I'm going to
comment out these lines. I don't need it for now. Now, here's why I show you my
current working directory, there is another
folder named Videos. If I open it, there is
a video, video one.nb4. And to read a video, here I'm going to create
a variable capture. Equal to here I'm going to
use video capture method. Cv dot, video catureT one. And rounds. This method either take Itiser value like
one, two, three. Otherwise, we can
pass a file path. Now you might a portion why we need to pass in dieser value. Because using integer value, you can connect
with your webcam. If you have one webcam,
you can pass zero. Most of the cases you webkem
reference by integer zero. If you have multiple one, then
you can change the number. But before, at first, I'm going to read the existing
video from our file path. I said the single quotation. First, we need to
provide the folder name, some type videos then
inside this videos folder, we have this video one. So Intro select video one.b4. Remember, reading videos is
different from reading image. We need to read videos
frame by frame. That's why we need to use Hilo. So here, I'm going to use Hilo. Wild, true. Then inside the While
loop is crew Comma, frame equal to capture
dot read function. Capture dot read function. This capture dot read
function going to read this video frame by frame. It read the frame and this wollen value going to tell that it is successful
you work or not. To display the video, we actually display the
individual frames. So after read every
frame of this video, we need to display the frames. To display these frames, we are going to use image
show function. So type Cv dot image show
is at the round presses, as you know, first,
we need to pass a window name, which is video. Then I'm going to pass
the actual frames. So I'm to pass this
variable frame. So copy the variable name, and I'm going to paste it here. For now, I don't need
this wet key function, so I'm going to remove it. Yes, we need wet key, but we're going to use
it with p condition. So her type, I v dot wet key, inside the roundresss held
pass twin and zero X F equal, equal to b, inside the
round resssOd of B, then it's going
to kill you into. Then inside the IP condition, I want to break this VI loop. Once we've done
outside the I loop, we need to release
the capture pointer. So we need to use release
functions on type captured Dot, release
function, release. Also, we need to destroy
all the windows. He tie, CV, dot, destroy all the windows. This one. This is it. Now recap the port. The capture variable is instance
of video capture gloss. Then instead the one loop, we read the video
frame by frame. Then we displayed every
frame using IMS method. If CVD 820 and zero
xFF equal to org, D, then break the load. So if I press B, then it's going to stop
show video frames. Basically I want to
say, if I press D, then it's going to
break the load. Then we release
the capture device and destroy all the windows. Now, let's set the
file and run the code. After I run this
code, as you can see, it play our video. And if I praise D, it close our window. Now, if you want to open
your webcam, yes, you can. Just do begin this line
and comment on previous one and change the
video capture video. I want to use zero because I have just
only one camera connected with my computer. After step this file, if I
run this code and wait for some segins Now you can directly capture
your video using OpenCV and you can read it off. So if you want to
close this window, just you need to
press B command. If you press any other key, it's not going to
close your window. In our condition, here we say, D, which going to
break the loop? Now, let me show you
an important thing. So first, I'm going
to kill my terminal, then I'm going to comment out this line and
uncomment this one. And I'm going to sub this file. After set this file, I'm going
to run this video again. This time, you can notice
after complete the video, it's going to show an
error in our terminal. So after 10 seconds, after complete the video, as you can see in my
terminal, there is an hero. It's mean when our
video is over, otherwise stopped, then it's
going to show the hero. If there is no frame in this
video, it can return hero. Specifically, it
return this error minus 215 assertion feel. It's mean obviously could not find any media file
at that location. It's going to return
the same type of error with image also. Let me show you. So
I'm going to kill my terminal and back
to main dot Pi file. If I change the file path, if I make it cat
three, as you know, there is no file available
in our directory cd three. If I set this file
and run this command, as you can see in my terminal, it sit on the same error, negative 215 assertion fade. It's exactly show the
same error as like video. It could not find any frame. So that is very much
for this video. In this tutorial, we learn
how we can read image. Also we learn how
we can read videos. With that, also we
learn how we can read videos through our webcam. So this is it for this tutorial. At the next tutorial, we
are going to learn how we can resize and
rescale any frames. It could be image,
otherwise video. So thanks for
watching this video Stune for the next tutorial.
4. Resizing and Rescaling Frames: Hey, guys, good to see you back. Once again, I'm back
with another tutorial related open ct. And
in this tutorial, we are going to learn
resizing and rescaling. So as you can see, we are jumping to the user
Studio code editor. In this section, we
are going to learn how we can resize and
rescale video and images using open C. To rescale
a video frame or image, we need to create a function, and our function name
is Dave scalFrame. Then inside the round less, here we need to pass
some parameter, frame to V size and
the scale will scale. Equal to here, we need
to pass scale value, how much we want to scale. For now, I'm going to set 0.75. Then inside the rescal function, I'm going to set with W
equal to frame dot shape, instead of the squared ss, I'm going to pass one. Then I'm going to
multiply with scale. And next, I'm going to duplicate this line and replace
with with height. And inside the square versus
I'm going to pass zero. Frame dot shape one,
basically with of your image and frame dot shape
zero specify the height. And as you know, width
and height are indiser, so we need to convert
it into a indiser. I'm going to use int function. It, and we need to use round *** also going to do
the same thing for height in round brass. And now I'm going to declare
a variable called dimension. Dimension equal to, and I
want to create a tuple. So instead the rounds, I'm going to pass what coma, and our next value is height. And next, I'm going to return
v dot resize function. So I'm going to return
return, Cv dot, resize. Inside the run press, first, I'm going to pass frame. Next, I'm going to
pass dimension. Comma intercolon value equal to Cv dot Enter area. Enter area. Civ dot resize is a
open CI function. It is used to resize an image, and frame is our image, what we want to resize. And dimension specify the
new size for the image, and it should be a tepel which going to represent
height and width. And this parameter specify the interpolition method
used to resize the image. Interpolution method
determine how pixel values are calculated
when an image is resized. CVI Enter area is a specific
interpoltion method, optimized for shrinking images. It may result in
better quality than other methods when
reducing the image size. Don't worry. We are going to
learn more about it later. Now I want to read a video five, so I don't need this one. So to revoke this one and
back to mean dot Pi five. From this section, I'm
to copy this portion. And back to rescale dot Pi five, and I'm going
to paste it here. For now, I don't need
this line because I don't want to use my webcam. Now, after I read this
frame in our VLU, I want to resize the frame. So we're going to
call this function. But before, I'm going
to declare a variable, and our variable name
is frame resize. Frame under sco resize. Equal to, I'm going to call
this function, rescale frame. So I copy the function name and I'm going to paste it here, then inside the round dresses and inside the round presses, we need to pass this
variable frame. Then we need to
show this variable using image show method. So I'm going to
duplicate this line. And first, I'm going
to name this window, which is video resized. And then we need to pass
the frame resized variable. So I copy this variable
and repress frame with it. And I'm going to set this file. And before I run this code, I'm going to remove
this two line. I don't need this anymore. Otherwise, I can
comment out this one. And I'm going to set
this file again. After I set this file,
if I run this command, it's going to open
total two into. First, it's going to
open original window, then it's going to open
the resized window. Let me prove you that.
So after set this file, if I run this code
in my terminal, as you can see, it's going
to open total two Window. This is the original window
with high resolution, and this is the
scale window with low resolution. So
this is how it's work. This video is resize by 0.75%. If you want to reduce it more, just you need to
reduce the value. By default, this function every time going to
scale down 0.75%. But if you want to
pass your own value, just you need to pass
comma had and provide scale value scale 0.20%. And if I run this code, here you can see this
is our original size, and this is the
decisive version. Now, it's scaled down 20%. So I'm going to praise D to D the window before
it ran out of frame. Using the same function, you can resize image. Let me show you how we
can resize an image. But before, I'm going to
comment out these lines. Somebody use multi lein comment to comment out all the lines. Then I create a variable, and our variable name
is resized image. Underscovimage. Resis imaged equal to here I'm going to use the
function recess frame. So I'm going to copy
the function name and I'm to call the function. Instead the round process, we need to provide the
image which I want to show. So I'm going to comment
out these lines again, and I'm going to
pass this variable in this function. Image. And then I'm going to
use I'm Show function. I copy this line
and paste it here. First, I'm going to
change the Window name, which is resize asco C. And then I'm going to
call cv dot wetKeyFunction. Cv dot weit key. Inside the Rundress, I'm going
to pass zero. That's it. Before I set this file, and run the code, I didn't mistake. Here, we need to pass this
variable. Resized image. And again, I'm going to set
this file and run this code. Up to run this code,
as you can see, this is the original
size of this image, and this is the scale
version of this image. We scale down the image
window successful. So using same function,
we can scale images. Also we can scale videos. Remember, this method
is work for images, video and light videos. So using one function
you can do all tics. So this is it for this tutorial. At the next tutorial, we're
going to learn how we can draw shape using OpenCL.
5. Draw different shapes and put text on image: Hey, everyone, and welcome
back to another ID. In this tutorial, I'm
going to talk about how can we draw
and write images? So let's jump into the
is a studio code editor. So at first, I'm going
to create a Python file. So here I'm going to
type, draw dot py. And then I'm going to
import our libraries. At first, I'm going
to import CV Import, CV two as CV. Then I'm going to
import NumPi as NP. Import, Num Pi, as N
P. Also you can see, I already put that image in
my current wing dy cat JPG. Then I'm going to read this
image using INRdFunction. So here type first hemo
crative variable IMG equal to, I'm going to use
C dot image read. IaRad. Then the round
brass, I at the quotation. We need to provide
the file path, CAT two dot JPG. Then we need to display this
image using I show function. So I will type cv
dot image show IM, so Is the roundreF we need to assign a
name to this window, which is C. And next, I'm going to pass the variable, which is ING image. And then we need to use
Wet kwatho v dot wet key, wet key, and inside
the round presses, I'm going to pass Z. I'm
going to set this file. There are two ways
to draw on images. You can draw anything in a standalone image
like this one. Also, you can create
a blank image. So I decide on where to
create a blank image. Here types blank, equal to, I'm going to use tt zeros. Then the round brass, I'm going to pass
resolution 500 by 500. Also, we need to put
it in a round brass. They I'm going to pass
D type, bigger type. B, D type parameter. Is at this single course,
I'm going to type INT eight. Then close the round races. This is the data
type of an image. And if you want to
see this image, just you need to
type this command. You may show copy
this command and hern pist and I'm going to
replace at with ban. Also, I want to
change the variable. I NG two blank, and I'm going to set this file. After set this file, I'm
going to run the scole. So I'm going to click
this Run button. After click this run button, as you can see, now you can see, now you trend two Window. One window is KAT image and another window
is blank image. And as you can see, this
is a squaring image, 500 by 500 pixel. So instead of using KAT Image, I'm going to draw my object
in this blank image. So here, I'm going to comment out the lines,
these two lines. Otherwise, we can remove it. For now, I'm going
to comment out. Now at first, I'm going to paint this blank image
in a certain color. So I'm going to type a coman paint the image in
a certain color. To fill the blank
image with new color, here you need to pass another. We need to pass attribute
four color channel. We are going to use to
three color channel RGB. Now we can able to use
three color channel value. So to color the blank image, first time going to type blank, then you say the square ress
I'm going to pass codon. It's me or the pixel I go
to select all the pixels. Then then I want to fill
it with green color. So I'm going to pass
0255, 255, zero. It's going to fill this black
image with green color. Then we need to run
image show function. So here to type dot Imho the runderF to provide
a nameFirst we need to provide a name for
the window and I'm going to type green. At the next value, we need
to pass the variable. The does. And I'm going to set this.
Up to set this file, if I run this command, you can see it with a two window.
This is black window. This is the default
one, and this is the new window with
green field color. And to get different color, just you need to
change the values. Suppose hand of pass zero
comma zero, come up, 255. I'm going to set this
file and run this code a. Up to run this code, now you
can see a different result. This time, this window
filled with red color. He we use slicing technique
to select all the pixel. Not only that, if you want to fill a certain area
with colour, yes, you can, you need to provide the vile of starting
point and end point. Suppose at our first view I'm
going to pass 200 by 300. At the next, I'm going
to pass 300 go 400. If I set this file
and run this code, now you can see it at a red
square box at that position. So this is how we
can fill recolored. The next thing I'm
going to do, I'm going to do to draw a rectangle. So first, I'm going to remove both the lines because
I don't need it, and then I'm going to type
a single leg command. So I'm type hashtag and
I pass draw a rectangle. To draw a rectangle, we
are going to use a method, which is cv dot Rig
tangle. This one. And to draw the rectangle first, we need to pass an image. So inside the round recess, I'm going to pass black. And also the function
take multiple argument. Image 0.1 0.2, color, thickness, and if you want to provide line
type, yes, you can. So for the starting point, inside the round ress I'm
going to pass zero comma zero, and for the end point, I'm going to pass 250
pixel by 50 pixel. Then we need to pass color. And for the color value, I'm going to pass 02550. And here, also, we need
to pass thickness, Sun type, thickness, and I'm going to pass
thickness Vo two. And when it's done, also we
need to show this image. So I'm going to type
cv dot image show IM, so inside the roundresses first will to pass a window name. So I'm going to type rectangle. Comma. The pass the image. In which image, I want to apply the rectangle, which is black. This one. I'm going
to set this file. After I set this file, if I run this code, you can
see the result. Now you can see that window in this window, it draw rectangle. But you notice Harry create total three window because we call image show
function three times. So for now, I'm going
to comment out. So for now, I'm going to
comment out this line. This one, also, I'm
going to comment out this one. I don't need it. Also, I don't wonder
for the square box, so I comment out it. So if I run this code again,
you guys see the result. This is the rectangle we
create in the blank image. And as you can see, we start this rectangle from
the origin position. And if I change the
origin position, suppose 50 by 50. Then set this file
and run this code again, you guys see the result. Now it start from
at that position. And if you want to increase
the height of this rectangle and you want to use full
of the image height, in that case, just pass. Again, I'm going to start 0-0, and this time I'm going
to pass 500 by 500. And I'm going to set this file. After I set this file, I'm
going to run this code again. Now you can see it
cover the whole image frame with the rectangle. And if you want to
use the hub of area, just you need to pass 250500 and set this file and
delete this window. After set this file and run this code, you can
see the result. So you can define
size and position of this rectangle from this vets. Now you decide you want to fill this rectangle with color. For that, just want to
change the thickness value. So at first, I'm going
to delete this window, and then I'm going to
change the thickness, thickness equal to v dot field. So after set this story if I un this code, you can
see the result. As you can see, this time I want rectangle fill with
green colored. Now I'm going to
show you something. Instead of passing these values, you can use shape. You can use exactly bland
shaped area. Let me show you. So I'm going to
duplicate this line and comment on previous line. And this time, instead
of using numeric value, I'm going to use shape. So I'm tied the
image name blank. Blank, dot, shape. Then inside the square resis,
here I'm going to pass one. And then I'm going to
divide this value with two. Then I'm going to
pass comma and I'm type blank dot shape. And inside this square resis
I'm going to pass zero, and I'm going to
divide it with two. If I set this slide
and run this code, basically it's going to create a square shape because
as you can see, here is at height,
500 and width 500. And if we divide 500 with two, then it's going to return 250. Also, if we divide
this 500 with two, again, it's going to return 215. So it's going to
create a square. Basically, we are going
to scale down the shape. So after set this soil
if I run this squad, here you can see it
creates a square shape. And its height is half
of the image height, and also is half of
the image width. At the same way, if
I divide it with three and then s up
this file and run code, now you can see a
different shape. This time, height
is divided with three and image wide
is divided with two. That's why it created a
rectangle shape Basically, I want to show you instead of
using exact numeric value, you can use the shape
to calculate the value. Now moving on to the next shape. Let's draw a circle. So here I'm going to type
a single line comment, and I'm type draw a circle. And also, I'm going to comment out the lines. I don't need it. To create a circle, we need to use cv dot circle
method, cv dot circle. And inside the roundss first, we need to provide the
image which is there. Then we need to pass the coordinate of
the center position. So inside the round
addresses, for now, I'm going to pass
mid position of this image, which is 25250. Then in this function, you need to pass
the radius wave. And here I'm going to
pass radius value 40. Also, we need to
provide the color. So inside the round recess, I'm going to pass 002 55, which is going to
return red color, BGR. And then we need to provide
the thickness of this border. So tight thickness, I need and I'm going to
pass three pixel or three. And then we need to
display this image. So Hemo type cv dot
image show Im show. Then inside the round
presses first want to pass the window name and
window name is circle. And the next window I'm going to pass is the image
name which is there. And I'm going to set this
file. After I set this file, if I run this code
in my terminal, this is what it create. It creates a circle at the
middle position of this image. And now, if you want
to fill this circle, then you need to change
the thickness viel. But this time, I'm going
to use another do. Instead of using Cv dot field, here I'm going to use minus one. If I pass this value and set this file and run
this code again, now you can see our circle
filled with red color. So this is another method that
you can elmate with color. Just you need to pass
thickness value minus one. Also, you can use
cv dot fill it. And now I'm going to
show you how we can draw a standalone line using open C. So I'm going to
dig this window, and first, I'm going to
comment out both the lines. And here, I'm going
to try to comment, and I'm going to
try draw Hey, line. To create the line,
we need to use v dot line method, cv dot line. Then ins the round dress, first, we need to pass the
image where are we to draw the line,
which is black. And now we need to pass the
coordinators of this line. For that, I'm going to
use the same values. So I'm going to copy this
section and paste up to that. Just I'm going to change the
thickness value this line. Here I'm going to pass
thickness value three. Also, I'm going to
change the color, and I'm going to use
white color, fully white. So I'm going to
pass 255255, 255. After I set this file,
if I run this code in my terminal, you
can see the result. And before set the
file, I run this code, we need to show this image. So I'm going to
copy this section, paste it here and I'm going
to rip the circle with line. That's, I set this
file and run the code. After I run this code,
you can see the result. So this is the light we
create using open Ceri. And if you want to change
the origin position, just need to change the view. Suppose I'm going to
start it from 2020 do. If I set this file and delete this window and
rewrite this code, you can see this time it
start from at that position. So we already cover
most of these shapes. The last thing I'm going
to talk about text, how we can put text on image. For that, first, I'm going
to delete this window, then I'm going to
comment out the lines. Also, I'm going to
type a comment. This time, we're going
to write text on image. It is very straightforw
art to put text on image. First, you have to type cb dot, and there you need to
call put text method. Put text. Then inside the
roundsesF we need to select the image where we
are going to put the text. In our case, blank. Poma, ten, we need
to provide the text, which you want to
put in our image. In our case, hello n. Then we need to define the position from where
we want to start or text. So inside the round
phase, I'm going to pass 225 comma 225. Next, now we can
specify font phase. So here to pass the font phase and to
pass the font phase, we need to use some OpenCV
in below font phases. And we are going to
use cv dot font, underscore, here we
have a lot of options. We have, complex small, duplex plane, but we're
going to use x, this one. And the next will we
need to pass for scale. And I want to scale up to 1.0. And next, we need to provide the font color and
her pass zero, 255 comma zero, and also
to pass the thickness. And for thickness, I'm
going to pass something. I think two is enough, and oops, I need to provide command
between these vs. That's it. Then we need to call image
show function, the dot Imho. Then inside the
roundre at first we need to provide a window
name which is text. Then we need to provide
the image black. And I'm going to set this file. After I set this file,
if I run this code in my terminal, you
can see the result. So as you can see,
we successfully put taste on this image. So I hope it's clear
for you how we can create different
shapes and lines. Also, we can put
taste on an image. So this is it for this tutorial. So thanks for
watching this video. Sad for the next tutorial.
6. Important function in open CV: Hey, everyone, and welcome
back to another video. In this tutorial,
we're going to talk about basic function
in open saving. Now, let's start
with first function. Using this function,
we can convert images into grayscale image. We already select this image
and display this image. It's a BGiRimageT
channel color image. BgiR stands for
blue, green and red. Now there are ways that we can convert BGiRimage
to grayscale image. For that, we are going
to use CVD color method. First I'm going to
declare a variable gray equal to CV, dot, CV decolor. There Is the round
presses, first, we need to provide the image, which image I want to
convert into grayscale, and our image variable is IMG
Now at the next parameter, we need to pass the color code, which is CV dot, colored, underscoe BGR,
to grade this one. Next, I'm going to
display this image. I'm going to copy this
function image show, and I'm going to paste it here. Then I'm going to
replace cat gray image with gray variable. I want to set this
file. Up set this file, if I run this code
in my terminal, you can see the result. This is the color image and
this is the grayscale image. It is very easy to convert
VgRimage into grayscale image. At the next function, we are going to discuss
how we can blur image. Here I'm going to type a single comment and I'm
going to type blur. I'm going to move this
line above the blur coma. There are multiple blur
technique in Open CP, but we're going to
use Gersionblur. At first, I'm going to
declare a variable blur. Blur equal to Cv
GusionblRtie, GH Gasianblur. Then inside the
Runress first only to provide the image
source which is IMG. Comma, next, in this function, we need to provide
the kernel size value and we need to provide the value as a tapel here we mention the
size of the blurred image. Is the round versus,
I'm going to pass and here I'm going to pass kernel size three y three. Next, we need to
define the border. I'm going to pass CV dot, border default. This one. Then we need to display
this blur image. I'm going to use the same
function, image show. I copy this function and
I'm going to paste it here. First, I'm going to replace the window name, gray with blur. Then I'm going to replace
the variable name, our image source, which is
blur. I'm going to satisfy. But before I run this code, I'm going to comment
out these two lines. I don't need it anymore. So after set this file, if I run this code
in my terminal, you can see the result, but it is not too clear. In this image, we
add little in it. If we compare it with
the original image, let me show you I delete this terminal and
commend this line. Also, this time I'm going to
increace the kernel value. I'm going to make
it three to 77 by seven and the set this file and run this
code in my terminal. Now you can compare
both the image. This is the normal image
and this is the blur image. Here we add Gersionblur. Using kernel value, we can
add more blood in English. Now, let's jump into
the next function, which is age cascade. Here I'm going to
create a single line coming and I'm going to type, age cascade. Using this function, basically
we find edges in an image. There are many H cascade
available in open C. But in this tutorial, we are going to use CIH cascade, which is pretty famous in
computer vision world. Basically, it is a
multi step process that involves blurring, grading, and many.
So let's start. So first, I'm going to
declare a variable, and our variable name is an Canny because we are
going to use any cascade, assigned with C we dot Cy. Inside the roundresses at first, we need to provide
this source image IMG W that also we need to
pass to threshold value. First value I'm
going to use 125, and the second value
I'm going to use 175. Now, let's try to show
this image in our window. But before I'm going
to copy this function, Image show and comment
out this function. Then I'm going to paste it
here and replace BlaiKN Also, I'm going to replace
the variable name, Ken. Now let's run the
code in our terminal. After run this code
in our terminal, this is what it looked like. This is what it written. So as you can see
in this example, using as cascading,
we are able to detak the edge of
the SCAT Image. Not only that, also we
can reduce the edges if we pass a blur
image. Let me show you. So this time, I'm going to pass blurry age a Em
source, this one. I'm going to replace
ING with blood, and I'm going to set this file. After satis file, if I run
this code in my terminal, now you can see, we are
able to reduce the noise. Because of using blurry Image, that's why it reduced the noise. But if we reduce
the blood value, let me show you if
I use Gerson blood, three y three, and
then this file, if I uncode, now you
can notice the result. Now you can see it
increases the edges. It increases the
noise of this age. If you change the blood
value of an image, then you can detect
he more clearly. At the next function,
we are going to discuss how we can
dilate an image. To dilate the image,
we are going to use specific structure
and element. The structure and the
elements we are going to use actually are the ages. At first, I'm going
to delete my terminal and then I'm going to
type a single comment and type dilating the mache. And then I'm going to
declare a variable. He type dilate it equal to Cv dot, dilate. Then inside the roundreesF I'm going to pass the CE image, this one, a Image so. To pass the variable CE then we need to provide
the kernel size. After inside the round dresses, I'm going to pass 33, and also it iterations. I'm type iteration one. Now let's display this image. For that, I'm going to use
the onto function and I'm going to paste it here and
replace Canny with dilate it. I copy this variable
name and replace Canny with dilated and also we need to
replace the variable. Then I'm going to set this file, and then I'm going
to set this file. Let provide some space
between the functions. Otherwise, it look very glancy. Then I'm going to
comment out this line. I don't want to show the
original image this time. I want to show only the canny
image and dilated image. Please satisfy if I run
this code in my terminal, you can see near
the same result. This is the dilated image, and this is only the Ky image. These are the edges and
these are the dilated edges. If you notice carefully, you can see the dilated edges are much more thicker
than previous one. If I increase the iteration
value, let me show you. I'm going to close
the terminals. And then I'm going to increase
the iteration Willow. Oh, I'm going to make it. So I'm going to make it three
and then set this file. Then I'm going to run this
code again in my terminal. After I run this
code in my terminal, you can see the result. Dilated edges are
much thicker than C edges. This is how its work. Now I'm going to discuss
the lst function, which is how we can
resize and crop image. Here, I'm going to type
a single end command and I'm going to type resize. We already learned how we can resize our frame in
our previous videos. This function is
very easy to use. First, we need to declare a variable and our
variable name is resize. Equal to Cv dot resides. Then inside the
run recess, first, we need to pass the source
image, which is ING. Here are the pass ING, and next, we need to pass
the dimension of the image. I'm going to set it 500 by 500, but it's going to
ignoring the aspec ratio. I'm going to comment out what
the previous onto function. I don't need it for now and I'm going to
set this file and also need to call the
function p the resize image. To replace this
variable with resins. Also, I'm going to
change the window name resides and set this file. Now let's on the code
and see what it written. After on this code, you
can see the result. It rest our image successfully, but it do not follow
the aspect ratio. If I compare it with the
original image, let me show you. If I uncommen this line and
the set this file again, and this time around
to pass 400 by 400, 400 by 400 and set this file and run this code,
you can see the result. This is the original image, and this is the resize image, you can see the difference
between their aspect ratio. Next, I'm going to show you how we can cropping the image. I delete the windows and I'm going to tie
another comment, cropping. On the base of
their pixel value, we can select the
portion of an image. So let's drop the image. So at first, I'm
going to declare a variable crop Tikal two, I'm going to pass the
source image IMG. Then we need to
use square braces and we need to pass the points. First, I'm going to
select 50 colon 200. And the next point
is 200 to 400. Then I'm going to
display this image using image function. So I copy this
function and I'm going to paste it here
and I'm going to replace the variable size with Cro Also change the window name crop I'm
going to set this file. Before I set this file, we
need to comment out this line. Also, we need to compare it
with the original image. So after set this file, if I run this code
in my terminal, you can see the result. This is the original image, and this is the cropped image. This is the crop image portion
of this original image. So if I try to match with that, yes, we can match with that. This is how it slowed. So that's pretty much for this video. We just learned
basic function in open C. At the next tutorial, we are going to learn about
image transformation. So thanks for
watching this video. Stay tuned for the
next tutorial.
7. Open Cv Image Translation part1: Hello, and welcome to
Python Open City course. In this six set, we are going to learn basic image
transformation. These are common technique you would like to apply
on your images, including translation,
rotation, resizing, flipping. Let's start with translation. Translation is
basically shipping the image along the xs and yx. Using translation, you can ship the image up
down left right. Also you can mob it combinedly. To translate the image, we can create a
translation function. Let's declare a function. For that, I'm going to tie BA and our function
name is translate. Translate inside the Rundreses and I'm going to pass
throughout three parameter. First, we need to
pass the image, which image I want
to translate IMG. Then I'm going to pass X xs and next I'm going to pass YXs. Then inside this function, now to translate the image, we need a translation matrix. So I'm going to declare
a variable transmt. Transmat require two,
and I'm going to use a function called float float
32 from the Numbi module, NP, tot, float, dot, float. 32. But before we need to
import the NumPi module. Here we type, import
NumPi as NP and this one, take a list and
inside this list, have to pass to other
list. Another list. In our first list, we're going
to say one, zero comma X, and in our second list, I'm going to pass
zero, one comma Y. Once we create the
translation of the matrix, then we need to
provide the Tmage. I'm going to declare another
variable called tension. Dimension equal to
am pass a tuple. Inside the round ress here Amul pass, image shaped methyl. Image shape inside
the square ss, am type one, as you know, it is defined the image with. Also, we need to provide
the image height. For that, we need to
type image, dot, shape. Inside the square dresses, I'm going to pass zero. It's defined liquid. Then we can return some type, return vt fine. F fine. Inside the round presses, I'm going to pass the image. Then I'm going to pass the matrix transmt and we
need to pass the dimension. Now let's create our
first translated image. But before I'm going to tell
you if I pass minus X value, then it's going to shift
the image lip side, and if I use minus Y value, then it going to ship
the image upside. And if I want to move
the image right side, then we need to pass
positive X value. And if I move it downside, then we need to use
positive Y value. Now, let's create the
translated image. So to type another
variable, translate it. Translate it equal to translate. Translate, then inside
the round process, we need to pass the image. First value, I'm going
to pass this image, ING. Then we need to pass the xs
direction and xs direction. I'm going to pass
positive hundred value for xs for YXs also I'm going
to pass positive hundred. After satisfy, now we need to display this
image in our Window. For that, we need to use c dot Imho methor Cv
dot Imo Image show. Then instead the
roundreF we need to provide a name to this
window, which is translated. I copy this variable name
and I paste it here. Next, we need to use the image. Which image I want to fw. I want to fold this
image translate it. I call this variable and
I'm going to set this file. After set this file, if I run
this code in my terminal, here you can see the result. That's the difference
between two rezar. This is our original image, and this is our
translated image. Here we ship the
image, 100 point xs and positive 100 point yxs. It's shipped down by 100 pixel and shipped right by 100 pixel. Now, let's ship the image, left by 100 pixel and
down by 100 pixel. This time, I'm going to
use negative values. I want to move it -100
pixel Xs direction. I want to say lip side and also, I want to move it downside. So our X va in sale. Now, after set this file, let's run the code
in our terminal. After run the code
in our terminal, you can see the result. As you can see
this time it moved the image lip side and also
it moved the image downside. But if you want to
move the image upside, in that case, just give it
to pass minus YXS value. -100 pixel. Now let's run the code and see is it work properly or not? Up to run the code, you
can see the result. This time we ship this image, 100 point left side
and 100 point upside. This is how translate work. Also in our commence section, I mentioned the positive and negative value working method. If you want to move
it at left side, then use minus X, and if you want to
move it upside, then use minus Y. If you want to move
it right side, then use positive X, and if you want to
move it downside, then use positive Y. This is it for this tutorial at the next part we to
talk about rotation.
8. Open Cv Image rotation part2: Good to see you back guys. In this section, we're
going to learn how we can rotate image
using Python OpenC. As you can see in my
Visual Studio code editor, Harry already
created Python file, rotate dot pi, and I already distend image using
image it function. Then rotation is exactly
what it sounds like. Rotating an image
in some angles. At first, I'm going
to create a function, Dave and our function name is rotate this function going to
take total three argument, the image, and
next it's going to take angle, the rotating angle. Also, you take another
parameter which is rotating point, some t, ROT, point, equal to nun. Then inside this function, we need to grab the height
and width of this image. For that, instant the
rounds amo type height, width equal to, I'm going to
use ING shape method, Ng. Inside the square Basis
Hemotype colon two. I mean the first two value,
which is height and. Next, I'm going to
use IP condition. If the rotate point is none, then inside this IP condition, we are going to rotate
the image around center and to get
the center point, I'm going to type another
variable root point to get the center point, her type rot point equal
to inside the roundress, we need to divide with with two. So type width, divided,
divided by two. Also at the same way, we need to divide height
with height divided by two. Is going to return
the center position, otherwise center
coordinates of this image. Now we need to create
the rotation matrix. For that, I'm going to tie
first I'm going to declare a variable rot man
rotation matrix. Rotation matrix equal to v
dot get rotation matrix. Gate rotation matrix
two d, this one. Then inside the roundresses, here you need to pass
the rotation point, which is center position. I'm going to pass this
value, rot point. Ot point. When that also
we need to pass the angle. Then I'm going to
pass scale vial. If you want to scale the image, when you rotate the
image, in that case, you can pass to otherwise, otherwise, you can
use the default scale value, which is 1.0. Then we need to mention
the dimension as tuple. Type dimension dimension
equal to is at the tuple, I'm going to pass
width and height. Now we can return
the rotate image, SmtiRturn here I'm going to
call the method v dot, fine. Then inside the round brass, here first, I'm going
to pass the image. Then I'm going to
pass the rot mat, rotation matrix, and also
to provide CA between them. Then we need to
pass the dimension. So that's the function
that can rotate the image. Now, we need to
call this function. For that, I'm going to create
a variable named rotate. Rotate equal to here I'm
going to call the function rotate and inside
the roundres first, I'm going to pass the
source image IMG, then I would pass the
rotating angle, which is 45. I'm going to set this file. Now we need to for this
image in our window. For that, we need to
use image function. So here type v dot Im
inside the round press, at first, we need
to provide a name to this window which is rotated. I copy the name
and pass it here. Then we need to use
comma and we need to pass the image source,
which is rotated. I'm going to set this
coin. After set this file, if I run this code
in my terminal, you can see the result. You can see this is
our original image, and this is our rotated image. Here we rotate the image
counter clockwise. If you want to rotate
this image clockwise, just you need to pass
negative video -45 degrees. After satisfied if I re run this code, you can
see the result. This time it rotate
the image clockwise. This is how we can rotate
an image using open saving. We can rotate this
image at any angle. This is it for this tutorial. Thanks for watching this video.
9. Open Cv Image Translation croping part 3: So welcome to another tutorial
related transformation. In this tutorial, we
are going to learn resizing, flicking and cropping. So let's start the practical. We already learn about resizing in our
previous tutorials, we need to use Cv
dot resize function. So at first, I'm
going to declare a variable named reside. Resized equal to C dot
resize function, resides. Then inside the rounds, first, I'm going to use a variable
IMG image inside the rounds, we need to provide
the resize dimension. I have to pass 300 by 300. Next, we need to pass
the interpoliion value. I have typed
interpolation and there, I'm going to use v
dot InterqPT one. Enter Quvit is slow, but it return better image. Then just agree to call the image show function
and place it here, and this time, at first, I'm going to change the
window name. This is resized. Also, I'm going to
call the variable, this one. I'm going
to set this one. Now, let's set the file and
run the code in our terminal. After run this code,
you can see the result. This is our original image, and this is our resized image. Now let's talk about our
next transformation, which is flipping. First, I'm going to
comment out this line. Then here I'm going to learn how we can flip an
image using open sNing. For that, we don't need
to define any function. First, I'm going to type a single length command and
I'm going to type flipping. Then I'm going to declare
a variable called flip. Slip equal to c dot flat. Then in the round presses, I would pass the image
which I want to flip IMG. Here we need to pass a u. It would be zero, one, otherwise, negative one. Let's start with zero. Then I'm going to
display this flee. For that, we need to use
image show function, and I'm going to replace the window name,
resize with flip. Also, I'm going to
replace the variable laid. I'm going
to set this file. Zero basically implies
flipping the image vertically, and if you pass one, then it's going to flip
the image horizontally. If you pass minus one, then it's going to
flip the image, both the direction,
horizontally and vertically. Let's start with
vertically, pass zero. After set this file,
if I run this code in my terminal, you
can see the result. Vertically it flip the image. Now I'm going to lit this image
horizontally. I pass one. After set this file,
if I run this code, now you can see it flip
the image horizontally. Basically it
mirroring the image. You can see the mirror reset. And if you want to
flip this image both the direction for that, you need to pass minus one. If I set this file and run this code, you can
see the result. Now you can see it flip the
image, both the direction, vertically and horizontally.
This is how flip work. Next, I'm going to comment
out this line and this time, we are going to learn
about cropping, how we can crop an image. We already learn about cropping in our previous tutorials. First, I'm going to declare a
comment and Healt cropping. Then I'm going to declare a variable crop Crop equal to then we need to pass
this source imagemG. Then you side the square recess, we need to define the position, the point, the starting point and the ending
point of the crop. Here I'm going to pass little slicing
technique 200 cold 400. This is the starting point,
and for the end point, I'm going to pass 300 400. This is the point, and
I'm going to satisfyle. After set this file, we
need to display this image. For that, we need to use
image show function. So I copy this function and paste it here and
replace flip with ra. Also we need to replace
the variable length crop and we need to set this file. After set this file, if I run
this code in my terminal, you can see the result. Oops, it's not working. I think I did some mistak. Whoops, how you used a comma. We need to use colon Si. After then again, I'm
going to set this file and run this code. If I run this code, you
can see the result. This is what it return. This is the cropped image. I think this is this
exact position. If I change the crop
value something 50, by 200 and 100 by 200. Then set this file and
run this code. Let's see. This is the area it returned. We already learn about that
in our previous tutorials. So this is it for this tutorial. In the next tutorial, we are going to learn
contre detection. Thanks for watching this video, Styune for the next tutorial.
10. Contour Detection Opencv: Hello, everyone. Welcome
to another video. In this video, we
are going to talk about how we can
identify contours. Cues are basically
boundary of objects. It made with line and curves. In a simple sentence, contour is the outline
or shape of something. It is especially a curve,
otherwise, irregular figure. The contour word came from a Italian word,
which is contour. But if I talk about
mathematical point of view, Ctures and edges are
different thing. Oturor very useful thing when you involve
in shape analysis. Also it useful for detection
and recognization. In this tutorial,
I would like to give you the idea
of contours and how we can use it in
open C. As you can see, her we read a image file and this image using IMF function. If you learn this
code in my terminal, you can see this image. It is our CAT image. It is the exact CAT image. The next thing I'm going to do, I'm going to convert this
image into grayscale image. For that, hemotypeGray. Gray equal to CV dot. I'm going to use a
method called CVD color. C VT color inside the round resssF we need to pass the source image, which is IMG. So to pass this variable. Then I'm going to
use cv dot color. Cv colored. Color and I want to convert
this color BGR green. Underscore BGR gray. This one. And then we need to
display this image. I'm going to copy this
line and paste it here. I'm going to replace Tati gray. Also, we need to replace
the variable name, gray. I want to set this file. After set this file, if I done
this code in my terminal, you can see the result,
you can see the different. This is our an image, and this is our grayscale image. After this, I want to grab
the edges of this image. For that, I'm going to
use E edge detector. At first, I'm going to
declare a variable KE equal to C dot K. Then inside
the round versus, at first, I'm going to pass the source image, which is ING. Then then you need to
pass total two threshold. And here I'm going to pass 125 comma and the
second value is 175. Then I'm going to
display this image. I'm going to copy this function
I am show and paste it here and replace
gray with K. Also, I'm going to comment out
this actual colored image. I don't want to show three
image in my display. Upset this file, if I run
this code in my terminal, here you can see the result. This is the grayscale image, and this is the edge
detecting image. And now I want to find the
contours in this image. So we are going to use contour
method. Let me show you. So here I'm going to use
this method and this method to basically written
contours and hierarchies. So here I'm going to type contours the second
variable is hierarchies. So here, I'm going to use the
method Civ dot find contu. Then inside the round ress I'm going to par
the edges as can. They take moves in which
mode it find the contues. It could be civtRorcT. Let me show you Civ
dot retorscoeTT one. There are a lot of tour. We have external mode, if I show you rear,
external com list, floodF now, I would like to go with list because it's going to return all the contour, without using tree, I would
like to go with list. The next method
I'm going to pass, which is contour
approximation method. So after COVA, I'm going to type Cividt chain approach,
nun, this one. Now, let's explain
this line again. Here you can see
CV fine contues. This function ret condues
from a binary image, and Ki is the source image. In which image you want
to detect the conduce. CVR least is the mode of contu retraable
and the next method, CV dot chain approximant I store all the point of the contues and hierarchy
is the variable. It contained information about image tropolos and
the contour variable is the list of all contues. It's going to store all the
contours which we find in this image and each contour
store a Nampi at a point. As I told you,
contour is a list, and if you want to print how many contour we find in
this image, yes, you can. Just you need to use
print function print then inside the round recess, you need to use len
function A ten. Then inside the round recess, I'm going to pass the contours. This one, and I'm going
to set this file. After set this file, if I
run this code in my ravinal, this is the result we found, which is basically it's
returned 359 contoures. And if you want to reduce
the noise of the contoures, you can blur this image. So let's blur the gray image. So here, I'm going to create
another variable blur. Blur, sorry, not blue. Blur, equal to v
dot Gaussian blur. Then inside the round recess, we need to pass the source
image which is gray. With that, we need
to pass to values, how much gray I want. After pass the source image, also, we need to
define the K size. I want to say kernel size. Then inside the round resis, we need to pass the kernel size. So here I pass five CA five. Then we need to pass
another value, border time. So I'm going to type CV dot, border, and here I'm use the
default one, border deform. Also, I would like to
display this blur image. So I copy this function, I am show and I'm
going to paste it here and replace gray with blur. Then I'm going to set this file. After set this file, if I run this code in my terminal,
you guys see the result. This is the blur gray image, and this is the Kyimage. Next, I'm going to find
the edges of this image. For that, we need to pass the
variable and that section. Here, can. I want to satisfy. Appro satisfied. If I run
this code in my terminal, this is what it return. Now you can see it
return total 23 contu. It reduced the number of
contour because hero use lnge. This is how we can find
contu from an image. There is another way that we can find contour from an image. But I'm not going to
cover in this trutorial. I'm going to cover it
in the next tutorial.
11. Contour Detection Opencv part 2: Hey, good to see you back, guys. This is the second
part of this tutorial. In this section, we're
going to see how we can use different method
to find conduits. Without using Canny
edge detector, we are going to use threshold. It is another
function from OpenCV. So at first, I'm going to commit out on the lens.
I don't need it. Then down there, first, I'm going to declare
a variable, with. Also, I'm going
to declare trash. Equal to cv dot h. This one. And it's going to
take this gray image. So I'm going to
copy this in gray. Also, we need to pass
threshold value. The first threshold value
I'm going to pass 125, and the second threshold
value I'm going to pass 255. Don't worry about
it. We are going to learn about threshold in
our upcoming tutorials. Threshold basically
binarize the image. If the intensity of A
pixel is below the 125, then it's going to set to zero. I want to say, then it's
going to set to black color. And if the image intensity
is above the 125, then it going to set then it's going to set to 255,
otherwise, white color. And in our fine contour method, we are going to pass
this thrash variable. Trash. Not only that, here
we need to pass another value in
threshold function, which is threshold t. So the pass comma,
and here I want to tie cv dots BinarT one. Now, set this file
and run the code in a terminal and see how
many contour it return. Up to run the code,
as you can see, totally return 1073 contoure. Now, let's display the
thrashimage, not the gray image. So I copy this line and
comment out this line and call the onshoFunction, and replace gray with trach. Again, I'm going
to set this file and run the code in my terminal. Up to run the code
in my terminal, this is the result we farm. This is the result it return. This is the Black
and white image. No, it is the binary image, and it's made with
black and white dots. Don't worry about it.
We are going to talk about in our advanced section. Just one thing you
need to remember, hresold basically
binarize the image. It is the binary
form of an image. Zero indicates that color and
255 indicates white color. Now, let me show you one thing. At first, I'm going
to import num pi. He tie, import num Pi as NP. Apart from these,
I'm going to create a blank variable
B Blan equal to, and here I'm going to call
function from NumPiNp dot Os. Then inside the round recess, I'm going to grab the image
shape, this variable shape. So here, I'm going
to type mg dot Shae. Then inside the square
resist colon two, I'm going to grab
all of this to v. Next, we need to pass
the data type B, type, and here inside
this single quote, I'm going to pass UI NT, H. And then I'm going to display this bank image using
image show function. I copy this line
and paste it here. And rip this image with blank. I'm going to set this
file. Up set this file. If I run this code
in my terminal, you can see the result. This is the blank
image we create. This is the exact dimension
of this CAT image. Now the question is, for what purpose we create
this blank image? Because in this blank image, I'm going to draw or contue. I'm going to create this
contute in this blank image. That's why I create
this blank image. And to draw the contours, we need to use a function called draw contube so type v dot, draw contes this one, and inside the round dress, first, we need to
pass the blank image. So type black. Next, we need to pass
the contour list. This variable. As I told you, this contour is a list. I store all the control ws. So to pass the contra contuses. Next, we need to
pass contour index. I want to say how many
conti you want to pass, and I want all Op. So I'm going to pass Native one. Next, we need to
provide the color, and we need to provide the
color in Bzier format. So I want red color.
00 comma 255. Also we need to pass
the thickness and I want to pass thickness two. And now we need to
dist this blank image. So to copy this line, I am sure and I'm going
to paste it here. And also, I comment out the previous line. I
don't need it now. And I'm going to set this file. After I set this file, let's run the code in our terminal. A I run the code in my
terminal not working properly. I think there is a
mistake in my code. I think I did some mistakes, so let's check it. Here, this one, I don't need
this slicing technique. Just want to use
image dot shape. It's going to grab
the image shape area. So let's set the
file and rerun it. Up to run this code,
as you can see, this is what it returned. It returned the cat image. Basically, here we draw a new image using
the condut points. First, we take a blank image, then we use the condued points from the thresh word image and apply it in the blank image.
It is not the same thing. From the threshold image, go to grab the edges
and using these edges, we draw an image
in our blank page. If I reduce the thickness, I think it looks more
prettier. Let's see. I'm going to reduce
the thickness 2-1 and re run this cool. Yes, nerves look pretty clear. Also, you can get
the different result if you change the
threshold value. Suppose if I reduce the
threshold value 255-175, then it's going to return
a different result. Don't worry. We are going to learn more about thresholding in our
upcoming tutorial. So thanks for
watching this video, Statute for the next tutorialm. In the next tutorial,
we are going to talk about color spaces.
12. Color Spaces: Hello, guys, good
to see you back. Once again, I am back with another tutorial
related open CV. And in this tutorial payer
we will learn color spaces. Color spaces are basically
space of colored. RGB is a color space, gray scale is a color space. We have also different
color spaces such as HSV, AAD and Mini. Now, let's start with to try to convert the
image with gray scale. So we are going to convert
GR image to grayscale image. As you know, Bier is the
default color mode in open C. For that, I'm going to
declare a variable gray, gray equal to assigned, CVI dot, I'm going to use a method
called CVD color, CVT color. Then inside the rounds, first, we need to pass the source
image which is IMG, then we need to pass the color. Smit color. Underscore, and I want
to convert BGR to gray, this one, and I'm going
to sub this file. Then I'm going to display
this image using Imho method. Here tight, Cm dot, IMO. Inside the round brcess first, I'm going to pass the
window name which is gray. Next, we need to pass
the source image, which is gray variable. And I want to set this
file. After set this file, if I run this code
in my terminal, as you can see, this is our original image and this
is our grayscale image. Here we convert BGR image
into grayscale image. Now I'm going to convert
BGR to HSV format. HSV stands for hue saturation W. First, I'm going to type a coman BGR HsVT apply the HSV color and want to copy the same codo. And based it here. First, I'm going to change
the variable name grade two HsV. Then I'm going to
change Bijiac two HSV. This one. Also, I'm going
to change the window name, replace gray with HSV
and variable length. I want to satisfy.
After satisfy, if I ra this code
in my terminal, let's see what it return. This is the gsV color
mode of this image. Now, let's talk about
another type of color space, which is AAB. Here I'm going to type another
single command, hashtag. This time, I'm going
to convert VGR to LAB. So again, I'm going to copy this section and I'm
going to paste it here, and this time, I'm going
to change HSB to AB. Also, I'm going to change
the color code, jia to LAB. Then we need to change
the window name, LAB, also in to change
the variable length. After set this file, if I run
this code in my terminal, let's see what it return. This is the LAV color
mode of this image. This is how it slow. As you know, by default, OpenCV use gR format. But outside of OpenC
we use RGB format, which is opposite of BGR. It's a inverse color
mode of BGR format. Now I want to open this
image in RGB mode. I'm going to open this image
outside of the open C. First, I'm going to
comment out on the lines, then I'm going to
import Moni mat leave, some type, import, Mt leave. This one, He call a particular
function Pi plot a plot. If you don't install this
module in your system, then just open your
Windows Power shin and tie PIP install Matt
Blot and Tick Enter. It's going to install
it in your system. I already installed it, so I'm
not going to install it E. So mad Blot leap, read the image in AGM. But have we opened
this image using open C. I read the image in BGM. Lets open this image
using Mt Blot leap. He type PLT, dot, I would run IMS
function, I am so. Then inside the runres
I'm going to pass the source image IMG,
image, this one. Then I'm going to
display this image using PLT dot SFunction. Let's certify and runs
code in our terminal. Appear on this code
in our terminal, this is the BGiR mode of this image and this is the
RGB mode of this image. Basically, it apply
opposite color of this original image. This is BGiRimage and
this is RGB image. Open read this image
as Bzier mode and by default Mtlot so the
image in RGB mode. That's why Matt Plotly
thinks is an RGB color. That's why we can see the
opposite version of this color. Now, let me show you something. I'm going to delete this window and comment out both the lines. Let me show you something. I comment out both this
line and this time, I'm going to convert
BGA to RGB colored. Here type of comment
BGR two RGB. So first, I'm going to
declare a variable RGB equal to CV dot CVD color, CVT color. Then is that the
round res is image IMG, comma vt color. Underscore. This time I want to
convert BGR to RGB. This one. Then I'm going
to display this image. Hemotype CV that I am show. Is the roundreses first, we need to pass
the display name, which is RGB, then we need to pass the variable
name RGB. That's it. Now, let's run the code
and see what it returns. Up run this code, also, you can see it written a
RGB version of this image. It is now it looks
pretty similar to matt because here we
convert VGA to RGB image. But if I run this to code, copy this to code
and paste it up that and comment out and
uncomment both the line, and this time as variable, I'm going to pass RGB not image and set this file
and run this code again. Now you can notice this time metap return the
actual original image, which is similar
with cat window. This is our original BGR image. Now, our RGB image
and BGR image look same because we convert BGR image into RGB
image using OpenCeM. Then I pass this image in image to function
using matplot leave. That's why it
returned this result. Just one thing you
need to remember met plot leave default color code is RGB and OpenCV default
color code is GR. So now you can see Harry convert
Bzierimage to greyscale, Bzierimage to HSV,
Bier image to LAB. But there is a downside
on Open C. You cannot convert grayscale
image into HSV. Also you cannot convert
HSV to LAB directly. If you want to do that, first, you need to convert
disk image into BGR, then you can change
BGR image into HSV. I'm going to convert
HSV to BGR image. Here type is single and command
and I type this time I'm going to convert H a to BGR. And first, I'm
going to declare a variable HHV underscore, GR. These are variable and
this time I'm going to run C dot, CVT color. Inset this function, I'm
going to pass at first, I'm going to pass the
source image, which is HSV. I'm going to uncomment
this section. And this time I'm going
to pass HSV, this one. The next, I'm going
to pass the color, which is CV dot, color, underscore and this time
I'm going to convert Hs V to BGR, this one, I'm going to
display this image. So type cv dot No
inside the run verses, inside the single codes, first I'm going to provide
a name which is HSV to BGR. Then I'm going to
provide the variable. Before I set this
file I run this code, I'm going to comment out
all the image functions. I want to show the original one. Also, I don't know what
to show these ones. I also found out
these two lines. Uptrastep this file, let's sun the code and see
what it returned. Up step this file, you
can see the result. This is our original image, and this is our
HSV two videoing. For better understand, let
me show you the HSV image. If I show the HSE image, and I'm going to show this file. After I run this
code, as you can see, this is the original cat
image, I don't need it. This is the HSV
version of this color, and we convert HSV image
into BjerimageEgy. But we cannot convert HSV image directly
into grayscale image. So this is it for this tutorial. In this tutorial,
I tried to cover some little color spaces
concept in OpenCV. So thanks for watching this video Stune for
the next tutorial.
13. Color Channels: Hello, guys. It's
good to see you back. Once again, I'm back with another tutorial
real tate O pensimi. And in this tutorial, we are going to learn
color channels. In this tutorial,
especially we are going to learn how we can
spit color channels, and also we are
going to learn how we can merge color channels. A color image basically
consists multiple channels, red, blue and green. All the images you see in a digital media made with
three colored channel, red, blue and green. So image are made with the
three colored channel, and OpenCV allow us to
spit the colored channels. As you can see in my
current working directory, we have an image, PIC dot JPG. If we open this image, as you can see, we have
to do three subtle, three surgen with three
different colors, red colored, green
colored, blue colored. This is a single image, and OpenCV can fit the
three colored channels, blue, green and red. So that's we are going
to do in this video. Besidally we are going
to use this image and split this image into
three color channel. So for the three color channel, I'm going to take
three variables, B, G R equal to, I'm going to speed
the color channels. For that, I'm going
to type Cv dot, I'm going to run spit function, Cv dot, spit function, speed. And inside the round press, I'm going to pass the image IMG. This split function
going to split the image into blue,
green and red color. But at first, quits convert this image into
the grayscale image. And if you want to show this
image in a window for that, I'm going to use, we can
use Image show function, so type cv dot IM show. Then the round presses first, we need to pass a window name, at first I want to
show the blue color, so I want to type
Window name blue. C, then I'm going to pass
the variable length B. Then I'm going to begin
this line two t. Next, I want to show the green color, and for the green color, I'm going to pass G variable, and last one is for red
color for the red color, I'm going to pass R variable. I'm going to stifle. Before I run this quote, let me
tell you something. Basically is this
function going to convert this image
into Griskal image. Then according to the
color channel value, we can see the dark and
bright part. Let me show you. After set this file, if I run
this code in my terminal, here you can see three razor. Now it's look pretty confusing. This is the original image. As you can see in
the original image, we can see three of the
color red, green, and blue. But if I show you
the blue window, here you can see now blue
color become white color. Now you can notice
the blue window and the blue circles
become white. Now you mi have cooten
why it's become white, and other two
colors become lack. As I told you when Opensi found the blue color
at that image, then it increased the
intensity of this color. And as I told you, si function convert the image
into grayscale image. And whenever we run blue color channel in
this gray scale image, then it highnt and represent the blue color with white color. And as you can see, this
is the exact blue color. That's why it become white
colored, exact white color. OpensNi did not found any
blue color on this image. I do not found any blue
color at green color circle. Also you do not found the blue
color at red color circle. That's why it become black. At the same way, when we
run the red color channel, this time, red color
become white colored. As you can see, now red circle turn into
white circle because this time it hich
the red color and increase the intensity
of this circle. At the same way, green
colored channel work. This time it increase the
green color intensity. You can do the same experiment
with any colorful image. But for your convenience and
to understand how it's work, we use this circle, three different color circle,
red, green, and blue. And then groove circle, there is no green color available. That's why it become
black colored. If any of green color percentage is available at that portion, then it become a little lighter. And remember, the original image come with three
colored channel and the green window radio
window and blue window come with zero color channel.
Let me prove you that. If I delete this window
and print the shape, let me show you type print
inside the round ss, I'm going to type image,
image variable shape. I'm going to get this line. And this time, I
want to print one of the color channel
low, suppose grey. I tie G. If I set this file
and run this code to A, now you can see in my terminal, first we print the dimension 300 by 500 and then we
print the color channel. In our first image, I want
to say the original image, we have to draw
three color channel. But in the next image, which is green window, we don't have any color channel. By default, we can call it
one color channel value, but basically, black and
white are not colors. Basically, in this
window, black and white represent the particular
color intensity. And now I'm going to
comment out both the lines, and I'm going to merge
this three image. But before I'm going
to comment out this line, the original image. I don't want to this window, and to merge the three
curve channel image, I'm going to use merge function. First, I'm going to
declare a variable and our variable name is merge. Merge equal to Is the rounds Hem to coll a
function called Marge. Sometime v dot merge. Is the round brass inside the square dresses
I'm going to pass. Herem to pass list of
three con channel, B, G R. And then I'm going
to display this variable. For that, again, we need
to run civet IMF function. In the roundress is first going to pass the
window name which is merged. Copy the variable. This is the merged window and I'm
going to pass the coma, and I'm going to pass
the variable in merged. And I'm going to set this
file. After I set this file, at first, it got for this
three color channel window. Then it got for
the merged window. Let me show you. So
after run the code, you can see, first it print. At first, it displayed
the blue colored channel, then red, then green. And at last, it display
the merged color channel. And after merged the
three colored channel, again, it return
a colorful image. So this is for this tutorial. In the next tutorial, we're going to learn about blurring. So thanks for
watching this video stadium for the next tutorial.
14. Blurring: Hello, guys, good
to see you back. Once again, I'm back
with another tutorial related Open C. And
in this tutorial, we are going to learn blood. We are going to learn
different method of blurring. Generally, we smooth and blur the image when we have
noise in our image, and applying some blur, we can reduce the noise. In our previous tutorials, we already learn
about gaussian blur, which is the most popular
method for blurring. Without Gersion blur, there
are many blurring techniques, and we are going to
learn all of it. But before we start
our practical, we need to understand something. We need to understand
what is blurry. How actually blurring work. Here I'm going to open an image. This is our image. What exactly goes on when we apply blur. At first, we need to define
something called kernel. Otherwise, you can call it indo. So here at that position, I'm going to draw a window. Suppose this is our window size. Here we draw a window over the image at a
specific position. As you can see, this
window has a size, and it is known as kernel size. And kernel size
basically number of rows and number of columns. Here I'm going to draw three row and also I'm going
to draw three columns. Cerdles made with row
and columns or pixels. These are all pixels. So here we have total three
column and three row, so its kernel size
is three by three. Essentially, what happened? We have multiple
method to apply blur. Blurred applied to
this middle pixel, and this pixel is applied to
result fraud rounded pixels. It apply the surrounding pixel
result at the middle one. Let's discuss the first method of blurring, which is averaging. Let's get to the
users to your code. And here, I'm going to type a single line command hasta and I'm going
to type average. Now the question is how
averaging blur is work. If I open this image
again, as I told you, b always apply to
the middle pixel and averaging blur work according to the surrounding pixel average. Suppose this pixel
intensity value is two. This one is five. This one is seven.
This one is three. This one is four. This one is two again. This one is eight, and this one is nine. As you, middle pixel
intensity is four. Now, it's going to extract all the pixel intensity
value and apply this value and apply this
value to the middle pixel. This is how averaging work. Basically you want
to say surrounding pixel average going to be
applied to the middle pix. Let's back the
users to your code. To apply the blur, first, I'm going to declare a variable and our variable is average. Average equal to CV
dot Blur method. Then instead the run pass first, we need to pass the source, the source image, which is ING. And then we need to
define the kernel size. And for kernel size, I'm going to take three
column and three row asi. And we can display this
image in our window, so I'm going to run CV dot If Mthood inside the roundre
inside the single code, first, I'm going to
provide a window name which is average, comma, and then I'm going
to pass the variable, which is also avg. If I sat this file and run
this code in my terminal, as you can see, it
both the image. This is the original cat image, and this is the
average blurred image. If I open it side by side, you can see the
little difference. It is look pretty sharp, but this one is not sharp because here we
apply average blur. And if you want to increase
the blur intensity, then just to pass
higher kernel value. Let me show. So without using
three by three kernel size, I'm here to pass seven by seven and set this file
and run this code again. Up to run this code, you can
understand the difference. Up to increase the kernel value, it also increase the
blurriness of this image. Now let's apply another
blur, which is Gasianblur. Delete both the windows and here type another single
comment Gersionblur. To apply the Gersionblur, herb to declare
another variable guess equal to Cm dot Gersion. Is the round presses, first we to pass
the source image, which is ING then
we need to provide the kernel size and here I'm
going to apply seven Casern. Also to pass Sigma X. It is used for X
dictation deviation. For now, I'm going
to set in zero, and I'm going to
show this image on a window so I'm going
to copy the on from Mthod and past it here. I'm going to rename
Average with Gertian. Also change the
variable name GUS. If I set this file and run
this code in my terminal, now you can compare
both the blood. This is average blur
and this is Gasienbld. But you can notice here we use same kernel size seven by seven. But in average, it
applied more blood, but in Gasian it
look natural blood. That's why most of
the time we use Gersienbl because it
looks more natural. Then we are going to apply
another blood. Let me show. Or next name is median blood. Hemotype is single and
command median blood. Then Hembo declare a median. Median equal to C
dot median blood. Then you set the
rounds first L to pass the source
image which is IMG. Instead of pass
full kernel size, here to pass one intser. Suppose I'm going to pass three. Now open C we can assume that its kernel size
is three by three. Now, let's display the
image in a window, so I'm going to
copy the Imro map hood and I'm going
to paste it here. First, I'm going to
replace the window name. I'm going to make it median. Also, I'm going to replace
the variable length. Medium, set this file. After set this file, if I run
this code in my terminal, now you can compare
both the blur. This is the normal image. This is the median blur, and this is the Gasionblur
and this is the average blur. Maybe you cannot understand the blurriness if I increase
the value, let me show you. I'm going to did all the window and increase kernel size value. I'm going to make it nine and then set this file again
and run this code again. Now you can see the
median blur effect. If you compare the blur effect
with other blur effects, it look like Oil t. Now, let's apply
the st blur effect, which is bilateral blurring. So here, I'm going
to create a single and comment and I will type. Bilateral blur. Bilateral blur is not a
blur. It is a filter. It is highly effective in noise removal while
keeping edges sharp. But this operation is slower than if I compare
to other filters. Using bilateral blur,
you can your image. Also you can maintain
the sharpness of edges. Here, I'm going to declare a
variable called bilateral, so I copy this name and I'm
going to declare a variable bilateral equal to Here
called the bilateral filter. Some type CV dot
bilateral fitter. Then you say the rundresssF we need to pass the source
image which is IMG variable, then we need to
pass the dilateral in and I'm going to
set the value five. Next, we need to pass
this Sigma color. I'm going to set it 154 now. The next value, we need
to pass Sigma space. Also I'm going to pass it and
also I want to set it 15. Now, let's display the image. I'm going to copy the iron from Math wood and I'm going
to paste it here. First, I'm going to
replace the Window name, Blteral then I'm going to
paste the very well name, Blteral I'm going
to set this file. After set this file, if I on
this code in my terminal, here you can see DE result. This is bilteralblA. I think it is pretty
similar to our genome. Let's increase the
value and make it nine. And also, I'm going to tell
the Sigma values 75 and 75. After set this file, if I have the scorde
in my terminal, now let's see what it written. Now you can compare it
with the original image. This is how bilateral
filter work. This is the original image, and this is the
bilateral filter blur. So these are the four type of blurs we can apply in OpenCV. I hope now it's
clear for you how we can apply different
blur in open CV. So thanks for watching
this video Studio for the next tutorial.
15. BITWISE operations: Hello, guys. Good
to see you back. Once again, I'm back with a
new tutorial related OpenCV. And in this tutorial, we're going to learn
Batis operations. In OpenCV, there are
four Biti operator. First one is N XR and the
last operator is naught. In OpenCV, Bitois
operator are used for pixel level operation between two image or between
an image and a mask. These operations are useful
for task like blending, masking and manipulation
specific part of an image, and many more. As I told you, we have to
tell for BTI operator. First, I'm going to type a
single lend command here. No, I'm going to use multi
length comen for that. Then in the multiline command, I'm going to paste all
the operator's name and what purpose we use for. As I told you, this is
the first operator, BTI. It applies a logical
operation to each pixel of two images
or an image and a mask. Next repertor we have BTs or it applies a
logical operation. Then we have bitwis ZR. It applies a logical
ZR operation last word is bitwise no. Basically, it invert the pixels. You can compare it with
logical node operator. Remember, bitwise operator
working in a binary mender. If we pass zero value, then it's going to turn of the pixel and if
we pass one value, then it will turn on the pixel. I represent it white
and black color. Now let's import another
module, num pipe, Hemotpimport, numb Pi as NP. Then I'm going to take
a variable called Blend and I would set it NP dot zero
is at the RunssF, we need to provide the
shape of this window. I'm going to take
here I'm going to set window size 400 400. Also, we need to
provide the data type. D type equals two is at a
single codes INT eight. Basically, her create
a blank black image and we say height
400 and width 400. Basically, we are going to
use this blank image as basis to draw
rectangle and circle. Next, I'm going to
create a rectangle in this blank image. I'm going to type a
variable RECt assigned with cv dot rectangle
Method. Rectangle. Then inside the
round ress first, we need to pass the image in which image we will used
to create the rectangle. I want to use blank dot copy. I'm going to take copy
version of this blank image. Then we need to
define the margin. We need to define the
margin from Xs and Y xs. I'm going to take it 30 30. Then we need to define the
size of the rectangle. I'm going to take it
370 pixel by 370 pixel, then we need to provide color. As you know, it's
a binary image, so I'm not going to use
three color channels, so I'm going to provide
255, just white color. And at last, we need
to pass the thickness. So I'm going to
pass negative minus one because it will see this
image with white color. After I create the rectangle, we need to create
another circle. So I'm going to create
circle variable, circle equal to
CV, circle metal. Then inside the roundresses, first, we need to
define the image. In which image I want to create
this su type bland, dot, copy, then then we need to define the center
position of this circle. I'm going to pass 200 by 200. Ma, then I'm going to provide
radius to this circle, which is 200 and also to provide the color so I'm going
to use single colored, so I want to pass 255,
fully white colored. After that, we need to pass
the thickness of this circle, so I'm going to
pass minus one to fill the circle
with white colored. Then we need to display both the geometrical shape
in different windows. He run, C, dot, IN so method, No inside the round br
inside the single first, I want to display the
rectangle, so tie rectangle. Then we need to provide the
variable name, which is rec. Next, I want to
display the circle. I'm going to replace
rectangle with circle and also we need to replace the
variable length circle. I'm going to set this file. Now, after set the file, let's run the code and
see what it will return. After I run this
code in my terminal, you can see, here we have the two image that
I'm going to work with. It is a rectangle
and it's a circle. Now let's start with the
first basic Btis operator. So let's start with our
first Bitwise operator, which is some creative
single and common Bitwise A. Before we discuss about
bitwis and operator, let me show you what it does. So at first, I'm
going to declare a variable bitwise a
Bowie, underscore, N N, and equal to C dot, bitwise and operator. Bois and then then inside
the round presses, need to pass to source. So as source, I'm going
to pass the rectangle DC and as source too, we are going to pass circle. Oops, circle spelling is wrong, so I'm going to
make it correct CR. And now I'm going to show this result in
a different Window. Here I'm going to run CV, dot, on for method, M show. Then inside the round presses, first, we to pass
the window name, which is Bitwise A. I copy the variable name as it
is and paste it here. Then also to provide
the source image, which is bitwise
N, this variable. I want to set this file.
After set this file, I'm going to run the
code in my terminal. A unicode, as you can see, this is Bitois and
operator result. Bitwise operator Bitwise
and operator took this two image and placed it top each other and return
the intersect reason. If I move this circle
above the rectangle, then return the
common intersect area of the combined shape, and it represents the
common reason area with white colour and remove
the other section. Now, let's jump into the
bitwise or operator. Here I'm going to
comment out this slide. Then I'm going to type another single and
command and I'm going to type bitwise or
operator or basically, bitwis orbor return
the intersecting area it return the non
intersecting area. Let me show you that. Again, I'm going to type
a variable bitwise or Bitwis equal to c dot
bitwis or operator. Then it's at the roundres I'm going to pass the rectangle. Then as source to, I'm going to pass the circle. If I display this
image in a window, so I'm type C dot and informo. Then at the
roundressF we provide a window name which is BTI R. Then we need to provide the
source, the source image. After I set this file, if I
don't code in my terminal, now you can notice,
as I told you, it returned the
intersection area, also it return non
intersection area. It combined the
common result with non common reason and
create a new shape. Now moving on to the
next Bitwie operator. For that, I'm going to write
another single end command, but before I'm going to
comment out this line and here I'm going to create another
single endm bitwise. I'm going to type bitwise
undersco Z operator, Bitwise and this
operator name is O Zo. Let's see what it going to do. I create another variable, BitwisUnderscoe, Zo equal to. Basically, it's going to return the non intersected reason. Let me show you Sol type, CV, dot, Bis, Zr. Is the Runbss at first, I'm going to pass the rectangle. Then I'm going to
pass the circle. And I'm going to
display this image. So type, CV dot, I am fmthod. I am show. Then instead the undress, first, I'm going to provide
a window name as window name and pass the same
variable name, BTI zone. Then we need to pass
the variable BTI zone. Now, let's set the
file and run this code in our terminal and
see what it return. Up to this code in my terminal, as you can see, as I told you, it's going to return the
non intersected reason. If we put this image
top of each other, it's going to create
intersecting reason and non intersecting reason and Za return the non
intersecting reason. These operators are
very useful for STI. Now, let's talk about the last Bitwis operator,
which is bitwisnO. So here, I'm going to type
another single end comment and I'm going to
type bitwisPtisn. Basically, it does
not return anything, it inverts the binary color. It turned white into black and black into white. Let
me prove you that. So here, I'm going to
declare a variable called beat is underscope naught. Bitwise naught equal to Civ
thought, bitwise, naught. Then inside these
round dresses for now, I'm going to pass this circle. To show the example,
we don't need to combine both the
geometrical shape. Just we need only one
geometrical shape. In our case, I'm going
to use this circle and then C thought I'm from Matin. Then inside the round dresses, first, we need to
provide the window nape. I'm going to copy the
same variable leap as Window nip and then we need
to provide the variable. And I'm going to set this file. After I set this file, also, I'm going to comment
out this line, I don't need this line for now. I don't know how
to show this one. So after set this file, if I run this code
in my terminal, as you can see, this
is Btis not operator. As I told you, it
inverse the color. Not inverse the color. Basically, it turn zero
into one and one into zero. That's why our something
look black and our outside the circle area and
the window area look white. This is it for this structurim. I hope you understand
what is the usage of BTs operators and
how we can use it. It is very essential to know these operators are very
important when we work with us. Don't worry, we
are going to learn about it in our
project sections. So thanks for
watching this video. Stay tuned for the next tudio.
16. Masking: Hello, guys. Good
to see you back. Once again, I am back
with another tutorial, related Python open CV. And in this tutorial, we are going to
talk about masking. In our previous tutorial, we discussed about
BTIs operator, and essentially using BTI
operator, we perform Musk. In a simple sentence, muskin is used to focus on specified reason of an image while ignoring other positions. In open semi, muskin
is used to focus on specified reason of an image
while ignoring others, and this is achieved by
creating a binary mask image where the other results of intersets are white and
the rest are black. As you know, white
represent with 255 and black
represent with zero. And the mask can be applied to the original image to filter or manipulate the desired areas. Suppose if you have image of people and you want to
focus on only their faces, in that case, then
you can apply mask on their faces and hide
unnecessary areas. Now let's see how
actually it worked in open C. As you can see, here I read a file and
display this image. And next, I'm going
to import numbi moding because we are going to create a blank image using it. So I'm o type, import numPi as N P. Then I'm going
to create a blank image. So here I'm going to
declare a variable blank, an equal to Np dot zero. Then inside the round
process to provide the shape to grab the shape, am type image, dot, shape. Then inside the square resis, we need to provide the
dimension of the shape. Remember, the dimension of
mask is very important. We need to take the same
size of this image. Here I'm going to pass colon
two and also we need to take the datatype for
Tata type D type. Is single at Imo type I NDA. And if you want to
display this blank image, you can just into type
Cm dot, and for method. Then inside the round is
hero type black and coma, and I'm going to pass
the variable black. I will satisfy. Now I'm going to draw a circle on the blank image and I'm
going to call the mask. Here, I'm going to
create a variable called mask mask equal to mask, equal to C dot subcal. Inside the round recess, first, we need to pass the image where we are going to create
and draw the mask. So pass black. Then we need to pass
the center position, and to grab the center to define the center
position, her type, IMG image, dot, shape, Is the square recess, one divided, divided by two. And next, image, dot, shape, is a square is zero
divided, divided by two. And now also we need to pass the radius for
radius for now, I'm going to take 100 pixel, and then we need to pass
the color and I want to 55 ful white color as thickness equal to minus one to fill the
circle with white color. And now we need to
display this mask on image for that helm type. C V dot, I show then in such roundreses at
first to provide a window name, which is mask. And then we need to
provide the variable mask. In which image we want to
create a mask, this one. And if you under scot, it's going to display
tal three image. It's going to open total three
window. Let me show you. First, it's going to
open the cat image, then it's going to
open the blank image, and this is our mask image. And now I'm going to use
Bitwise operator to mask it. For that, I'm going to create
another variable mask equal to CV dot bitwise and,
Bitwise operator. Then inside the
round resist, first, I am going to pass image
as source, IMG Coma, image as second source, then we need to type mask mask equal to I'm going to pass this
variable called mask. So using bitwise and operator, we apply this circle
mask on this image. So if I display this image, so I'm going to type Ammtho
copy this line and paste it here and replace mask with mask. They set this file and
then code in my terminal. Let's see what it reted. As you can see, this
is what it reted. This is our mask image. After I applied the mask,
this is how it slow. At first, we took this image, then we put this image over this original image
and create a mask. Basically bits and operator, we did the intersecting
result of this area. If you want to move this
mass, yes, you can, let me show you how you need
to change the coordinates. So here, I'm going to
uncoven unnecessary lines. I don't need it, so I comment
out all of these and here, a divided by two, I want to move the mask, so I want to add
fortified pixel. Here I add fortified in
with these coordinates. After set this file
and on this code, you can see, we
move the mask area, we move the mask position. Instead of using circle, you can use a rectangle off. Just use it to replace
circle with a rectangle. Just one thing you need to
remember to applying mask. You need to take the actual size of image for black image. If you use any other dimension, suppose greater than the
other bower than this, then it going to throw arrow. Using image shape, here we grab both the varies of
image resolution. And if we change it, then
it will to throw error. Let me show you the example. I want to duplicate
slide and comment out previous one here instead of using the exact
shape of this image, I'm going to take 300 by 300. If I save it this
code turn in no, as you can see, it's 30. It's not going to work
because we need to take actual shape of
image to create the mask. I'm going to comment out
this line and uncomment the previous line and set this file and
run this code again. Now you can see it
return the result. This is how we can apply mask. I hope now it's clear for you. Thanks for watching this video statute for the next tutorial.
17. Grey scale image histogram opencv: Hello, guys. Good
to see you back. Once again, I'm back
with a new tutorial related Python open CB
and in this tutorial, we are going to learn histogram. Now the question is,
what is histogram? A histogram is a chart that
plots the distribution of numeric variable value
as a series of bars. We are going to understand it when we start our practical. As you can see, here I open a Python file man dot Pi
and I import a image, and I display this image
using Imghofunction. Histogram basically
allow you to visualize the distribution of pixel
intensity in an image. Whether it's a colored image or whether it's a
grayscale image, you can visualize
the pixel intensity. You can understand pixel
intensity distribution with the help of histogram. Which is like a grab or a plot. That will give you high
level intuition of the pixel distribution
in the image. We can compute a histogram for Griske image and compute
histogram for RGB images. We are going to start off with computing histogram
for Griskalimages. At first, I'm going to convert this image into grayscale image. For that, I'm going to
declare a variable called gray gray equal to Cv dot CVT Color in the round dresses I
passimge IMG Image variable, comma, and then I'm
going to convert this ZR image into Grisker
image, summary use CV, dot, color, underscore
BGR, to gray. This one. Then I'm going to show this image in our window. I type CV. I'm going to type CV, dot, IMs method, IM so. Then you said the
round dresses first I'm going to provide a
display name which is gray. Then I pass coma and here I'm going to
pass the variable, gray. Now, to actually compute
the grascal histogram, what we need to do
essentially, first, I'm going to declare
a variable and our variable is gray,
underscore hist, gray histogram,
equal to CV taught, here I'm going to use a method, which is calc hist, calc hist, this one. Then inside the run recess, here we need to compute
the historum for the image that we pass into. And here we are only interested
to pass a grayscal image. I copy this variable
gray and put it here. Apacoma, then we need to
pass the color channel, and inside the square recess, I'm going to pass
zero because we are using grayscale image,
not colorful image. Now the next thing we have to do we need to provide a mask. So here, if you want to get histogram specific
position of an image, then you can specify
the mask position. But for now, I'm going to use the whole image, so
I'm going to pass none. And next, we need to
provide the t size and size is basically number of bits that we want to use for
computing the histogram. When we talk about
plot histogram, we need to understand
the concept of B. But essentially for now, just set this two, five, six. Also we need to wrap
this value as a list. The next thing, we
need to specify the range of all
possible values, all possible pixel values. For our case, inside the
square se I'm going to pass from zero comma to five, six, and that's it. Now I'm going to import
a module, matplot leap. Here, I'm going to import
matplot leap as PLT. So type import Mc
plot leave as PLT. Basically from this module, I want to import Pi plot leave. Here I'm going to pass
McloteaptPi plot leave Pitht then I'm going to
type PLT dot figure. I call this method, and next I'm going to
type PLT dot title. Then in su roundre we need to set a level and
our level is gray scale. It is a gray scale histogram. I'm going to type
grey scale histogram. Then I'm going to set X label. So type PLT, dot, X level. Inside the rounders, I'm
going to call these bins. Next, we need to
set the Y levels. So type PLT, dot Y level. Here we need to pass
number of pixel of pixel. Finally, we need to use
the plot PLT, dot plot. And inside the round recess, I'm going to pass the
very well called gray scale histograph gray hist. Also, we need to set the
limit cross of the xs. I'm going to type PLT line. Inside the round recess, I'm going to pass the
list from 022 56. Finally, we can
display this image. Here I'm going to type
PLT dot show Method. So I'm going to set this file. After I set this
file, I'm going to run this code in my terminal. Up to run this code
in my terminal, this is the image of histogram. It is the gray scale
histogram image of this cat. Basically, this is
the distribution of pixel in this image. As you can see, the
number of beans across the Xxs represent the intervals
of pixel intensities. So so as you can see, there is a peak
at that position. This means that is
close to 125, 125 bins. It's mean in this image
there are close to 9,000 plus pixels that have
intensity of 125. Basically, I want to
say at that position, pixel intensity
cross nearly 9,000. So this is essentially compute the gray scale histogram
for the entire image. Now what we can do is we can
essentially create a mask. Then we can compute
the histogram only the particular mask and
to create the mask, we need to import Numbi module. So here, I'm going to
type import Numbi as NP. To create the mask, here, I'm going to declare a
variable called blank, blank equal to Heuse a
method called np dot zero. Np dot zero inside
the roundreses here, we need to grab the image shape. Image dot shape and I want to grab to tell two values inside the square is colon two,
the first two value. It's going to create a
same size of blank image. Then we need to provide
the D type value. D type equal to inside the
single course UI NT eight. Then I'm going to create the
mask using the blank image. So here, I'm going to type mask assigned width equal
to CV dot circle. We are going to
create a circle mask. Inside the round presses to
draw this circle, first, we need to provide the
image, which is black. Then we need to define
the center position to define the center position
inside the round presses. He to use image dot shape. Inside the square dresses I'm
going to pass first value and it's divided with
divided divided by two, we need to grab the vertically and horizontally
center position. Egomotypeimage, dot, shape, Is that the
square adress says, I'm going to pass zero
divided, divided two. This calculation
going to provide the center position
of this image. Then we need to provide
the radius of this circle, which is 100 pixel. Next, we need to
provide the color. For color, I'm going to
use 255 white color, single white colored and also
I want to fill this circle. I pass minus one vu. This is the thickness value. If we pass minus one, it's going to fill the circle
with white colored. And then I'm going to show
this mask in N window. So I'm going to use Andromthod v dot IM and set the RunressF I'm going to
declare a window name mask, mask, and I'm going to
pass the variable mask. Now I'm going to create the gray scale histoum
for this mask. I'm going to set this parameter
mask parameter as mask. Instead of using
nun Hemotype mask. But before I run this code, I think I did some mistake. It's not a mask, it's a circle. It is a circle. He create a circle. Here we
create only this circle. We do not apply
mask on this image. So to create the mask we need
to use Bitwise operator. It is a very sllistic. So declared mask equal to CV dot and to use Bie
endoperator bitwise and in said the rounds, first, I want to select
the greyscalimage, this source as a source,
gray as second source, also multi pass gray. And we need to
have to pass mask, mask value equal
to circle, circle. Here we apply this circle on the grayscale image as a mask. I'm going to set this file and run this code in my terminal. Up to run the scude
in my terminal, here you can see the result. This is the mask area. And this is the histogram
result of this mask image. Now you can notice there
are a lot of spike on this histogram image because
most of the areas are white. That's why pixel intensity
is high on this image. As you can notice at
30 Bins position, we have 500 pixel intensity. This is how we can compute the
grayscale image histogram. At the next tutorial, we will learn how we can use
colorful image histogram. This is it for this tutorial. Thanks for watching this video, stay tuned for the
next tutorial.
18. Colour image histogram: Hello, guys. Good
to see you back. Once again, I'm back with another tutorial
related histogram, and in this tutorial, we are going to learn how we can extract Cana image
histogram graph. So as you can see, I already input our important libraries, CV two and Mt plot leave. Then I already read the image file using
image it function. Also, I display this orgenic
image using IM So Method. Next, we need to create
a new figure window for the histogram plot. For that, I'm going to
type PLT dot figure, and then we need to
pass the round dress. After that, we need to set
the title of histogram plot. For that, I'm going to
type PLT dot title. Inside the round res, I'm
going to pass the title nm and the title name is
color histogram. Next, we need to take the
X level name for that, I'm going to type
PLT dot X level. Inside the round dresses, here I'm going to take beans. Level the Xxs as beams, where each been represent a
color intensity value 02-55. At the same way, I'm
going to take Y level. I duplicate this
line and replace X with Y here I'm going to type number of pixel,
number of pixels. The Y xs represent the
color count of pixel for each intensity
level in the histogram. Up to define title
levels and figure. Next, we need to create a tuple. Here I'm going to
tie a variable name and our variable name is color. Colors equal to, as I told you, it's a tupel so I'm going to use round briss side the round bris here I'm going to take
total three tape value. Our first value
is B, B for blue. Next, I'm going to
take G. G for green, and our last one is R, R for red, R represent red. As you know, default opens
V images in RGB mode. Here we define tape
with color channel labels to indicate which color
channel is being processed. After that, here,
I'm going to take a follow flop to iterate
all of the color channel. I'm going to type four, I comma call, call
stands for color. And I stands for index in enumerate inside
the rounds colors. Then inside this fall loop, I'm going to create the
histogram, the histogram image. Hemo type a variable
named HIST hist equal to CV I'm going to use a method called calc
hit, calc hist. Then inside the rounds, we need to pass a list, and as a first list item, I'm going to pass the sequence, which is image IMG. This variable. Then we need to pass the channel,
the color channel. Which color channel
I want to target? One day what I want to target
all of the color channel. I'm going to pass
the index number. Instead the square adresses
I'm going to pass I, then I'm going to pass the mask. For now, I'm not going
to apply any mask, so I'm going to pass none here. Next, we need to define the hist size and
for his size I'm going to pass inside
the square sus 256. Next, we need to specify the intensity range to
consider for the histogram. Here, inside the square ses, I'm going to pass 02-55. At first, we pass
the source image. Then we specify
the color channel, zero for blue, one for green, two for red, and I want
to target all of it, here I pass I. I is the index
number of color channel. I represent the index number and call represent the
color channel limb. Now, the hist variable stored the calculate
histoom values, which indicate the frequency of each pixel intensity
in that color channel. Now we need to plot
the histoom data for the current color channel. For that, herameertype,
PLT plot. Then inside the round presses, at first, we need to pass a arc, arc like here as an arc, I'm going to pass
Hite, this variable. Then we need to pass the color. Color equal to call variable. After that, we need to
set X excess limit from 022 56 to match the
intensity range. For that herramtygPLT,
dot lime, limit. Inside the round
press here to pass a list item from 022 56. Now outside the four loop, I'm going to display the
histogram plot using plt doth method. I'm
going to set this file. After set this file, if I
run code in my terminal, as you can see, it open our
histogram, color histogram. This is our original image. It's a colorful image, and as you can see
in this histogram, we have total three
color channel, red, blue and green. And if you notice each
of the color channel, suppose for red at
Essex win position, it touched the
intensity peak for blue at 110 point it
touched the peak. And at 155 XX position, you can see the green
color channel Peak. You can see the distribution of each color channel in this grab. This process is useful for analyzing the colored
distribution in an image and understand its
overall color composition. Basically in this tutorial, we learn how we can extract
colored image histogram, and in our previous tutorial, we learn how we can extract
grayscale image histogram. Thanks for watching this video, stay tuned for our
next tutorial.
19. Thresholding openCV: Hello, and welcome back to
another video related Opens. And in this video, we are going to talking about thresholding. Now the question is,
what is thresholding? Basically, thresholding is
a binarization of an image. In general, we're
going to take an image and convert it into
a binary image where zero represents
black color and 255 represent white color. Basically, we compare each pixel with the threshold value. If the pixel value is below
then threshold value, then we set it zero means black, and if it greater than, then
we set it 255 means white. In sense, we can
essentially create a binary image just from a
regular standalone image. And in this tutorial, we are basically talking about two different type
of thresholding, simple thresholding and
adapted thresholding. Let's start with
simple thresholding. At first, I'm going to type
a single leg command here. I'm going to type
simple threshold. Simple threfolding. Before I apply
simple threfolding, we need to convert this image, this colored image
into grayscale image. For that, here, I'm going
to declare a variable gray, assign with CV CT colored. This method. Then inside
the round versus at first, we need to pass the
source image which is IMG variable image, and then we need to
use the colored code. Cvt color, underscore
BGR two gray, this one. Then I'm going to
display this image. So of type, CV do I am
Somethor in the round brass, first, I'm going to provide
a window name which is gray, and then we need to
provide the image gray. Now to apply the
simple thresholding, we need to apply a function. We need to use a function
called Cv dot threshold, and this function
return threshold. And also it return Trash. Equal to, then we need to use
the method, vt threshold, threshold and inside the
round dresses, first, we need to pass the source
image which is gray, and next, we need to pass the threshold
wave, a floating point. For now, I'm going to pass 150. Next, we need to pass
the maximum value. If the pixel intensity
value is greater than 150, then I want to
binarize the image. If it cross the label, then I want to set it 255. I mean, fully white. Next, here we need to pass the thresholding type
for thresholding type, I'm type C dot binary. This one. Another
question is what it does. Basically it's going
to compare each of the pixel value with
this sash value. If it is greater than 150, then it's going to set it
255, meaningfully white. If it is lower than 150, then it's going to set it zero, which represent black color. Essentially it return
two things TAS, which is the Hefold image
or the binarized image. Also it return Tfold which is essentially same value
what we passed here. In our case, 150. Now, let's display
this image, so type, Su type dot and Mthods
the Rundverses at first, we need to provide the window name type, simple threshold, and then we need to pass the images this one now let's run the code in a terminal and C to
work propeller naught. As you can see, this
is our original image. This is the grayscale image. This is the griscal version
of this original image and this is the threshold image. This is essentially we get, if we say threshold value 150. Now, let's change
the threshold value. I did this terminal and
I'm going to change, I'm going to make it
100 threshold value. Also, I don't want to
show the original image. I comment out this line and also I don't want
to show the gray image. Also, I comment out this line. I'm going to sub this file. After set this file, if I run
this code in my terminal, now you can see this
is what it's written. If we say thrash value 100. Now, the pixel value above
the 100 become 255 and the pixel value below
the 100 become zero. Instead of passing higher value, if I pass lower value,
let me show you. If I pass 220. Then set this file and run
this code in my terminal, now you can see the
different result. Now you can see only two
eyeballs, nothing else. This is how you can
get different result if you pay with threshold. Now, let's inverse
this threshold of two. For now, I'm going
to make it for now, I'm going to make it
120 and set this file, then I duplicate this section. Then I'm going to change the even pra underscore
inverse, I NV. Also, we need to
change the method. Cv dot binary inverse, N. Also, I need to call this
variable Trash underscope NV and simple threshold NV. This is the window
name. Basically, it's going to inverse the image. Image pixels, let me show you. After set this file,
you can see the result. This is the original
simple threshold, and this is the threshold
image with inverse mod. As you can see it inverse
the pixel values. Now, white portion becomes black and black
portion become white. This is the inverse, the
version of this image. Now, instead of setting pixel
intensity which is greater than 120 into 255, it's going to set it zero. Because here we
inverse the image. We use thrash binary inverse. Basically, you want
to say which value is larger than 120, it going to set it zero and which value is lower than 120, it going to set it to 55. This is simple threshold. Now let's move to the
adaptive threshold. Here I'm going to type a
single encoman a stack, and I'm going to type
adaptive thresholding. In simple threshold process, we need to provide the
threshold volume manually. But in adoptive threshholding, we left the computer to find the optimal threshold
value by itself, and using that value
it binarize the image. And to contact the
adopting threshold, here, I'm going to declare
a variable called A underscoe A equal to test
for adoptive thrash, equal to Cv dot here, we need to use a method
called adaptive threshold. This one. Then then
inside the round ss, at first, we need to
pass the source image. Here I'm going to pass
gray, the gray image. Then also, we need to
say the maximum value. We don't need to set the
thrash value by manually. We need to say the
maximum value and I'm going to say 255. Next, we need to use the
method adaptive method, which method will apply
for the adaptive trash. I'm going to type v dot adaptive
trash main C, this one. Also, we need to
call threshold Ti, which is Cv dot
binary, this one, then we need to pass block
size for block size, I'm going to pass 11 at last, we need to pass CV and I'm
going to set Cvu three. Now, what does that mean? The function called
adoptive threshold is a thresholding method, which is very useful
for images with varying lighting condition
across the different area. If I talk about this parameter, C adoptive mean undersco C, this parameter define the method used to calculate the
threshold for each pixel. At the next parameter we
define the thresholding time. Here you can see her
we pass everywhere. It is for block size. I represent the size
of neighborhood area. Here 11 represent 11 by 11
block up pixel will be used. At last we pass three.
I represent weightage. It helps fine tune
the threshold. Method these methods helps
in handling images where lighting may very
significantly cross different areas as it
adapts to local variations. Now, go ahead and
display this image. Here I'm type Cb dot
I'm from method. And inside the rundresses I'm going to pass same
variable name as window name as adopted and comma I'm going
to pass the variable. Also I want to comment out
these two line, this one, and this If Math I'm going
to display the gray image. I'm going to set this
fine. Now, let's run the code in our terminal
and see what it return. This is what adoptive
Tash return. This image. Here we define the
kernel size 11 w 11. And OpenCV computes a main
over those neighborhoods. I want to say
neighborhood pixels, and it's find the
optimal threshold do for the specific part. I know it technically
sounds crazy and it's difficult to understand what is going on behind the scene. So this is how we can apply
adaptive threshold on image. Threshold going to
play a key role in our projects in our
upcoming projects. That's why we need to
understand how threshold works. So thanks for
watching this video, Syuned for our next tutorial.
20. Displaying the coordinates of the points clicked on the image using Python OpenCV: Hello, guys. Good
to see you back. Once again, I'm back with another exercise related Python open C. Today in this exercise, we are going to display
the coordinate point when I click on the image. Suppose if I press right click on this image
at that position, as you can see, it print the coordinates X XS
and YXS coordinates. And the same way, if I
praise right click on it, you can see also it
print the coordinates. Let's see how we can achieve this program using PyonopenC. So finally, we are in my
Visal studio code editor. OpenCV help us to control
and manage different type of mouse events and give us the flexibility
to operate there. There are many type
of mouse event. This event can be displayed
by the running this code. As you can see, Hey use a one liner loop for I
DIR function CV two. I eve P in. It's going to print all the
events from the OpenCV, from the open CVrator. Let's run the code
in my terminal and see how many evate we have. Up on this code, as you can see, we have multiple keyboard
and mouse event. Flack old key, control key, flag lip button, mouse button, R button, shift key, but we need to display the coordinates when I
click on this image. For that, we need lift button
down and write button down, R button down, this to event. Let's start the code. So first, I'm going to kill this terminal. Then I'm going to
comment on this line. Next, here, I'm going to Read and image using image
root function, IM read. At first, I'm going to declare a variable called image IMG, image equal to CV
two ImtFunction. Im read. This one. Then you said the round presses. At first, we need to provide the file path,
the image source. Is the single course, I'm going to pass
cat two dot JPG. With that, we need to
pass the color channel and our color channel mode
is BGR. I want to pass one. If I pass zero, then equal
to return grayscale image. Next, we need to display this
image using IMS function. He type, semi two dot Im
instead the run press, first, we need to provide a
window name which is image. Then we need to
provide the variable, the source image IMG. And now we need to set a
mouse called that function. We going to pass
the image window and use a defined
function as parameter. Here modo tie CV two dot
set mouse, call that. Then inside the rounds, we need to pass the image name, the image window name, not the variable and the
image window name is image. Is the single code,
Imo type image. Next, we need to pass a user defined function as parameter, and our function
name is click event. So type, click,
underscore key event. This function going to handle
all the mouse key events. Also it's going to
return the coordinates. Next, I'm going to
call the wet so type CV two dot, weit key. Wet key is a d zero. I'm not going to explain
you why we need to use Quit key because we already learn about it in
multiple videos. Also, I'm going to
destroy all the Windows. So type CV two dot,
destroy all Windows. Next, before I create
this function, I want to move all
the code inside the if named equal to
equal to mean function, which is our driver function. I'm going to type I
ndscoe underscoenme, undsco underscore, equal
to equal to inside the double codes
undersco underscoe M, undersco underscoe. Then inside this
driver function, I'm going to move on the code. We use if naan equal to equal
to main function because we are going to
call this function from the existing page. That's why we need to create it. Now I'm going to create
this function, click event. First, I'm going to copy the
function name, click event, and there so here T D our
function name, click event. Then inside the round presses, this function going to
take multiple argument. Our first argument is even. Our next argument is Xs. Then I'm going to pass Y. Also, it's going to take flag. And last one is pardon. Then inside this function, for now, I'm going to type pass. For now, I think I need to
print something in a terminal. So I'm going to type
print statement, print. Inside the process, I'm
going to type, it's work. Before I run this code, let me explain what we did. As you can see here we use
a function from OpenCV, cv two dot, set mouse callback. It's a mouse called
that function. Wherever I click this
window image window, then it going to call this
function, click event. And in our click event function, we just print its
work in our terminal. If I set this file and run
this code in my terminal, you can see the image. And whenever I click this image, as you can see it
print, it works. Basically, whenever I
hover my mouse on it, also it it work because
in our function, we do not specify which particular event we
are going to use. That's why it work on any events when I
hover my carton on it. Also, if I press any key, I think it's still
going to work. No, it's not going to
work if I press key because here we use only
mouse call that function. Now let's talk about the
parameter of this function. As you can see, we have
total f parameter, X, Y, flax and params. Using event parameter, we
can define the mouse event. It may be lip click,
otherwise, right click. X and Y, the coordinates
open mouse event. Then come flax parameter. It is not used, but but it required by the
callback signature. I want to say any
relevant flags passed by Open C. And then
come patterns. Pattern is also
additional parameter. If it's necessary, then we are going to use
this parameter. So first, I'm going to use IP
condition in this function. So type, we are going to checking for
the mouse click event. I even equal equal to CV two dot event here I'm
going to use a event, and I'm going to use lit button down event,
some type event. Underscoe L, button
down, this one. Then inside this IP
condition, first, I'm going to display
the coordinates, and I'm going to display the
coordinates in our terminal. Subtype prin inside the rounds, X I'm going to provide a space, so I pass the whole codes, I'm going to pass Y coordinates. Then I'm going to
duplicate this section. I want to study IP condition. Because here we need to
secret two IP condition. One is for it button down and another one is for
write button down. I'm going to set this file. Now I'm going to run this code. Run this code in my terminal. If I click on anywhere
on this image, let's see what it
written in my terminal. As you can see, it
print the coordinates. First, it print the xx position, then it print the y position. If I praise red kick
at that position, also print the coordinates. It is for lip kick and red iOs because we
use the IP condition. So we're almost done. We need to print these
coordinates on the image. So at first, I'm
going to use font. For that, we need to use fonts, sort of Ti, font, EPI two. Here I'm going to use font
from OpenCV, CV to do, and here I'm going to use font, underscore, He semis,
this one, this one. Now I'm going to use a
inviil function from OpenCV, which you going to put
the text on image. For that, I'm going to
type CV to that Put text. Inside the Put text
function, at first, I'm going to pass the
source image, which is IMG. C then I'm going
to pass a string. So I'm going to call
string function TR. Then inside the
round verses, first, I'm going to print X, X. Then I'm going to concatenate
with Cgation sine. I'm going to pass inside the
single codes C. Then again, I'm going to use
congatenation sine again, I'm going to print a string
for Y VD to print the Y VD. STR, Is the rounds is
Y after this stream, we need to define the position where we need to place the text. And for that, inside the rounds, I'm going to pass
excesses and Y xs. Somnotype X, Y. Then we need to pass
the font phase. Which font I'm going to use. I'm going to use
this variable form because in this variable
we define the font. Last, we need to
define the font scale, which is one, then we need to define the
color font colored, and I'm going to use BGR. I said the runbress and I'm
going to use blue colored. 255, the first value, second value is zero,
also third value is zero. Then at last, we need to define the thickness
of this font, which is two. That's it. Then we need to display this image using
IM show function. So I'm going to copy this line, and I'm going to
paste it after that. Also, I'm going to copy
this section and paste it for this condition for the right key. I'm
going to set this file. After set this soil, let's run the coat and see is it
to work properly or not. So up this cod inout terminal, if I click anywhere, so fast I'm going to
praise let click. UterPrais let click,
as you can see, it prints the coordinates
on the image. X is X excess is
130 and Ys is 170. Also it prints the same
value. In our term. Now I'm going to praise Rattle. If I press R cli as you can see, this time also print
XXs and XS coordinate. This is how we can print the XXs and xs
coordinate using OPC. I hope now it's clear for
you how we can create. Thanks for watching this video, Schedule for the next project.
21. White and black dot detection using OpenCV: Hello, and welcome to another
exercise real tt Open City. In this exercise, we are
going to learn how we can count that dot and white
dot using Python open City. So let's see the demonstration. As you can see in my screen, side by side, HeaOpen
total two different image. First image come with
white background. And in this image, we have multiple black dots. And the second image come
with black background. And also, in this image, we have white dots. But if you notice, you can see
the dot size is different. Some doors are
three squared pixel and some doors are
nearly 20 squared pixel. So we need to write a
program using Open CD, which is going to count
all the black dots, A which is going to count
all the white dots. So let's jup into the
visor studio code and see how we can create it. Hello, and welcome to another
Python Open Cit exercise. In this exercise, we
are going to perform White and Black dot
detection using Open CP. Image processing
using Python is one of the hottest topic
in today's world, but image processing is a bit complex and beginner get bored
in their first approach. We have very basic image
processing Python program to count black dots in white surface and white dots in black surface
using OpeCVFunction. In this exercise,
we are going to use function like image
read threshold, fine contours and contour area. As you can see in my
current working directory, we have a Python
fine mean dot pi. Also, we have a image
black dot one dot GPH. This is the image
we are going to use to find black dots
in white surface. As you can see in this image, we have multiple
black.in white surface. We need to count the
black dots using OpenCV. So let's get to the Visual
Studio code editor. So first, we need to
import the OpenCV lining. So here we type,
import, import C two. Then we need to define
the image five bath, which is black dot one dot GBG. I'm going to declare
a variable path equal to inside the Dual codes, black, dot one dot JBG. Now, we need to convert this
image into grayscale image. For that, I'm going to
use image at function. First, I'm going to
declare a variable gray, assigned with, I'm going
to call a function C two dot IMRdimageRad. Then inside the undresssF only to provide the
image file path. This one then we need to provide a tu flat value, which is Z. This can convert any image
into black and white image. Now, we need to convert this griscal image
into a binary image. For that, we are going
to use threshool. Type a single and command
threshold and hen type TH and another
variable thrust. S equal to CV two dot ahold. Then inside this
function, first, you need to pass the
grayscale image, this one. Next, we need to pass to. First, we need to
pass the thrash B. Here I'm going to pass 100. Then we need to pass maximum
level win, which is 255. Then we need to pass the
type, which is binary. I'm going to type two dot
thrash Trash binary IN V. Then I'm going to use Odd
operator and here to type CV two dots underscore
forts, this one. Notice this file. Now, let me explain the
threshold function. The function CV
two threshold work as if pixel value is greater
than a threshold value, it is assigned one, maybe white, else, it is assigned another
value, maybe black. Basically, it's going to convert this image into black
and white image. I want to say pure
black and white image. You can call it binary
from of an image. The first argument
is image source, which should be a
grayscale image. Second argument is
the threshold value, which is used to classify
the pixel values. If the image pixel
value is less than 100, then it's going to set zero. If the image pixel
is greater than 100, then it's going to say 255. As you know, zero stands for black color and 255 stands
for pure white color. C two, under scoe binary uncoe
IND is a threshold time. It's mean the output
binary image will have the inverted color and OTsU is used to automatically determine
the optimal ThessalVdo. I know it quite sounds
technical. Don't worry about it. Just do what I'm doing. Next, we need to
find the condues. Now the question is,
what are the condues? Condes can be explained
simply as a curve joining on the conduce
point along with boundary. Keep the color and
intensity is same. The condues are a useful
tool for shape analysis and object detection,
recognization, and menu. Contours give better accuracy
for using binary images, and the find conduit function
take total three argument. First one is source image. Second one is contuRtrival, and third one is
contour approximation and fun condus return
a Python list. Let's use the function. First, I'm going to
declare a variable, CNT equal to condues equal to CV, fine conduFine. This one, fine contues. Then inside the roundressF we need to pass
the source image, which is thrash, this one. I copy this variable
and paste it here. Then we need to pass
the retrib moon. For that, I'm going to use C
two dot ETR ist. Retv list. And the third value is
approximation method. For that, her pass CV two dot chain approximple. This one. The chain approx sample
method comprise horizontal, vertical and diagonal segment and leaves only the end points. This results in fewer points
being stored in memory, which is useful for simplifying
conduit representation. Then next, inside the square
versus m to pass minus two. This is used to
handle differences in the written values of
Cout fine conduce method. It is used to control
differences in different version of
open C. In some version, it return total three value, contues hierarchies and image, and in some version, it return total two value, only contut and hierarchies, and especially we need
contours and hierarchies. We don't need images. That's why hero use slicing technique
minus two because we need to select only
this two value, conduce and hierarchies. So this is going to find the on the contest and store this
contour in the CNTs value. Now, we need to
filter this confuse. We need to filter those
condue based on their area. We need to keep those
confuse whose area 3-20. I want to say three
and 20 square pixel. After filter this conduce, we need to store this
contule in a variable. So let's get the filter. So here we need to filter
the contube by their area. So first, I'm going
to define a one. This is our first feeder point, which is three square pixel. Let's I duplicate this line, and this is our second
feeler point, two, which is 20 square pixel. Now I'm going to
create a empty list, which is X CNTs X
contubes equal to, it is an empty list. And now we are going to run the loop through
all the contues. For that, I'm going
to use 44 CNT, contuN conduce, for conduce in contours variable is
with all the contes then at the fall He
use p condition. E is one, I S one
means condition one, three square pixel,
less than CV two. He to use contour area
method contour area. Then inside the rounds, we need to pass the cones CNT. I mean if each of the contour
match this condition, N contours and with that, it is less than S two, me condition two, with
inside this tip condition, I want to append
all the contours which is match this
condition in this list. Basically, I want to append
all the tues in this list, which is match this condition. Hemo type N X conduce
append function. Inside the run press,
I'm going to pass the particular conduits,
CNP. That's it. Now, let's explain this form. First, I run a follow to all of these contours which we
store in this list, tu list. Then each of the contour makes this condition
and the condition is in the contour is between three square pixel
to 20 square pixel, they append this contour
at at least X contues. And her create the Dist conduce. Now we need to print the
length of the conduce to print how many.in our image. For that, outside the fur
lo amtype print statement, print inside the round brasses, inside the double codes, Hemotype total
number of that dot. And here, I'm going to
use the Estring method. E string, then
inside the curses, I want to print the
length of this list. So I'm going to use
the length function AN and inside the round recess, I'm going to pass exent the list. I'm going
to set this file. And before I run this code, let's check the code if I
did some mistake or not. I think it's fine. Oops, there's a mistake. Yes, this one. We need to move this selection
outside the round braces. So I cut this section and put it outside the round braces. And I'm going to set this file. And after I set this file, let's run the code and see
easy to work properly or not. So to run this code,
as you can see, the total number of
black dot is 23. So it worked perfectly. So we have totaled 23
black.in this white surface. So I hope now it's clear for you how we can count black dots in white surface using Python
Open C. Using this program, you can count black dots. Also you can count
the white dots. Let me show you.
So as you can see, we have another image in my
current working directory. This image come with
black background, and in this black background, we have white dots, but we have hundreds
of white dots, and white dot size is different. Some white dot is nearly
three square pixel and some white dot is
nearly 20 square pixel, we need to count all of them. Let's duct the users studio pod. So as you can see in my
current working directory, we have this image,
white dot dot pinch. First, I'm going to copy the file path, otherwise file in. Then I'm going to
replace the file path, so I'm going to
duplicate this line and comment on previous line and replace that dot
with white dot png. Next, I'm going to change the
total number of white dot. Es, also, I'm going to
run this same program, but just only to
change one thing. This time, I don't
need this inversion. I just need thresh bind. So I'm going to duplicate
this line and comment out previous one and remove INV. We just need thresh bind, and I'm going to set this file. Up set this file. If I run this code in my terminal,
let's see what it written. Up run this code, as you can see total number of white dots, 583. So this is how we can
count the white dot also. Just we need to change
the thrash bind. If you want to count
the black dot, then you need to invert
the thrash bind, and if you don't, then you
don't need to invert it. So this is it for this tutorial. Thanks for watching
this video Stay tuned for the next tutorial.
22. Draw rectangular shape and extract objects: Hello, guys. Good
to see you back. Once again, I'm back with
another Python open CP project, and in this project, we are going to draw
a rectangle shape and extract object using
Python open Civing. Let me show you
the demonstration. As you can see, here I
open window of cat image. Now I'm going to select
some area of this image. I praise lip click and
release at that position. As you can see, here it
create a rectangle shape. And if your selection
is not well, also you can restore the window. Also you can reset the window, you need to press R. Again, you can make
this selection. And if you think your
selection is good, then you need to press C
keyword in your keyboard and it's going to
crop this section and display it in a new window. As you can see, Harry
paint crop image, and it display the
image at that window. If you want to
quint this window, just to press Q to
quote the program. And to run the program, we are going to use common
line argument. This is what we are going
to create in this project. So let's start the practical. Here we are going to write a Python code which allowed
to user to select and crop portion of an image by drawing a rectangle
using mouse weds. At first, we need
to import modules. First, I'm going
to import OpenCV, sound type CV two. Then I'm going to import another module
which is arc perse. I'm going to type
import Rc parts. So what is the usage
of Rc peruse module? Rc peruse module is used to handle command line arguments. Don't worry, we are going
to learn about it later. Now, let's initialize the
list of reference point. Here I'm going to create a
empty list, and for that, I'm going to take
this variable name RF underscore reference point. Reference point equal
to it is an empty list. Then I'm going to declare
another variable crop, by default, Cp equal to Fs. This condition is forbs. Reference point
variable is empty list. Which going to store the starting point and the
ending point of the rectangle. And crop is the Boolean value, which is used to track if
cropping is in progress. Next, we are going to use
to mouse event function, which is going to track
the mouse clicks. For that, we are going to use
basically two mouse event, which is lap bottom
down and lap bottom up and we are going to use this to mouse event
in a function. Hem drew declares function F and our function name
is shape selection. Shape underscope selection. Shape selection inside
the round dresses. First, it's going to
take event as parameter. Here we need to pass the event. Event, then we need
to pass Xxs and YXs. Next, I'm going to
pass the flags. At last, I'm going
to pass pattern. Inside this function, we need
to use these two variable. Here I'm going to declare this two variable
as global variable, some type global reference point and the next variable is crop. Now, if the late mouse
button was clicked, record the starting
X coordinates and Y coordinates and indicate that cropping is
being performed. I want to say if I click the
lap button on the window, then it's going to grab the
starting rectangle point. So we need to use
IP condition event equal to equal to here I'm going to use mouse
lip button event. I'm going to type C two
dot event underscore, and I'm going to
use lip button down event button down, this one. Then inside this IP condition, I'm going to say the reference
point wave reference point equal to here I'm going to
say it inside the squareds, inside the rounds, I'm going
to set the first value. The first value is X, Y. Here we use if event equal
to equal to let button down, then it's going to grab
the Xxs and YX and coordinate and save it
in this empty list. Now also we need to grab the end point for that
hem type ellip condition. Else if event equal to equal go. This time, I'm going
to use CV two dot, even, underscore, and this time I'm going to
use lipped button, p button. I mean when we release
the lapped button, then I want to grab the endpoint of the rectangle
to grab the coordinate, we need to use apenFunction. We need to append
another xs point and YXs point to
this empty list. For that, I'm going to type
reference point dot apen. When you said the roundss, I'm going to pass
the coordinates, coordinates of XX YX. Now we can draw a
rectangle because now we have the starting point
and the N coordinates. Is the function and
outside the condition, I'm going to draw the rectangle. So type C two dot rectangle. Then inside the roundses
in our first parameter, we need to pass image. In the second parameter, we need to pass the first coordinate. Xxs coordinate and Y excess
coordinate together. To select the first coordinate, I'm going to tie reference
point reference point. Inside the square resis, I'm going to sleep
the first view. So I'm going to pass zero. And then at the cooma again, I'm going to type
reference point. Inside the square resis,
I'm going to pass one. It's going to select
the second view. Where you already store, the Ss and Xs coordinate. After that, we need
to select color, the color of rectangle. Inside the round dresses, we need to provide
color as GR format. For blue, I'm going
to pass zero. For green, I'm
going to pass 255. And for red, again, I'm
going to pass zero. Basically, I want green color. Then we need to pass the thickness of the
rectangle border. I'm going to pass two and then I'm going to show the
rectangle in this image. For that, I'm going
to use IMS method, sometimee CV two dot IMsO then
inside the round presses, I will pass inside
the double code, need to pass the window
name which is image. Then we need to pass
the image variable. Image. We write a
mouse event function which can grab starting point of rectangle and
endpoint of rectangle and also it can draw a
rectangle on an image. And now we need to construct the argument parser and
parse the argument. Is the function. At first, I'm going to
declare a variable called AP. AP equal to heard
I'm going to call this module arc
parse type R parse. From here, I'm going
to run a function called argument
parser. This one. At the next line, I'm going
to parse the argument. For that, I'm going to type AP dot here I'm going to use another
function call add argument. Add underscoe argument. Add argument inside
the rounders, here at first, we need
to pass a screen. Wh going to work as
name, otherwise, flax. Inside the double course,
I'm going to pass I. Then outside the quotation, we need to pass
the same argument. Inside the double course,
I'm going to pass image. Com this argument
is very important, so I'm going to
make it required. Required, equal to true. And to understand
the line, also, I'm going to type
and to understand the line for what purpose
I type this line. I'm going to add a hell. Hell equal to inside the double course,
I'm going to type. This is the path to image, so type path to image. Don't worry, I'm going to
explain it step by step. Next, we need to
parse the argument. For that, I'm going to type
another variable args. Args equal to, I'm going to
call a function called words. Words inside the round
ress I'm going to pass apt parse arc. I'm going to call a
function parse arc. Now let me explain
you what is that? Why I use these functions. Basically, it is construct an argument parser using
the arc peruse library. At first, here we create
a argument parser object. The initialization and
argument parser object, which will handle command line argument pass
to the script. Next we add an image argument. I and image, these are the short and long
option for the argument. The user can specify either
when running the script, and then we pass required true. It's meant the
argument is mandatory. If the user does not
provide the image path, the script will
through an error. Next we pass L basically this provide
description of an argument, and at the next line, we parse the argument. First, we talk
about apt pass Rg. This method parse the
common line argument and return an object. Then we use a function
called verse. The verse function convert parse argument
into a dictionary. You can easily access the
value by their argument name. Basically, when we run this
script from our terminal, then we can directly provide
the path of an image. And now we need to load
the image and clone it. Also, we need to set up
the mouse Globe function. Let me show you how
to load the image, I'm going to create a
variable name image. Image equal to. Here I'm going to read the image using image read function. So type v two dot
image read, I am read. Then inside the round presses, here we need to
provide the image file path to read the image. But as I told you, we are going to
pass the file path when we run this
script as a argument, and we already did it
using arc purse method. So from the arc purse method, I'm going to pass RCs this variable and inside
the square recess, we need to pass the image. Then I'm going to
clone this image, so type another
variable clone equal to image dot copy function. Next, I'm going to assign a
window name for this image. Sono type CV to name window, name
window, Window. Inside the roundres I'm going to pass the
same name image. And now I'm going to set the
mouse callback function. C V two dot set mouse call back. Inside the run process, first, we need to pass the
window in which window, I want to set the mouse
callback which is image. Then we need to call an event
in which event we want to call and I want to call
this event shape selection. Copy the event name
and paste it here. Now let's check it and is
it to work properly or not. Here, I'm going to type
C two, dot, quite key. Inside the round process,
I'm going to pass Z and also I'm going to
destroy all the windows. I'm going to type C two dot,
destroy all the window. Puts code in my terminal. As you can see, I didn't
mistake the spelling of. It's E, not A. I'm
going to set this file. If I run this code in my
terminal, as you can see, it's going to return an error because Harry do not
pass the argument. But if I pass the argument, I want to say the
command line argument. If I pass the command
line argument, so I'm going to type the script, some to run the script,
some type Python. Space, the file name M dot Pi. Then I'm going to type ID next I'm going to
pass the FlePAT. As you can see, our file
path is cat two dot JPG. Here I'm going to
type cat two dot JPG. Then I'm going to hit Enter. After I hit Entered, I
think there is an error. Still there is an
error. But it working. As you can see, I can
draw the rectangles. As you can see, it's
written in index error, least index out of
fringe, line number 18. First, to resolve the problem. I'm going to delete
this terminal and then jump into
the line number 18. Yes, I think I
identify the problem. The main problem is we need to move this two line inside
the lip condition, not outside the lip condition. So I'm going to move it inside the lip condition and
the set this file again. If I diggle another
code in my terminal, it's going to throw an error. But if I tie Python, if I pass the command
line argument, main dot pi I, then I pass the image name
gat two dot JPG, I hit Enter. As you can see this time it work perfectly and it don't
throw any error, and as you can see, we can still draw rectangle using our cursor. Our mouse events work perfectly, and it do not throw any
error in our terminal. As you can see, we can
draw multiple rectangles. Then click the mouse, I assume as a starting point and when I release the mouse, mainly the left click,
then it's going to assume the release point as a ending point of the rectangle. We done our 80% of work. Now we need to crop
the selection area of this image. Let's do it. I'm going to read
the terminal also, I'm going to comment
out both the lines, otherwise, also,
we can remove it. Now I'm going to run a loop
which is going to display the image infinitely
until we praise Q. Here I'm going to
type file. Two. Then inside the Value, at first, I'm going to display the image and wait for keypress. For that, I'm type V two, do I am so method, I am so. Then inside the round dresses, I'm going to pass the
window name image. Also, I'm going to pass the source image which is stored in a image
variable image. Next, I'm going to assign
a variable called key. Key equal to CV two dot wet key. Inside the round presses,
I'm going to pass one. Now the quoti is YI pass one. It's going to wait key
praise for one millisecond. If a key is praised, it captured the key
code, with that, I'm going to pass a bite, a bitwise code, zero X if H. This bitwise operation ensure that only the last eight
bits are considered. Best ensure cross
platform compatibility for the capturing key coodes. Now after I create
the rectangle, if I press rkyword, then I want to reset the window. For that, we need
to see a condition. We need to set IP condition. If key as I told you, it's going to capture
the key code. Key equal equal to her to
run a function called ORD, or inside the rundres
inside the Dugal codes, R key, then inside
the IP condition, I want to clone the image. I'm going to type
image equal to clone. Dot copy. Next, if I pass C, then I want to break the loop. Hemotype condition, LC K
equal to equal to ORD, is at the roundres
is at the quotation, C, then break the loop. Break. Now the question is, what is RD function? ORD function convert
the R character, otherwise C character into a AI V I allow comparison
with the key code, which you capture from
the Wek function. Now outside the Y loop, we need to crop the image. For that, again, I'm
going to use I condition. So root I here I want to call the len function AN
inside the round verses, I'm going to use the
called reference point. I copy this reference point and paste it inside
the len function. If length of this reference
point equal to equal to two, then insert the condition. Crop the image. So type
variable, crop underscope image. Crop image equal two, and here I'm going to
use the clone image. I'm going to copy
this variable clone. Then inside the square says, we need to define
the crop position. But let me explain what is reference point
equal to equal to two. Why I use this condition. Up the user phis selection
of the area and create the rectangle and ater
pressing C to bake the loop. The code going to check the reference point
hold two value or not, and the two value represent the starting point and the
ending point of the rectangle. Then from the clone image, we need to define the cropping
area to crop the image, we are going to use
slicing technique. Here Imo type, reference point R underscoe point
is at the squares, I'm going to pass zero. Then again, I'm going to use square resis here I'm
going to pass one. Then I'm going to use colon. Don't worry, I'm going
to explain the points. Again, I'm going to
type reference point. Is that the square addresses
this time I'm going to pass one and one. Co the next value, I'm going to pass for weed. Reference point, I
at the square resis, I'm going to pass zero. Also, I'm going to pass zero. Colon, reference point
inside the square s is Helm to pass one and then inside the squares is I'm
going to pass zero. Now let me extend the coordinate
this slicing technique. This selection is for
height selection. It's going to select the
area from the YXs direction. It's work row is. It's going to select from
the YXS point and ending YXS point and the next
selection is for column wise. It's going to select the width. First, it going to
select the starting xx point and then
you're going to select the ending XS point. Then I'm going to
display this crop image in a new window
using IMshoFunction. Here I'm going to
type cv two dot Imo inside the run presses first fully to pass the window
name which is Crop Image. Crop underscore image, comma, then I'm going to pass
the variable crop image, the exact area I want to show, and also I'm going to run ct C two wet key Heunpass zero. Updo that inside
the IP condition, I'm going to destroy
all the window. For that, I'm going to type CV to dot, destroy all Windows. That's it. I'm going
to set this file. Now, let's set the file and if I run this code
directly in my terminal, as you know, it's going
to throw an arrow. I'm going to run this code
from the command line. First I'm going to
type Python space, fine name main dot pi
dot pi space I space, and then we need to
provide the file path, which is cat two dot JPG. If I hit Enter, as you can
see, it show the image. Now, I'm going to select
some area from this image. I'm going to select this is our starting point and
this is our endpoint. I select this area. I think
I don't like this selection. I want to remove this selection. For that, as you know, we already use a Kort, which is R. If I press RKOd it's going to
remove the selection. If I select the
image multiple time, also if I click R, then it's going to
remove this selection. But after select the
area, if I praise, C, then as you can see, it crop this section and
display it in a new window. This is how it's worked. I
hope now it's clear for you, how we can create this one. Thanks for watching this video, stay tuned for the
next tutorial.
23. Text Detection and Extraction using OpenCV and OCR: Hello, guys. Good
to see you back. Once again, I'm back with another project related
Python open CV. And in this project we are
going to perform takes detection and extraction
using Python open CV. As we're going to
take help of ORC. I want to say Tsat ORC. As you can see in our
current working directory, there is an image
named simple dot JPG. If I open this image, as you can see in this
image, we have text. We have a sentence.
This is a simple text. Also, you have another sentence. Text is at different reason. And now we need to extract the text from this
image and we need to save it in a text file
to extract the text, we are going to take
help of Tsat Oc. Now let's see what is Tsat Os and how we can download
it in our local system. Before we start
our next project, we need to download Tsat OC and we need to install
it in our local machine. Open Google and
search for Desert OC. Then it will provide a
link and you need to click on the first link TesarC
OpensurceOC engine. But before I open this link, let's talk about Tsat OC. Test is an optical
character recognization zin for various operating system. It is a free software released
under the Apache license. Basically, it is a optical
character recognizer. Using it, we can extract
text from a image. Before we start the project, we need to install it
in our local machine. I'm going to click
on the first link. It will redirt to
a Github web page. TaserecOC is a common line tool. We can run it through our
common palette and using it, we can extract text
from an image. Now you need to jump into
the installing section, scroll down a little bit, and here you can see this
section, Installing Tresorat. Then you need to
install TrasatYPbuild binary package. Just
click on this link. It will redirect to this page. From this page, you can found the different version
of this traseract. Also you can found different
operating systems. I am Windows user, so I'm looking for Windows. If you are Mcuser then use this link. So I'm
a Windows user. That's why I jump
into this section. Then you need to
click this link, Taser at UB Minh link. If you click this link
it will provide 32 bit. As it provides 64 beat. I click this link and it
redirect to this page. I am using 64 bit
operating system, so I'm going to click
this link Tsat ORC, W 64. I want to click this link
and it start my download. Now after finish the download, you need to open the folder and you need to install it
as like other software. Then you can directly open it. Otherwise, you can open
it run as administrator. Then you need to
select the language, then click Next and you need to follow this simple
process. Next Mix next. I agree. I want to install it for everyone
who using the computer, so I click next and
then just press mix. Also from this section,
you can select the destination folder and I'm going to leave it as it is. Then I click Install and start
the installation process, and our installation
is complete. I click Next again
and click on finish. Then we need to found
the installation folder. I click, so I'm going to click Crve and then I'm going jump
into the program files. You can see we found the
folder called TsarcORC. If you open this folder, here you can find all the
necessary file for Trasact. Then back to the program
file and at first, I'm going to change the
name of this folder. I just want to name it Tsat. I'm going to remove
dash and ORC. I try to name it Tsat. And then I said this name. Now we need to open
this folder ag and I'm going to copy
the file location. I click on this file path and
I just copy this file path. Then you need to add this
file location to your path. For that, search for PAT and you need to open these
environment variables. Then just click on
environment variables, and you need to select
the PT section. Then just it and add a new Path. Now you need to paste
the Tsat file path. Set this one then you need to open your
Windows power shell, and you need to tie Tsat. TE WSE RACT. Tasert hyphen hyphen hell. If I press Enter, let's
see what it returns. As you can see, it's working. From this command line,
you can directly use Desarat and extract
text from a image. But we're going to use Python programming language and create a sophisticated program. In this section, we learn
how we can install Disac and how we can set it as
environment variable. We are going to use
Dsarec with Python. Also we need to install
a module, so tie P, install space Pi desert, PI TEs E RA CT. It's not sat, it's Tsat. I'm going to remove
R and replace with T. Then just install this
module in your local system. It is necessary for our
project. I hit Enter. After hit Enter, as you can see, it download this module and install it in
our local system. So after install TsaicOs and set it as a
environment variable. With that, we already install
a module, which is Pitste. Using the help of
Pitsact module, we can run Tsaric functions
in our Python file. So at first, let's
import the modules. At first, we need to
import OpenCV module, Stipe, import CV two. Then I'm going to
import Pisact module, Styimport Pi Tsat. Then we need to mention
the tasaic location in our local system and we install
Tsaric in our PoormFle. So to pass the name of
this Poa, which is Tsat. Here type command
from Pisaic mode. 40, PisacPiTsac dot Again, I'm going to type Pi tasact dot. This time I'm going to
run a command Tsact CMD. Tasert underscores CMD here, we need to mention the location. Inside the single code, I'm going to type
the folder name, which is only Taserc
not tasaic over C. In a small case, I'm
going to type taser. As you know, TsariT is
a common line program. That's why we need
to use Tsat command. Then we need to read the image. In which image, I want
to extract the text. First, I'm going to
declare a variable IMG, and G equal to C two
dot IMRT method, IMRaD. Then inside the roundresses, we need to pass
the file location and our filename
is simple dot JPG. After read the image, you can convert this image
into grayscale image. Also, if you don't
want to convert this image in grayscal
image, yes, you can. But I would like to convert this image into grayscal image. I'm going to declare
another variable. Gray, gray, equal
to and from CV two, I want to run a method CVDlor CVT color inside
the rounress first, we need to pass the
variable in which variable we store the
image, which is IMG, then we need to pass CV
two dot color Bsa to gray, Colored underscore, BGR to gray. Now, let's show the
image in a window. For that, I'm going to tie
CV two dot, I am so method. Then the rounds. At first, we need to pass a window
name which is simply D and then we need to pass the source
image, which is gray. Next, I want to run
wet key method, CV two wet key, I that the round brrass zero. So let's run the code and see is it convert this image
gray scale or not. So to run this code,
you make a terminal, as you can see, and it convert the colorful image
into grayscale image. Now we need to extract
the text from the image. For that, we need
to run a method. We need to run a method
from Pi resurrect module. So first, I'm going to do
a variable text, equal to, I'm going to call this
module Pi resurrect Pi resurrect from Pi resurrect, I'm going to run a function
which is image to stream. So type image. Underscoe to underscoe stream. This one, the last one. Then inside the round presses, we need to pass the file path, the file source, which
is gray variable. Now I'm going to comment
out both the lines, I don't need these
lines and I want to pin the text in my terminal. Some type print
statement, print, Is the round dresses, I want to pass the
text variable. Image to string method going
to extract the text from this image and it's going to set this text in this variable. So let's set the file and run this code in my terminal and see is it to work
properly or not. After run this code
in my terminal, as you can see, it print
the text successfully. This is simple text. Text is at different reason. If I open this image file, you can see both the text. I print as it is. It can identify capital letters, also it can identify
small letters. Now we need to set this
text in a document file. For that, I'm going
to open a file. First, I'm going to
declare a variable. File equal to open
inside the roundresses. First, we need to
declare a fine name, which is recognize dot PHD, and I want to open this file, W plus mode, so type W plus. Then we need to write
the text in this file. So Five, dot, I'm going
to call right function. Right. Then inside
this write method, I'm going to pass the variable. I'm going to pass the
variable, which is text. As you know, it is a good
habit if you open any file, also you need to close the file. I'm going to type file
dot close method. So this is it. If I set this file and
run this code again, now you can see
my terminal first it print, this is simple text. Then also, you can see my
current working directory, it creates a recognized
dot text file. If I open this file, here
you will see the same text, which we extract
from this image. This is how we can use Pi
resact and OpenCV together, and we can easily extract
text from an image. I hope now it's c for you, thanks for watching this video
Stu for our next project.
24. Invisible T shirt using OpenCV: Hello, and welcome to another
exciting Open CD project. Today, this project,
we are going to create invisible T shirt. Let me show you
the demonstration. As you can see, this is me and I am wearing black t shirt. And now I'm going to bring
my invisible T shirt. As you can see, this is
my invisible T shirt. If I wear this T shirt,
I become invisible. And also you can see the bed
all through this t shirt. It's become coopeti invisible. As like Harry Potter
Invisible clock, we made our T shirt invisible. To create this kind of effect, I'm going to take
red colored T shirt. Also you can take green color, otherwise blue colored, but
red color is perfect for me. And in four step, I'm going to copy this project. At SAS, we need to store and capture the
background frame. The, we need to detail
the red color cloth using color detection and
segmentation algorithm. Next, we need to generate mask and we need to substract
the red color, and at last, we need
to generate the final out that's when you create
the magical effect. Without wasting your
time, let's jump into the computer screen
and start the protein. So finally, we are in my
ser studio code editor. At first, we going to
import the module. So type, import CV two. Then I'm going to
import another module, and I'm going to import NumPi
module. It is necessary. Import NumPi as NP. At first, we need to
capture the video, and I'm going to capture
the video using our webcam. Here I'm going to declare a variable name capture
underscore video capture video, assign with, I'm going to call a function
called video capture, Cv dot Video capture. We're already familiar
with this function. Then inside the round verses, I'm going to pass zero. Now the question is
why I pass zero? Because if we pass zero, then we can directly
access our webcam. We can directly access
our default webcam. If you have multiple webcam and suppose you have three
webcam in your system, then according to your needs, you can change the number. I want to start my default
web cam so I pass zero. Next, we need to
provide a little time to our camera to arma. I'm going to use T module. He type, import, and
I want to import time and time dot slip method. Inside the round bss, I'm going to pass one. Here I provide 1 second
delay to our code. Then I'm going to
declare variable. First, I'm going to declare
a variable named count, count assign with zero. Then I want to declare another variable
called background. Background assigned with zero a. Now we need to detect
the background. Also, we need to
capture the background. We can easily use
this background for masking to capture it, we need to run a loop
through this variable. So here I'm going to use
for loop for I in range, and I want to set
range up to 60. Then inside the for loop, I'm going to declare a variable, written val, return,
underscore, value, val, comma. As I'm going to use this
variable called background equal to I want to run
capture dot Rt function. Capture dot Video Tat and I
want to run read function. I want to read the video
which we get from our webcam. So Tyt video, read read method. Then I'm going to
set an IP condition. I written Vallow written Val
equal to equal to false, then inside this IP condition, I want to continue this rule. Continue. It may pretty
confusing for you. Let me explain this
code step by step. This nippy Do Python
code is used to capture the background
frame from a video. In our case, we are going to
read this video from webcam. Over a certain number of frame, in this case, 60 frame. Then it go to processing
the video frames and extract a background frame
for our further task. We can use this frame for
background substraction, otherwise, special effect
like green screen, et cetera. At first, this loop
will run 60 time, which means the core
inside the roof, we execute for the first
60 frame of the video. Then here we run a read method, capture video dot Red Method. It is a method from OpenCV, that's read a frame
from video feed. And the first
variable written well indicates whether the frame was successfully
captured or not. It's written true if the
frame is successfully read. Otherwise, it written
falls if it fails. Then come another
variable background. As you can see, first we declare background
equal to zero. This background
variable going to hold the actual frame which
you grab from the video. Basically, it's going
to capture an image, and then we compare a condition if written well equal
to equal to false, this check whether the frame
was successfully captured. If written well fails, the prographs skip to the
next iteration of the room. And if the frame
was not captured, the continuous state
may tell the poorer to skip the rest of
the loop code and process the next
iteration t going to ensure that only successfully captured frames are considered. And now outside the four loop we need to flip
the capture frame. Basically, we want to
mirror the capture frame. Otherwise, it's going to
return the opposite image. For that, I'm going to
tie background equal to what we call a method from NumPi tie NP taught flip method. Then you said the
run process first, we need to provide the source
file, which is background. As you know, we stored the frame in our
background variable. So to pass background, coma also we need to pass
Xs x is equal to one. I going to flip the frame. Up to successfully
flip the image frame, also, we need to flip
the video frame. For that, here I'm going to run inside the roundreses
captured video, and here I'm going to
run is open method. Dot is open, then while loop, written al comma,
image, heretical, another variable called
image equal to capture video dot read Mnood capture video dot read after
read the video, I want to reflect
the whole video. I want to film the whole video. Then again, I'm going
to run Hip condition. If not written well, then inside this IP condition, I want to break the loop, break and outside
the Ip condition, I want to increase the
count variable v. Count, count equal to count plus one because we need to
incrementate the loo plus one, I want to increment
this loop by one. So to read the whole video
frame by frame, we need to. We need to flip all the frame. So to read all the
video frame by frame, we need to flip the frames. Here, inside the loop, I'm going to type
image, equal to. Again, I'm going to type
np dot flip Method. Flip. Then inside
the round presses I'm going to pass
the variable IMG, comma X is equal to one. Then also interst the xs. X is equal to one. In our previous group,
we read only 60 frame, and then I reflect one frame. But in this Valu, we read whole video and
we select all the frames, and then we flip all the images. Here we flip our video and
background image successfully, and then we have to convert
the images into HSV color. By default, it's
come with BGR color. Here I'm going to declare
another variable called HSV. HSV equal to CV two to change the color, we need to use a method
called CVD color, CVT color. Then inside the
round press, first, we need to pass image, the source image,
Summer type ING. Then to change the color, I'm going to call CV two
dot color, underscore. From here, we need to select the exact vizier two HSV color. So type PGR two HSV. This one. It's going to change all the frame color
into GR to HSV. I hope you are already
familiar with HSV color, and now we need to
create two mask for isoluting certain
color from an image, especially we're focusing
on red color too. Basically, we are focused on extracting red colored
tone in an image. That has been converted
to HSV color. Here, we need to create
total two mask and each of the mask need
two colored tone, lower red color tone and
upper red colored tone. Here, I'm going to type a single en comin setting the lower and upper
range for mass one. I'm going to declare a variable. At first, I want to set
the lower red tone. This is for mass one. So who type lower
underscore rate. Equal to here, I'm going
to use Nb AA Mth Np AI. Then inside the roundss here
I'm going to pass an array. Is the square passes, I'm going to type 140, 40. Also, I'm going to say
the upper red wave. I do begin this line and I'm going to replace lower
rate with upper red. And this time, I'm going to
say it 100 value is 100, saturation value is
255 and value is 255. Now, let me explain
this two line. As you can see, we create a number array and
in this array, we have total three Vo, Henry p three Vo 140 and 40. The first value represent Hu, the second value
represent saturation, and the last one is
represent value. Basically, represent
colored tag. I measure in degrees. For Open CB, we
use 021 80 degree, but traditionally, its
range is 023 60 degree. Then come saturation. Saturation determine the
intensity or purity of the cool. Zero is gray and 255 is
fully saturated color, and here we use 40 and the saturation we
use relatively low. It means the color will be
somewhat vast or fed up. And the last value
control the brightness. It controls the
brightness of the color. Zero represent black and 255
represent full brightness. The value we use is blue. Which means the color
will be relatively dark. In this variable, we capture kind of greenish blue color
rather than red color. At the same way, we
say upper red Value. Also, upper red variable
contain the same color, but its brightness and
intensity is high. It is not fade out. And now I'm going to create
the mask one variable. Ms one. This is mask one, Hemel mask one assigned with CV two dot, and I'm going to call a
method called in range. I range. Then you said the round presses, I'm going to te HSV, comma will to pass the lower
rid and the upper rate. Lower rate comma, also need
to pass the upper rate. This is our first mask. Here we create a binary mask based on specified color range. As I told you, we need
to create another mask. I do get this section and first, I'm going to change the command. This is for mask two, and also we need to
use the same variable, lower rate and upper rate. But this time, I'm going to
change the color angles. Also, I'm going to
change the values. So here, I'm going
to tie 155 degree. And for upper right, I'm going to pass 180 degree, and then I'm going to
change the variable length. This is mask two. And now I'm going to
combine both the mask. So here, I'm going to type
mask one equal to mass, one plus mask two. If we march it, then it allow more comprehensive cotage
of red colored detection. After march both the mask, we need to refine the mask. We need to remove all
the noise from the mask. For that, hemorrhotide,
mask one, mask one, assign W, here, we need to run a method from OpenCB which call morphology X. It can remove all the
noise point from the mask. Hemorrhotide, CV two
dot, morphology X. This one. Then inside the
roundresses as first prater, we need to pass the source, the source image metal, which is mask one. After coma, here you need to call morphological
opening operation. Again, I'm type C
two dot morph open. This one, first we pass a
binnymsk that was created earlier where white pixel
represent the area of interest, and black pixel represent the background or ignored areas. Then we conduct an operation,
morphological opining. It is a two step operation. First, it perform erosion, then it perform delusion. Erosion remove
small white noises. Especially isolated
pixels and dilution, restore the size of
larger white areas. This operation is particularly useful for removing
small objects. In our case, noise. Noise is not part of
our mean detection. That's why we need to remove it. Now we need to create a structural element,
otherwise kernel size. For that, I'm going
to pass comma, and I'm going to use function
from Numbi type NP Oce. Then inside the round presses, I'm going to pass kernel
size three by 333 Club. Then also, we need to pass
the data type of this pixel, which is NP dot UN tight. This unsigned Edwit indser
is commonly used in Ig processing because it
can represent pixel values. Then after coma, we need to
pass the iteration value. Iterations, and I want
to run it for twice. I'm going to pass
two. This iteration specified how many times the opening operation
is applied. In this case, this
opening is applied twice. Multiple operation increase
the tinning effect. That's why we need
this iteration. After that, we need to perform
the dilation in mask one. Again, I'm going to type mask one equal to here I'm going to call the
method from CV two, which is known as dilate it. So type C two, dot, dilate. Then inside the
roundss at first, we need to select the mask one. I'm type mask one, then we need to pass
the kernel sides. Again type NP dot one. Inside the round brass, I'm going to pass
three comma three. Then we need to
mention the data dive, which is NP INT eight. Then atracma also we need to mention the
iteration, but before, we need to move all this
section inside the round ***. Here I'm going to
mention the iteration. This time we want
only one iteration. Now the quotien is what is dialed and basically
what you going to do? This operation expand the
white area of the mask, making the detected result larger and filling
in small holes. And these s improve
the mask robusts. Next, we need to
invert the mask. We need to turn white pixel into black and black
pixel into white. And to do that, we need to
use VT is not operator. So first, we need to type mask, two to select the
mask, two equal to Here type C V two, dot, biting, some type
B, bitwise naught. We already learn
bitwise operator. I'm not going to
explain it here how bitwise operators
work, like Bt nought, bitwise inside the Rundresses
I'm going to pass mask one. Here we refine the mask corresponding to the
detected red color. After refine our mask section, we need to generating
the final out. And now we need to create
total two separate output. In the first output,
I'm going to show the background where
red tlor is detected. In the second output, I want
to exclude the ate areas. Let's create the first
output for the background. Here, I'm going to type and I'm going to me this
very well as resource one. R is one resource one equal two, and we need to use PTs
Operator, PTI and operator. So tie CV two, dot, B wise, and then inside
the round verses, I'm going to pass
the background. The background is the image, which represent the scene
where you want to show the parts of the images based
on the detected red curve. First, we need to
pass the background and we already create
the background. Let me show you.
This background. As you can see, we
flip the background. I'm going to copy the
background variable. I'm going to paste
instant toys and operitoT is the
background image, which represent the scene
where you want to show parts of the image based on
their detected red color. Then after coma we need
to pass the source, source two, which is background. Next, we need to pass meta. And I'm going to type mass
equal to mass mass one. Basically, these variables store the background where the
red color is detected. Now we need to extract the current frames
without red color. Again, I'm going to declare
another variable RDs two. This is resos two
equal to C two, again when we use tis operator. Is N. Then inside the
round presses this time, I'm going to pass
the video source. So as you can see, we set this video source
as image variable, IMG. This is our flip video. I'm going to copy this variable, and I'm going to pass it here. Again, we need to
pass the source two, this is source two, and I want
to assign it with st two. I'm going to type metal
mask, Tkoado mask two. Basically, B towise and operator perform
operation on two images, otherwise, between
an image and a mask. It is used to merge
two images based on a logical operation
that compares the pixel value at each corresponding
position of the image. First source is
first input image and second source is
second input image, and the mask parameters specify which pixel to
consider for the operation. Now we need to
create the final o. So I'm going to declare a
variable final underscore out. Final output equivalent to here I'm going to call a method, CV two, C two dot add weight. Sound type A, D, this one, aight it. Basically, we use this
method to bind two images. Here I'm going to
provide the first image, resource one, RD is one. Also, we need to pass
the Alpha value, which is a float point. So to pass one comma then
we need to pass source two, and this is R is resource two. And again, well to
pass the float point. In this time, well to pass the beta float point. Again,
I'm going to pass one. At last, I'm going
to pass Gama Val. Gamma is zero. This function bind
two images together by combining them
with specify weight. At first, we need to pass
the source one value, then we need to pass
the Alpha value, then source two, then we
need to pass the beta value, at last, we need to
pass the Gamma value. Now we need to show the
final output in a window. For that, I'm going
to tie CV two dot, Im method, IMF method, IMF. Then the round dresses,
I'm going to pass. First unit for byte
the window name, which is invisible T shirt, and also we need to
pass the final out. I copy this variable and apracma I'm going
to pass the value. Final. Next, we need to exit the program where we
praise a specified key. We prese ESC key
in our keyboard, I want to quit the program. For that, I'm going to
declare a variable K equal to CV two wet key method. Wet key inside the
round presses, I'm going to pass ten
millisecond delay. Then I'm set a deep condition
I K equal to equal to 27, then in such a condition, I want to break the loop, break. I think I did some mistic. I did a very sllistic we need to move the code inside
the indentation level, we need to move it intent six. I'm going to provide the space and move it inside the
indentation level. Otherwise it's not going to run. One by one, I'm going to move all the line inside
the intendion level. Now it's look perfect. As you know, 27 is asykey. It is code for skipped key. When we praise scape key, it's going to break the
loop and it our program. Now, let's set the file
and run the code in our terminal and see is it
to work properly or not. So I'm going to hit Run button. After I hit the Run button, we need to wait for
a couple of seconds. It takes time to
start the webcam. Also, I'm going to
move out in front of this webcam because we need
to grab the background. As you can see on your screen, it open the webcam
and also you can see, I am not in this window. So this is me and I'm
wearing a black t shirt. Now I'm going to take
my invisible t shirt. This is my invisible t shirt. As you can see it
completely invisible, you can see the background
world through it. Now I'm going to wear this t
shirt and become invisible. After I wear this t shirt, as you can see, I'm become
completely invisible. Also you can notice
little glitches and is, but it is work pretty fine. This is the project where we
build a invisible t shirt. I hope you like this project. Thank you very much and stay
tuned for the next tutorial.
25. Background subtraction – OpenCV: Hello, guys. Good
to see you back. Once again, I'm back
with another project related Open CPI and
today in this project, we are going to execute background subscription
using Open SIP. Background subscription is a way of eliminating the
background from image. To achieve this, we extract the moving foreground from
the static background. Background substraction has several use cases
in everyday line. It is being used for
object segmentation, security enhancement, pedestrian tracking, counting
the number of visitor, number of vehicle in
traffic, et cetera. It is able to learn and
identify the foreground mask. In Open Civi we have three algorithm
to do this operation. Let me show you the algorithms. These are the three
algorithms that we can use to subtract background. This is our first algorithm, background substract or MG. It is a Gaussian mixture based background foreground
segmentation algorithm. We are going to use it first. At first, I'm going to
import the libraries. I'm going to tie Import
and our first libraries is NumPi NumPi as N P. Next, we need to import the most
important library CV two. Tie Import. CR two. Now, first, we need to create an object using this algorithm. Which algorithm our first
algorithm, data substrctorG? I to copy this algorithm name and first I'm going to declare a variable where I will
stood our first object. Basically, it is a foreground to background
substructor algorithm. I'm going to name
it G BG, G VG one. This is found and
vector subsector one, equal to hermano
type, CV two dot, and we need to call
another method, and our method name is
BG AC GA, this one. Then I'm going to type dot, and we need to create
the object type, create, and I'm going to paste our first background
substructor, this one. At last going to pass down ss. This is the first object
which I create using this algorithm, background
substructor Mogen. Next, I'm going to capture
frame from the camera. So I'm going to take a
variable CAP capture, CAP cap, assign with her and call C dot
videocapture Metal. Video capture. Then
inside the round recess, we need to pass the sources. We are going to turn
on my default web cam so I'm going to pass zero. After that, here we need to
run a infrate while loop. So I'm going to type I
inside the roundres one, I mean i true. If the condition is true, then inside the i, first, I want to
capture the frame. We need to read frames. To read frames, also need to
declare Gulen value type. This is the Willian
Valu stored to Bullen Value also I am going to return the
image from the frame. I'm going to type ING, equal to, and we need to
run the read function. Cab dot read. This method will
read on the frames. At first, it's going
to return a vol value, what a Bullen value. It going to store if
there is a frame or not. Then then the image, it going to store
the actual image, otherwise, actual frame. Then inside the VIL,
going to create the mask, the first mask using this image. For that, I'm going to create
a fun mass G mass one. This is G mass one, equal to Helm to apply
this image to this object. Helmer type G, BG one
taught apply mass boot. Then you said the round presses, I'm going to pass IMG image. Here, we apply mask for
background substruction. Then I want to display this mask and also I want to display
the original wing. At first, I'm going to
display the original window. I'm going to tie C
two dot IMF metal, IM so then inside the round resist inside
the single coat, this is the original window, where I want to do
fi webcam footage, some type original and here I'm going to
pass variable image. As you know, image variable
stored the original footage. That's why I need to pass image. And then again,
I'm going to call Ema Show math. I
duplicate this line. But this time, I want to
show the Mg algorithm. I copy MG and plus name, replace original with MG. I replace IMG with
GmGmsk G mask O. And now we need to
call wet K method. Here tie K equal to CV
two dot wet K method. Then inside the round reverses, I'm going to pass
30 millisecond. Also, here, I'm going
to pass BTIs operation, so I'm going to use operator and I'm going to pass
a By code, zero XF. Then we read this line. Then I'm going to set a
condition for SkepKHertype, I K equal to equal to 27, then inside this E block, I want to break the loop. Break. As you know, 27 is the code for scape key. Then after break the loop, I will set the IE loop, I want to release the camera. So tied, capture, CAP cas, tot, release meth wood. Rise. Also, I need to
destroy all the windows, so I will type C two, tot destroy all the window. So let's set the file
and I'm going to run it. After I run this code, it's going to open total two window. In our first window, it going to the original video footage. I want to see the
colorful video footage and the second window, it going for the for
down background mask. Now, let's set the code and
see what it going to return. I want to hit the
run button and we need to wait for some
time to start the camera. As you can see, turn on my
webcam and I'm going to move this window beside of this original window that
you can easily compare. As you can see, this is our original window and
this is the MOG mask. As you can see this filter
written bitwise operator, and when I move my hand, otherwise body, you can identify the moving
portion with white color. Basically, it defined the
area with white color. This is the moving
part of my body. That's why it captured
these sections. And if you want to
become invisible, just stay still and don't move. As you can see time to time, I become invisible and
when I move my hand, then again, it track
the moving area. This is how we can
substract background and display only
the moving portion. Now let's talk about
the next algorithm. I'm going to delete
on the windows. Now, let's create the next
object using Mg two algorithm. Background substructed Mg two. First, I'm going to
duplicate this line and I'm going to change
the variable name. This is MGMG two. Also we can remove it. We don't need this
method for this, and I'm going to replace
MOG with Mog two. Then I'm going to create a mask. For that, I'm going to duplicate this section and
replace mask one with mask two and also we need
to replace the variable, this one, Healtide GBG two, and we apply this image
to this second object. After that, we need to
display it in our window. I'm going to duplicate
this section and I'm going to display mask two. I'm going to change
the window name. This is MOG two. Let's see after I
apply this function, what it going to return. To set this file, again, I'm going to run this
code in my terminal, and we need to wait for
a couple of seconds to start the webcam. Now, as you can see, we have to tell three Window. This is MOG two, this is MOG and this is
the original Window. Now let's see the difference. As you can see, if we
compare Mg two with MG one, OG two provide
better adaptability to varying scene due
illumination changes. I want to say it adopt light
more accurate than MOG. That's why you can recognize
my facial expressions. As you can see, when
I move my hand, Mg two return the
complete image of hand because this algorithm
read lights better than MOG. Now let's apply our
next algorithm. I will do only wins. This type, I'm going to apply
better substructor GMG. Again, I'm going to
duplicate this section and replace GBG two with GBG three. This is Fun background three. Again, we need to
type Cv dot BG ACG, create Bagnew substructor and
here we need to pass GnG. And here you can see this algorithm combine
statistical background image, estimation, and perfixel
basin segmentation. Bgrou substructor Mg two is also a Gaussian mixture based background foreground
segmentation algorithm. Let's create a mask for
the third algorithm. Super create the object, I'm going to create the mask. I'm going to create this line. And I'm going to replace
mask two with marks three and also need to replace
the object name. And also need to change the
object name, GBG three. After I create the mask, also we need to display
this mask in windows. Again, I'm going to duplicate
this line and replace Mg two with GnG here, we need to display the
mask three for down background masks three
and we'll do satisfied. After step this file, again, I'm going to run the
score in my turning. Now you can understand
all of the algorithms. Now you can see we have
total four window and I'm going to arrange it
according to the order, original OMG, OMG two, and GMG. This is our original window. This is the MG window, and this is Mg two
and last one is G&G. Now you can identify the
difference between all of the. You can compare all of three
result with each other. If you notice GMG here you can notice once the background
model is established, the algorithm applies
basin segmentation at each pixel and Bayesian segmentation is a probabilistic method. That calculates the
likelihood of a pixel belonging to the foreground or background based
on its intensity. Also you can notice it
exaggerate the pixel of orders. But all of these three
algorithms work perfectly. It substract the background
and represent it with black color and display the moving portion
with white color. That's why these algorithms are widely used in real
time video analysis, surveillance camera, and
traffic monitoring system. Also, it is used for
object tracking. So this is how we can substract background from foreground. I hope now it's clear for you. So in this tutorial, I show you, we have total three algorithm that can be used for
background substriction. And according to me, Sig one is best MOG two. Otherwise, you can go with MOG. GG algorithm is pretty noise. These algorithms are pre
built in Open Series, so we don't need to do too much for that, we need to apply it. I hope you like this project. Thanks for watching this video, stay tuned for the
next tutorial.
26. Face Detection using Python and OpenCV with webcam: Hello, it's good
to see you back. Once again, I am back with a new project related
Python OpenCV. And today in this project, we are going to create
phase detection. Yes, we are going to create
phase detection using open C. So before I
start our program, let's see the demonstration. And as you can see, it
successfully detect our face and create
rectangle around it. And whenever I move
my face with my face, it's also move the rectangle. It's mean our program
work perfectly. It can detect faces. And if I move my hand
in front of my face, now you can see it removed the rectangle and
if I move out it, again, it show the rectangle. So without wasting your time, let's start the practical. This project utilized
OpenCV library to make a real time phase detection using rob Cam as
a primary camera. But to create the project, we need to download something. We need to download a hear
casket classifier fine. So let's jump into the browser. As you know in this project, we are conducting
phase detection. So here I open my search engine, Google, and here I
search har Cascade, nscFontPas underscodfault,
dot N. Yes, we need to download a EML file. After search this
file, as you can see, it provides a
website, GitHub link, arcascadeFontalfas, underscofat dot N. I'm going
to click on this link. I'm going to redirect
to this page, and this is the ML
page we need to down. There are lots of hard
casket find available for different body
part, such as eye, eyeglasses, fontalfas,
fontalpas default, full body, lip type,
and many more. But we need to select this one, Hardcasket underscoFontal
phase, undersco default. A the big question is, what is that and why we
need to download this file. This file is a pre
tent classifier used by Popsii for
detecting phases in images. This classifier is trained to
detect human fontal phases. A HRGske is an object detection algorithm used to identify objects in an image
or video stream. It works by training
the model to recognize pattern in the image
using her like features, which are similar to
edge dection filters. And this classifier is already built by
training on a lot of positive images such as image of fontal face and
also negative images. I want to say image
without faces. Based on this training, the classifier learns to
detect facial patterns. So already saved a lot of
facials algorithm on this file. So we don't need to teach
our model by ourselves. I'm going to download
this file and I'm going to wp this file in my
current working directory. So as you can see in my
current warranting directory, there is a file
name hung cascade, underscoeFondl phase underscoe default dot
L we need this file. It is a pre trained har
cascade classifier, which is a specify
algorithm designed to detect phase in images. In a single sentence, this file contain
the necessary data to detect phases in images. At first, we need to
import the library. Here I'm going to
import our library OPC. Import CV two, and then we need to load the
pre tad hard casket fine. For that, I'm going to declare a variable and I'm going to declare and our
variable is A, A, assign Wi, CV two dot from here, I'm going to call the
meth cascade classifier, some type cascade classifier. Then inside the round brass, inside the double codes, I'm going to copy the file path. Otherwise, define herame. Here, I'm going to copy the fine name and back
to the main dot Pi file. Then inside the double code, I'm going to paste
the fine name, HcascdUnderscoe fontal phase,
underscoe default, XML. This file is saved
in XML file format. Next, we need to capture the video feed
from the U webcam. For that, I'm going to
declare another variable, and our variable name is
CAN Cam equal to from here, we are going to run a Math mode, CV two dot video capture. Then inside the round recesses, we need to provide
the camera source. Here I am use my default webcam, so I'm going to pass Z Next, we need to run an
infinite loop to capture the frame continuously
from this camera. For that, we are going to
run a uh in inside this IU, we need to read the
frame from the camera. Hem type Cam Cam
dot read Method. Read this method, going to
return to dull two vdu. So we need to store this
vario in a variable. First, it return a
Boolean value that indicating the frame was
successfully read or not. So I'm going to sort this
Bolen value in RET variable. C then it's going to
return the actual image. To store the image, here I'm going to declare
another variable, CNKscimage IMG, assigned
with Cam dot read function. This variable going to
store the boolean value and this variable going to
store the actual image, which you get from this frame. And now we need to convert this colorful frame
into briske image. Soap hair, I'm
going to type gray. Gray, assign with, I'm going to run a
function called CVD color. Cv CVT color. Then inside the
round res as source, I'm going to pass this
variable cab image. We are going to convert the CaM image files
into gray color. Then as second source, we need to pass the
initialized code, which is CV two dot, and I want to convert color
to BGR color to gray colored. So type, colored under scope
and we need to convert BGR color to gray colored
type BGR to gray color. This one. Basically,
phase detection work better on
grey scale images, which simplify the process. Now we need to detecting the phases from the
grey scale frame. For that, we need to use
a meth detect multiscale. Here I'm going to
declare another variable and my variable name is phase, assign with a dot, dettT one. Then inside the round presses, first, we need to pass the
source image which is great. Then we need to pass the
scale factor, which is 1.3. Next, we need to pass minimum
number of never rectangle. For that, I'm going to pass six. In this method, first we
pass the image source gray. Then we pass the
scale factor win. Here I pass 0.3. 0.3 is the scale factor that control how much
the image size is reduced at each image scale and six is the minimum
number of levers. A rectangle should have to
be considered as a phase. This phase variable going to
store all the phases from the grayscale image and
except the phase coordinates. Now, using these coordinates, we need to draw
rectangle around it. So let's draw rectangle. We need to draw rectangle
around the detect faces. For that, we need to use form. For in at the Y. So I'm to type four inside
the roundresses here, we introdu some coordinates
as we do X one, Y one, W one, H one in phase. Then inside this pl boop, we need to draw rectangle
using this coordinates. So as you know, to draw rectangle, we
introduceectangle method. CV two dot rectangle. Then you set the roundressF we need to pass the source
image in which image we want to draw the rectangle as source image I would pass
this variable CaM image. Ma. Next, we need to pass the points to
draw the rectangle. But before we need to
understand the points. This four loop iterate
overall detective phases which is stored in
phase variable. And for each detected phase, XY and Y one represent the
top lip corner of the phase, and W one and H one
represent weed and height. These two value combined
represent top lip corner and this value represent wet and this value
represent height. Using this to using
these coordinates, we are going to
draw the rectangle. And as you know, to draw
the rectangle in OpenCV, we need to pass total two, top lip corner and
bottom right corner. For the top left corner, we already have the coordinates. So here, I'm going
to pass X Conga Y, X one, Y one. Then then we need to pass the bottom right corner value and we don't have the
bottom right corner value, but we can calculate the
bottom right corner value. Let me show you
how. To calculate the bottom right corner value, we need to addition with y1s value with rectangle
with, which is one. Then we need to calculate
YR plus H one, height. At first, we addition
with with xs value, then we addition
height with Y xs in. Combined, it's going to return the right button corner v. Now, we need to pass
another value which is colored here I'm going
to use blue colored. First, I'm going to
pass 2550, zero. Next, I'm going
to pass the ness, the thickness of the
rectangle border. I'm going to pass three and also you can use
higher value for that if you pass five or six. I'm going to pass five. After I draw the rectangle
around the face, we need to display the frame
with the detected phase. To display the frames
at detected phase, we need to go outside from this four loop and here we
need to display IMF metal. C two dot Imho they
said the roundresses. At first, we need to provide a window name and
window name is face, and then we need to
provide the source image, which is Cam Image. That's. Also, we need to set the wet key
to stop the program. Here, I'm going to type I CV two dot weit key inside the roundre I'm going
to set it for one again, one milliseconds and zero
F equals to equals to ORD. Inside the run presses, if we praise small A, then inside this p condition, I want to break the loop. I want to break
the Y loop, break. If we praise A, then it's going to bring the loop
and kill you into. Next, we need to release
the web cam resurce. Outside the ID loop, Hemel type, Cam dot,
release method. Also we need to destroy
all the window, S type, CV two, dot, destroy
all Windows. This one. I'm going
to set this file. Now, let's run the code in my terminal and see is it
to work properly or not. After set this file, I'm
going to hit the Run button, and we need to wait for some
time to start my webcam. Finally after wait,
couple of second, it open our window. As you can see it
successfully detect our face and create
rectangle around it. Whenever I move my
face with my face, it's also move the rectangle. It's mean our program
work perfectly. It can detect faces. I I move my hand in
front of my face, now you can see it
removed the rectangle and if I move out it again
it show the rectangle. Using this pretend
fontalPashar casket file, we create this detector. Now let's close the window and back to the
visual studio code. So I successfully create phase detection
program using OpenCV. It is not a very difficult one. It's a very easy project. I hope you enjoy this
project very much and also understand how
phase detection work. So thanks for watching
this video schedule for our next project.
27. Detecting objects of similar color in Python using OpenCV: Hello, guys, good
to see you back. Once again, I am back
with a new project related Pythonopen CV. Today in this project, we are going to perform
object tracking using color segmentation. Yes, we are going to track object using color
based segmentation. Object tracking is a computer vision technique for locating position of an object
on image or video. Remember, recognizing the object and tracking the object
are two different. In object tracking, you can
track any moving object, but in object detection, you can identify the object. These are two totally
different things. There are many type of object
tracking method in OpenCV, such as color
segmentation based, frame differencing base,
and feature matching base. I institutorial, we
are going to perform color segmentation
based object tracking, and to perform the
effective color tracking, we are going to convert
BGiR color into HSV color. As you know, HSV stands
for hue saturation vue. Hue stands for color, Vallow stands for
amount of color and saturation stands for
gray amount of color. Without talking too
much, let me show you an example what we are going
to create in this tutorial. Now you can see it
successfully track our object. It can track our blue
colored bottle cap. In our frame window, you can see the original footage
and in our mask window, here you can see the
BTIs operator footage. Here, white represent
the bottle cap and black color represent
unselected area. But in our third window RIAs, here you can see the
actual color object. Here you can see the
exact bottle cap. Without this bottle cap,
you can't see anything here because it track only this color segmentation
from this window. This is what we are going
to create in this project. Let's jump into the computer screen and start the project. So as you can see, finally, we are in my visa
studio code editor, and I already created a
Python file main dot py. At fast, I'm going to import
our important library. I'm going to type import, and I'm going to import CV two. Then I'm going to import Numpi. Yes, Numpi is important to
create this kind of project. I'm going to type
import NumPi as NP, next, we need to import Pi
plot from the matplot leaf. He type, import matplot
leaf Pi plot as PLT. Here we are going
to perform object tracking based on
the object color. Object tracking and object recognizing is totally
different things. In object tracking, we can
track any moving object. But in object recognizing, we need to recognize
the particular object. As I told you earlier, there are many methods that
we can use for object track, such as color segmentation, frame differencing,
feature matching, machine learning
waste, et cetera. But as you know, we are going to perform color segmentation
based object tracking. Now let's start the practical. At first, we need to create a video object to
capture the frame. To capture the frame, we are
going to use our webcam. Here I'm going to type
a variable name, CAP, CAP, Assign W and here we need to use a method
called video capture. Cv dot Video captured this one. Then inside the round dresses, here we need to
pass the source Z. Then after that, I'm going
to define a empty function. Why we need empty function? Because we are going to create track bar using it and to
create this track bar, we need to return
empty function. Who is going to return nothing? Here, I'm going to type Dave and our function
name is nothing. Nothing. Inside
the round dresses, I'm going to pass a normal
parameter called X and inside the function,
Hemo type pass. Basically it's going
to return nothing. Then using this empty function, we are going to
create track bar. But before I create track bar, we need to define
the track Bar name. Here, I'm going to type
V two dot named Window. Here I'm going to
use a method called name Window named Window, and we need to provide
a name for this window, for our Tug bar
Window I'm going to type tracking now here, I'm going to create
six track bars because we need to grab upper Hue saturation
value and we also to grab lower
Hue saturation value. Let's create our
first track bar. Here I'm going to type C
two dot, create track bar. This function. Then
inside the round brasses, at first, we need to
create track bar for lower bound value for Hu. Here I'm going to use
the short from lower Hu H. After you create
the track bar name, we need to pass the window name. In which window, we want
to show the track bar. Inside the double codes, I'm going to pass this
window name tracking. Next, we need to pass a value. Here, we need to pass
the lower value, which is called zero. Then we need to pass the higher value and
for higher value, I'm going to pass 255. At last, we need to
pass a function. This function will
return nothing, so I'm going to use
this function, nothing. Just want to copy the function
name and put it here. I'm not going to set
any default value to our track bar because we are
going to set it minually. Then I'm going to duplicate this line and this
time I'm going to change the name of this
track bar, which is As. As stands for lower saturation. Then I duplicate it A and
this time I'm going to name it L V, lower value. These three track bars stands
for lower bound value. Now we need to pass
upper bound value. I'm going to duplicate
the whole three line. So our last three trig
word is for higher value, higher hue and syn is higher saturation and
last one is higher wave. Now we need to run infinite loop to continuously
process the video frame. For that, we are going
to use While loop. Here I'm going to type YT then inside this illoop I'm
going to use read method. Read method capture
each frame from the webcam to store the capture value and
the Bullen value, I'm going to declare
total two variable. First, I'm going to declare B, which is going to store
the Boolean value, then I'm going to declare
another value called frame, frame equal to cap
dot read method. Then after read all
the color frame, we need to convert
color frame into HSV. As you know, by default, OpenCBuse BGR color format. We need to convert it
into HSB color format. I'm going to declare it
very well Hs V equal to V two, dot, CVD color. Then inside the rounddresses, I'm going to pass the frame. At first, we need to
pass the image source. I'm going to pass the
frame variable frame. Then I'm going to
convert it CV two dot, color, underscore, and I'm
going to convert it BG HSV. Search for GR to HSV. This one. Next, we need to get HSV
value from these track bars. For that, we need to declare different variables to
store the track bar value. So for lower, I'm going to type underscoe H. Equal to grab
the data from this track bar, we need to use a method
called gate track bar pause. I'm going to tie CV two dot
gate, track bar, pause, this one, and then inside
the round presses, inside the double codes, first, we need to pass
the track ban name. From which track bar, I want
to grab the, which is H, H. Next, we need to
define the Window name, and our window name is tracking. I'm going to use this
window name, tracking. I copy the window name and inside the double codes,
I'm going to paste it here. At the same way,
we need to create five other variable to get
the value from the track bar. I duplicate this
line. This time, I'm going to extract
value of AS. He type underscore is also interchange the
track banname L is. At the same way, I'm going
to create all of it. This one is for V, and I'm going to
change the name, V. Then I duplicate
this three line and I'm going to make
this one HH H underscoeH. This is for high U value. Next one is for high
saturation value, HH the last one is for HV. Also to change the
track but name, which is H V. So these lines retreat
the current position of the HSV track bars, which represent the lower and upper bound of the
HSV color range. We are going to use
it for masking. Next, we need to create lower
and upper HSV bound array. To create those array, I'm going to declare a
variable L underscore B. It stands for lower bound y to create this
lower bound array, we are going to use
NumPi p dot array, Is the rounds, here
I'm going to pass all the lower bound
value Lunde H, underscore a, and underscore V. At the same way, we need to
create upper bound value. I duplicate this line and I'm going to change
the variable name. U UV stands for
upper bound value. Here also interchange
the variables name. I'm going to make it HH. HH stands for higher value Hs HH stands for
higher saturation, higher U, higher saturation
and higher value. Next, we need to create mask using lower bound value
and upper bound value. To create this mask, we are
going to use in range method. This is the most important
part of this video. At first, I'm going
to declare a variable called mask mask equal to CV two.in range
math boot in range. Then inside the round presses, first, we need to
pass the source. As source, I'm going to
pass this variable HSV. So tie HsV. Then we need to pass
the lower bound value. As you can see, we store the lower bound value
using this variable, underscoe B, lower bound. L underscoe B. Also, we need to pass
the upper bound value. And we represent perbund value using using underscore,
B, that's it. Here we create mask
using ne function. In function used to
create mask based on color values in
the HSV color space. At first, we pass
HSV color space. This is HSV color space image. We convert BGR image
into HSV here. I pass this variable. First we pass array of lower bound value, then we pass array
of upper bound. Now, the bit inne
function convert these values into
a binary image, and we stored this binary
image into mas variable. As you know, binary image
create it total two value. While zero represent black and 255 and 255 represent white. Now this mask highlight
the reason in the frame that match the
specified color range. Suppose you select blue
colored innu track mark, then it's going to show only the blue colored
portion in into. Now we need to apply this
mask with BtIs and operator. Here, I'm not going
to explain in depth how Boys and
operator work. To apply the mask on
Bitwise and operator, I'm going to declare
a variable RES. Rs equal to, we need to
use BTI and sub type CV two dot B twice and operate. Then inside the round
brasses, at first, we need to pass
the first source, which is frame as second source, I'm going to pass
another variable frame. In our first video, we pass the original frame and in our
second video also we pass the original frame because since we are masking the
original width itself, and now we need to pass
the mask variable. Hemotype mask equal to the mask. This create an output
that shows only the part of original frame that match
the specified color range. Basically, you're
going to isolate the object based on the color. Now I'm going to display the
video and the mask output, also the ras variable. Here I'm going to
use IMso Mathod some type V two dot IM show. Then inside the round
presses, first, I'm going to pass
the window name in which window I want
to display the frame. Our window name is frame. Then I'm going to pass
the variable frame. Basically, this window going to show us the original footage, the colorful footage,
Bier color footage. Then I'm going to show
you the mask footage. I duplicate this line and
replace frame with mask. And at last, I'm going to
display this res variable, this footage, this up tar
intersect with Bt operator. So here first, I'm going to
change the window name Rs. Also, I'm going to replace
this variable mask with REs and I'm going
to set this file. Now we need to set the
IP condition to break the loop using Wetey method. Here I'm going to
type E condition I CV two dot wet key wet key. Inside the round
process, I'm going to wait for 25 millisecond and zero x FF equals
two equals to ORD. Inside the rounder says, if I praise small Q in our keyboard, then I want to break the loop. Break. This is it. After that, we need to
release the capture. Cap release method. This is going to release
our video capture. Also to destroy all the windows. V two, dot, destroy all windows. So we are done, but here
I did a silly mistake. As you can see, it's an array, so we need to pass this video
inside the square basis. Here at square basis. Now I'm going to set this file again and I'm going
to run this code. Now, let's run the
code in our terminal. After a click Run
button, we need to wait for some time
to start our webcam. As you can see, her need
to open our window. Here we have to
tell four window. First one is for tracking bar, and second one is
for original frame and third one is for mask. It is for Bis mask and
I'm going to place all the windows
side by side that we can compare the
result very easily. As you can see, this is the single colored
bottle cap that I'm going to use to par from
the object tracking. Here I use solid blue
colour variant bottle cap. You can use any solid
colored object, and I'm going to match this
color using our track bar. And now we need to play with this track bar to match
the bottle cap color. We need to play
with the six value. At first, I'm going to
increase the high value. Then I'm going to increase
the high saturation value. We need to adjust these bars
to get the perfect result. As you notice, when
I change the values, it reduce the noise and try
to focus on our bottle cap. I'm going to first
far this process to get the perfect result. Now these coordinates nearly
give me the perfect result. Now you can see it
successfully track our object. It can track our blue
colored bottle cap. In our frame window, you can see the original footage
and in our mask window, here you can see the
BTs operator footage. Here, white represent
the bottle cap and black color represent
unselected area. But in our third window REAs, here you can see the
actual color object. Here you can see the
exact bottle cap. Without this bottle cap,
you can't see anything here because it track only the color segmentation
from this window. Now let's kill the window
and back to the program. This is how we can
perform object tracking based on
color segmentation. I hope now it's clear for
you how we can create it following the
nearly same process, we can create air
Canvas with it. We're going to learn about
it in our upcoming tutorial. Don't worry about it. Thanks
for watching this video. Stay tuned for our
next tutorial.
28. Import modules and setup mediapipe object: Good to see you back, guys. Once again, I am back with a new exciting project
related OpenCV. And today in this
project, we are going to create Finger Counter, with the help of
Media Pipe module. Now, let me show you an example. One we are going to create. So as you can see, it
start our web camera. And now I'm going to
raise my closed hand. As you can see, now it show zero fingers and also it track all the
fingers successfully. Here you can see
all the landmarks, and now I'm going to
spread all of my finger. As you can see on your screen, now it print five finger. You can notice it track all
the finger joints and teeth. And if I close my thumb, now you can see it
print four finger. And if I rise it again,
again, it print five. And if I move my index finger, as you can see, now
it print four finger. At the same way it
track all the fingers. It count all the fingers and also it track all the
finger coordinates. This is how our
finger counter work. Finally, we are in my
Visual Studio code editor, and as you can see,
here I already create a Python
file main dot pi. At first, I'm going to import
our main module, CV two. So to type, import CV two. Then we need to install
a important module, which is media pipe. To install the media Pipe, we need to type this command. P install OpenCb Python Media Pipe then you
need to hit Enter. As you can see, requirement
already satisfied. I already download this module, but you need to complete
your download process. Then back to the main
dot Pi Pi and this time, you need to import media pipe. The media pipe module. So to type Import
Media pipe as MP. Now you might have question,
what is Media pipe? Media pipe is a powerful open
source framework by Google, designed for building multimodel machine
learning pipeline. It provides a collection of
cross platform library and pre trained model
for implementing computer vision and machine
learning task in real time. And if I talk about
its common usage, it is used for phase detection. It can detect phase and
track phases in real time. Then we can use it
for pose estimation. It can identify human body pose, which is useful for fitness app. Also, we can use it
for hand tracking. Using it, we can track hand movements and
recognize the gesture. Also, we can use it for three D object detection
for argumented reality. But in this tutorial, we are going to
use hand tracking. This module is very
lightweight and fast. Also, it is work on
variety of devices, including low power device. Now, after input our libraries, we need to initialize
the media pipe hands. For that, first, I'm
going to declare a variable and our
variable name is MP, underscopean, media pp hand, media pipe hand equal to PTT, solution from here, I
want to call hands. It's going to exit the
hands solution from the media bith then
hands equal to punch P undercoe hand that I'm going to run
a method called hands. Then inside the rounds, here we need to pass
some parameters such as static image mode, maximum number of hands, and minimum detection
confidence. First, I'm going to
pass static image hand, underscore, image, underscore, mode, static
image mode equal to, and I'm going to make it false. Then I'm going to pass
maximum number hands. Maximum max, undercoe
number, NUM, underscoe hands, maximum
number of hands, equal to, for now, I'm
going to pass one. Don't worry. I'm going
to explain it later. At last, I'm going to pass
minimum detection confidence. So type, mean, underscore, detection underscore confidence, equal to hermon to set it 0.7. As you can see, here we
run a method called hands, and as a first parameter, we pass static image mode. We set it falls.
What does that mean? It's mean it is
optimized for video, and it detects hand only in the first frame and tracks
in subsequent frame. Then at the second
parameter, we set the limit. Here we set number of hands
limit, and here we pass one. He limit detection to one hand. At last, as you
can see, herro use another parameter, minimum
detection confidence. Here we set minimum
detection confidence zero point side Is wing it sets the threshold for
the detection confidence. Lower thresholds may
result in false positives. Next, we need to
define the MP drawing. It is a utility for
drawing landmark and connection on detected
hands. Let me show you. Here type, P underscoe drawing
equal to hero type pt, a method called solution
dot drawing Utes, underscoe utils. As I told you, it is
a utility for drawing landmarks and connection
on detected hands. So basically, in that section, Hero initialize media pipe hand. Next, we need to
create a function. We need to create finger
counting function, and I'm not going to
do that in this video. So this is it for this video. At the next video, we are
going to define a function. So thanks for
watching this video, Sta tune for our next studio.
29. Creating finger counter function: Good to see you guys. This is another part
of this project. And here we are going to define a function that can
count our finger. Also, it can count our thumbs. In our previous tutorial, we initialize the
media Pipe hand, and here we are going
to create a function. We're going to define
a function called D Dave our function
name is count finger. UnderscoeFinger. Count finger, and inside the surround recess is
going to take one argument, which is hand landmark,
underscoe landmark. Here we are going to pass the detected hand landmarks
to this function. Then inside this function, I'm going to define a list, a empty list, and our
list name is fingered. Fingers equal to a empty list. This list going to store
the state of each finger. And now we need to
define the landmark. For that, I'm going to declare a very well call landmarks, Landmarks equal to hand,
Landmarks Landmark. Landmark is a list of 21 points, W going to hold X coordinate Y coordinate
and z coordinates? It is a list of 21 points corresponding to different
parts of the hand. As you can see in this image, these are the
Landmark coordinates, which is provided by Media Pipe. This is how media
pipe read our hands. As you can see, this
is our thumb finger, this is index finger,
this is middle finger. This is ring finger, and this is pinky finger. As you can see, it can detect all the joints
of our finger. Also, it can detect the tip of this finger and each of the coordinate defined
with a number. We are going to use these
numbers and XXs and YXS coordinate to count
the fingers from our hand. Let us back to the
is studio code. So it can recognize 21
points from our hand. Now we need to detect the thumb. We need to check X coordinates for left hand and right
hand orientation. For that, HemrotypFingers,
dot append method. Inside the round dresses, hemotyp landmarks and I want
to is from this landmark, I want to is fourth index, Tt, I want to excess Xs
coordinates and less than Landmarks inside
the square ss Amel pass index three dot XX. As I told you, our hand
divided into 21 landmarks. As I told you, landmark
is a list of 21 points each point represents
specific part of our hand and each point hole
total three coordinate. X xs coordinate, XS codent
and Z xs coordinate, and Landmark index
four represent the tip of the thumb
as per media pipe, and Landmark three represent
the thumb joint near the tip these are the X coordinate of the
thumb tip and the joint. Now let's talk about what
is the logic behind it? Why I use smaller than sine? Why Landmark four dot x less
than Landmark three dot X? It's mean if the coordinates of the thumb tip is smaller than
coordinate of the joint, it means the thumb
is extended outward. This is usually true
for the left hand when the thumb is extended
and for the right hand, this logic works because the frame is flipped
horizontally. And it ensured that
the X coordinates are constant for the detection. Now after I
understand the logic, we might have questioned
why we need to append this in this finger list. Here, finger dot apens store the result,
true either falls. If the condition is
true, its main thumb is extended outward. If fails, thumb is not extended. So this code is for
thumb detection. Now we need to detect
other fingers. For that, I'm going
to use four loop for Pep in inside the square ss, I'm going to check Y coordinates
for that 812, 16, 20. Then inside this four loop, now we need to understand
the four loop. What does that mean?
For tip in eight, 12, 16, and 20. It's mean it iterate through
the tip of index finger, which is represented
eight middle finger tip, which is represented with 12, then ring finger tip, which is represented with
16 and pingy finger tip, which is represent with 20. Then inside this four loop, Hematite fingers dot append
inside the round resis, mark inside the square resis, P, and I want to access
the Y Y coordinates. Less than Landmark,
inside the square resis, PP minus two, I want to
access the Y coordinates. Now the question is, what is the meaning of this condition? What is the meaning
of this logic? Basically, it's going to check our fore finger are
extended or not. And Landmar TP, refer
the current finger tip. Landmar tip dot YX, refer the current finger
tip and Landmark tip minus two dot YXsRfer the base
joint of the same finger. Let's try to understand
with a real image. As you can see, this is the way media pipe observe our hand. As you can see, eight
is the index finger tif and 12 is the
middle finger tip, and 16 is the ring finger tif and 20 is the
pinky finger tip. And if we minus two
from the tip point, then you can see the joint. Here you can see the base joint. If we minus two from the eight,
it's going to return six. Then six is the base joint
of this index finger. At the same way, we can
identify other finger based joint, ten, 14, 18. So this is Y
coordinates of the tip, and this is Y coordinates of the base joint in our fingers. And here we compare
the Y coordinates of the tip with the base joint. And if the tip is higher, the finger is
considered extended. And if the tip is lower, then finger is
considered folded. And our append method going to add the true false result for each finger in this finger list in this empty finger list. If the condition is true, it's mean the
finger is extended, and if it falls, it's mean the finger is folded. So now we have the list that can identify the extended
finger and folded finger. And now we need to
count the list. For that, outside
this four loop, her type return, we need to count the
finger list, nothing else. Finger, fingers dot,
count method, count. And inside the runbs we need to count only the true values,
not the false values. So here I type, true. That's it. Now, let's wrap up this section E. And side by side,
as you can see, I open my visual
studio code editor and this image to understand
the diates the landmarks. Here, first, we define a function and our function
name is count finger, and it's going to take
hand landmark as argument. Then here we create a
EmptyList list of fingers. It's going to store the
value in two or false? True means the finger is extended and false means
the finger is not extended. Then we access the landmark. Landmark is a list of 21
points, and as you can see, these are points
provided by media Pi and each of the point hold
total three coordinates. XXs YXs and ZXs. At the next part, we dett the thumb using
this append method. As you can see, this is the
thumb image of right hand. And this is the landmark number of the thumb, which is four. And from this thumb, here we extract the xs of
this thumb value. Then we access the thumb joint,
which is landmark three, and from this landmark three, again, we extract the xs value. And if the condition is true, then it's mean our
thumb is extended. If Landmark four S value is lower than Landmark
three x value, it means the condition is true. It means the thumb is extended. If you notice you can
see the distance between the borderline from
this Landmark four is something 20 pixel and the distance
between borderline two, Landmark three is
something 60 pixel or 80 pixel Landmark three X value is higher than Landmark four, I mean the thumb is extended. If we fold our thumb, ten, landmark four will would be
greater than Landmark three. I mean the thumb is folded. Here we compare thumb tip
with thumb based joint. At the same way
for other fingers, at first, using this loop, we access all the tips. In this list, here we
pass index finger tip, middle finger tip,
ring finger tip, and pinky finger tip. Then we extract the Y
coordinates from this tip, and then we compare it with
Ys direction from this way. For our thumb, we compare
with Xs direction. But this time we compare
with YXs direction. If the thumb base joined
value greater than thumb tap, then the condition is true. And it's going to append the
true value in this list. And next we count
the true values. How many true value we
have in this single list? Suppose we have total five true value in this list
according to this image. Then it's going to return five. It's going to count
all the true values. If one of the
condition is false, then it's not going to
count the false value, then it's going to return four. So this is how our
function going to work. So this is it for this video. At the next part, we are
going to start our main loop. We are going to read frame. So thanks for
watching this video, Stay tuned for our next part.
30. Creating the main loop and run the project: Good to see you
guys. Once again, I'm back with another video
related to this project. And in this video, we are
going to learn the main loop. At first, I'm going
to open the webcam. To open the webcam, first, I'm going to
declare a variable, and our variable name is CAP, CAP equal to cv dot
Video capture Method. Video capture. Then inside the round presses, you need to pass
the camera source. I'm going to turn on
my default camera, so I'm going to pass zero. Here I'm going to type Y, cap is open, is open. Then inside this while oop, first, I'm going to
run read method. Cap dot read. As you know, read method going to
return to two result, a Bullen value and
the original frame, and to store it in a variable hero type to store the wollen
I'm type success. Comma and to store the
frame multi frame, equal to Cabot read method. Then a mutua on a EP condition. If not success, then
inside the IP condition, I want to break the loop. Wake, that's it. And now we need
to flip the frame horizontally for the lty
e, for the mirror review. So HemtiFrame equal to
CV two dot flip method. Flip. Then inside
the round dresses, I'm going to pass the frame and also going to
pass the flip code. And I'm going to flip
it horizontally, so I'm going to pass one. Then we need to convert this
Bier frame into RGB color. For that, I'm going to declare another variable RGB
under scope frame, equal to hemotype CV
two dot CVD color. This one. Then inside
the round dresses, first need to pass
the source file, so I'm going to pass frame, then only to pass
the colored code. CV two dot here I'm
going to convert color, underscore BGR to RGB, this one. Here we convert this
frame into, BGR to algeb. Now, after convert this
frame into algeb color, we need to process the frame. For that, we need to
run process method. Here I'm going to
tie result equal to hands, Do process method. Process. Then inside
this roundness, you need to pass the RGB frame. RGB, underscore free. Next, here I'm going
to run I condition. If result got multi
hand So type, multi underscoehand, hand,
underscoe landmarks. Then insert this condition, I want to run a four loop. He type four, hand, uncoe landmarks in result, dot, multi hand landmarks. So I'm going to copy this
line, multi hand landmarks. Another pesttive. Then
inside this four loop, we need to draw the landmarks. Basically, I talk
about these landmarks. We need to draw these landmarks. Here we check the detected
hands in result variable. It contain the detected
hand landmarks, which is written by
media pipe hands module. If a hand is detected, this will hold a list of
hand landmark objects. If no hands are detected,
this value is none, and then we iterate through the detected hands using for for hand landmarks in result
multi hand landmarks. As you know, each
object contains 21 landmark points
like thumb, TP, index finger tip, et cetera, and each of the
landmark coordinate save XXs YX and ZXs value. And now we need to draw
landmark in our hand. For that, here I'm going to type inside this
follow, amino type, MP underscoe and drawing
NP underscore drawing, dot, here, I'm going
to draw the landmark. So I'm type, draw
underscoe landmark. Draw landmark is that
the rounder says, first, I'm going to provide the frame, in which frame I want
to draw the landmark. So pass frame. Then we need to provide
the hand landmarks, the hand landmark
variable, and landmarks. This variable hand landmarks is basically the main object, which is going to
contend 21 points. Next, we need to define
which points are connected, like joins and tips. For that, I'm going to
type MP underscore, Hence, draw the landmarks also
into account the fingers. For that, I'm going to declare another variable fingered
underscore count. Finger count equal to, I'm going to type a
method, count finger. Is the rounder says, here I'm going to type hand
landmark hand mark, this one. Basically, here I
call the function, which I already create, this one, count finger. And as you can see as parameter, we need to pass hand landmarks. So we call this function. Now after count the finger, we need to display
the finger count. For that, here, I'm going
to use put text method. So type CV two dot, put text. Put text. Then inside the roundress first, I'm going to pass the
frame with source image. Frame, comma, and here I'm
going to use string method. Then inside the double coats, I'm going to type fingers, colon inside the Cibass, I'm going to pass this variable, Tinger count. This one. And next, we need to define
the position of this text. So inside the round press,
I'm going to pass 1070. Then you need to
define the font style. So I'm going to tie CV to
the font hoch simplex, underscoeh simplex, this one. Then you need to
define the font size, so I'm going to pass two. It is the font scale value. Next, we need to
define the font color. So as a font color, I'm
going to pass blue color. We need to pass it
in a BGR format. First, I'm going to
type 2550, zero. After that, you need to
pass the font thickness. As a phone thickness,
I'm going to pass two. So we successfully put
this text in this frame. After that, we need to
display this frame. For that, we need to run, I'm ShomthodOside
the IP condition. So here, outside
the IP condition, I'm going to type CV two dot, CV two dot, I am
Shoathod I am show. Then inside the rounds, inside the double
course, I'm going to type, finger counted. Inside the double course, first, we need to define
the windowame and our windowname is
fingered counted. Then we need to pass
the frame name, which frame I want to display
our frame name is frame. Also we need to break the
loop using weitey method. I type I, CV two dot wet key. Inside the round pieces, I'm going to wait for
one millisecond and zero X F equal to equal to
ORD inside the roundresses. Inside the single course,
I'm going to pass Q. Then inside this IP condition, I want to break lute, break. Then I'm going to
release the captured. Cap dot release. Also, we need to
destroy all windows. Sum type, CV two dot,
destroy all windows. This one. And before we run this code, let
me show you something. Here, we need to make it
draw underscore landmarks. We need to add a, also into type mphand dot hand connection. It is going to define which
points are connected, like joints and tips, et cetera. After that, you can set this file and run this
code in your terminal. And if you run this
code in your terminal, at first, it's going
to show some warning. This warning, then it's
going to start your webcam. You need to wait for some
time. As you can see, it start our web camera. Now I'm going to
raise my closed hand. As you can see, now it show zero fingers and also it track all the
fingers successfully. Here you can see
all the landmarks and now I'm going to
spread all of my finger. As you can see on your screen, now it print five finger. You can notice it track all
the finger joints and teeth. And if I close my thumb, now you can see it
print four finger. If I rise it again,
again, it print five. And if I move my index finger, as you can see, now
it print four finger. At the same way it
track all the fingers. It count all the fingers and also it track all the
finger coordinates. This is how our
finger counter work, and if you want to
close this program, then you need to praise small Q in your keyboard and it's
going to break the loop. I hope you enjoy this project. Thanks for watching
this video and stay tuned for our next project.