使用边界规则选择感兴趣的区域



我想在图像中的每个像素周围提取一个小窗口。当然,我可以使用Python列表切片来实现这一点。但是,单独的列表切片并不能解决"边缘情况",即W大小的窗口在像素周围根本不存在,因为它靠近边缘。考虑简单矩阵M

1 1 1 1
1 1 1 1
1 1 1 1

如果我想在M(1,1)周围选择一个3x3大小的窗口,我将无法选择,因为它上面或左边没有任何东西。

Skimage中是否有一个Numpy函数或其他东西可以让我指定当列表索引超出界限时会发生什么?例如,如果我只是想复制最近的邻居,该怎么办?

我当然可以自己写这个逻辑,因为这是一个微不足道的算法。我只是想知道这样的选项是否已经存在于Numpy,Skimage,OpenCV等中

通常,首先通过np.pad()(文档(或cv2.copyMakeBorder()(文档(填充图像,然后根据填充的大小移动要选择的索引。这两个函数的好处是,它们提供了很多不同的选项,可以填充图像的值。Numpy有更多的选项,但您想要使用的大多数标准选项(重复边缘像素、镜像边缘像素、包裹边缘像素或恒定填充(在这两个库中都可用。

numpy边界类型直接列在文档中,但我将在此处复制它们:

mode : str or function
One of the following string values or a user supplied function.
‘constant’
Pads with a constant value.
‘edge’
Pads with the edge values of array.
‘linear_ramp’
Pads with the linear ramp between end_value and the array edge value.
‘maximum’
Pads with the maximum value of all or part of the vector along each axis.
‘mean’
Pads with the mean value of all or part of the vector along each axis.
‘median’
Pads with the median value of all or part of the vector along each axis.
‘minimum’
Pads with the minimum value of all or part of the vector along each axis.
‘reflect’
Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.
‘symmetric’
Pads with the reflection of the vector mirrored along the edge of the array.
‘wrap’
Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.
<function>
Padding function, see Notes.

它对传入的任意函数有进一步的注释,这是一个很酷的功能。

OpenCV边界类型不是直接在copyMakeBorder()文档中指定的,但您可以通过在文档上搜索边界类型来找到它们。同样,只是为了让他们上SO:

BORDER_CONSTANT 
Python: cv.BORDER_CONSTANT
iiiiii|abcdefgh|iiiiiii with some specified i
BORDER_REPLICATE 
Python: cv.BORDER_REPLICATE
aaaaaa|abcdefgh|hhhhhhh
BORDER_REFLECT 
Python: cv.BORDER_REFLECT
fedcba|abcdefgh|hgfedcb
BORDER_WRAP 
Python: cv.BORDER_WRAP
cdefgh|abcdefgh|abcdefg
BORDER_REFLECT_101 
Python: cv.BORDER_REFLECT_101
gfedcb|abcdefgh|gfedcba
BORDER_TRANSPARENT 
Python: cv.BORDER_TRANSPARENT
uvwxyz|abcdefgh|ijklmno
BORDER_REFLECT101 
Python: cv.BORDER_REFLECT101
same as BORDER_REFLECT_101
BORDER_DEFAULT 
Python: cv.BORDER_DEFAULT
same as BORDER_REFLECT_101
BORDER_ISOLATED 
Python: cv.BORDER_ISOLATED
do not look outside of ROI

最新更新