Mat格式 3通道直方图
2018-03-17 10:39
239 查看
#include "stdafx.h"
#include "highgui.hpp"
#include "cv.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main() {
//读取图像,并显示图像
Mat img = imread("H:\\3.jpg",1);
if (img.empty())
cout << "Load image error" << endl;
imshow("raw image",img);
//设置直方图bins数目以及每个bin的范围
int bins = 256; int hist_size[] = { bins };
float range[] = { 0,256 }; const float* ranges[] = { range };//const 不能省否则calcHist会报错
//创建三个hist
MatND hist_b, hist_g, hist_r;
//计算b g r 分量的直方图
int channels_b[] = { 0 };
calcHist( &img ,1, channels_b, Mat(), hist_b,1, hist_size, ranges,true,false);
int channels_g[] = { 1 };
calcHist(&img, 1, channels_g, Mat(), hist_g, 1, hist_size, ranges, true, false);
int channels_r[] = { 2 };
calcHist(&img, 1, channels_r, Mat(), hist_r, 1, hist_size, ranges, true, false);
//获取b g r分量中的最大值
double max_b, max_g, max_r;
minMaxLoc(hist_b, 0, &max_b);
minMaxLoc(hist_g, 0, &max_g);
minMaxLoc(hist_r, 0, &max_r);
int scale = 1;
int hist_height = 256;//直方图宽度
Mat hist_img = Mat::zeros(hist_height, bins * 3, CV_8UC3);//创建直方图图像
//画直方图
for(int i = 0;i<bins;i++)
{
float bin_val_b = hist_b.at<float>(i);
float bin_val_g = hist_g.at<float>(i);
float bin_val_r = hist_r.at<float>(i);
int intensity_b = cvRound(bin_val_b * 256 / max_b);
int intensity_g = cvRound(bin_val_g * 256 / max_g);
int intensity_r = cvRound(bin_val_r * 256 / max_r);
//b分量直方图
rectangle(hist_img,
Point(i*scale, hist_height),
Point((i + 1)*scale, (hist_height - intensity_b)),Scalar(255,0,0), FILLED);
//g分量直方图
rectangle(hist_img,
Point((i + bins)*scale, hist_height),
Point((i + bins + 1)*scale, (hist_height - intensity_g)), Scalar(0, 255, 0), FILLED);
//r分量直方图
rectangle(hist_img,
Point((i + 2 * bins)*scale, hist_height),
Point((i + 2 * bins + 1)*scale, (hist_height - intensity_r)), Scalar(0, 0, 255), FILLED);
}
imshow("BGR histogram", hist_img);
waitKey(0);
return(0);
}
#include "highgui.hpp"
#include "cv.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main() {
//读取图像,并显示图像
Mat img = imread("H:\\3.jpg",1);
if (img.empty())
cout << "Load image error" << endl;
imshow("raw image",img);
//设置直方图bins数目以及每个bin的范围
int bins = 256; int hist_size[] = { bins };
float range[] = { 0,256 }; const float* ranges[] = { range };//const 不能省否则calcHist会报错
//创建三个hist
MatND hist_b, hist_g, hist_r;
//计算b g r 分量的直方图
int channels_b[] = { 0 };
calcHist( &img ,1, channels_b, Mat(), hist_b,1, hist_size, ranges,true,false);
int channels_g[] = { 1 };
calcHist(&img, 1, channels_g, Mat(), hist_g, 1, hist_size, ranges, true, false);
int channels_r[] = { 2 };
calcHist(&img, 1, channels_r, Mat(), hist_r, 1, hist_size, ranges, true, false);
//获取b g r分量中的最大值
double max_b, max_g, max_r;
minMaxLoc(hist_b, 0, &max_b);
minMaxLoc(hist_g, 0, &max_g);
minMaxLoc(hist_r, 0, &max_r);
int scale = 1;
int hist_height = 256;//直方图宽度
Mat hist_img = Mat::zeros(hist_height, bins * 3, CV_8UC3);//创建直方图图像
//画直方图
for(int i = 0;i<bins;i++)
{
float bin_val_b = hist_b.at<float>(i);
float bin_val_g = hist_g.at<float>(i);
float bin_val_r = hist_r.at<float>(i);
int intensity_b = cvRound(bin_val_b * 256 / max_b);
int intensity_g = cvRound(bin_val_g * 256 / max_g);
int intensity_r = cvRound(bin_val_r * 256 / max_r);
//b分量直方图
rectangle(hist_img,
Point(i*scale, hist_height),
Point((i + 1)*scale, (hist_height - intensity_b)),Scalar(255,0,0), FILLED);
//g分量直方图
rectangle(hist_img,
Point((i + bins)*scale, hist_height),
Point((i + bins + 1)*scale, (hist_height - intensity_g)), Scalar(0, 255, 0), FILLED);
//r分量直方图
rectangle(hist_img,
Point((i + 2 * bins)*scale, hist_height),
Point((i + 2 * bins + 1)*scale, (hist_height - intensity_r)), Scalar(0, 0, 255), FILLED);
}
imshow("BGR histogram", hist_img);
waitKey(0);
return(0);
}
相关文章推荐
- python读取mat数据后转格式提示“KeyError: 0”
- Python OpenCV学习笔记之:计算彩色图像各通道的直方图及图像区域直方图
- .pnt .pcf .dmt .mat .sym 等格式的文件如何导入Windchill
- Python 读入 .Mat格式数据的方式
- RGB三个通道分别计算直方图
- 将pgm图片使用Sequence file存储到hdfs,并读取为Mat数组格式
- MATLAB中将图片数据存储为.mat格式
- 【opencv】Mat数据直方图计算函数
- c++ 写.mat格式文件
- opencv学习之四:rgb通道的分离以及直方图的显示计算
- ImageMagick++格式转化为Opencv的Mat格式
- Matplotlib 绘图双纵坐标轴设置及控制设置时间格式
- matlab的mat格式
- opencv3.0 IplImage* 与Mat 格式相互转换
- numpy和matplotlib绘制直方图
- 多通道直方图汇总
- matplotlib绘制直方图、条形图和饼图
- IplImage QImage Mat 格式互转
- opencv 通道和深度和图像格式
- matplotlib 柱状图、饼图;直方图、盒图