pythonopencv图像形态_openCV和python:形态转换超越界限

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?

For example, how does openCV decide in kernel's elements outside boundaries for dilation? Does it ignore them or use its neighbor's value?

解决方案

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值