OpenCv按位运算物理意义

时间:2017-12-03 17:54:34

标签: python opencv computer-vision

我最近刚开始使用OpenCv,在线文档似乎相当不错。然而,有一个关于图像添加和"按位操作"的教程。我非常关注以下几点

 `# Now black-out the area of logo in ROI
  img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

  # Take only region of logo from logo image.
  img2_fg = cv2.bitwise_and(img2,img2,mask = mask)`

我读到了有点明智的操作,我明白它们是什么。但我还没能用非常简单的语言找到上面两行的内容?上述函数中的参数掩码是做什么的?

我正在遵循以下教程

http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html

如果有人能用非常简单的语言向我解释这些代码行中发生的事情,或者如果有人能指出一个从最基本的角度解释它的好来源,我将非常感激。

此致

Nischal

2 个答案:

答案 0 :(得分:0)

代码将创建一个图像,其中所有原始像素为img2,位于前景蒙版中,图像具有图像roi的所有原始像素,位于背景蒙版中(倒置的前景蒙版)

因此结果是一个图像,其中所有非掩模像素变为黑色,其余像素不变。

首先,它计算图像的按位AND,这将只是图像本身(确切地说,每个位1后面将是1)。但另外它应用了掩码并留下了掩码中无效的每个像素0。

Imho这有点被黑了,代码应该是:

# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi,mask_inv)

# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,mask)

但为了使其工作,掩模必须具有与图像相同数量的通道,因此这通常不起作用,并且会通过将掩模转换为多通道图像来引入一些开销。

答案 1 :(得分:-1)

创建过滤器以从原始图像中删除颜色徽标

# Now black-out the area of logo in ROI
 img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

仅从原始图像中提取徽标

# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)`

将它们添加到仅提取徽标(颜色)

要获得深入信息,您可以使用

help(function)

在python控制台中

>>> help(cv2.bitwise_and)
Help on built-in function bitwise_and:

这就是你希望它能帮助你理解究竟发生了什么

Help on built-in function bitwise_and:

bitwise_and(...)
bitwise_and(src1, src2[, dst[, mask]]) -> dst
.   @brief computes bitwise conjunction of the two arrays (dst = src1 & src2)
.   Calculates the per-element bit-wise conjunction of two arrays or an
.   array and a scalar.
.   
.   The function cv::bitwise_and calculates the per-element bit-wise logical conjunction for:
.   *   Two arrays when src1 and src2 have the same size:
.   \f[\texttt{dst} (I) =  \texttt{src1} (I)  \wedge \texttt{src2} (I) \quad \texttt{if mask} (I) \ne0\f]
.   *   An array and a scalar when src2 is constructed from Scalar or has
.   the same number of elements as `src1.channels()`:
.   \f[\texttt{dst} (I) =  \texttt{src1} (I)  \wedge \texttt{src2} \quad \texttt{if mask} (I) \ne0\f]
.   *   A scalar and an array when src1 is constructed from Scalar or has
.   the same number of elements as `src2.channels()`:
.   \f[\texttt{dst} (I) =  \texttt{src1}  \wedge \texttt{src2} (I) \quad \texttt{if mask} (I) \ne0\f]
.   In case of floating-point arrays, their machine-specific bit
.   representations (usually IEEE754-compliant) are used for the operation.
.   In case of multi-channel arrays, each channel is processed
.   independently. In the second and third cases above, the scalar is first
.   converted to the array type.
.   @param src1 first input array or a scalar.
.   @param src2 second input array or a scalar.
.   @param dst output array that has the same size and type as the input
.   arrays.
.   @param mask optional operation mask, 8-bit single channel array, that
.   specifies elements of the output array to be changed.