前置知识:
image1 | image2 | 与 | 或 | 异或 |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
void QuickDemo::bit_wise_Demo(Mat& image) {
//空白图像创建
Mat m1 = Mat::zeros(Size(256, 256), CV_8UC3);
Mat m2 = Mat::zeros(Size(256, 256), CV_8UC3);
//在图像内添加矩阵
rectangle(m1, Rect(100, 100, 80, 80), Scalar(255, 255, 0), -1, LINE_8, 0);
rectangle(m2, Rect(150, 150, 80, 80), Scalar(0, 255, 255), -1, LINE_8, 0);
imshow("m1", m1);
imshow("m2", m2);
}
其中,使用rectangle函数生成矩形,其定义如下:
rectangle(输入图像,形状函数(形状参数),颜色, 线宽, 图形学处理~一般用LINE_8即可,0)
bitwise_and(m1, m2, dst);——与
bitwise_or(m1, m2, dst);——或
bitwise_xor(m1, m2, dst);——异或
与1中代码结合如下:
void QuickDemo::bit_wise_Demo(Mat& image) {
//空白图像创建
Mat m1 = Mat::zeros(Size(256, 256), CV_8UC3);
Mat m2 = Mat::zeros(Size(256, 256), CV_8UC3);
//在图像内添加矩阵
rectangle(m1, Rect(100, 100, 80, 80), Scalar(255, 255, 0), -1, LINE_8, 0);
rectangle(m2, Rect(150, 150, 80, 80), Scalar(0, 255, 255), -1, LINE_8, 0);
imshow("m1", m1);
imshow("m2", m2);
Mat dst;
//bitwise_and(m1, m2, dst);
//bitwise_or(m1, m2, dst);
bitwise_xor(m1, m2, dst);
imshow("像素位操作", dst);
}
此外,还有非操作
bitwise(input_src, output_src);主流写法
Mat dst = ~image; 非主流写法
运行结果示例: