学OpenCV
===============================================================================================
这里额外看了下使用mask的情形
===============================================================================================
代码
1 #include <iostream> 2 3 #include <opencv2/opencv.hpp> 4 #include <opencv2/core/utils/logger.hpp> 5 6 7 template <typename T> 8 T* MakeSerialArr(int datasize) 9 { 10 T* ptr = new T[datasize]; 11 for (int i = 0; i < datasize; i++) 12 { 13 ptr[i] = (T)i; 14 } 15 return ptr; 16 } 17 18 #define SerialArr_Heap(name,type,size) \ 19 type* name = new type[size];\ 20 for (int i = 0; i < size; i++) \ 21 {\ 22 name[i] = (type)i;\ 23 } 24 25 #define SerialArr_Stack(name,type,size) \ 26 type name[size]={};\ 27 for (int i = 0; i < size; i++) \ 28 {\ 29 name[i] = (type)i;\ 30 } 31 32 33 void Test1() 34 { 35 cv::Mat img; 36 img = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\lena.png)"); 37 if (img.empty()) 38 { 39 std::cout << "请确认图像文件名是否正确" << std::endl; 40 return ; 41 } 42 43 cv::imshow("img", img); 44 45 cv::Mat img0 = cv::Mat::zeros(200, 200, CV_8UC1); 46 cv::Rect rect0(50, 50, 100, 100); 47 img0(rect0) = cv::Scalar(UCHAR_MAX); 48 cv::imshow("img0", img0); 49 50 cv::Mat img1 = cv::Mat::zeros(200, 200, CV_8UC1); 51 cv::Rect rect1(100, 100, 100, 100); 52 img1(rect1) = cv::Scalar(UCHAR_MAX); 53 cv::imshow("img1", img1); 54 55 //logical operation 56 cv::Mat mmand, mmor, mmxor, mnot, imgnot; 57 cv::bitwise_and(img0, img1, mmand); 58 cv::bitwise_or(img0, img1, mmor); 59 cv::bitwise_xor(img0, img1, mmxor); 60 cv::bitwise_not(img0, mnot); 61 cv::bitwise_not(img, imgnot); 62 63 cv::imshow("and", mmand); 64 cv::imshow("or", mmor); 65 cv::imshow("xor", mmxor); 66 cv::imshow("not", mnot); 67 cv::imshow("not", imgnot); 68 } 69 70 void Test2() 71 { 72 cv::Mat img; 73 img = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\lena.png)"); 74 if (img.empty()) 75 { 76 std::cout << "请确认图像文件名是否正确" << std::endl; 77 return; 78 } 79 80 //cv::imshow("img", img); 81 82 cv::Mat img0 = cv::Mat::zeros(200, 200, CV_8UC1); 83 cv::Rect rect0(50, 50, 100, 100); 84 img0(rect0) = cv::Scalar(UCHAR_MAX); 85 //cv::imshow("img0", img0); 86 87 cv::Mat img1 = cv::Mat::zeros(200, 200, CV_8UC1); 88 cv::Rect rect1(100, 100, 100, 100); 89 img1(rect1) = cv::Scalar(UCHAR_MAX); 90 //cv::imshow("img1", img1); 91 92 cv::Mat mask = cv::Mat::zeros(200, 200, CV_8UC1);//mask 93 cv::Rect rect(100,0,100,200); 94 mask(rect) = cv::Scalar(UCHAR_MAX); 95 cv::imshow("mask", mask); 96 97 cv::Mat mask2 = cv::Mat::zeros(512, 512, CV_8UC1);//mask 98 cv::Rect rect2(256, 0, 256, 512); 99 mask2(rect2) = cv::Scalar(UCHAR_MAX); 100 cv::imshow("mask2", mask2); 101 102 //logical operation 103 cv::Mat mmand, mmor, mmxor, mnot, imgnot; 104 cv::bitwise_and(img0, img1, mmand, mask); 105 cv::bitwise_or(img0, img1, mmor, mask); 106 cv::bitwise_xor(img0, img1, mmxor, mask); 107 cv::bitwise_not(img0, mnot, mask); 108 cv::bitwise_not(img, imgnot, mask2); 109 110 cv::imshow("2and", mmand); 111 cv::imshow("2or", mmor); 112 cv::imshow("2xor", mmxor); 113 cv::imshow("2not", mnot); 114 cv::imshow("2not", imgnot); 115 } 116 117 int main() 118 { 119 cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR); 120 121 Test1(); 122 Test2(); 123 124 cv::waitKey(0); 125 126 return 0; 127 }
效果
标签:xor,name,img,int,bitwise,异或,type,size From: https://www.cnblogs.com/wlsandwho/p/18371993