Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Try doing several erosion/dilation in a row:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main(void)
{
    Mat frame = imread("landscape.jpg");

    if (frame.empty())
    {
        cout << "Could not open image file." << endl;
        return 1;
    }

    threshold(frame, frame, 127, 255, THRESH_BINARY);

    // Dilate the white regions
    int dilation_size = 1;
    Mat dilation_element = getStructuringElement(
        MORPH_RECT,     
        Size(2 * dilation_size + 1, 2 * dilation_size + 1),
        Point(dilation_size, dilation_size));

    // Erode the white regions
    int erosion_size = 1;
    Mat erosion_element = getStructuringElement(
        MORPH_RECT,
        Size(2 * erosion_size + 1, 2 * erosion_size + 1),
        Point(erosion_size, erosion_size));

    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    dilate(frame, frame, dilation_element);
    dilate(frame, frame, dilation_element);
    dilate(frame, frame, dilation_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    dilate(frame, frame, dilation_element);
    dilate(frame, frame, dilation_element);
    dilate(frame, frame, dilation_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);

    imshow("frame", frame);

    waitKey();

    return 0;
}

Try doing several erosion/dilation in a row:

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")

#include <iostream>
#include <cmath>
using namespace std;


int main(void)
{
    Mat frame = imread("landscape.jpg");

    if (frame.empty())
    {
        cout << "Could not open image file." << endl;
        return 1;
    }

    threshold(frame, frame, 127, 255, THRESH_BINARY);

    // Dilate the white regions
    int dilation_size = 1;
2;
    Mat dilation_element = getStructuringElement(
        MORPH_RECT,     
        Size(2 * dilation_size + 1, 2 * dilation_size + 1),
        Point(dilation_size, dilation_size));

    // Erode the white regions
    int erosion_size = 1;
2;
    Mat erosion_element = getStructuringElement(
        MORPH_RECT,
        Size(2 * erosion_size + 1, 2 * erosion_size + 1),
        Point(erosion_size, erosion_size));

    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    dilate(frame, frame, dilation_element);
    dilate(frame, frame, dilation_element);
    dilate(frame, frame, dilation_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    dilate(frame, frame, dilation_element);
    dilate(frame, frame, dilation_element);
    dilate(frame, frame, dilation_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);
    erode(frame, frame, erosion_element);

    imshow("frame", frame);

    waitKey();

    return 0;
}