学OpenCV

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

简单的看下效果。

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

 1 #include <iostream>
 2 #include <opencv2/opencv.hpp>
 3 #include <opencv2/core/utils/logger.hpp>
 4 
 5 #include <vector>
 6 
 7 void AlphaMat(cv::Mat& mat)
 8 {
 9     CV_Assert(mat.channels() == 4);
10 
11     int rows = mat.rows;
12     int cols = mat.cols;
13 
14     for (int i = 0; i < rows; i++)
15     {
16         for (int j = 0; j < cols; j++)
17         {
18             cv::Vec4b& pixel_bgra =mat.at<cv::Vec4b>(i, j);
19             pixel_bgra[0] = UCHAR_MAX;
20             pixel_bgra[1] = cv::saturate_cast<uchar>(float(cols-j) / ((float)cols) * UCHAR_MAX);//归一化
21             pixel_bgra[2] = cv::saturate_cast<uchar>(float(cols - j) / ((float)cols) * UCHAR_MAX);//归一化
22             pixel_bgra[3] = cv::saturate_cast<uchar>(0.5*(pixel_bgra[1]+ pixel_bgra[2]));
23         }
24     }
25 }
26 
27 void Test1()
28 {
29     cv::Mat mat(480, 640, CV_8UC4);
30     AlphaMat(mat);
31 
32     std::vector<int> vctPicParam;
33     vctPicParam.push_back(cv::IMWRITE_JPEG_QUALITY);//压一个设置,在压一个数值
34     vctPicParam.push_back(100);
35 
36     bool bRet=cv::imwrite(R"(C:\ProjectHome\test\LeanOpenCV\Test_SavePicture\test1.jpeg)",mat, vctPicParam);
37     if (!bRet)
38     {
39         std::cout << "save failed!" << std::endl;
40     }
41     else
42     {
43         std::cout << "save ok!" << std::endl;
44     }
45 
46     return;
47 
48 }
49 
50 int main()
51 {
52     cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);
53     
54     Test1();
55 
56     return 0;
57 }