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

 #include "iostream"
#include "opencv2/opencv.hpp"
#include "vector" using namespace std;
using namespace cv; /*计算真彩色图像的直方图*/
void splitRgbImgPro(const Mat img, vector<Mat>& imgRGB);
void calImgHistPro(vector<Mat> imgrgb, vector<vector<int>>& histData);
void showImgHistPro(vector<vector<int>>& histD);
void rotateImgPro(Mat& img, float angle);
int main()
{
Mat imgSrc = imread("E:\\VideoPlateRecog\\ETC\\1008\\SRC\\144.bmp");
/*色彩空间分离*/
vector<Mat> imgRGB;
splitRgbImgPro(imgSrc, imgRGB);
/*计算三通道直方图*/
vector<vector<int>> histData;
calImgHistPro(imgRGB, histData);
/*绘制三通道直方图*/
showImgHistPro(histData); system("pause");
return ;
} void calImgHistPro(vector<Mat> imgrgb,vector<vector<int>>& histData)
{
vector<Mat> imgrgbSrc = imgrgb;
int width = imgrgb[].cols;
int height = imgrgb[].rows;
for (int n = ; n < imgrgb.size(); n++)
{
Mat imgSrc = imgrgbSrc[n].clone();
int img_sum = -;
vector<int> p();
for (int m = ; m <p.size() ; m++)
{
p[m] = ;
}
for (int i = ; i < height; i++)
{
uchar* ptr = imgSrc.ptr<uchar>(i);
for (int j = ; j < width; j++)
{
int k = ptr[j];
p[k]++;
}
}
/*find max element of vector*/
for (auto it = p.begin(); it != p.end(); it++)
{
if (*it > img_sum)
{
img_sum = *it;
}
}
for (int m = ; m < ; m++)
{
p[m] = p[m]* / img_sum;
}
histData.push_back(p);
}
} void showImgHistPro(vector<vector<int>>& histD)
{
vector<vector<int>> histSrc=histD;
int cols = histD[].size();
int rows = -;
Mat histRGB(cols , cols * , CV_8UC1, Scalar());
for (int n = ; n < histD.size(); n++)
{
vector<int> hist = histD[n];
Mat histImg(cols, cols, CV_8UC1, Scalar());
for (int i = ; i < cols; i++)
{
uchar* ptr = histImg.ptr(i);
for (int j = ; j < hist[i]; j++)
{
ptr[j] = ;
}
}
/*旋转90°*/
Mat histImg1(cols, cols, CV_8UC1, Scalar());
for (int l = ; l < cols; l++)
{
for (int k = ; k < cols; k++)
{
histImg1.at<uchar>(l, k) = histImg.at<uchar>(k, cols-l-);
}
}
histImg1.copyTo(histRGB(Rect(cols*n,, cols, cols)));
}
imshow("Img_R_G_B", histRGB);
waitKey();
} void rotateImgPro(Mat& img,float angle)
{
Mat retMat(img.cols, img.rows, CV_8UC1, Scalar());
float anglePI = (float)(angle*CV_PI / );
int xSm, ySm;
for (int i = ; i < retMat.rows; i++)
{
for (int j = ; j < retMat.cols; j++)
{
xSm = (int)((i - retMat.rows / )*cos(anglePI) -
(j - retMat.cols / )*sin(anglePI) + 0.5);
ySm = (int)((i - retMat.rows / )*sin(anglePI) +
(j - retMat.cols / )*cos(anglePI) + 0.5);
retMat.at<uchar>(i, j) = img.at<uchar>(xSm, ySm); }
}
} void splitRgbImgPro(const Mat img, vector<Mat>& imgRGB)
{
Mat imgSrc = img.clone();
int width = imgSrc.rows;
int height = imgSrc.cols;
Mat imgR(width, height, CV_8UC1);
Mat imgG(width, height, CV_8UC1);
Mat imgB(width, height, CV_8UC1);
for (int i = ; i < width; i++)
{
Vec3b* imgPtr = imgSrc.ptr<Vec3b>(i);
uchar* imgSPtr = imgSrc.ptr<uchar>(i);
uchar* imgRPtr = imgR.ptr<uchar>(i);
uchar* imgGPtr = imgG.ptr<uchar>(i);
uchar* imgBPtr = imgB.ptr<uchar>(i);
for (int j = ; j < height; j++)
{
imgRPtr[j] = imgSPtr[];
imgGPtr[j] = imgSPtr[];
imgBPtr[j] = imgSPtr[];
imgSPtr += ;
}
}
imgRGB.push_back(imgR);
imgRGB.push_back(imgG);
imgRGB.push_back(imgB);
}
上一篇:在Linux上搭建VisualSVN Server(svn服务端)


下一篇:EasyPusher手机直播编码推送之图像旋转90度后画面重复的问题