Ask Your Question
0

Structuring Element or Morphological Op to extract thick lines?

asked 2018-06-29 14:25:05 -0600

sazr gravatar image

Is the a morphological operation I can use to preserve my thick lines in the below image but also remove thin lines/noise? I have tried the regular approaches such as; close the image, open the image, erode the image n times then dilate the image n times. The approaches either eat into my thick lines too much (creating breaks in them) or don't remove enough noise.

So I am thinking I am maybe not aware of a structuring element or morphological operation that is good for preserving thick lines whilst removing noise/thin lines.

image description

If I threshold I get all the thick lines I want but I also get noise:
image description

My desired result is:
image description

Is there a good structuring element or morphological operation that is good for preserving thick lines whilst removing noise/thin lines?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-06-29 16:34:39 -0600

sjhalayka gravatar image

updated 2018-06-29 16:37:41 -0600

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 = 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 = 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;
}
edit flag offensive delete link more

Comments

1

@sjhalayka read erode doc and dilate :

erode(frame, frame, erosion_element);
erode(frame, frame, erosion_element);
erode(frame, frame, erosion_element);

is

  erode(frame, frame, erosion_element,Point(-1,-1),3);
LBerger gravatar imageLBerger ( 2018-06-29 16:49:40 -0600 )edit

Thanks for the information.

sjhalayka gravatar imagesjhalayka ( 2018-06-29 17:00:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-06-29 14:25:05 -0600

Seen: 747 times

Last updated: Jun 29 '18