Found 33676 Articles for Programming

How to detect polygons in image using OpenCV Python?

Shahid Akhtar Khan
Updated on 05-Dec-2022 11:08:22

6K+ Views

We first detect all the object contours in the image to detect a polygon. Then Loop over all contours. Find the approximate contour for each of the contours. If the number of vertex points in the approximate contour is 5 or more then draw the contour and set it as a triangle. See the below pseudocode. for cnt in contours: approx = cv2.approxPolyDP() if len(approx) >= 5: cv2.drawContours() cv2.putText("Polygon") Steps We could use the following steps to detect polygons in an ... Read More

How to detect eyes in an image using OpenCV Python?

Shahid Akhtar Khan
Updated on 05-Dec-2022 11:06:08

12K+ Views

A "haar cascade classifier" is an effective machine learning based approach for object detection. To train a haar cascade classifier for eye detection, the algorithm initially needs a lot of positive images (images of eyes) and negative images (images without eyes). Then the classifier is trained from these positive and negative images. It is then used to detect eyes in other images. We can use already trained haar cascades for eye detection. For eye detection in the input image, we need two haar cascades one for face detection and other for eye detection. We will use the following two ... Read More

How to change the contrast and brightness of an image using OpenCV in Python?

Shahid Akhtar Khan
Updated on 26-Aug-2023 08:24:50

49K+ Views

In OpenCV, to change the contrast and brightness of an image we could use cv2.convertScaleAbs(). The syntax we use for this method is as follows − cv2.convertScaleAbs(image, alpha, beta) Where image is the original input image. alpha is the contrast value. To lower the contrast, use 0 < alpha < 1. And for higher contrast use alpha > 1. beta is the brightness value. A good range for brightness value is [-127, 127] We could also apply the cv2.addWeighted() function to change the contrast and brightness of an image. We have discussed it in example 2. Steps ... Read More

Opencv Python – How to display the coordinates of points clicked on an image?

Shahid Akhtar Khan
Updated on 05-Dec-2022 11:01:44

15K+ Views

OpenCV provides us with different types of mouse events. There are different types of muse events such as left or right button click, mouse move, left button double click etc. A mouse event returns the coordinates (x, y) of the mouse event. To perform an action when an event is performed we define a mouse callback function. We use left button click (cv2.EVENT_LBUTTONDOWN) and right button click (cv2.EVENT_RBUTTONDOWN) to display the coordinates of the points clicked on the image. Steps To display the coordinates of points clicked on the input image, you can follow the steps given below − ... Read More

How to detect a face and draw a bounding box around it using OpenCV Python?

Shahid Akhtar Khan
Updated on 05-Dec-2022 10:59:10

5K+ Views

We detect a face in an image using a haar cascade classifier. A haar cascade classifier is an effective machine learning based approach for object detection. We can train our own haar cascade for training data but here we use already trained haar cascades for face detection. We will use haarcascade_frontalface_alt.xml as a "haar cascade" XML file for face detection. How to Download Haarcascades? You can find different haarcascades following the GitHub website address − https://github.com/opencv/opencv/tree/master/data/haarcascades To download the haar cascade for face detection, click on the haarcascade_frontalface_alt.xml file. Open it in raw format, right click and save. ... Read More

How to perform image transpose using OpenCV Python?

Shahid Akhtar Khan
Updated on 05-Dec-2022 10:56:29

2K+ Views

In OpenCV, the image is NumPy ndarray. The image transpose operation in OpenCV is performed as the transpose of a NumPy 2D array (matrix). A matrix is transposed along its major diagonal. A transposed image is a flipped image over its diagonal. We use cv2.transpose() to transpose an image. Steps We could use the following steps to transpose an input image − Import required libraries OpenCV and Matplotlib. Make sure you have already installed them. Read the input image using cv2.imread(). Specify the full path of the image. Assign the image to a variable img. Transpose the input ... Read More

Color quantization in an image using K-means in OpenCV Python?

Shahid Akhtar Khan
Updated on 05-Dec-2022 10:54:13

3K+ Views

In the process of Color Quantization the number of colors used in an image is reduced. One reason to do so is to reduce the memory. Sometimes, some devices can produce only a limited number of colors. In these cases, color quantization is performed. We use cv2.kmeans() to apply k-means clustering for color quantization. Steps To implement color quantization in an image using K-means clustering, you could follow the steps given below − Import required libraries OpenCV and NumPy. Make sure you have already installed them. Read two input images using cv2.imread() method. Specify the full path of the ... Read More

How to create a depth map from stereo images in OpenCV Python?

Shahid Akhtar Khan
Updated on 05-Dec-2022 10:51:10

10K+ Views

A depth map can be created using stereo images. To construct a depth map from the stereo images, we find the disparities between the two images. For this we create an object of the StereoBM class using cv2.StereoBM_create() and compute the disparity using stereo.comput(). Where stereo is the created StereoBM object. Steps To create a depth map from the stereo images, you could follow the steps given below − Import the required libraries OpenCV, Matplotlib and NumPy. Make sure you have already installed them. Read two input images using cv2.imread()method as grayscale images. Specify the full path of ... Read More

How to implement FLANN based feature matching in OpenCV Python?

Shahid Akhtar Khan
Updated on 05-Dec-2022 10:49:01

5K+ Views

We implement feature matching between two images using Scale Invariant Feature Transform (SIFT) and FLANN (Fast Library for Approximate Nearest Neighbors). The SIFT is used to find the feature keypoints and descriptors. A FLANN based matcher with knn is used to match the descriptors in both images. We use cv2.FlannBasedMatcher() as the FLANN based matcher. Steps To implement feature matching between two images using the SIFT feature detector and FLANN based matcher, you could follow the steps given below: Import required libraries OpenCV, Matplotlib and NumPy. Make sure you have already installed them. Read two input images ... Read More

OpenCV Python – Implementing feature matching between two images using SIFT

Shahid Akhtar Khan
Updated on 05-Dec-2022 10:47:08

9K+ Views

We use Scale Invariant Feature Transform (SIFT) feature descriptor and Brute Force feature matcher to implement feature matching between two images. The SIFT is used to find the feature keypoints and descriptors in the images. A Brute Force matcher is used to match the descriptors in both images. Steps To implement feature matching between two images using the SIFT feature detector and Brute Force matcher, you could follow the steps given below − Import required libraries OpenCV, Matplotlib and NumPy. Make sure you have already installed them. Read two input images using cv2.imread() method as grayscale images. Specify the ... Read More

Advertisements