1. bitwise_and(A和B的交集)
/** @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.
*/
CV_EXPORTS_W void bitwise_and(InputArray src1, InputArray src2,
OutputArray dst, InputArray mask = noArray());
错误示范
cv::Mat srcImg = cv::imread("washington_infrared.tif");
cv::Mat mask = cv::Mat::zeros(srcImg.size(), srcImg.type());
int cols = mask.cols;
int rows = mask.rows;
cv::Mat roi = mask(Rect(cols / 4, rows / 4, cols / 2, rows / 2));
roi.setTo(1);
cv::imwrite("mask.jpg", mask);
cv::Mat result;
bitwise_and(srcImg, mask, result);
cv::imwrite("bitwise_and_result.jpg", result);
原图 掩膜(0,1) 结果
正确示范
cv::Mat srcImg = cv::imread("washington_infrared.tif");
cv::Mat mask = cv::Mat::zeros(srcImg.size(), srcImg.type());
int cols = mask.cols;
int rows = mask.rows;
cv::Mat roi = mask(Rect(cols / 4, rows / 4, cols / 2, rows / 2));
roi.setTo(255); //位操作,应该是8位都做与操作或者或操作
cv::imwrite("mask.jpg", mask);
cv::Mat result;
bitwise_and(srcImg, mask, result);
cv::imwrite("bitwise_and_result.jpg", result);
原图 掩膜(0,255) 结果