分离RGB彩图颜色通道 也就是把每种分量的亮度图提出来
vector channels;
split(image1, channels);
Mat R = channels.at(0);
Mat G = channels.at(1);
Mat B = channels.at(2);
这样R,G,B每个图就是这个图的颜色分量图了
图片的克隆,深拷贝!
Mat image1_copy = image1.clone();
申明一个点操作
Point pt;
pt.x = 10;
pt.y = 10;
这样就可以得到一个点,你可以把它当作圆心来进行画圆操作
circle(image1_copy,pt, 6, CV_RGB(255, 0, 0), -1, 8, 0);
图,点,半径,颜色,-1代表填充1代表不填充,8和0都是默认参数
也可以申明两个点,进行画线操作。
line(image1_copy, Point(20,20), pt2, CV_RGB(0, 255, 0), 1, 8, 0);
这里也可以直接在函数里面写Point(20,20)也代表了一个点,但就不能在其他地方用这个了。
申明一个矩形的操作,也可以叫矩形的ROI
Rect rect;
rect.x = 10;
rect.y = 10;
rect.width = 90;
rect.height = 90;
rectangle(image1_copy, rect, CV_RGB(243, 125, 254), 1, 8, 0);
下面是直方图统计图的画法
void showHist(Mat& img, Mat& dst)
{
//1、创建3个矩阵来处理每个通道输入图像通道。
//我们用向量类型变量来存储每个通道,并用split函数将输入图像划分成3个通道。
vectorbgr;
split(img, bgr);
//2、定义直方图的区间数
int numbers = 256;
//3、定义变量范围并创建3个矩阵来存储每个直方图
float range[] = { 0,256 };
const float* histRange = { range };
Mat b_hist, g_hist, r_hist;
//4、使用calcHist函数计算直方图
int numbins = 256;
calcHist(&bgr[0], 1, 0, Mat(), b_hist, 1, &numbins, &histRange);
calcHist(&bgr[1], 1, 0, Mat(), g_hist, 1, &numbins, &histRange);
calcHist(&bgr[2], 1, 0, Mat(), r_hist, 1, &numbins, &histRange);
//5、创建一个512*300像素大小的彩色图像,用于绘制显示
int width = 800;
int height = 600;
Mat histImage(height, width, CV_8UC3, Scalar(0, 0, 0));
//6、将最小值与最大值标准化直方图矩阵
normalize(b_hist, b_hist, 0, height, NORM_MINMAX);
normalize(g_hist, g_hist, 0, height, NORM_MINMAX);
normalize(r_hist, r_hist, 0, height, NORM_MINMAX);
//7、使用彩色通道绘制直方图
int binStep = cvRound((float)width / (float)numbins); //通过将宽度除以区间数来计算binStep变量
for (int i = 1; i < numbins; i++)
{
line(histImage,
Point(binStep * (i - 1), height - cvRound(b_hist.at(i - 1))),
Point(binStep * (i), height - cvRound(b_hist.at(i))),
Scalar(255, 0, 0)
);
line(histImage,
Point(binStep * (i - 1), height - cvRound(g_hist.at(i - 1))),
Point(binStep * (i), height - cvRound(g_hist.at(i))),
Scalar(0, 255, 0)
);
line(histImage,
Point(binStep * (i - 1), height - cvRound(r_hist.at(i - 1))),
Point(binStep * (i), height - cvRound(r_hist.at(i))),
Scalar(0, 0, 255)
);
}
dst = histImage;
return;
}
其中值得学习的函数有:
创建画布:
int width = 800;
int height = 600;
Mat histImage(height, width, CV_8UC3, Scalar(0, 0, 0));
归一化高度宽度
normalize(b_hist, b_hist, 0, height, NORM_MINMAX);
normalize(g_hist, g_hist, 0, height, NORM_MINMAX);
normalize(r_hist, r_hist, 0, height, NORM_MINMAX);
对直方图函数处理后的每个统计直方图大小的处理
height - cvRound(b_hist.at(i - 1)
因为画布是从上往下数的
计算灰度图的直方统计量函数
int numbers = 256;
//3、定义变量范围并创建3个矩阵来存储每个直方图
float range[] = { 0,256 };
const float* histRange = { range };
Mat b_hist, g_hist, r_hist;
//4、使用calcHist函数计算直方图
int numbins = 256;
calcHist(&bgr[0], 1, 0, Mat(), b_hist, 1, &numbins, &histRange);
calcHist(&bgr[1], 1, 0, Mat(), g_hist, 1, &numbins, &histRange);
calcHist(&bgr[2], 1, 0, Mat(), r_hist, 1, &numbins, &histRange);
int h = R.rows;
int w = R.cols;
int hisgramR[256] = {0};
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
hisgramR[R.at(j, i)]= hisgramR[R.at(j, i)]+1;
}
}
针对于每个像素进行统计
int nHistWidth = 256;
int nHistHeight =400;
Mat matHistImage(nHistHeight, nHistWidth, CV_8UC3, Scalar(255, 255, 255));
for (int i = 0; i < 256; i++) {
line(matHistImage, Point(i, nHistHeight-1), Point(i, nHistHeight-hisgramR[i]*400/5000), CV_RGB(255,0, 0), 1, 8, 0);
}
制造画布,且归一化可能不太标准,找了一个比较大的数进行相乘除