The Wayback Machine - https://web.archive.org/web/20241009145751/https://www.geeksforgeeks.org/python-morphological-operations-in-image-processing-closing-set-2/
Open In App

Python | Morphological Operations in Image Processing (Closing) | Set-2

Last Updated : 04 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In the previous article, the Opening operator was specified which was applying the erosion operation after dilation. It helps in removing the internal noise in the image. Closing is similar to the opening operation. In closing operation, the basic premise is that the closing is opening performed in reverse. It is defined simply as a dilation followed by an erosion using the same structuring element used in the opening operation.

Syntax: cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)

Parameters:
-> image: Input Image array.
-> cv2.MORPH_CLOSE: Applying the Morphological Closing operation.
-> kernel: Structuring element.

Below is the Python code explaining Closing Morphological Operation –

Python3




# Python program to illustrate
# Closing morphological operation
# on an image
  
# organizing imports  
import cv2  
import numpy as np  
  
# return video from the first webcam on your computer.  
screenRead = cv2.VideoCapture(0)
  
# loop runs if capturing has been initialized.
while(1):
    # reads frames from a camera
    _, image = screenRead.read()
      
    # Converts to HSV color space, OCV reads colors as BGR 
    # frame is converted to hsv
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
      
    # defining the range of masking
    blue1 = np.array([110, 50, 50])
    blue2 = np.array([130, 255, 255])
      
    # initializing the mask to be
    # convoluted over input image
    mask = cv2.inRange(hsv, blue1, blue2)
  
    # passing the bitwise_and over
    # each pixel convoluted
    res = cv2.bitwise_and(image, image, mask = mask)
      
    # defining the kernel i.e. Structuring element
    kernel = np.ones((5, 5), np.uint8)
      
    # defining the closing function 
    # over the image and structuring element
    closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
     
    # The mask and closing operation
    # is shown in the window 
    cv2.imshow('Mask', mask)
    cv2.imshow('Closing', closing)
      
    # Wait for 'a' key to stop the program 
    if cv2.waitKey(1) & 0xFF == ord('a'):
        break
  
# De-allocate any associated memory usage  
cv2.destroyAllWindows()
  
# Close the window / Release webcam 
screenRead.release()


Input Frame:

Mask:

Output:



Previous Article
Next Article

Similar Reads

