OpenCV python: Beginner’s guide
OpenCV is a huge open-source for computer vision, machine learning, and image processing that currently plays a big role in real-time operations. This article will tell you about OpenCV. Then it will show the implementations of different applications of OpenCV.
In OpenCV, CV means Computer Vision, is defined as a field of research that helps computers understand the content of digital images such as photos and videos.
In the previous article, we have already covered the basics of OpenCV. In this article, we will cover the implementation of some very interesting applications of OpenCV. So let’s get started with OpenCV python.
Table of contents
Best-suited IT & Software courses for you
Learn IT & Software with these high-rated online courses
What is OpenCV?
OpenCV is an open source library and is a great tool for image processing and computer vision related tasks. It is an open source library that can be used to perform tasks such as face detection, object tracking, landmark detection and more. It supports multiple languages including python, java C. Although in this article we are limited to python only. The library contains hundreds of useful functions and algorithms, all freely available to us. Some of these functions are very common and are used in almost all computer vision tasks. Many features have not yet been explored and have not received much attention.
OpenCV supports various programming languages such as C++, Python, and Java and is available on various platforms such as Windows, Linux, OS X, Android, and iOS.
This blog will give you practical insights about OpenCV use.
You can also read
Must Read: Top 10 Powerful Python Libraries for Data Science
Must Check: Top Python Online Courses and Certifications
Must Read: What is Python
OpenCV Python
We will not be going into very basic operations of OpenCV like reading, resizing, Converting Grayscale from an RGB image, cropping, flipping, and saving an image. Want to learn these operations? You can follow our blog: Image Processing using OpenCV.
We will be learning some exciting features of OpenCV by practically implementing it. So let’s get started-
By the way, I’ll be working with OpenCV using Google Colab. As an editor, PyCharm is available. Most libraries in Google Colab are already installed by default; with PyCharm, however, you must explicitly install the library before importing and using it. For help installing any packages or libraries in PyCharm, search for information online.
Must Read: Why use Python Datetime Module?
Must Read: Abstraction in Python
Loading libraries
Import numpy as np # used for linear algebra, transformations, etc.import cv2 as cv # used for computer vision tasksfrom google.colab.patches import cv2_imshow
We are unable to utilize the cv.imshow() or cv2.imshow() function directly in Colab or a Jupyter notebook. Because it interferes with jupyter sessions, cv2.imshow() raises the DisabledFunctionError error, making it illegal or restricted in Colab. To display images, think about utilizing google.colab.patches import cv2 imshow. However, while working with any other Python, we utilize cv2.imshow() in PyCharm or any other editor.
Reading an image
# Reading the imageimage = cv2.imread('image.jpg')cv2_imshow(image) cv2.waitKey() # This is necessary so that the image doesn't close immediately. #It will run continuously until the key presses. cv2.destroyAllWindows()
We’ll read an image using imread(), and you can also determine the picture’s data type (which prints that the dtype of an image is a numpy ndarray). Additionally, you may examine the array’s form and the manner in which the open cv renders the image. Both Colab and PyCharm use a similar approach to how this imread() function operates. One parameter is required: the picture name and extension (and if the image is stored in some other drive or folder, then we need to mention the path as well).
Writing on image
font = cv2.FONT_HERSHEY_COMPLEXcv2.putText(image,text='NaukriLearning',org=(10,150), fontFace=font,fontScale= 4,color=(0,255,0),thickness=6)plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB));
You can see we have used putText() to write text where we have defined its size, font, color, font scale, and thickness.
Blurring image
img_blur_3 = cv2.blur(image, (20,20)) cv2_imshow(img_blur_3)cv2.waitKey(0)cv2.destroyAllWindows()
Drawing a circle on the image
ima=cv2.circle(image,(180,180), 30, (255,255,0), 120) cv2_imshow(ima) cv2.waitKey(0) cv2.destroyAllWindows()
Checking red,green, and blue pictures of the original picture
image=cv2.resize(image,(180,180))b,g,r = cv2.split(image) Cv2_imshow(b) #This will show blue imagecv2_imshow(g)cv2_imshow(r)cv2.waitKey(0)cv2.destroyAllWindows()
The first image represents the blue channel image, the second image represents the Green Image channel, and the third image is the Red Image Channel. This is done by splitting the image.
cv2.split() is used to split colored/multi-channel images into separate single-channel images.
Convert Images into Cartoon image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)gray = cv2.GaussianBlur(gray,(5,5),-1)edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,9,10)color = cv2.bilateralFilter(image, 20, 245, 245)cartoon = cv2.bitwise_and(color, color, mask=edges)image=cv2.resize(image,(400,400))cv2_imshow(cartoon)cv2.waitKey(0)
We have three types of blurring.
Gaussian Blurring-It is a common effect in graphics software, usually used to lessen detail and visual noise.
Median Blur-A nonlinear digital filtering method called the Median Filter is frequently used to eliminate noise from an image or signal. It eliminates noise while maintaining edges.
Bilateral Blur-A nonlinear, edge-preserving, and noise-reduction smoothing filter for images is known as a bilateral filter. The weak edges are discarded while keeping the sharp edges in this.
Note: We have to smooth the image, so we used a Bilateral filter. We have to highlight the edges, so we have used adaptive filters.
Bitwise_and operation is used for creating masks of the image, and it is applied on edges, so we wrote mask=edges and will get a cartoon image.
There is a slight difference in RGB encoding in OpenCV; this is BGR. So we use the cvtColor() function, which means to convert the color and take two parameters: the first – the name of the image variable, and the second – the color scheme. In our case, we use the color COLOR_BGR2GRAY.
Canny edge detection using OpenCV
edges = cv2.Canny(image,100,200)plt.subplot(121),plt.imshow(image,cmap='gray')plt.title('Original Image'), plt.xticks([]), plt.yticks([])plt.subplot(122),plt.imshow(edges,cmap='gray')plt.title('Edge Image'), plt.xticks([]), plt.yticks([])image=cv2.resize(image,(1000,900))plt.show()cv2.waitKey(0)
All the above arguments are combined into a single function called cv2.Canny by OpenCV (). We’ll examine its application. Our input image is the first argument. Our minVal and maxVal are the second and third arguments, respectively. This function detects the edge in the image.
Conclusion
OpenCV is an integral part of the computer vision community, with the help of which thousands of amazing applications can be created. Some of these applications you might think we use in our daily life. The cartoon image we created was a copy of the Snapchat version of the image.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio