1 所用到的主要OpenCv API
/** @brief 逻辑与
@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());
/** @brief 逻辑或
@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_or(InputArray src1, InputArray src2, OutputArray dst, InputArray mask = noArray());
/** @brief 逻辑异或
@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_xor(InputArray src1, InputArray src2, OutputArray dst, InputArray mask = noArray());
/** @brief Inverts every bit of an array.取反
@param src input array.
@param dst output array that has the same size and type as the input
array.
@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_not(InputArray src, OutputArray dst, InputArray mask = noArray());
2 程序代码
#include "opencv2opencv.hpp" #include <iostream> using namespace std; using namespace cv; int main(int argc, char *argv[]) { //创建图片1 Mat src1 = Mat::zeros(Size(400, 400), CV_8UC3); Rect rect(100, 100, 100, 100); src1(rect) = Scalar(0, 0, 255);//bgr imshow("input1",src1); printf("创建第一张图片... "); //创建图片2 Mat src2 = Mat::zeros(Size(400, 400), CV_8UC3); rect.x = 150; rect.y = 150; src2(rect) = Scalar(0, 0, 255);//bgr imshow("input2", src2); printf("创建第二张图片... "); //取反操作 Mat src = imread("G:\CVworkstudy\program_wwx\研习社140课时\ZhaiZhigang140\lena.jpg"); imshow("input", src); Mat dst; bitwise_not(src, dst); //逻辑操作 Mat dst1, dst2, dst3; bitwise_and(src1,src2,dst1); bitwise_or(src1, src2, dst2); bitwise_xor(src1, src2, dst3); //结果显示 imshow("取反操作", dst); imshow("逻辑与", dst1); imshow("逻辑或", dst2); imshow("逻辑异或", dst3); waitKey(0); return 0; }
3 运行结果