学OpenCV
===========================================
根据公式,这个mean在这里是平均值
mask只能用单通道的uchar类型。
===========================================
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 SerialArr_Stack(arr, int, 5*5*3); 36 37 cv::Mat img(5, 5, CV_32SC3, arr); 38 int channels = img.channels(); 39 40 cv::Scalar sarr1=cv::mean(img); 41 42 for (int i = 0; i < channels; i++) 43 { 44 std::cout <<"Test1 mean " << sarr1[i] << std::endl; 45 } 46 47 uchar arr2[] = { 1,1,1,1,1, 48 1,1,1,1,1, 49 0,0,0,0,0, 50 0,0,0,0,0 , 51 0,0,0,0,0 }; 52 53 cv::Mat mask(5, 5, CV_8UC1,arr2); 54 55 cv::Scalar sarr2 = cv::mean(img,mask); 56 57 for (int i = 0; i < channels; i++) 58 { 59 std::cout << "Test1 mean with mask " << sarr2[i] << std::endl; 60 } 61 } 62 63 void Test2() 64 { 65 SerialArr_Stack(arr, int, 5 * 5 * 3); 66 67 cv::Mat img(5, 5, CV_32SC3, arr); 68 int channels = img.channels(); 69 70 cv::Scalar mean, stddev; 71 cv::meanStdDev(img,mean,stddev); 72 73 for (int i = 0; i < channels; i++) 74 { 75 std::cout << "Test2 mean " << mean[i] << std::endl; 76 } 77 for (int i = 0; i < channels; i++) 78 { 79 std::cout << "Test2 stddev " << stddev[i] << std::endl; 80 } 81 82 uchar arr2[] = { 0,0,1,0,0, 83 0,1,1,1,0 , 84 1,1,1,1,1 , 85 0,1,1,1,0 , 86 0,0,1,0,0 }; 87 88 cv::Mat mask(5, 5, CV_8UC1, arr2); 89 cv::Scalar mean2, stddev2; 90 cv::meanStdDev(img,mean2,stddev2); 91 92 for (int i = 0; i < channels; i++) 93 { 94 std::cout << "Test2 mean with mask" << mean2[i] << std::endl; 95 } 96 for (int i = 0; i < channels; i++) 97 { 98 std::cout << "Test2 stddev with mask" << stddev2[i] << std::endl; 99 } 100 } 101 102 int main() 103 { 104 cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR); 105 106 Test1(); 107 Test2(); 108 109 cv::waitKey(0); 110 111 return 0; 112 }
效果
标签:meanStdDev,int,name,++,mean,type,单张,size From: https://www.cnblogs.com/wlsandwho/p/18371470