openCV和python:边界外的形态转换 [英] openCV and python: Morphological transformation outside boundaries
问题描述
我正在使用python和OpenCV. 我有一个(矩形或其他任何东西)内核,并尝试执行一些形态转换.我的问题是图像边界如何?
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?
例如,openCV如何确定边界外的内核元素以进行扩展?它会忽略它们还是使用邻居的值?
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内核,Point anchor = Point(-1,-1),int迭代次数= 1,int borderType = BORDER_CONSTANT,const Scalar& borderValue = morphologyDefaultBorderValue() )
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 [,迭代[,borderType [,borderValue]]]]]))→dst
Python: cv2.morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst
您会看到该函数默认情况下会创建一个具有恒定值的边框.此值取决于形态操作的类型,并由morphologyDefaultBorderValue()
定义:
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); }
,然后将其校正为实际的矩阵类型.因此,对于CV_8U
矩阵,边界值为0(对于 dilate )或255(对于 erosion ).
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).
请注意,所有其他形态学运算都是 dilate 和腐蚀的不同序列.
Note that all others morphological operations are different sequences of dilate and erode.
FilterEngine
中定义的边界长度实际执行形态学操作为:
The border length is defined in the FilterEngine
that actually performs the morphological operation as:
int borderLength = std::max(ksize.width - 1, 1);
其中ksize
是结构元素的大小.
where ksize
is the size of the structuring element.
因此,默认情况下,OpenCV使用特定值创建(根据内核,正确的borderLength
)所需的其他边界.该值保证了跨边界的形态操作是一致的.
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.
这篇关于openCV和python:边界外的形态转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!