How could I make the discontinuous contour of an image consistant?

后端 未结 6 1376
孤街浪徒
孤街浪徒 2021-02-04 15:32

In the task, I got an discontinuous edge image, how could make it closed? in other word make the curve continuous. And the shape could be any kind, cause this the coutour shadow

6条回答
  •  轮回少年
    2021-02-04 16:04

    Here are a few ideas that may get you started. I don't feel like coding and debugging a load of C++ in OpenCV - oftentimes folks ask questions and never log in again, or you spend hours working on something and then they tell you that the single sample image they provided was not at all representative of their actual images and the method that it has taken 25 minutes to explain is completely inappropriate.


    One idea is morphological dilation - you can do that at the command line like this with ImageMagick:

    convert gappy.jpg -threshold 50% -morphology dilate disk:5 result.png
    


    Another idea might be to locate all the "line end" pixels with Hit-and-Miss morphology. This is available in OpenCV, but I am doing it with ImageMagick to save coding/debugging. The structuring elements are like this:

    Hopefully you can see that the first (leftmost) structuring element represents the West end of an East-West line, and that the second one represents the North end of a North-South line and so on. If you still haven't got it, the last one is the South-West end of North-East to South-West line.

    Basically, I find the line ends and then dilate them with blue pixels and overlay that onto the original:

    convert gappy.jpg -threshold 50%  \
       \( +clone -morphology hmt lineends -morphology dilate disk:1 -fill blue -opaque white -transparent black \) \
       -flatten result.png
    

    Here's a close-up of before and after:

    You can also find the singleton pixels with no neighbours, using a "peaks" structuring element like this:

    and then you can find all the peaks and dilate them with red pixels like this:

    convert gappy.jpg -threshold 50% \
        \( +clone -morphology hmt Peaks:1.9 -fill red -morphology dilate disk:2  -opaque white -transparent black \) \
        -flatten result.png
    

    Here is a close-up of before and after:

    Depending on how your original images look, you may be able to apply the above ideas iteratively till your contour is whole - maybe you could detect that by flood filling and seeing if your contour "holds water" without the flood fill "leaking" out everywhere.

    Obviously you would do the red peaks and the blue line ends both in white to complete your contour - I am just doing it in colour to illustrate my technique.

提交回复
热议问题