From the course: Hands-On AI: Computer Vision Projects with Ultralytics and OpenCV

OpenCV basic operations on image

- [Instructor] In this video, we will cover basic operations of OpenCV that we will use in this course. The resources that we will use in this video are inside the course code subdirectory O1-02. We also have the input image in the subdirectory. We will use OpenCV to perform different operations on this image. Let's go to line number 7. Here, we need to perform first operation. Read the image from directory. We will use OpenCV, imread function to read the image. It accepts single argument that is part to image file. Our image is already in the same directory as our code file, so we just need to pass the image file name alongside extension. Let's store this in the image variable. So that we can use it for the next operations. Let's jump to line number 11. Here, we need to perform Operation-2. Blur the image, we will use OpenCV blur method. It requires two argument. One is the image we just read above, and second is kernel size. Let's use the kernel size of 50, 50. The kernel size value depends on project requirements. If we use higher kernel size value, we can get more intensity in blur and vice versa. Let's store it in the blurred variable. Let's jump to line number 15. Here we need to perform third operation. Visualize the image. We will use OpenCV imshow method. It requires two arguments. One is window name. Let's use test as window name. And second is the image we want to display. We have currently two images in the memory, original image and the blurred one. Let's display the blurred one. In that way, we can also see the blurred effect. But we also need to uncomment the line number 16. Otherwise we will not able to visualize the output clearly. Let's run the code. Now, I just opened the terminal and you can see I am not in the 01-02 subdirectory. I am in the course directory, which is the parent directory of the course. Let's go to 01-02 directory first, and now we just need to run the code. (keyboard clacking) So here we can see the image is displayed correctly. We can see the blurred effect as well. Let's jump to line number 20. Here we need to perform Operation-4. Write the image in the directory. But before this, let's comment the line number 15 and 16 because we do not want to display the results again. To write the image, we will use OpenCV imwrite function. It requires two argument, one is the output file name. Let's use test.png as output file name. And the second argument is the image that we want to write. Let's write the image that we just blurred. And that's all. Now, if we run the code again, we will see in the output directory, we have the test.png. So if I click here, we can see the output file also have the blurred effect. Let's comment this line, as we do not want to write the image again. Let's go to line number 24. Here, we need to perform the Operation-5, crop the image and display it. OpenCV do not support the crop operations on an image. For this, we will need to use some slicing operation. To do this, we will take input image that we just read and we will set specific coordinates in the form of x1, y1, x2, and y2. We can set any value for x1, y1, x2, y2. But here let's use the value of x1, 240, y1, 450, x2, 680, and y2, 900. And then we also need to comment the line 25 and 26. Line 25, we will use to display the image and line 26 to correctly display the output. Now we are all set. Let's run the code again. So here we can see, there is a very small area that is cropped and we have done this with the help of slicing because OpenCV do not support the crop operation on image directly. Let's close this window and continue to next operation. Let's go to line number 31. Here we need to perform Operation-6, read and process video file. But before this, let's comment the above Operation-5 code. As we do not want to crop image every time. Let's jump to line number 31 and uncomment all the code from line 31 to 40. Here we are using the video capture method from OpenCV to read the video file. It's almost similar as like we have done for the image, but instead of imread, we are using VideoCapture. And here we need to pass the video file part. We already have the video inside the 01-02 subdirectory of the course that we can use. Then on line 33, we are looping over the video capture. And then on line 34, we need to extract the frames. To extract the frames, we will initialize the variable success and the frame. And then we will call the read method of video capture. Read method will provide us the success in the form of bool variable and also the input frame from the video. Then on line 35, we have a check if the frame is captured correctly. We then need to display the frame on line 36. For this, we will use the same method, imshow as we have used above, and we will pass the window name, for example, test and the image we want to display. In the video case, the image will be framed. So you are all set. And then at the end, we just need to use the waitKey to display the video properly. And that's all. So now we have the video capture ready. Let's run the code and let's see what output we are getting. Here, we can see the video is running smoothly. So this is how you can perform different operations on the video and image using OpenCV. These operations will be very helpful in the upcoming sections of the course where we will deprocess the image, where we will resize the image or even when we will write the video files with the help of video writer or read the video file with the help of video capture.

Contents