【问题标题】:OpenCV, Python: How to use mask parameter in ORB feature detectorOpenCV,Python:如何在 ORB 特征检测器中使用掩码参数
【发布时间】:2018-01-30 07:32:15
【问题描述】:

通过阅读有关stackoverflow的一些答案,到目前为止我已经学到了很多:

掩码必须是numpy 数组(与图像具有相同的形状),数据类型为CV_8UC1,值从0255

但是,这些数字的含义是什么?是否会在检测过程中忽略相应掩码值为 0 的像素,而使用掩码值为 255 的任何像素?两者之间的值呢?

另外,如何在 python 中初始化数据类型为CV_8UC1numpy 数组?我可以使用dtype=cv2.CV_8UC1

这是我目前使用的代码,基于我上面所做的假设。但问题是,当我为任一图像运行detectAndCompute 时,我没有得到任何关键点。我有一种感觉,这可能是因为掩码不是正确的数据类型。如果我是对的,我该如何纠正?

# convert images to grayscale
base_gray = cv2.cvtColor(self.base, cv2.COLOR_BGRA2GRAY)
curr_gray = cv2.cvtColor(self.curr, cv2.COLOR_BGRA2GRAY)

# initialize feature detector
detector = cv2.ORB_create()

# create a mask using the alpha channel of the original image--don't
# use transparent or partially transparent parts
base_cond = self.base[:,:,3] == 255
base_mask = np.array(np.where(base_cond, 255, 0))

curr_cond = self.base[:,:,3] == 255
curr_mask = np.array(np.where(curr_cond, 255, 0), dtype=np.uint8)

# use the mask and grayscale images to detect good features
base_keys, base_desc = detector.detectAndCompute(base_gray, mask=base_mask)
curr_keys, curr_desc = detector.detectAndCompute(curr_gray, mask=curr_mask)

 print("base keys: ", base_keys)
 # []
 print("curr keys: ", curr_keys)
 # []

【问题讨论】:

  • "我如何初始化一个 numpy 数组" -- 你有没有尝试阅读numpy documentation on data types
  • 问题是,CV_8UC1 对应于该列表中的哪种数据类型?由于 8 和 U,我倾向于相信它是 uint8,尽管我还没有找到任何证实这一点的文档。问题是我没有从中得到任何关键点
  • docs.opencv.org/2.4/modules/core/doc/basic_structures.html -- 第一段。你没看错,uint8。 |检查面具并确保它们有意义。

标签: python opencv numpy feature-detection


【解决方案1】:

所以这是大部分(如果不是全部)的答案:

这些数字是什么意思

0 表示忽略该像素,255 表示使用它。我仍然不清楚两者之间的值,但我不认为所有非零值都被认为与掩码中的 255 “等效”。见here

另外,如何在 python 中初始化数据类型为 CV_8UC1 的 numpy 数组?

CV_8U 类型是无符号的 8 位整数,使用 numpy 时为 numpy.uint8。 C1 后缀意味着数组是 1 通道,而不是彩色图像的 3 通道和 rgba 图像的 4 通道。因此,要创建一个 1 通道无符号 8 位整数数组:

import numpy as np
np.zeros((480, 720), dtype=np.uint8)

(三通道阵列的形状为 (480, 720, 3)、四通道 (480, 720, 4) 等)此掩码会导致检测器和提取器忽略整个图像,因为它全为零。

如何更正[代码]?

有两个单独的问题,每个单独导致每个关键点数组为空。

首先,我忘了设置base_mask的类型

base_mask = np.array(np.where(base_cond, 255, 0)) # wrong
base_mask = np.array(np.where(base_cond, 255, 0), dtype=uint8) # right

其次,我使用了错误的图像来生成我的curr_cond数组:

curr_cond = self.base[:,:,3] == 255 # wrong
curr_cond = self.curr[:,:,3] == 255 # right

一些非常愚蠢的错误。

这是完整的更正代码:

# convert images to grayscale
base_gray = cv2.cvtColor(self.base, cv2.COLOR_BGRA2GRAY)
curr_gray = cv2.cvtColor(self.curr, cv2.COLOR_BGRA2GRAY)

# initialize feature detector
detector = cv2.ORB_create()

# create a mask using the alpha channel of the original image--don't
# use transparent or partially transparent parts
base_cond = self.base[:,:,3] == 255
base_mask = np.array(np.where(base_cond, 255, 0), dtype=np.uint8)

curr_cond = self.curr[:,:,3] == 255
curr_mask = np.array(np.where(curr_cond, 255, 0), dtype=np.uint8)

# use the mask and grayscale images to detect good features
base_keys, base_desc = detector.detectAndCompute(base_gray, mask=base_mask)
curr_keys, curr_desc = detector.detectAndCompute(curr_gray, mask=curr_mask)

TL;DR:mask 参数是一个 1 通道 numpy 数组,其形状与您尝试在其中查找特征的灰度图像相同(如果图像形状为 (480, 720),则 mask 也是)。

数组中的值是np.uint8 类型,255 表示“使用此像素”,0 表示“不使用”

感谢 Dan Mašek 引导我找到部分答案。

【讨论】:

    猜你喜欢
    • 2018-04-21
    • 2011-11-06
    • 2019-09-19
    • 2013-04-28
    • 2017-07-09
    • 2013-03-18
    • 2018-01-15
    • 2021-04-12
    • 1970-01-01
    相关资源
    最近更新 更多