• C++ 彩色图像(RGB)三通道直方图计算和绘制,图像逆时针旋转90° 实现代码


      1 #include "iostream"
      2 #include "opencv2/opencv.hpp"
      3 #include "vector"
      4 
      5 using namespace std;
      6 using namespace cv;
      7 
      8 /*计算真彩色图像的直方图*/
      9 void splitRgbImgPro(const Mat img, vector<Mat>& imgRGB);
     10 void calImgHistPro(vector<Mat> imgrgb, vector<vector<int>>& histData);
     11 void showImgHistPro(vector<vector<int>>& histD);
     12 void rotateImgPro(Mat& img, float angle);
     13 int main()
     14 {
     15     Mat imgSrc = imread("E:\VideoPlateRecog\ETC\1008\SRC\144.bmp");
     16     /*色彩空间分离*/    
     17     vector<Mat> imgRGB;
     18     splitRgbImgPro(imgSrc, imgRGB);
     19     /*计算三通道直方图*/
     20     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(cols, cols, 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         uchar* imgSPtr = imgSrc.ptr<uchar>(i);
    135         uchar* imgRPtr = imgR.ptr<uchar>(i);
    136         uchar* imgGPtr = imgG.ptr<uchar>(i);
    137         uchar* imgBPtr = imgB.ptr<uchar>(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 }
  • 相关阅读:
    与数学有关的几个猜成语
    tex 字体斜体设置
    Beamer中左边画图, 右边文字解释
    Beamer制作索引
    唐诗
    大学生数学竞赛试题荟萃2018-11-06更新
    浙江省2018年高等数学竞赛工科类试题
    读书笔记
    word中中文保持正体,英文用斜体的方法.
    山果(转载《人民日报》)
  • 原文地址:https://www.cnblogs.com/zhibei/p/11787818.html
Copyright © 2020-2023  润新知