📜  开幕| C++中OpenCV的形态转换

📅  最后修改于: 2021-05-30 17:49:08             🧑  作者: Mango

在本文中,讨论了称为“打开”的形态操作。

打开操作与腐蚀类似,因为它也从图像边缘去除了前景像素。打开操作是腐蚀操作,然后进行膨胀。通常用于消除图像内部存在的内部噪声。该运算符在除去其他所有内容的同时,保护与结构化组件或装配在结构化元素内部的前景区域相似的前景区域。

句法:

参数:

  • src:它是输入图像。
  • dst:它是输出图像。
  • op:形态学操作的类型。
  • 内核:用于结束的结构元素。
  • 锚:在结构元素内的锚位置。默认值为[-1,-1},表示位置为结构元素的中心。
  • 迭代:应用关闭的次数。
  • borderType:边框类型(BORDER_CONSTANT,BORDER_REPLICATE等)
  • borderValue:边框值
  • 返回值:输出图像(垫子对象)

打开运算符由以下表达式给出:

该表达式表示A o B是子集(A的子图像)。开启运算符消除内部噪声和图像内部存在的细小突起。

下面是演示打开形态操作的C++程序:

C++
// C++ program to demonstrate the
// opening morphological transformation
#include 
#include 
  
// Library include for drawing shapes
#include 
#include 
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;
}


输出:

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”