opencv中可以对Mat对象可以进行位操作,主要有与,非,或,异或操作
具体代码为 1 #include <iostream>
1 #include <iostream> 2 #include <opencv2/opencv.hpp> 3 using namespace cv; 4 using namespace std; 5 6 int main(int args, char** argv) 7 { 8 Mat src = imread("E:/OpenCV/bin/Resource/lena.jpg", IMREAD_UNCHANGED); 9 if (src.empty()) { 10 printf("could not find image"); 11 return -1; 12 } 13 namedWindow("input", WINDOW_AUTOSIZE); 14 imshow("input", src); 15 16 //图像取反 17 Mat reverse; 18 bitwise_not(src, reverse); 19 imshow("bitwise_not", reverse); 20 21 Mat reverse1; 22 Mat mask=Mat::zeros(src.size(),CV_8UC1); 23 Mat mask2 = Mat::zeros(src.size(), CV_8UC1); 24 int w = src.cols / 2; 25 int h = src.rows / 2; 26 for (int row = 50; row < h; row++) 27 { 28 for (int col = 100; col < w; col++) 29 { 30 mask.at<uchar>(row, col) = 127; 31 mask2.at<uchar>(row, col) = 50; 32 } 33 } 34 bitwise_not(src, reverse1,mask); 35 imshow("bitwise_not1", reverse1); 36 37 //与操作 38 Mat and1; 39 bitwise_and(src, src, and1, mask); 40 imshow("bitwise_and1", and1); 41 42 //或操作 43 Mat or1; 44 bitwise_or(src, src, or1, mask); 45 imshow("bitwise_or1", or1); 46 47 //异或操作 48 Mat xor1; 49 bitwise_xor(src, and1, xor1); 50 imshow("bitwise_xor1", xor1); 51 while (waitKey(50) != 27) 52 { 53 54 } 55 destroyAllWindows(); 56 return 0; 57 }
显示结果为: