学OpenCV

==============================================

通过掩模,可以实现抠图和通道选择的效果。

这里用的是min

==============================================

 

  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     float arrA[12] = { 1,2,3.3,4,5,9,5,7,8.2,9,10,2 };
 36     float arrB[12] = { 1,2.2,3,1,3,10,6,7,8,9.3,10,1 };
 37 
 38     cv::Mat imga(3, 4, CV_32FC1,arrA);
 39     cv::Mat imgb(3, 4, CV_32FC1, arrB);
 40 
 41     cv::Mat imgas(2, 3, CV_32FC2, arrA);
 42     cv::Mat imgbs(2, 3, CV_32FC2, arrB);
 43 
 44     cv::Mat max, min,maxs,mins;
 45     cv::max(imga, imgb, max);
 46     cv::min(imga, imgb, min);
 47     cv::max(imgas, imgbs, maxs);
 48     cv::min(imgas, imgbs, mins);
 49 
 50     std::cout << "Test1 max " << max << std::endl;
 51     std::cout << "Test1 min " << min << std::endl;
 52     std::cout << "Test1 maxs " << maxs << std::endl;
 53     std::cout << "Test1 mins " << mins << std::endl;
 54 }
 55 
 56 void Test2()
 57 {
 58     cv::Mat imgLena = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\lena.png)");
 59     cv::Mat imgNoobcv = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\noobcv.jpg)");
 60     if (imgLena.empty() || imgNoobcv.empty())
 61     {
 62         std::cout << "bad file" << std::endl;
 63         return;
 64     }
 65 
 66     cv::Mat min, max;
 67     cv::min(imgLena, imgNoobcv, min);
 68     cv::max(imgLena, imgNoobcv, max);
 69     cv::imshow("Test2 imgLena", imgLena);
 70     cv::imshow("Test2 imgNoobcv", imgNoobcv);
 71     cv::imshow("Test2 min", min);
 72     cv::imshow("Test2 max", max);
 73 }
 74 
 75 void Test3()
 76 {
 77     cv::Mat imgLena = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\lena.png)");
 78 
 79     cv::Mat mask1 = cv::Mat::zeros(cv::Size(512, 512), CV_8UC3);
 80     cv::Rect rect(100, 100, 300, 300);
 81     mask1(rect) = cv::Scalar(255, 255, 255);
 82 
 83     cv::Mat min, max;
 84     cv::min(imgLena, mask1, min);
 85     cv::max(imgLena, mask1, max);
 86     cv::imshow("Test3 min", min);
 87     cv::imshow("Test3 max", max);
 88 }
 89 
 90 void Test4()
 91 {
 92     cv::Mat imgLena = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\lena.png)");
 93 
 94     cv::Mat mask1 = cv::Mat(512, 512, CV_8UC3,cv::Scalar(0,0,255));
 95 
 96     cv::Mat min, max;
 97     cv::min(imgLena, mask1, min);
 98     cv::max(imgLena, mask1, max);
 99     cv::imshow("Test4 min", min);
100     cv::imshow("Test4 max", max);
101 }
102 
103 void Test5()
104 {
105     cv::Mat imgLena = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\lena.png)");
106     cv::Mat imgNoobcv = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\noobcv.jpg)");
107     cv::Mat imgLenaG, imgNoobcvG;
108     cv::cvtColor(imgLena, imgLenaG, cv::COLOR_BGR2GRAY);
109     cv::cvtColor(imgNoobcv, imgNoobcvG, cv::COLOR_BGR2GRAY);
110     cv::Mat minG, maxG;
111     cv::min(imgLenaG, imgNoobcvG, minG);
112     cv::max(imgLenaG, imgNoobcvG, maxG);
113 
114     cv::imshow("Test5 min", minG);
115     cv::imshow("Test5 max", maxG);
116 }
117 
118 int main()
119 {
120     cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);
121     
122     Test1();
123     Test2();
124     Test3();
125     Test4();
126     Test5();
127 
128     cv::waitKey(0);
129 
130     return 0;
131 }

==============================================

掩模

 

==============================================

效果

 

==============================================