Python | Morphological Operations in Image Processing (Opening) | Set-1
Morphological operations are used to extract image components that are useful in the representation and description of region shape. Morphological operations are some basic tasks dependent on the picture shape. It is typically performed on binary images. It needs two data sources, one is the input image, the second one is called structuring compone
3 min read
Python | Morphological Operations in Image Processing (Gradient) | Set-3
In the previous articles, the Opening operation and the Closing operations were specified. In this article, another morphological operation is elaborated that is Gradient. It is used for generating the outline of the image. There are two types of gradients, internal and external gradient. The internal gradient enhances the internal boundaries of ob
2 min read
Image segmentation using Morphological operations in Python
If we want to extract or define something from the rest of the image, eg. detecting an object from a background, we can break the image up into segments in which we can do more processing on. This is typically called Segmentation. Morphological operations are some simple operations based on the image shape. It is normally performed on binary images
3 min read
Difference between Opening and Closing in Digital Image Processing
Opening and Closing are dual operations used in Digital Image Processing for restoring an eroded image. Opening is generally used to restore or recover the original image to the maximum possible extent. Closing is generally used to smoother the contour of the distorted image and fuse back the narrow breaks and long thin gulfs. Closing is also used
2 min read
Python OpenCV - Morphological Operations
Python OpenCV Morphological operations are one of the Image processing techniques that processes image based on shape. This processing strategy is usually performed on binary images. Morphological operations based on OpenCV are as follows: ErosionDilationOpeningClosingMorphological GradientTop hatBlack hat For all the above techniques the two impor
7 min read
Morphological operations in MATLAB
Morphological Operations is a broad set of image processing operations that process digital images based on their shapes. In a morphological operation, each image pixel is corresponding to the value of other pixel in its neighborhood. By choosing the shape and size of the neighborhood pixel, you can construct a morphological operation that is sensi
2 min read
Point Processing in Image Processing using Python-OpenCV
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. Point processing in spatial domain All the
3 min read
Image Processing in Java - Colored Image to Grayscale Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and set PixelsIn this article, we will be converting a colored image to a grayscale image. RGB Color Model - The RGB color model is an additive mixing model in which red, green, and blue light are added together in various ways to reproduce a broad array of color
3 min read
Image Processing in Java - Colored image to Negative Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored image to Grayscale Image Conversion In this set, we will be converting a colored image to a negative image. Colored Image (RGB Color Model) - The RGB color model is an additive mixing model in which red, green, and
3 min read
Image Processing in Java - Colored Image to Sepia Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image Conversion In this set, we will be converting a col
3 min read
Mahotas - Closing Process on Image
In this article we will see how we can perform closing on the image in mahotas. Closing is a process in which first dilation operation is performed and then erosion operation is performed. It eliminates the small holes from the obtained image, it is used for smoothening of contour and fusing of narrow breaks. In this tutorial we will use “luispedro
2 min read
Mahotas – Closing Holes in Image
In this article we will see how we can close holes of the image in mahotas. Closing holes means to remove the holes present in the image, closing is a process in which first dilation operation is performed and then erosion operation is performed. It eliminates the small holes from the obtained image, it is used for smoothening of contour and fusing
2 min read
PyQtGraph – Closing Image View
In this article we will see how we can close the image view object in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.). Widget used for disp
3 min read
Image processing with Scikit-image in Python
scikit-image is an image processing Python package that works with NumPy arrays which is a collection of algorithms for image processing. Let's discuss how to deal with images in set of information and its application in the real world. Important features of scikit-image : Simple and efficient tools for image processing and computer vision techniqu
2 min read
Image Processing in MATLAB | Fundamental Operations
1. Reading Images Images are read into the MATLAB Environment using imread() function which takes filename with applicable extension as the argument For Example: >> I=imread('nature.jpg'); This will read JPEG image 'nature' into the image array. Note: The semicolon(;) at the end of command line is used to suppress the output in MATLAB.If ';'
3 min read
Image Edge Detection Operators in Digital Image Processing
Edges are significant local changes of intensity in a digital image. An edge can be defined as a set of connected pixels that forms a boundary between two disjoint regions. There are three types of edges: Horizontal edgesVertical edgesDiagonal edges Edge Detection is a method of segmenting an image into regions of discontinuity. It is a widely used
4 min read
Image Processing in Java - Colored to Red Green Blue Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image Conversion In this set, we will be converting a colored image to an image with either red effect, green effect, or blue
5 min read
Image Processing in Java - Creating a Random Pixel Image
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image ConversionImage Processing in Java - Colored Image
2 min read
Image Processing in Java - Creating a Mirror Image
Prerequisite: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image ConversionImage Processing in Java - Colored Image t
3 min read
Image Processing in Java - Watermarking an Image
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image ConversionImage Processing in Java - Colored Image
3 min read
Image Processing in Java - Changing Orientation of Image
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image ConversionImage Processing in Java - Colored Image
3 min read
PyQt5 QCalendarWidget - Closing when use is done
In this article, we will see how we can close the QCalendarWidget. Sometimes closing a calendar is necessary after the use of it is completed. It can be done using a close method, this method sends the calendar to the QCloseEvent. In order to do this we will use close method with the QCalendarWidget object.Syntax : calendar.close()Argument : It tak
2 min read
PyQt5 - Closing pop up items of ComboBox when push button is pressed
In this article we will see how we make combo box to hide its elements without clicking on it, in order to do this we will use hidePopup method. Syntax : combo_box.hidePopup() Argument : It takes no argument Action performed : It will hide the pop up elements Steps for implementation : 1. Create a combo box 2. Add items t the combo box 3. Create a
2 min read
PyQt5 QSpinBox – Closing the spin box
In this article we will see how we can close the spin box, while making GUI(Graphical User Interface) when the use of widget get completed there is a need to remove the widget from the screen it can be done by hiding it but best of doing this is by closing the widget. In order to do this we will use close() method. Syntax : spin_box.close() Argumen
2 min read
PYGLET – Closing Window
In this article we will see how we can close a window in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be set to fill an entire screen
2 min read
PyQtGraph - Closing Plot Window
In this article we will see how we can close plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) and second is to pro
2 min read
Opening and Closing Tabs Using Selenium
Selenium is a tool which is used to automate browser instructions. It is utilitarian for all programs, deals with all significant OS and its contents are written in different languages i.e Python, Java, C# etc. In this article, we are using Python as the language and Chrome as the WebDriver. Installation Python selenium module can be installed usin
2 min read
Image Processing In Java - Get and Set Pixels
Prerequisite - Image Processing in Java - Read and Write In this set, we will learn about the pixels of images, how we can get pixel values of an image and how to set pixel values in an image using Java programming language. Pixels are the smallest unit of an image which consists of four components Alpha (transparency measure), Red, Green, Blue, an
3 min read
Image Processing without OpenCV | Python
We know OpenCV is widely used to operate on images and has a wide spectrum of functions to do so. But what if we want to process the image files without using any external library like OpenCV. Let's see how can we do this. Image Scaling (Using Nearest Neighbour Interpolation): Nearest Neighbour interpolation is the simplest way of interpolation. Th
4 min read
Image Processing with SciPy and NumPy in Python
In this tutorial, we will discuss Image Processing in Python using the core scientific modules like NumPy and SciPy. The images are made up of NumPy ndarrays so we can process and manipulate images and SciPy provides the submodule scipy.ndimage that provides functions that can operate on the NumPy arrays. We will discuss how to open and write to im
6 min read
Practice Tags :