opencv学习之直方图绘制
来源:互联网 发布:excel提取单元格数据 编辑:程序博客网 时间:2024/06/05 15:51
图像直方图就是用表示数字图像中亮度分布的直方图,标绘了图像中每个亮度值的像素数,可以借助观察该直方图了解如何调整亮度分布。
直方图意义如下:
.直方图是图像中像素强度分布的图形表达式
.它统计了每一个强度值所具有的像素个数
绘制H-S直方图示例:
#include"stdafx.h"#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/core/core.hpp>#include <opencv2/objdetect/objdetect.hpp>#include<opencv2/photo/photo.hpp>#include <string>#include <vector>#include <iostream>using namespace cv;using namespace std; int main(){Mat image = imread("E:\\pictures\\For_Project\\Opencv_Test\\beauty.jpg", -1);if (!image.data)cout << "Please check the fold exit or not!" << endl;Mat hsvImage;//转换为HSV颜色模型cvtColor(image, hsvImage, COLOR_BGR2HSV);//将色调量化为30个等级int hueBinNum = 30;//将饱和度量化为32个等级int saturationBinNum = 32;int histSize[] = { hueBinNum,saturationBinNum };//色调变化范围为0-179float hueRanges[] = { 0,180 };//饱和度变化范围为0-255float saturationRanges[] = { 0,256 };const float*ranges[] = { hueRanges,saturationRanges };MatND dstHist;//计算第0和第1通道的直方图int channels[] = { 0,1 };//进行直方图的计算calcHist(&hsvImage, 1, channels, Mat(), dstHist, 2, histSize, ranges, true, false);double maxValue = 0;//查找数组和子数组的全局最小值和最大值存入maxValue中minMaxLoc(dstHist, 0, &maxValue, 0, 0);int scale = 10;Mat histImage = Mat::zeros(saturationBinNum*scale, hueBinNum * 10, CV_8UC3);//双重循环进行绘制for (int hue = 0;hue < hueBinNum;hue++) {for (int saturation = 0;saturation < saturationBinNum;saturation++) {float binValue = dstHist.at<float>(hue, saturation);int intensity = cvRound(binValue * 255 / maxValue);rectangle(histImage, Point(hue*scale, saturation*scale), Point((hue + 1)*scale - 1, (saturation + 1)*scale - 1), Scalar::all(intensity), FILLED);}}imshow("原图", image);imshow("H_S直方图", histImage);while((char)waitKey(0)!='q'){}return 0;}
绘制一维直方图示例:
#include"stdafx.h"#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/core/core.hpp>#include <opencv2/objdetect/objdetect.hpp>#include<opencv2/photo/photo.hpp>#include <string>#include <vector>#include <iostream>using namespace cv;using namespace std; int main(){Mat image = imread("E:\\pictures\\For_Project\\Opencv_Test\\beauty.jpg", 0);if (!image.data)cout << "Please check the fold exit or not!" << endl;imshow("原图", image);MatND dstHist;int dims = 1;float hranges[] = { 0,255 };const float*ranges[] = { hranges };int size = 256;int channels = 0;calcHist(&image, 1, &channels, Mat(), dstHist, dims, &size, ranges);int scale = 1;Mat dstImage(size*scale, size, CV_8U, Scalar(0));double minValue = 0;double maxValue = 0;minMaxLoc(dstHist, &minValue, &maxValue, 0, 0);int hpt = saturate_cast<int>(0.9*size);for (int i = 0;i < 256;i++) {float binValue = dstHist.at<float>(i);int realValue = saturate_cast<int>(binValue*hpt / maxValue);rectangle(dstImage, Point(i*scale, size - 1), Point((i + 1)*scale - 1, size - realValue), Scalar(255));}imshow("一维直方图", dstImage);while((char)waitKey(0)!='q'){}return 0;}
绘制真彩色RGB图像直方图示例:
#include"stdafx.h"#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/core/core.hpp>#include <opencv2/objdetect/objdetect.hpp>#include<opencv2/photo/photo.hpp>#include <string>#include <vector>#include <iostream>using namespace cv;using namespace std; int main(){Mat image = imread("E:\\pictures\\For_Project\\Opencv_Test\\tree.jpg", 1);if (!image.data)cout << "Please check the fold exit or not!" << endl;imshow("原图", image);//参数准备int bins = 256;int hist_size[] = { bins };float range[] = { 0,256 };const float*ranges[] = { range };MatND redHist, grayHist, blueHist;//计算红色分量直方图int channels_r[] = { 0 };calcHist(&image, 1, channels_r, Mat(), redHist, 1, hist_size, ranges, true, false);//计算绿色分量直方图int channels_g[] = { 1 };calcHist(&image, 1, channels_g, Mat(), grayHist, 1, hist_size, ranges, true, false);//计算蓝色分量直方图int channels_b[] = { 2 };calcHist(&image, 1, channels_b, Mat(), blueHist, 1, hist_size, ranges, true, false);//参数准备double maxValue_red, maxValue_green, maxValue_blue;minMaxLoc(redHist, 0, &maxValue_red, 0, 0);minMaxLoc(grayHist, 0, &maxValue_green, 0, 0);minMaxLoc(blueHist, 0, &maxValue_blue, 0, 0);int scale = 1;int histHeight = 256;Mat histImage = Mat::zeros(histHeight, bins * 3, CV_8UC3);//绘制for (int i = 0;i < bins;i++) {float binValue_red = redHist.at<float>(i);float binValue_green = grayHist.at<float>(i);float binValue_blue = blueHist.at<float>(i);//分别得到要绘制的RGB三分量的高度int intensity_red = cvRound(binValue_red*histHeight / maxValue_red);int intensity_green = cvRound(binValue_green*histHeight / maxValue_green);int intensity_blue = cvRound(binValue_blue*histHeight / maxValue_blue);//分别绘制三分量的直方图rectangle(histImage, Point(i*scale, histHeight - 1), Point((i + 1)*scale - 1,histHeight - intensity_red), Scalar(0, 0, 255));rectangle(histImage, Point((i+bins)*scale, histHeight - 1), Point((i + bins + 1)*scale - 1,histHeight - intensity_green), Scalar(0, 255, 0));rectangle(histImage, Point((i+2*bins)*scale, histHeight - 1), Point((i + 2*bins + 1)*scale - 1,histHeight - intensity_blue), Scalar(255, 0, 0));}imshow("RGB直方图", histImage);while((char)waitKey(0)!='q'){}return 0;}
0 0
- opencv学习之直方图绘制
- opencv 学习之 直方图
- opencv学习之(五)-直方图计算和绘制图像直方图
- opencv学习之(五)-直方图计算和绘制图像直方图
- opencv之直方图计算及绘制
- opencv学习笔记9 直方图均衡化并绘制直方图
- OpenCV--绘制直方图&直方图均衡
- opencv 学习之直方图统计
- Opencv学习之图像直方图
- opencv绘制灰度直方图
- opencv绘制灰度直方图
- 【OpenCV】绘制直方图
- opencv:绘制直方图
- opencv 绘制图像直方图
- 【OpenCV】绘制直方图
- opencv 绘制灰度直方图
- opencv 图像直方图绘制
- MATLAB学习笔记之-----绘制直方图
- struts2值栈分析
- (斯坦福机器学习笔记)支持向量机
- 【video】流媒体协议介绍(rtp/rtcp/rtsp/rtmp/mms/hls)
- 咖啡知识大全
- AsyncTask异步任务
- opencv学习之直方图绘制
- nginx监听相同端口,根据域名请求不同的server
- 微信小程序上手篇(1)
- 机器学习笔记(四)决策树
- Unity动态字体文字破碎的解决方法(Dynamic Font Broken)
- 蓝桥杯B组C语言-三羊献瑞
- PAT B1004. 成绩排名 (20)
- nodeJs链接Mysql,做增删改查
- 技术人员应对「考核」的一些思考