问题
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:
- Create a bigger image, completely black.
- Copy your image in the middle of the new image.
- Perform the operations
- 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