Python OpenCV Computer Vision
Transcript
Everyone and welcome to this Python and open CV course. In this course, we'll be talking about everything you need to know. To get started with open CV in Python, we're going to start off with the very basics that is reading images and video, manipulating those media files with image transformations, and how to draw shapes and put text on those files. Then we're going to move on to the most advanced parts of open CV that is switching between color spaces bitwise operators, masking, histograms, edge detection and thresholding. And finally, to sum things up, we'll be talking about face detection and face recognition in open CV, so how to detect and find faces in an image and how to recognize them using inbuilt methods. In the last video, we'll be building a deep computer vision model to classify between the characters in The Simpsons based off some images. All material discussed will be available
on my GitHub page, and all relevant links will be put up in the description below. If that sounds exciting, don't forget to head over and subscribe to my channel. And I'll see you in the course. Hey, everybody, and welcome to this Python and urban TV coast. Over the next couple of videos, we're going to be talking about using the open CV library to perform all sorts of image and video related processing and manipulations. Now I won't be delving into what open CV is really is. But just be brief. It is a computer vision library that is available in Python, c++ and Java. A computer vision is an application of deep learning that primarily focuses on deriving insights from media files, that is images and video. Now, I'm going to assume that you already have Python installed on your system. And a good way to check this is by going to terminal and typing Python
dash dash version. Now make sure you're running a version of Python of at least 3.7 above whatever we do in this post wonderly work in some older versions of Python, and especially Python two, so just make sure that you have the latest version installed, go ahead to python.org and download the latest version from bet. Now assuming that you've done this, we can proceed to installing the packages that we require in this course. The first one is open C. So go ahead and do a pip install Open CV dash contrib dash Python. Now sometimes you may find people telling you to install just open CV dash Python. Well, this open team dash Python is basically the main package the main module of open CV, open CV dash contract dash Python includes everything in the main module, as well as a contribution modules provided by the community. So this is something I recommend you install as it includes all
of open CV functionality. You may also notice that urgency, we tried to install the NumPy package. Now NumPy is kind of a scientific computing package in Python, that's extensively used in matrix an array manipulations, transformations, reshaping and things like that. Now, we'll be using NumPy in some of the videos in this course. But don't worry if you've never used them before. It's simple and relatively easy to get started with. Now the next package, I'd like you to install a sphere. So go ahead and do pip install seer. Now, slight disclaimer, this is a package that I built to basically help you to speed up your workflow. Sierra is basically a set of utility functions that will prove super useful to you in your computer vision journey. It has a ton of super useful helper functions that will help speed up your workflow. Now, although we're not going to be using this for a good part of this course,
in fact, we'll only begin to use this in our last video of this course when we're building a deep computer vision model. I recommend you install it now so that you don't have to worry about the installation process later on. If you're interested in contributing to this package, or just simply want to explore the codebase I'll leave a link to this GitHub page in the description below. Okay, that's it for this video. In the next video, we'll be talking about how to read images and video in open CV. So I'll see you guys in the next video. Hey everybody, and welcome back to another video. In this video, we're going to be talking about how to read images and video in open CV. So I have a bunch of images in this photos folder, and a couple of videos in this videos folder. In the first half of this video, we'll be talking about how to read in images in open CV, and towards the
end we'll be actually talking about how to read in videos. So let's start off by creating a new file and call this reader dot p y. And the first thing we have to do is actually input CV two as CV. So the way we read in images in open CV is by making use of the cv.im read method. Now this method basically takes in a path to An image and returns that image as a matrix of pixels. Specifically, we're going to be trying to read this image of a cat here. So we're going to say photos slash cat dot jpg. And we're going to capture this image in a variable called IMG. Now you can also provide absolute paths. But since this photos folder is inside my current working directory, I'm going to reference those images relatively. Now once we've read in our image, we can actually display this image by using the cv.rm show method. Now this method basically displays the image as a new window. So the
two parameters we need to pass into this method is actually the name of the window, in this case is going to be kept and the actual matrix of pixels to display, which in this case is IMG. And before we actually move ahead, I do want to add an additional line a CV dot wait key zero. Now the CV or wiki zero is basically a keyboard binding function, it waits for a specific delay, or time in milliseconds for a key to be pressed. So if you pass in
zero, it basically waits for an infinite amount of time for a keyboard key to be pressed. I didn't worry too much about this, it's not really that important for this course. But we will be discussing some parts of it towards the end of this video. So let's actually save this and run by saying Python, read dot p y, and the image is displayed in a new window. Cool. Now this was a small image, this was an image of size 640 by 427. Now we're going
to try and read in this image of the same cat, but a much larger version, a 2400 by 1600 image. So we're gonna say Cat on a school large dot jpg. Let's save that and run. And as you can see, this image goes way off screen. The reason for this is because the dimensions of this image were far greater than the dimensions of the monitor that I'm currently working on. Now currently, open CV does not have an inbuilt way of dealing with images that are
far greater than your computer screen. There are ways to mitigate this issue. And we'll be discussing them in the next video when we talk about resizing and rescaling frames and images. But for now, just know that if you have images, if you have large images, it's possibly going to go off screen. So that's it for reading images, we can then move on to reading videos in open CV. So that's called reading videos. So what we're going to do
is we're actually going to read in this video of a dog, and the way we read in videos is by actually creating a capture variable and setting this equal to CV dot video capture. Now this method either takes an integer arguments like 0123, etc, or a path to a video file. Now you would provide an integer argument like 012, and three, if you are using your webcam or a camera that is connected to your computer. In most cases, your webcam would
be referenced by using the integer zero. But if you have multiple cameras connected to your computer, you could reference them by using the appropriate argument. For example, zero would reference your webcam, one would reference the first camera that is connected to your computer to would reference the second camera and so on. But in this video, we'll be actually looking at how to read an already existing videos from a file path. Now specifically, we'll be reading this dog, this video for dog here. And the way we do that is by providing the path so videos, slash dog dot mp4. Now, here's where reading videos is kind of like different from reading images. In the case of reading and videos, we actually use a one loop and read the video frame by frame. So we're going to say while true. And the first thing we want to do inside this loop is say is true. And frame is equal to capture dot
read. Now this capture dot read basically reads in this video frame by frame, it returns the frame and a Boolean that says whether the frame was successfully read in or not. Do you display this video we can actually display an individual frame. So we do this by saying TV on show and we call this video and we pass in the frame and finally for some way to stop the Do from playing indefinitely is by saying if CV don't wait, Ki 20 and 0x
ff is equal to equal to Ord of D. There we want to break out of this while loop. And once that's done, we can actually release the capture pointer. And we can destroy all windows. And we can get rid of this. So basically just to recap, the capture variable is an instance of this video capture clause. Inside of while loop, we grab the video frame by frame. By utilizing the captured read method, we display each frame of the video by using
the CV dot m show method. And finally, for some way to break out of this while loop, we say if See, we don't wait ki 20 if and 0x f f is equal to or D, which basically says that if the letter D is pressed, then break out of this loop and stop displaying the video. And finally, we release the capture device and we destroy all the windows since we don't need them anymore. So let's save that and run. And we get a video displayed in a window
like this. But once it's done, you will notice that the video suddenly stops and you get this error. More specifically a negative 215 assertion failed error. Now if you ever get an error like this negative 215 assertion failed. This would mean in almost all cases is that open CV could not find a media file at that particular location that you specified. Now, the reason why it happened in the video is because the video ran out of frames, open
CV could not find any more frames after the last frame in this video. So it unexpectedly broke out of the while loop by itself by raising a CV to error. And now you're gonna get the same error. If we comment this out, we uncomment this out. And we specify a wrong path to this image. So I see me Oh wait, wait key, zero, save that and run and we get the exact same error. This basically again says that open CV could not find the image or the video frame
at a particular location basically, it could not be ready. That's what it's saying. So that's pretty much it. For this video, we talked about how to read any images in open CV and how to read in videos using the video capture class. In the next video, we'll be talking about how to rescale and resize images and video frames in open CV. So see you then. Hey, everyone, and welcome back. In this video, we're going to be talking about how to resize
and rescale images and video frames in open CV. Now, we usually resize and rescale video files and images to prevent computational strain. Large media files tend to store a lot of information in it and displaying it takes up a lot of processing needs that your computer needs to assign. So by resizing and rescaling, we're actually trying to get rid of some of that information. rescaling video implies modifying its height and width to
a particular height and width. Generally, it's always best practice to downscale or change the width and height of your video files to a smaller value than the original dimensions. The reason for this is because while most cameras your webcam included, do not support going higher than its maximum capability. So for example, if a camera shoots in 720 P, chances are it's not going to be able to shoot in 1080 P or higher. So to rescale
a video frame or an image, we can create a function called def rescale frame. And we can pass in the frame to be resized and scale the value which by default we're going to set as point seven five. So what I'm going to do next is I'm going to say with is equal to frame dot shape of one of one times scale. And I'm going to copy this and do the same thing for the height. Now remember frame no shape of one is basically the width of your
frame or your image and frame note shape of zero is basically the height of the image. Now since width and height are integers, I can actually convert these floating point values to an integer by converting it to an iron T. And what we're going to be doing is we're going to create a variable called dimensions, and set this equal to a table of width, comma height. And we can actually return CV don't resize the frame, the dimensions, and we can
pass in it interpolations of CV dot into on the school area. Now we'll be talking about CV dot resize in an upcoming video. But for now, just note that it resizes the frame to a particular dimension. So that's all a function does, it takes in the frame, and it scales that frame by a particular scalar value, which by default is point seven, five. So let's actually try to see this in action. Let's go back to this readout p y, and grab this
code. And we can paste there, we don't need us for now. uncomment these out. Now what I'm going to do is after I've read in the frame, I'm going to create a new frame call frame on this go resized, and set this equal to rescale frame of frame. And let's leave the scale value is point seven, five. And we can actually display this video resized by passing a frame on the scope resized. Resize. So let's save that and run Python rescale
del p why that was an error. Okay, we don't need this. Let's close that out, Save and Run. And this was our original video. And this is actually a resize the video with the video resize by point seven 570 5%. We can modify this by changing the scale of value to to maybe point two, so we rescaling to 20%. And we get an even smaller video in a new window. So let's close that out. Now you can also apply this on images. So let's uncomment
that out, change that to cat dot jpg. And we can do receive our show. Image and pawson the resized image. And we can create a resize image by calling rescale frame and we could pass in the IMG. So let's see that in Rome. And this is a small videos we're not concerned with that. This is actually the big image the large image. And this is the recent version of this image. So let's close that out. Now there is another way of rescaling or resizing
video frames specifically. And that's actually using the capture dot set method. Now this is specifically for videos, and will work for images. So let's go ahead and try to do that. Let's call this depth change rez. So we're changing we're changing the resolution of the image of video. And we can pass in a width and a height. And what we're going to do is we're going to say capture, don't set three comma with and we're going to do
the same thing with capture dot set four comma height. Now three info basically stands for the properties of this capture class. So three references the width and full references the height. You can also expand this to maybe change the brightness in the image. And I think you can reference that by setting this to 10. But for now we're going to be interested in the width and the height. Now, I do want to point out this, this method will work for
images, videos, and live video. Basically, for everything you can use this rescale frame method. But the changes function only works for live video. That is video you read in from an external camera or your webcam for instance. So video that is going on currently, this is not going to work on standalone video files, video files that already exist. It just doesn't work. So if you're trying to change the resolution of live video, then
go with this function if you're trying to change the resolution of an old already existing video, then go with this function. So that's pretty much it for this video that we talked about how to resize and rescale video frames and images in open CV. In the next video, we'll be talking about how to draw shapes, and write text on an image. So that's everything. I'll see you guys in the next video. Hey, everyone, and welcome back to another video.
In this video, we're going to be talking about how to draw and write on images. So go ahead and create a new file and call this draw dot p y. We're going to input CV two and CV, we're going to input the NumPy package that open CV had installed previously. And we're going to input that as MP, we will read in an image by saying OMG is equal to cv.rm, read person photos, photos slash cat dot jpg, we can display that image in a new window. And we can do
receive out of weight key zero. Now there are two ways we can draw on images by actually drawing on standalone images like this image of a cat to or we can create a dummy image or a blank image to work with. And the way in which we can create a blank image is by saying blank is equal to NP dot zeros of shape 500 by 500. And give it a data type of ui 98. You ID eight is basically an image the datatype of an image. So if you want to try
and see this image, see what this image looks like. We can say blank, and we can pass in like save that and run Python drawed or p y. And this is basically the blank image that you can draw on. So we're going to be using that in instead of drawing this cat image. But feel free to use this cat image if you'd like. So the first thing we're going to do is try to paint is trying to paint the image a certain color. And the way we do this is
by saying blank and reference all the pixels and set this equal to zero comma 255 comma zero. So by painting the entire image green, and we can display this image by saying green in passing the blank image, save that and run. Can I broadcast Yeah, okay, you need to give it a shape of three, basically, we are giving the shape of height, width, and the number of color channels. So just keep that in mind save up. And this is the green
image that we get cool, we can even change this and try to change this to red zero comma 255. Save that. And we get a red image over here. Now you can also call a certain portion of the image by basically giving it a range of pixels. So we can say 200 to 300. And then from 300 to 400. Save that and run and you got a Red Square in this image. The next thing we're going to do is we're going to draw a rectangle. And the way we do this is by using
the CV don't rectangle method. This method takes in an image to draw the rectangle over, which in this case is blank. And it takes in point 1.2, color, thickness and a line type if you'd like. So the point one will specifically be zero comma zero, which is the origin. And we can go all the way across to 250 comma 250. Let's give it a color of zero comma 255 comma zero, which is green, give it a thickness of let's say two, which
is basically saying the thickness of the borders. And once that's done, we can display this image by saying let's call this rectangle in passing and passing the blank image. We can comment this out since we don't need this anymore. And we get a green rectangle that goes all the way from the origin to 250 comma 250. You can play around with it if you like so we can go from 250 to maybe 500. And it goes all the way across the image. So you
basically divide the image in half. Now there is a way of filling in this image a certain color. And the way we do this is instead of saying thickness is equal to two, we say thickness is equal to CV dot field. That basically fills in the rectangle to get this green rectangle. Now Alternatively, you can also specify this as negative one, negative one. And we get the same result, what we can also do is, instead of giving it fixed values like 250, and 500,
what we could do is we could say, IMG done shape of zero, of one divided by divided by two, and image dome shape of zero, divided by divided by two. Let's save that and run. image is not in fact, God, this is blank, this is blank, save that and run. And we get a nice little rectangle, or square, if you will, in this image, what it basically did is it scaled the rectangle from instead of being these, this entire square, this rectangle
basically has dimensions, half of that of the original image. So moving on, let's try and draw a circle. Draw circle. This is also fairly straightforward, we do a CV dot circle. And we pass in the blank image. And we give it a center, which basically the coordinates of the center for now let's set this to the midpoint of this image by saying 250 comma 250. Alternatively, you could also get this let's give it a radius of 40 pixels, give
it a color of zero comma, zero comma 255, which is red BGR. And give it a thickness of let's say three. We can display this image, say, circle is equal to blank. And we get a nice little circle over here, that has its center at 250 km 250, and radius of 40 pixels. Again, you can also fill in this image by giving a thickness of negative one. Here, we get a nice little dot here in the middle. Cool. Now there's something else that I forgot.
And that is how to draw a line a standalone line on the image. That again, is fairly straightforward, say draw a line, we use a cv.in line method. And this takes in the image to draw the line on and two points, that's just copy these points, basically everything. And this basically draws a point from zero comma zero to half these image dimensions. So that's 252 50. And then it draws a line of color zero comma 255, comma zero. Let's set this to full white
2255, d 5255. And it's green thickness you can specify as three. And we didn't display this image. See you don't on show colas line, rule the line, blank image, and we get a line that goes all across from zero comma, zero comma zero to 250, comma 250. Let's try and play around with this. And let's draw a line from 100 to maybe 250. And then it goes all the way to 300 to 400, save that. And you've got a line that goes from 100 100 to 300,
comma 400. Cool. And finally, the last thing that we will discuss in this video is how to write text on an image that that's right text on an image. Now, the way we do this is very straightforward. We see we do a CV dot put text. And this will put text on the blank image. We specify what we want to put on. So let's say hello. We can give it an origin, which is basically where do we want to draw the image from? Let's set this to
225 and 225. And we can also specify font face. Now open CV comes with inbuilt fonts. And we will be using the CV dot font unschool Hershey ns go. We'll be using the triple x, you have complex you have duplex you have plain. You have script simplex and a lot of inbuilt phones. But for now, let's use a triplex. Let's give this a font scale, which is basically how much do you want to scale the font by, let's set this to 1.0. We don't want to scale
a font, let's give it a color of zero comma 255, comma zero, and give it a thickness of two. Commit that out. And we can display this image. So you don't I'm show let's call this text and pass in the blank image. And we get some text that is placed on the image. You play around with it and say, Hello, my name is Jason. Save and Run. And it goes off screen. I when we're dealing with large images, but we can there's no way of actually handling
this except for maybe changing the margins here a bit, too, we can do that by saying let's say it's zero comma two to five. And it sounds from zero and says Hello, my name is yes. So that's it. For this video, we talked about how to draw shapes, how to draw a lines, rectangles, circles and how to write text on an image. Now in the next video, we'll be talking about basic functions in open CV, that you're most likely going to come across
whatever project in computer vision you end up doing. So if that's it, I'll see you guys in the next video. Hey, everyone, and welcome back to another video. In this video, we're going to be talking about the most basic functions in open CV that you're going to come across in whatever computer vision project you end up building. So let's start off with the first function. And that is converting an image to grayscale. So we've written an image, and
we've displayed that image in a new window. And currently, this is a BGR image, a three channel blue, green and red image. Now there are ways in open CV to essentially convert those BGR images to grayscale so that you only see the intensity distribution of pixels rather than the color itself. So the way we do that is by saying gray is equal to CV dot CBT color, we pass in the image that we want to convert from, which is IMG, and we specify
a color code. Now this kind of code is CV dealt kind of unskilled BGR. To great, since we're converting a BGR image to a grayscale image. And we can go ahead and display this image by saying CV don't show passing gray and pass in the gray image. Save that and run your Python basic.pi. And this was the original image. And this is the grayscale image. Let's try this with another image. Slide with no this is the image of a park
in Boston save and maybe change that to Boston. And this is the BGR image in open CV, and this is its corresponding grayscale image. So nothing too fancy. We've just converted from a BGR image to a grayscale image. The next function we're going to discuss is how to blur an image. Now blurring an image essentially removes some of the noise that exists in an image. For example, in an image, there may be some extra elements that were there because
of bad lighting when the image was taken, or maybe some issues with the camera sensor and so on. And some of the ways we can actually reduce this noise is by applying a slight blur. There are way too many blurring techniques which we will get into in the advanced part of this goes. But for now we're just going to use the Gaussian Blur. So what we're going to do is we're going to create a blurred image. I think blur is equal to CV dot Gaussian Blur.
And this image will take an associate image which is the IMG it will take in a kernel size, which is actually a two by two tuple which is basically the window size that open CV uses to compute the blown the image. We'll get into this in the advanced part of the scope so don't worry too much about this, just know that this kernel size has to be an odd number. So So let's start a real simple and keep the kernel size to three by three. And another thing that we have to specify is CV dot border on school default. So go ahead and try to display this image, the same blur, and pawson blue. Now, you will be able to notice some of the differences in this image. And that is because of the blur that is applied on it. Right this people in the background are pretty clear on this image. And over here, they're slightly blurred. To increase a blind his image, we can essentially
increase the kernel size from three by three to seven by seven, save that and run. And this is the image that is way more blurred than the previous image. So that's it. The next function we're going to discuss is how to create an edge cascade, which is basically trying to find the edges that are present in the image. Now again, there are many edge cascades that are available. But for this video, we're going to be using the canny edge detector, which is pretty famous in the computer vision world. Essentially, it's a multi step process that involves a lot of blurring and then involves a lot of grading computations and stuff like that. So we're gonna say, Kenny, Kenny is equal to CV dot Kenny, we pass in the image, we pass in to threshold values, which for now I'm going to say 125 and 175. Let's go ahead and try to display this image, get the Kenny images. And we can pass in county.
Save that and run. And these were the edges that were found in this image. As you can see that it hardly any edges found in the sky. But a lot of features in the trees and the buildings. And quite a few, you know features and edges in the grass and stuff. We can reduce some of these edges by essentially blurring the image. And the way we do that is instead of passing the IMG, we pass in the blur. See that run. And as you can see that there were
far less edges that were found in the image. And this is a way you can basically reduce the amount of edges that were found by a lot by applying a lot of blur, or get rid of some of the edges by applying a slight blur. Now the next function we're going to discuss is how to dilate an image using a specific structuring element. Now the structuring element that we are going to use is actually these edges, the canny edges that were found, so we're gonna say dominating the image. And the way we do that is by saying dilated is equal to CV dot dilate. And this will take in the structuring element, which is basically the canny edges. And we'll take a kernel size, which we'll specify as three by three for now. And it will also take n iterations of one. Now, dilation can be applied using several iterations of the time, but for now, we're just going to stick with one. So go ahead and try to display
this image by saying CV dot m shope. Call this dilated. And we can pass in David. Save that and run. And if these were, if these were edges, these are the dilated edges, we can maybe increase the kernel size to maybe seven by seven and tried to see what that does hold on. And nothing much was done. Not much difference was that let's try to increase the number of iterations to maybe three. And it's definitely way thicker. But you're gonna
see subtle differences with the amount of features and edges that you find. Now there is a way of eroding this dilated image to get back this structuring element. Now, it's not going to be perfect, but it will work in some cases. So we're gonna say, call this roading and we call this eroded is equal to CV don't erode, it will take in the dilated image, pass and dilated, it will take a kernel size of let's start off with three by three
and given n iterations of one just for now. And we didn't display this image show coolest clothes eroded, eroded and if this was your structuring element, and this was your dilate image, this is basically the result you get from eroding this image. Now, it isn't the same as a structural element. But you can just about to make the features that. But you can see that between this and this, there is a subtle change in the edges and the thickness
of these edges, we can maybe try to match these values, so that we attempt so that there is an attempt to get back this edge cascade. And yes, we got the edges back there, as you can see that you compare these two, they look pretty much the same. And the edges are the same. So essentially, if you follow the same steps, you can, in most cases, get back the same edge cascade. And probably the last function that we're going to discuss is how to resize
and crop an image. So we're going to start with resize. So we come to resizing video frames and images in the previous video in one of the previous videos. But we're just going to touch on the CBO resize function just a bit. So we're going to say resized, resized equal to CV dot resize, this will take an image to be resized, and it will take in a destination size, which let's set this to 500 by 500. And so this essentially takes
in this image of the park, and resize that image to 500 by 500, ignoring the aspect ratio. So we display this image by saying saved out I'm sure resized and resized. Save that and run. And let's go back to this image. If this is the original image, this is the image that was resized to 500 by 500. Now by default, there is an interpolation that occurs in the background, and that is CV dot into on the scope area. Now this interpolation method
is useful if you are shrinking the image to dimensions that are smaller than that of the original dimensions. But in some cases, if you are trying to enlarge the image and scale the image to a much larger dimensions, you will probably use the inter underscore linear or the inter on scope cubic. Now cubic is the slowest among them all. But the resulting image that you get is of a much higher quality than the inter on scope area or the inter
underscore linear. So let's touch on cropping. And that's basically by utilizing the fact that images are arrays. And we can employ something called Array Slicing, we can select a portion of the image on the basis of your pixel values. So we can say cropped is equal to the image. And we can select a region from 50 to 200. And from 200 to 400. And we can display this image Cole is cropped, possibly cropped. And this is a cropped image of let's
go back here of this original image, you try to superimpose them, it's probably going to be you. Yeah, it's basically this portion. So that's pretty much it. For this video, we talked about the most basic functions in open CV, we talked about converting an image to grayscale by applying some blur by creating an edge cascade by dilating the image by eroding that dilated image by resizing an image and trying to crop an image using Array Slicing.
In the next video, we're going to be talking about image transformations in open CV, that's translation, rotation, resizing, flipping and cropping, so if you have any questions, leave them in the comments below. Otherwise, I'll see you guys in the next video. Hey, everyone, and welcome back to this Python and open CV course. In this section, we're going to cover basic image transformations. Now these are common techniques that you would likely apply to images, including translation, rotation, resizing, clipping and cropping. So let's start off with translation. Translation is basically shifting an image along the x and y axis. So using translation, you can shift an image up, down, left, right, or with any combination of the above. So so to translate an image, we can create a translating function, we're gonna call this def translate This translation function will take in an image to translate
and take an x and y, x and y basically stands for the number of pixels, you want to shift along the x axis and the y axis respectively. So do translate an image, we need to create a translation matrix. So we're going to call this transmit is equal to NP dot float 32. And this will take in a list with two lists inside of it. And the first list we're going to say, one comma zero comma x, and zero comma one comma y. And since we're using NumPy,
we can import NumPy, import NumPy as NP. And once we've created our translation matrix, we can essentially get the dimensions of the image saying dimensions, which is a tuple of image don't shave off one, which is the width an image dot shape of zero, which is the height. And we can return CV dot warp a fine. This will take in the image matrix to trans MIT animal taking the dimensions. And with that data, we can essentially translate
our image. And before we do that, I do want to mention that if you have negative values for x, you're essentially translating the image to the left, negative negative y values implies shifting up positive x values implies shifting to the right. And as you guessed, positive y values shifted down. So let's create our first translated image. We're setting this equal to translate, we're going to pass in the image, the image and we're going to
shift the image right by 100 pixels, and down by 100 pixels. That's to receive it on on the show, translated and translate tip. Save that and run Python krones formations dot p y. And this is your translated image, it was shifted down by 100 pixels and shifted to the right by 100 pixels. So let's change that. Let's shift the image left by 100 pixels and down by 100 pixels. So we pass in negative values for x and it moved to the left. Feel
free to play around with these values as you see fit. Just know that negative x shifts to the left, negative y shoves it up, x shifted to the right and positive y values shifted down. Moving on, let's talk about rotation. rotation is exactly what it sounds like rotating an image by some angle. Open CV allows you to specify any point any rotation point that you'd like to rotate the image around. Usually if the center but but with open CV, you could
specify any arbitrary point it could be any corner, it could be 10 pixels to the right 40 pixels down, and you can shift the image around that point. So to draw to rotate the image, we can create a rotating function, let's call this dev rotate. This will take an image angle to rotate around and a rotation point which we're going to say which we're going to set is not so we're going to grab the height and width of the image by pressing
by setting this equal to IMG dot shape of the first two values. Basically, if the rotation point is none, we are going to assume that we want to rotate around the center. So we're going to say rot point is equal to width divided by two divided by two in height divided by divided by two. And we can essentially create the rotation matrix like we did with the translation matrix. By setting this equal to rot met is equal to CV dot get rotation matrix 2d. We're
going to pass in the center the rotation point and angle to rotate around which is angle and a scale value. Now we're not interested in scaling the image when we've rotated so we can set this to 1.0. value we can set a dimensions variable equal to the width and the height and we can return the rotated image which is a CV don't warp a fine image rot met the destination size which is dimensions. And that's it. That's all we need for this
function. So we can create a rotated image by setting this equal to rotate, and we can rotate the original image by 45 degrees. So let's display this image, call this rotated, and pass and rotated. Save that in rock. And this is your rotated image. As you can see, it was rotated counterclockwise by 45 degrees. If somehow you wanted to rotate this image clockwise, just specify negative values for this angle, and it will rotate the image around
rotated clockwise. Now you can also rotate a rotated image that is take this image and rotated by 45 degrees further. So let's call this rotated, rotated rotated is equal to rotate or rotate tid. And we can rotate this image by another 45 degrees. So we're rotating it clockwise. And we can see the.on show called is rotated, rotated. And we can pause and rotated, rotated, whatever, rotate it. And this is your rotate rotated image. Now the
reason why these black lines were included is because if there's no image in it, if there's no part of the image in it, it's going to be black by default. So when you took this image and rotated it by 45 degrees, you essentially rotated the image, but introduce these black triangles. Now if you tried to rotate this image further by some angle, you are also trying to rotate these black triangles along with it. So that's why you get these kind of a skewed image. So there's additional triangles are included over here. But save yourself the trouble and basically add up these angles and you will get the final angle. So we can change that to 90 and retake the original image by negative 90. And this is essentially the image that we were trying to go for, take this image rotated 45 degrees clockwise and rotate this 45 degrees image by further 45 degrees, save yourself the trouble and add
those two angle values. So so far, we've covered two image transformations, translation and rotation. Now we're going to explore how to resize an image. Now this is nothing too different from what we've discussed previously. But let's touch on adjust a bit resizing. And we can create a resized variable and set this equal to CV don't resize, we can pass in the image to resize and the destination signs of maybe 500 by 500. And by default the interpolation
is CV dot inter underscore area. You can maybe change this to into underscore linear or inter underscore cubic. Definitely a matter of preference depending on whether you're enlarging or shrinking the image. If you're shrinking the image, you will probably go for into underscore area or stick with default. If you're enlarging the image, you could probably use the inter underscore linear or the dansko cubic cubic is slower, but the resulting image is better

Ask anything
We've read this whole study set, so questions about it get the best answers.