- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Detecting contours in an image using OpenCV
In this program, we will detect contours in an image. Contours can be explained simply as a curve joining all the continuous points having the same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition.
Original Image
Algorithm
Step 1: Import OpenCV. Step 2: Import matplotlib. Step 3: Read the image. Step 4: Convert the image from bgr2rgb. Step 5: Convert the rgb image to grayscale. Step 4: Perform thresholding on the image. Step 5: Find contours on the image. Step 6: Draw contours on the image. Step 7: Display the output.
Example Code
import cv2 import matplotlib.pyplot as plt image = cv2.imread('testimage.jpg') image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) ret, binary = cv2.threshold(gray, 127,255, cv2.THRESH_BINARY_INV) contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) image = cv2.drawContours(image, contours, -1, (0,255,0), 2) plt.imshow(image) plt.show()
Output
- Related Articles
- How to find Image Contours using Java OpenCV library?
- How to draw Image Contours using Java OpenCV library?
- Find and Draw Contours using OpenCV in Python
- Upsampling an image using OpenCV
- Downsampling an image using OpenCV
- How to find contours of an image using scikit-learn in Python?
- Detecting corners using Harris corner detector in Python OpenCV
- Cartooning an Image using OpenCV in Python?
- Draw an ellipse on an image using OpenCV
- Reading an image using Python OpenCv module
- Drawing borders around an image using OpenCV
- Draw rectangle on an image using OpenCV
- Find Circles in an Image using OpenCV in Python
- Performing an opening operation on an image using OpenCV
- Histogram equalization on an image in OpenCV using Java.

Advertisements