1

I have this image that I'd like to get the vertical and diagonal lines only. I use morphologyEx in OpenCV. However, the result I get doesn't meet up my requirement as I still these unwanted noises. Unfortunately, these noises have the same intensity as the lines, hence when I apply the Opening to get rid of the noise, the lines will also disappear. Can anyone please help me solve this problem?

int main() {

    Mat dst, dst1, dst2, dst3;
    Mat src = imread("cau3.png", IMREAD_GRAYSCALE);

    imshow("source", src);

    Mat kernel1 = getStructuringElement(MORPH_RECT, Size(45, 1), Point(-1, -1));
    Mat kernel2 = getStructuringElement(MORPH_ELLIPSE, Size(11, 11), Point(-1, -1));
    Mat kernel3 = getStructuringElement(MORPH_RECT, Size(21, 21), Point(-1, -1));

    // Apply opening operation

    morphologyEx(src, dst1, MORPH_OPEN, kernel1);
    morphologyEx(src, dst2, MORPH_OPEN, kernel2);
    morphologyEx(src, dst3, MORPH_OPEN, kernel3);

    dst = src - (dst1 + dst2 + dst3);
    imshow("Vertical and Diagonal lines with noise", dst);

    waitKey(0);
    return 0;
}

This is my original image:

This is my original image

This is my result image:

This is my result image

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Vincent T
  • 11
  • 1
  • https://stackoverflow.com/questions/45322630/how-to-detect-lines-in-opencv I think line detection should be the approach for you. – satvik choudhary Oct 02 '21 at 14:18
  • What are the “noises” you talk about? Is it the shorter lines and the dots? If so, you could look into area opening, for example. – Cris Luengo Oct 02 '21 at 14:19
  • @CrisLuengo the noises are the dots, sorry for this misunderstanding. I will try the area opening, thank you for your recommendation – Vincent T Oct 02 '21 at 14:26
  • @satvikchoudhary line detection is truly powerful, but my lecturer didn't teach that in advance, hence I have to come up with something else. – Vincent T Oct 02 '21 at 14:29
  • 3
    findcontours or connectedcomponentswithstats, then erase those features that are small (by width and height) – Christoph Rackwitz Oct 02 '21 at 15:11
  • To eliminate the noise is quite simple by using a [blob detector](https://learnopencv.com/blob-detection-using-opencv-python-c/). To detect the lines you may use [this approach](https://stackoverflow.com/a/60634986/11048887) – Yunus Temurlenk Oct 04 '21 at 06:25

1 Answers1

0

If you absolutely want to remove the noise using mathematical morphology operations, you have to use what is called an "area opening". It's an opening based on the shape surface instead of a structuring element.

If you're not stuck with mathematical morphology, what you can do is to remove small patterns:

  • label connected components, so in your case every group of pixels non black.
  • remove all component with a surface smaller than a given threshold.
FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • Your two bullet points are an excellent (and very common) way to implement the area opening for a binary image. – Cris Luengo Nov 01 '21 at 19:43
  • Thanks. It's easy for black & white images, but not for gray level images, for which you have to use other algorithms. – FiReTiTi Nov 01 '21 at 20:10