Morphological operations merges lines with edges of pictures

三世轮回 提交于 2020-02-04 01:30:11

问题


I have an image with nearly constant lines in it and I use algorythm with morphological operations to clear them out of the image. The thing is, that morphological opening or closing merges the lines with the edges of the image instead of only just melting the neighbor contours. I need only neighbor contours melted with morphological operations and to avoid that line merging with edge, because later I am unable to clear that merged peace of the line out of the image. The problem stays even if I invert the image and morph. operations (image pixels black to white, white to black, and instead of opening I use close).

How to stop this?

I use this operation to merge neighbor contours:

Mat morphKernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new org.opencv.core.Size(25, 25));
Imgproc.morphologyEx(src, src, Imgproc.MORPH_CLOSE, morphKernelClose);

Simple Canny image:

Image after using MORPH_CLOSE:

This error does not allow me to clear the lines fully (those merged parts are left) by using second morphology operation, opposite to used first.


回答1:


The simplest solution would be to increase the image size, fill the added contour with black, and then perform the operations:

  1. Create a bigger image, completely black.
  2. Copy your image in the middle of the new image.
  3. Perform the operations
  4. Delete the border added (copy the center of the image into a new one).



回答2:


Thanks to FiReTiTi and Micka for helping me to solve this. I used Micka's suggestion.

I used the third function of morphologyEx, which allows to manipulate the borders. The code given in the question is replaced with this one:

Mat morphKernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new org.opencv.core.Size(25, 25));
Point anchor = new Point(morphKernelClose.size().width/2, morphKernelClose.size().height/2); 
Imgproc.morphologyEx(src, src, Imgproc.MORPH_CLOSE, morphKernelClose, anchor, 1, Core.BORDER_CONSTANT, new Scalar(0,0,0));

Here is the result:

This method may be fast and easy, but not perfect, because it just cuts off the pixels near the borders.

If you have any suggestions how to improve this, post it in the comments!



来源:https://stackoverflow.com/questions/38624394/morphological-operations-merges-lines-with-edges-of-pictures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!