. 1 #include " the iostream " 2 #include " opencv2 / opencv.hpp " . 3 #include " Vector " . 4 . 5 the using namespace STD; . 6 the using namespace CV; . 7 . 8 / * calculate true color histogram of the image * / . 9 void splitRgbImgPro ( const Mat IMG, Vector <Mat> & imgRGB); 10 void calImgHistPro (Vector <Mat> imgrgb, Vector <Vector < int >> & histData); . 11 voidshowImgHistPro (Vector <Vector < int >> & histD); 12 is void rotateImgPro (Mat & IMG, a float angle); 13 is int main () 14 { 15 Mat IMGSRC imread = ( " E: the ETC \\ \\ \\ VideoPlateRecog 1008 \ \ \\ 144.bmp the SRC " ); 16 / * color space separation * / . 17 Vector <Mat> imgRGB; 18 is splitRgbImgPro (IMGSRC, imgRGB); . 19 / * calculate three-channel histogram * / 20 is Vector <Vector < int > > histData; 21 calImgHistPro(imgRGB, histData); 22 /*绘制三通道直方图*/ 23 showImgHistPro(histData); 24 25 system("pause"); 26 return 0; 27 } 28 29 void calImgHistPro(vector<Mat> imgrgb,vector<vector<int>>& histData) 30 { 31 vector<Mat> imgrgbSrc = imgrgb; 32 int width = imgrgb[0].cols; 33 int height = imgrgb[0].rows; 34 for (int n = 0; n < imgrgb.size(); n++) 35 { 36 Mat imgSrc = imgrgbSrc[n].clone(); 37 int img_sum = -1; 38 vector<int> p(256); 39 for (int m = 0; m <p.size() ; m++) 40 { 41 p[m] = 0; 42 } 43 for (int i = 0; i < height; i++) 44 { 45 uchar* ptr = imgSrc.ptr<uchar>(i); 46 for (int j = 0; j < width; j++) 47 { 48 int k = ptr[j]; 49 p[k]++; 50 } 51 } 52 /*find max element of vector*/ 53 for (auto it = p.begin(); it != p.end(); it++) 54 { 55 if (*it > img_sum) 56 { 57 img_sum = *it; 58 } 59 } 60 for (int m = 0; m < 256; m++) 61 { 62 p[m] = p[m]*255 / img_sum; 63 } 64 histData.push_back(p); 65 } 66 } 67 68 void showImgHistPro(vector<vector<int>>& histD) 69 { 70 vector<vector<int>> histSrc=histD; 71 int cols = histD[0].size(); 72 int rows = -1; 73 Mat histRGB(cols , cols * 3, CV_8UC1, Scalar(0)); 74 for (int n = 0; n < histD.size(); n++) 75 { 76 vector<int> hist = histD[n]; 77 Mat histImg(cols, cols, CV_8UC1, Scalar(0 )); 78 for ( int i = 0 ; i <cols; i ++ ) 79 { 80 uchar * ptr = histImg.ptr (i); 81 for ( int j = 0 ; j <hist [i]; j ++ ) 82 { 83 ptr [j] = 255 ; 84 } 85 } 86 / * 旋转° 90 * / 87 Mat histImg1 (cabbage, cabbage, CV_8UC1, Scalar ( 0 )); 88 for (int l = 0; l < cols; l++) 89 { 90 for (int k = 0; k < cols; k++) 91 { 92 histImg1.at<uchar>(l, k) = histImg.at<uchar>(k, cols-l-1); 93 } 94 } 95 histImg1.copyTo(histRGB(Rect(cols*n,0, cols, cols))); 96 } 97 imshow("Img_R_G_B", histRGB); 98 waitKey(0); 99 } 100 101 void rotateImgPro(Mat& img,float angle) 102 { 103 Mat retMat(img.cols, img.rows, CV_8UC1, Scalar(0)); 104 float anglePI = (float)(angle*CV_PI / 180); 105 int xSm, ySm; 106 for (int i = 0; i < retMat.rows; i++) 107 { 108 for (int j = 0; j < retMat.cols; j++) 109 { 110 xSm = (int)((i - retMat.rows / 2)*cos(anglePI) - 111 (j - retMat.cols / 2)*sin(anglePI) + 0.5); 112 ySm = (int)((i - retMat.rows / 2)*sin(anglePI) + 113 (j - retMat.cols / 2)*cos(anglePI) + 0.5); 114 retMat.at<uchar>(i, j) = img.at<uchar>(xSm, ySm); 115 116 } 117 } 118 } 119 120 121 122 123 void splitRgbImgPro(const Mat img, vector<Mat>& imgRGB) 124 { 125 Mat imgSrc = img.clone(); 126 int width = imgSrc.rows; 127 int height = imgSrc.cols; 128 Mat imgR(width, height, CV_8UC1); 129 Mat imgG(width, height, CV_8UC1); 130 Mat imgB(width, height, CV_8UC1); 131 for (int i = 0; i < width; i++) 132 { 133 Vec3b * imgPtr = imgSrc.ptr Vec3b> (i); 134 fly flying imgSPtr * = imgSrc.ptr <> (i); 135 fly flying imgRPtr * = imgR.ptr <> (i); 136 fly flying imgGPtr * = imgG.ptr <> (i); 137 fly flying imgBPtr * = imgB.ptr <> (i); 138 for ( int j = 0 ; j <height; j ++ ) { 139 140 imgRPtr [j] = imgSPtr [ 0 ]; 141 imgGPtr [j] = imgSPtr [ 1 ]; 142 imgBPtr[j] = imgSPtr[2]; 143 imgSPtr += 3; 144 } 145 } 146 imgRGB.push_back(imgR); 147 imgRGB.push_back(imgG); 148 imgRGB.push_back(imgB); 149 }