Identifying Number Types and Geometric Shapes in Images using CV2 and Numpy

Abstract: Classifies geometric shapes and numbers in images by converting to grayscale, thresholding, approximating contours with OpenCV, and analyzing vertex counts.

Created: 2026-09-13 by

Identifying number types and geometric shapes in images using OpenCV (cv2) and NumPy involves converting the image to grayscale, applying thresholding to create a binary mask, finding contours with cv2.findContours, approximating polygonal curves to determine shape properties, and finally classifying objects based on vertex count and aspect ratio.

Preprocessing the Image for Clarity

The first step in any computer vision pipeline is ensuring the input data is clean and standardized. Raw images often contain noise, varying lighting conditions, or color information that complicates shape detection. To simplify the problem, we convert the image into a format that highlights structural boundaries rather than color variations. Start by loading the image using cv2.imread. It is crucial to convert this BGR image to grayscale immediately. Grayscale conversion reduces the data from three channels (Red, Green, Blue) to a single intensity channel, which significantly speeds up subsequent processing steps. You can achieve this using cv2.cvtColor(image, cv2.COLOR_BGR2GRAY). Once the image is grayscale, you must separate the foreground objects (the shapes or numbers) from the background. This is typically done using thresholding. If the background is uniform and light while the shapes are dark, use simple binary thresholding with cv2.threshold. For more complex scenarios where lighting is uneven, consider using adaptive thresholding, which calculates thresholds for smaller regions of the image. This ensures that faded shapes remain detectable even if their contrast varies across the frame. After thresholding, you will have a binary image where pixels are either pure black or pure white, creating distinct edges for contour detection.

Finding and Approximating Contours

With a clean binary image, the next phase is identifying the actual objects. OpenCV provides the cv2.findContours function, which locates continuous curves that bound regions of identical color. In OpenCV versions 4.x, this function returns three values: the hierarchy, the contours themselves, and the retrieval mode. The contours are stored as a list of points defining the boundary of each object. However, raw contours are often jagged due to pixel-level noise. To classify shapes accurately, we need smooth, simplified polygons. This is where cv2.approxPolyDP comes into play. This function approximates a curve shape with another curve shape of fewer vertices. It requires two main arguments: the contour to approximate and an epsilon value. Epsilon defines the maximum distance between the original curve and its approximation. Choosing the right epsilon is critical. If it is too small, the approximation retains too many vertices, making a circle look like a hexagon. If it is too large, distinct shapes might merge. A common practice is to set epsilon as a percentage of the contour's perimeter. Calculate the perimeter using cv2.arcLength(contour, True) and then set epsilon to approximately 0.02 times that perimeter. This dynamic scaling ensures consistent approximation regardless of the object's size. The result is a simplified polygon represented by a few key vertices, which serves as the foundation for shape classification. Enhancing Crack Detection with ImageJ Pre-processing demonstrates similar pre-processing rigor, emphasizing how cleaning data before analysis leads to more reliable outcomes in structural identification tasks.

Classifying Geometric Shapes

Now that you have simplified polygons, you can classify them based on their geometric properties. The most robust method is counting the number of vertices in the approximated polygon. For triangles, the vertex count will be 3. Quadrilaterals, such as squares and rectangles, will have 4 vertices. Pentagons have 5, and so on. However, circles pose a unique challenge because they have infinite vertices. To detect circles, you can check the circularity of the shape. Circularity is calculated using the formula: $4 \times \pi \times Area / Perimeter^2$. A perfect circle has a circularity of 1. Values close to 1 indicate a circular shape, while lower values suggest irregular polygons. Additionally, you can distinguish between squares and rectangles by analyzing the aspect ratio. Get the bounding rectangle using cv2.boundingRect and calculate the width divided by height. A square will have a ratio very close to 1.0, whereas a rectangle will deviate from this value. When handling faded shapes, standard thresholding might fail to capture the entire object. In such cases, morphological operations like dilation can help connect broken edges. Use cv2.morphologyEx with a kernel to expand the white regions, effectively filling gaps in faded lines before re-running contour detection. This preprocessing step ensures that fragmented parts of a shape are treated as a single entity.

Identifying Number Types via Template Matching or OCR

While geometric shapes are defined by their boundaries, numbers are often identified by their internal structure or visual similarity to known templates. For simple digit recognition without heavy machine learning models, template matching is effective. First, extract individual character contours from the image. Sort these contours from left to right to reconstruct the sequence of digits. For each isolated character contour, crop the region of interest (ROI) from the original binary image. Resize this ROI to a fixed standard size, such as 20x20 pixels, to ensure consistency. Compare this resized ROI against a library of pre-defined digit templates (e.g., images of '0' through '9'). Use cv2.matchTemplate to find the best match. The template with the highest correlation score identifies the digit. For higher accuracy, especially with handwritten numbers, consider combining this with optical character recognition libraries like Tesseract, feeding it the cleaned binary segments. This hybrid approach leverages OpenCV's speed for segmentation and specialized OCR engines for complex character recognition.

Handling Edge Cases and Performance Optimization

Real-world images rarely behave perfectly. Shadows, reflections, and overlapping objects can confuse contour detectors. Always filter contours by area. Small noise artifacts often appear as tiny contours; ignoring contours below a certain area threshold (e.g., 100 pixels) cleans up the results significantly. Furthermore, performance matters when processing video streams or high-resolution images. Limit the search for contours to specific regions of interest (ROIs) if you know where the shapes will appear. This reduces the computational load on findContours. Also, ensure that your image data type is compatible with OpenCV functions; most CV2 functions expect uint8 images, so verify your data types before passing them to processing routines. By systematically preprocessing, approximating, and classifying, you build a robust system capable of distinguishing between basic geometric forms and numerical characters, laying the groundwork for more advanced automated inspection systems.

Frequently Asked Questions

What happens if the image has a noisy background?

Noisy backgrounds create false contours that clutter your results. Apply Gaussian blur using cv2.GaussianBlur before thresholding to smooth out high-frequency noise. This helps the thresholding algorithm focus on significant edges rather than random pixel variations.

How do I distinguish between a circle and a hexagon?

Use the circularity metric derived from area and perimeter. A hexagon will have a circularity significantly less than 1.0, typically around 0.8-0.9 depending on the approximation. Circles consistently yield values very close to 1.0, allowing for clear differentiation even with slight imperfections.

Why is my contour detection returning multiple pieces for one shape?

This usually occurs due to faint internal lines or holes within the shape. Use morphological closing operations to fill small gaps inside the object. Alternatively, adjust your thresholding method to ensure the interior is fully filled, preventing the detector from seeing separate inner and outer boundaries.

Can I detect rotated squares?

Yes, standard bounding boxes may not align with rotated shapes. Use cv2.minAreaRect instead of cv2.boundingRect. This function returns a rotated rectangle that tightly fits the object, allowing you to calculate accurate dimensions and aspect ratios regardless of rotation angle.

Is template matching suitable for all fonts?

Template matching works best for uniform, printed fonts. Handwritten or highly stylized fonts vary too much for simple pixel-based matching. For varied handwriting, integrate OCR tools like Tesseract or train a simple neural network using frameworks like TensorFlow or PyTorch for better generalization.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More