openCV and python: Morphological transformation outside boundaries

前端 未结 1 958
南旧
南旧 2020-12-18 09:28

I\'m using python and OpenCV. I have a (rectangle or anything) kernel and trying to perform some morphological transformations. My question is what about image boundaries?

相关标签:
1条回答
  • 2020-12-18 09:47

    As reported by OpenCV documentation for morphologyEx:

    C++: void morphologyEx(InputArray src, OutputArray dst, int op, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue() )

    Python: cv2.morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst

    You see that the function by default create a border with a constant value. This value depends on the type of morphological operation and is defined by morphologyDefaultBorderValue():

    //! returns "magic" border value for erosion and dilation. 
    //! It is automatically transformed to Scalar::all(-DBL_MAX) for dilation.
    static inline Scalar morphologyDefaultBorderValue() { return Scalar::all(DBL_MAX); }
    

    which will be then corrected for the actual matrix type. So, for CV_8U matrix the border values are either 0 (for dilate) or 255 (for erosion).

    Note that all others morphological operations are different sequences of dilate and erode.

    The border length is defined in the FilterEngine that actually performs the morphological operation as:

     int borderLength = std::max(ksize.width - 1, 1);
    

    where ksize is the size of the structuring element.

    So, by default OpenCV creates the additional boundaries needed (of the correct borderLength according to the kernel), with a specific value. This value guarantees that the morphological operation is consistent across boundaries.

    0 讨论(0)
提交回复
热议问题