OpenCV 图像的集合和逻辑操作

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)                                              结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪易

给我来点鼓励吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值