Transcript for:
OpenCV in Python Tutorial Series

Hello everybody. And welcome to a brand new tutorial series on open CV in Python. Now this is going to be a fairly long tutorial series. I'm not quite sure all of the videos that we're going to be making here. So please do leave a comment. If there's something specific that you would like to see, especially on this first video right here. And I'll definitely consider that as I create the rest of the series. But with that said, let me just give you a quick introduction to open CV in case any of you are not familiar with it. So open CV is an open source computer vision library that is used to perform image analysis, video analysis, image, processing manipulation. It can do a ton of really cool things. And in this video series, I'm going to be showing you how we can. First of all, load images, draw things onto images, how we can do facial detection and recognition, how we can do object detection, how we can track, uh, objects moving around in an image or in a video. And we have a ton of cool stuff to show here. Open CV is a really awesome library and it's actually really easy to use. So you don't need to be a machine learning expert. You don't even need to be that good at Python to follow along with this. And yeah, we're going to start with kind of the basics and then move our way into the more advanced stuff as we get through this series. So with that said, in this first video here, what I'm going to be doing is talking to you about the very basics of open CV. So how we load an image, how we display that image onto the screen, how we do some symbol manipulations. So say, you know, resize it or rotated or stuff like that. And then once we understand all of that, we can move on to some more advanced stuff, uh, in the rest of the videos in the series. So again, leave a comment with what you want to see. I'll try my best to accommodate you guys. And with that said, let's jump in. So the very first thing we need to do, one we're going to be using open CV is we need to install it. So if you were on windows, follow along with what I'm doing, if you were on Mac or on Linux, still follow along, but just realize the steps may be slightly different. So the first thing we want to do is we want to open up our terminal or our command prompt. So if you're on Mac or Linux, open up terminal, if you are on windows, open up your command prompt. Now, once we have this open, what we need to do is install open CV. So open CV is a Python package to install this. We use PIP so you can use PIP. If you're on windows, if you're on Mac or Linux, you're likely going to need to type PIP three. And then after that, you're going to say install and then open CV, hyphen Python like that. Now note that open CV is available for other programming languages as well. So you can use it in C, C plus plus C sharp, a bunch of other languages. We're going to be using Python. But anyways, this is how you install the Python module or library PIP install open CV Python. Now, if PIP doesn't work for you, try PIP three. And if for some reason, this command doesn't work at all for you, then try the following command Python, hyphen M PIP install, and then open CV, hyphen Python. Now, if that doesn't work for try Python, three hyphen M PIP install, open CV Python. And if none of those work for you, I will leave two videos linked in the description that you can follow along with that, we'll show you how to fix this command. Now they're not called exactly how to fix the PIP command, but if you watch those videos, they will walk you through how to fix this. So anyways, run those commands. One of those should hopefully work for you. I don't need to do this cause I already have it installed. And now we want to install one more thing. Or actually, I don't think we need to install anything else because this would have installed it for us. What I was going to say is we need to install num PI now. Uh, but I believe this will automatically install numb pie for us. All right? So once we have all of that installed, then what you're gonna want to do is open up your favorite code editor. I'm using sublime text. You guys can use whatever you want. You don't have to use this for this tutorial and we're just going to start writing some code. So the very first thing we need to do when we're using open CV is we need to import. So I'm going to say import CV two like that. And let me just blow this up a bit. So you guys can read it a little bit easier. So now that we have CB to import it and notice this is CB two, not open CV, which is kind of strange the name of the package. It's different than the name we use to actually install this through PIP. But regardless, what we're going to do now is load an image. So you can see, I have a folder open. This is actually on my desktop. It's called open CB tutorial inside of there. I have my tutorial one file. And then I have a folder called assets. Now you don't need to necessarily mimic what I'm doing here, but I would recommend that for this tutorial series set up a folder for yourself, put a Python file inside of that folder, call it whatever you want. Just don't call it CB two. And then it creates some folder called assets. And this is where we'll put videos, images, and all of that that we want to load throughout this series. Now you don't have to throw it in an assets folder, uh, but it just going to make sense to do so and grab any image that you want. I have my a logo right here that we're going to be loading and just throw it in the folder. So just name it something simple. Again, I named my logo dot JPEG, and then we can load it in. So just to summarize that, get some image and put it in the same directory as a Python file, and then you can move on. All right. So after we have that, what I'm going to show you how to do is load an image. So to load an image and CB two is really easy. It's IMG or whatever variable name you want is equal to, and then CV to dot. And then I am read. And then inside of, I am read, what you're going to put is the path to the image that you want to look. Now, I believe you can load JPEG and PNG images. You can probably load SVG images too, although I'm not sure. Uh, but JPEG and PNG will definitely work. So go with either of those. Anyways, I'm going to load from the assets folder, my logo dot PNG. So if you had named your folder something different here, then you would obviously change the name appropriately. What I'm saying is let's look in the assets folder. Let's look for logo PNG. All right. So this is my file name, where the path to my file. And then after that, I need to put the mode that I want to load this image in. So CB two by default is going to load your images in a blue, green, red, um, color pattern, color. I don't even know the word to describe it. Uh, but rather than loading it, red, green, blue, it's going to load it in BGR. So blue, green, and then red. Now that's not super important right now. We'll talk about that more in the next video, but I just wanted to mention that now we have three options here. When we load this image, we can load this in gray scale. We can load this as it's regular colored image, whatever it normally looks like. And then we can load it without, uh, considering transparency. So without considering the alpha values of any of the pixels. So if you're not familiar with transparency, sometimes you have an image and it has a transparent background. For example, if you want it to ignore that transparency, then what you would do is load it in the mode where you ignored the transparency. So I'm just going to copy in a kind of some notes here I have. So you guys can see what the modes are, but we have read and then hyphen or sorry, underscore color. This loads, a color image, any transparency of image will be neglected. It is the default flag. Now this is also negative one. So you could write dot. I am read underscore color. That's going to go as the second argument here or negative one, we then has have CV two dot. I am read gray scale. This is also a signified or identified with a zero loads image in gray scale mode. So just gray pixels are black and white pixels. And then we have CB two. I am read unchanged. So this loads, the image as such, including the alpha channel. So it has transparency. It will honor that. So you have negative one, zero and one. So pick which one you want. You can also write out the entire thing. So I'll show you first loading in gray scale, and then I'll go through the other modes. Let me remove these notes. So we will continue in one second, but in to quickly thank the sponsor of this video in this series, which is algo expert algo expert is the best platform to use. When preparing for your software engineering coding interviews, they have over 125 coding interview questions on the platform and more are coming out every few weeks. So with that said, check out algo expert from the link in the description and use the discount code tech with Tim for a discount on the platform. All right, so now that we've loaded an image. What we want to do is display this image. So display this image is really easy. All we're going to do is, uh, type CB two and then dot. I am show so standing for image show, and then the name of the image that we want to show. And actually before that story, we want to put the label for this window. So what this is going to do is create a window. This window will have some label for it. You can see this as our window name. So I'll just put image as my name and then the image I want to display goes afterwards, which is the variable where I just loaded in the image. All right, after I have that, what I need to do is make sure that there's a way for me to close this window. So I'm just going to write two lines of code. Then I'll explain them. We're gonna say V2 dot, wait key, and then add index zero. And then finally dot and then destroy all windows. Now what this is going to do is wait an infinite amount of time for you to press any key on the keyboard. So it's going to display the image. Then what the zero means in here is wait an infinite amount of time. If I were to put five here, this would mean wait, five seconds. And pretty much what this means is that if you don't press a key in five seconds, this is just automatically going to get skipped. Now, when I put a zero, this means wait, infinitely. So we will not go past this line in our program until we press a key. But again, if I put 10, then we'll wait up to 10 seconds for a key to be pressed. If one is not pressed, we'll move on to the next line. If one is pressing that time, we'll just immediately move on to the next line. So the reason we're doing this is because as soon as we press any key on the keyboard, we want to then destroy all of our windows so that we don't just have them kind of loaded in the background for no reason. So this is all you need to do to display an image and then be able to close it properly. So I'm going to save this file. I'm going to run this and we see seem to get an issue here. Let's see what this is. Ah, so the issue here is that my image is a JPEG image and I tried to load the PNG. So let's change this to JPEG and let's run this and there you go. We can see that we load the tech with Tim logo. It's quite large and it has the gray scale. So now I will show you loading this, uh, and by the way, press any key you want and it will close the window. You can also press the X button. I'll show you loading this in the default color mode. So negative one. So if I load this with negative one, you can see I get a nicely colored image. And then finally, if I load this in one, I should actually get the exact same thing because there's no transparent pixels in this image. So those are the different modes. That is how you load and display the image. But notice that this image is kind of off the screen, right? It's quite large. So what I want to show you now is how to resize the image, how to rotate the image. And then that is actually going to conclude this tutorial. So now that we have this image loaded, what we can do is rotate it or resize it. So to resize the image, I'm going to create a new variable. I'll just call this image again, actually. So I'll just change the value of this variable. And I'll say this is equal to C V2 and then dot. And I believe this is resize we're then going to pass the image that we want to resize, which is IMG. And then the dimensions that I want to resize it to. So if I wanted to resize it to 400, by 400, I would type 400 by 400. That's as easy as it is to resize an image. If we run that notice, now we get a much smaller image. There's actually, you know, we can see it on the screen. Okay. Now say you didn't want to resize an image by typing in the pixel values or typing in the number of pixels for height and width. And instead you want it to make it like half the height or like a one quarter, the height. Then what you would do is the following. You would change this to be zero, zero. And then I have to look at my notes here, cause I always forget what this is. And then you would type FX is equal to, and then some fraction or some floating point number that you want to multiply the number of pixels by. And that will change this. So you see what I mean? So if I type 0.5, uh, for FX and then F Y is equal to 0.5, this is going to Shrek, shrink the image by a scale of two. So we'll have the, uh, the X and it will have the Y. Now, if I made this too, it would double the size of the image. Uh, hopefully that's clear, but whenever you're using FX and F Y you just set this to be zero, zero. Okay. So let's change this to 0.5. Now, if I load this, notice the image is exactly half the size. And if I were to change this to two, to save run, you notice that it is double the size and it's quite much, okay. So that is how you resize an image. So we'll resize our image to be half the size, and now I'll show you how to rotate it. So to rotate, it is again, really easy. You type IMG or whatever variable you want to store the rotate image. And then you say CB two dots, and then rotate like that. Then inside of here, you're going to pass the image. So I am G and you're going to pass how you want to rotate this image. So if I wanted to rotate this image counterclockwise, or let's actually do clockwise, I would type CB two dots, CB two dots, and then rotate like that. And notice I'm getting a bunch of options popping up so I can rotate 90 degrees clockwise. I can rotate 90 degrees counter-clockwise and you can imagine that we can change the 90 to one 80. We can change it to two 70 and in any direction that we want. So here, I'm going to rotate 90 degrees. Counter-clockwise if you want to see all the options, you can look it up from the documentation, really straightforward. And now you'll notice that when I run this, we get a rotated image like that. So that's how you rotate an image. Again, we could do just rotate 90, and then instead of counterclockwise, we could just say clockwise, and this would then rotate it clockwise. Okay. So very last thing I will show you is how to write an image. So a lot of times you kind of do some manipulations to an image, and then you actually want to save it to do that is really straightforward. All you type is CB two like that, then I am right. And then you type the name of the image. Actually, let me make sure this is correct. So sorry, you don't type the name of the image, but you type the name of the file that you want to store this image to. So in this case, I'm just going to say new underscore IMG dot JPG. And then I will put my source image, which is IMG. And what this will do is just write out whatever we've done to this image. So whatever the image variable is to the file, new, uh, IMG dot JPEG. So let's run this and notice that here a new file gets created in the top left-hand corner. Now, if I go to this file, you can see, we have the rotated tech with Tim Logan. All right. So that's going to conclude this tutorial. I apologize if this wasn't terribly interesting, that is the basics of open CV. And now that you understand that we can move on to some more advanced stuff and do some kind of more interesting image, manipulations, video manipulations, all of that fun stuff. So that said, if you guys enjoyed it, make sure you pay, like subscribe to the channel and I will see you in another open CV..