Opening | Morphological Transformations in OpenCV in C++ Last Updated : 10 Feb, 2021 Comments Improve Suggest changes Like Article Like Report In this article, a Morphological operation called Opening is discussed. Opening operation is similar to erosion in the sense that it also removes foreground pixels from the edges of the image. Opening operation is erosion operation followed by dilation. It is usually used for removing internal noise present inside an image. This operator safeguards the foreground region that has similarity with the structuring component or the one that fits inside the structuring element while removing everything else. Syntax: morphologyEx (src, dst, op, kernel, anchor, iterations, borderType, borderValue) Parameters: src: It is the input image.dst: It is the output image.op: Type of morphological operation.kernel: Structuring element used for Closing.anchor: Anchor position inside the structuring element. The default value is [-1, -1} signifying position as the center of the structuring element.iterations: Number of times Closing is applied.borderType: Type of border ( BORDER_CONSTANT, BORDER_REPLICATE, etc.)borderValue: Border valueReturn: Output Image (Mat Object) The opening operator is given by the expression: The expression represents that AoB is a subset (sub-image of A). The opening operator removes internal noise and thin protrusions present inside an image. Below is the C++ program to demonstrate the Opening Morphological Operation: C++ // C++ program to demonstrate the // opening morphological transformation #include <iostream> #include <opencv2/core/core.hpp> // Library include for drawing shapes #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc.hpp> using namespace cv; using namespace std; // Function to demonstrate the // opening morphological operator int openingMorphological() { // Reading the Image Mat image = imread( "C:/Users/harsh/Downloads/geeks.png", IMREAD_GRAYSCALE); // Check if the image is // created successfully or not if (!image.data) { cout << "Could not open or" << " find the image\n"; return 0; } // Create a structuring element int morph_size = 2; Mat element = getStructuringElement( MORPH_RECT, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size)); Mat output; // Opening morphologyEx(image, output, MORPH_OPEN, element, Point(-1, -1), 2); // Display the image imshow("source", image); imshow("Opening", output); waitKey(); return 0; } // Driver Code int main() { openingMorphological(); return 0; } Output: Comment More infoAdvertise with us Next Article Opening | Morphological Transformations in OpenCV in C++ C Captain_JackSparrow Follow Improve Article Tags : C++ Programs Computer Subject C++ OpenCV Practice Tags : CPP Similar Reads Erosion and Dilation | Morphological Transformations in OpenCV in C++ Morphological Transformations are simple operations based on the shape of an image usually performed on a binary image. It takes our input image and a structuring element(kernel) which decides the nature of the operation. In this article, we will discuss the two basic morphological filters erosion 4 min read Write on an image using openCV in C++ In this article, we will discuss how to write over an image using OpenCV C++. Function putText() from OpenCV C++ library will be used to write text on an image. Program 1: Below program shows how to write text over a blank background image: C++ // C++ program to demonstrate the // above approach #in 2 min read Reading and Displaying an image in OpenCV using C++ In this article, we will discuss to open an image using OpenCV (Open Source Computer Vision) in C++. Unlike python, any additional libraries in C++ are not required. OpenCV C++ comes with this amazing image container Mat that handles everything for us. The only change seen from a standard C++ progra 3 min read C++ - Finding Maximum Value in Image using OpenCV Suppose you have an image stored in a matrix in C++ and you want to find the maximum value among all the pixels in the image. The matrix may be of any type, such as 'uchar' (for 8-bit unsigned integers, which are commonly used to represent pixels in an image), 'int', or 'float'. Your task is to writ 4 min read Draw a line using OpenCV in C++ In this article, we will discuss how to draw a line using OpenCV in C++. The idea is to use the line() function from OpenCV C++ library. Syntax: line(img, pt1, pt2, color, thickness, lineType, shift) Parameters: img: This is the image file.start: Start point of the line segment. The first point out 3 min read Like