一、编程环境:
OpenCV | 4.1.0 |
IDE | Visual Studio 2017 Enterprise (15.9.13) |
操作系统 | Windows 10 x64 中文专业版 (1903) |
二、图像像素位运算:
- 与:bitwise_and()
- 或:bitwise_or()
- 非(取反):bitwise_not()
- 异或:bitwise_xor()
三、示例代码:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char *argv[])
{
//创建第1张矩形图
Mat src1 = Mat::zeros(Size(300, 300), CV_8UC3);
Rect rect(50, 50, 100, 100);
src1(rect) = Scalar(0, 100, 100);
imshow("1--图1", src1);
//创建第2张矩形图(显示坐标与上一个错开)
Mat src2 = Mat::zeros(Size(300, 300), CV_8UC3);
rect.x = 100;
rect.y = 100;
src2(rect) = Scalar(0, 0, 255);
imshow("2--图2", src2);
//两张图像素逻辑操作
Mat dst1, dst2, dst3;
bitwise_or(src1, src2, dst1);
bitwise_xor(src1, src2, dst2);
bitwise_and(src1, src2, dst3);
imshow("3--或(bitwise_or)", dst1);
imshow("4--异或(bitwise_xor)", dst2);
imshow("5--与(bitwise_and)", dst3);
//使用实际图像演示像素取反
Mat src = imread("../images/test.png");
namedWindow("6--原图", WINDOW_AUTOSIZE);
imshow("6--原图", src);
//取反操作
Mat dst;
bitwise_not(src, dst);
imshow("7--取反(bitwise_not)", dst);
waitKey(0);
return 0;
}
四、运行效果: