- 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
Performing a closing operation on an image using OpenCV
In this program, we will perform the closing operation using the cv2.morphologyEx() function. Closing removes small holes in the foreground, changing small holes of background into foreground. This technique can also be used to find specific shapes in an image. The function we will use for this task is cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel).
Original Image
Algorithm
Step 1: Import cv2 and numpy. Step 2: Read the image. Step 3: Define the kernel. Step 4: Pass the image and kernel to the cv2.morphologyex() function. Step 4: Display the output.
Example Code
import cv2 import numpy as np image = cv2.imread('testimage.jpg') kernel = np.ones((5,5), np.uint8) image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) cv2.imshow('Closing', image)
Output
Advertisements