Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python OpenCv逐位运算的物理意义_Python_Opencv_Computer Vision - Fatal编程技术网

Python OpenCv逐位运算的物理意义

Python OpenCv逐位运算的物理意义,python,opencv,computer-vision,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 = mas

我最近刚开始使用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)`
我读过关于位操作的书,我了解它们是什么。但我无法用非常简单的语言找到上面两行代码在做什么?上述函数中的参数掩码在做什么

我遵循以下教程

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

问候


Nischal

该代码将创建一个包含前景遮罩中的所有
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)

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

代码将创建一个包含前景遮罩中所有
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)

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

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

# 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)`
仅从原始图像中提取徽标

# 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.

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

# 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)`
仅从原始图像中提取徽标

# 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.

嗨,米卡,说它计算两幅图像的交集是正确的吗?只有当两幅图像(或至少一幅图像)都是遮罩时才正确。否则,“交叉点”的含义就不清楚了。也许它真的是RGB空间中的某种交集,例如,如果第一幅图像中的一个像素是纯红色,而第二幅图像中的像素是纯蓝色,则按位和将是黑色的,因为它们在RGB范围内不“重叠”。但是,如果第一个像素是(128128),第二个像素是(127127),该怎么办?结果将再次为0,因为这里有10000000字节和01111111字节。因此,我认为一般来说,“交集”不是正确的词。这里可以看到通常使用逐位运算执行的问题/任务是什么:嗨,米卡,说它计算两幅图像的交集是正确的吗?只有当两幅图像(或至少一幅图像)都是遮罩时才正确。否则,还不清楚“交叉口”应该是什么意思。也许它真的是RGB空间中的某种交集,例如,如果第一幅图像中的一个像素是纯红色,而第二幅图像中的像素是纯蓝色,则按位和将是黑色的,因为它们在RGB范围内不“重叠”。但是,如果第一个像素是(128128),第二个像素是(127127),该怎么办?结果将再次为0,因为这里有10000000字节和01111111字节。因此,我认为一般来说,“交集”不是正确的词。通过按位操作通常会执行哪些类型的问题/任务,可以在这里看到: