Find Patterns in a Chessboard Using OpenCV Python

Shahid Akhtar Khan
Updated on 05-Dec-2022 11:19:48

9K+ Views

We could find chessboard corners in an image using cv2.findChessboardCorners() and to draw the chessboard corners with a pattern, we could use cv2.drawChessboardCorners(). Look at the below syntaxes for these two methods − ret, corners = cv2.findChessboardCorners(img, patterSize, None) cv2.drawChessboardCorners(img, patternSize, corners, ret) Steps To find the patterns in a chessboard, you can use the steps given below − Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed it. Read the input image of a chessboard using cv2.imread() and convert it to grayscale using cv2.cvtColor(). ... Read More

Detect Cat Faces in an Image using OpenCV and Python

Shahid Akhtar Khan
Updated on 05-Dec-2022 11:17:36

2K+ Views

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

Crop and Save Detected Faces in OpenCV Python

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

6K+ Views

We can use the already trained haar cascade classifier to detect the faces in the image. To detect faces OpenCV provides us with different haar cascades as xml files. We will use haarcascade_frontalface_alt.xml for human face detection in the image. The detected face coordinates are in (x, y, w, h). To crop and save the detected face we save the image[y:y+h, x:x+w]. 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 human face detection click on the haarcascade_frontalface_alt.xml file. Open it in raw format, right click ... Read More

Implementing K-Nearest Neighbor in OpenCV Python

Shahid Akhtar Khan
Updated on 05-Dec-2022 11:12:46

990 Views

k-Nearest Neighbor (kNN) is a simple classification algorithm for supervised learning. To implement kNN in OpenCV, you can follow the steps given below − Import the required libraries OpenCV, NumPy and Matplotlib. We define two classes Red and Blue each having 25 numbers. Then generate training data for these two classes using a random generator. Next, we generate the Labels for each training data. The label for Red family numbers is 0 and for Blue family members is 1. Now plot the Red and Blue family members. Generate a new number using the random generator and plot it. Initiate ... Read More

Detect Humans in an Image Using OpenCV in Python

Shahid Akhtar Khan
Updated on 05-Dec-2022 11:10:57

15K+ Views

To detect humans in an image and draw bounding boxes around them, you can use the steps given below − Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed it. Read the input image using cv2.imread() in a grayscale. Specify the full image path. Initialize a HOG descriptor object hog = cv2.HOGDescriptor() and set the SVM detector as hog.setSVMDetector() as default people detector. Detect humans in the input image using hog.detectMultiScale(). It returns the coordinates of detected humans in (x, y, w, h) format. Loop over all ... Read More

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

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

Display Coordinates of Clicked Points on an Image in OpenCV Python

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

Detect Face and Draw Bounding Box 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

Perform Image Transpose Using OpenCV in 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

Advertisements