初识Opencv4.X----图像直方图绘制

//图像直方图绘制
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
    
    
	Mat img = imread("person1.jpeg", IMREAD_GRAYSCALE);
	//统计0~255各个灰度值的像素个数
	const int channels[1] = {
    
     0 };
	Mat hist;
	const int histSize[1] = {
    
     256 };
	float a[2] = {
    
     0,255 };
	const float * ranges[1] = {
    
     a };
	calcHist(&img, 1, channels, Mat(), hist, 1, histSize, ranges);//得到的hist是一个单通道,width=1,height=256的Mat变量
	//绘制直方图,通过矩形的对角点来绘制实心矩形
	//每个矩形的宽定为2,则图像的width应为2*256=512,图像的height由像素的个数最大值max_value决定
	//i取值0,2,4...510,每次画矩形时,左下角坐标为(max_value,i),右上角坐标为(max_value-hist.at(i/2),i+2)
	double min_value, max_value;
	minMaxLoc(hist, &min_value, &max_value);
	max_value = max_value / 100;//通过设置断点可知最大值为3000+,缩小100倍,防止图片过大
	Mat img_zeros=Mat::zeros((int)max_value,512,CV_8UC1);
	for (int i = 0; i < 512; i = i + 2)
	{
    
    
		rectangle(img_zeros, Point(i, max_value), Point(i + 2, (int)(max_value - (hist.at<float>(i / 2)) / 100)), Scalar(255), -1);//千万要注意hist中的元素是float类型
	}
	namedWindow("hist", WINDOW_NORMAL);
	imshow("hist", img_zeros);
	waitKey(0);
	return 0;
}

在这里插入图片描述

//图像归一化直方图绘制
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
    
    
	Mat img = imread("person1.jpeg", IMREAD_GRAYSCALE);
	//统计0~255各个灰度值的像素个数
	const int channels[1] = {
    
     0 };
	Mat hist;
	const int histSize[1] = {
    
     256 };
	float a[2] = {
    
     0,255 };
	const float * ranges[1] = {
    
     a };
	
	calcHist(&img, 1, channels, Mat(), hist, 1, histSize, ranges);//得到的hist是一个单通道,width=1,height=256的Mat变量
	//归一化hist
	normalize(hist, hist, 1, 0, NORM_L1, -1);//NORM_L1方法为某个像素对应的数目除以最大值
	//绘制直方图,通过矩形的对角点来绘制实心矩形
	//每个矩形的宽定为2,则图像的width应为2*256=512,图像的height由像素的个数最大值max_value决定
	//i取值0,2,4...510,每次画矩形时,左下角坐标为(max_value,i),右上角坐标为(max_value-hist.at(i/2),i+2)
	double min_value, max_value;
	minMaxLoc(hist, &min_value, &max_value);
	max_value = max_value * 1000;//通过设置断点可知最大值为0.038+,扩大1000倍,防止图片过小
	Mat img_zeros=Mat::zeros((int)max_value,512,CV_8UC1);
	for (int i = 0; i < 512; i = i + 2)
	{
    
    
		rectangle(img_zeros, Point(i, max_value), Point(i + 2, (int)(max_value - (hist.at<float>(i / 2)) * 1000)), Scalar(255), -1);//千万要注意hist中的元素是float类型
	}
	namedWindow("hist", WINDOW_NORMAL);
	imshow("hist", img_zeros);
	waitKey(0);
	return 0;
}

在这里插入图片描述

//图像直方图比较
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
    
    
	Mat img = imread("person1.jpeg", IMREAD_GRAYSCALE);
	//统计0~255各个灰度值的像素个数
	const int channels[1] = {
    
     0 };
	Mat hist, hist_nor;
	const int histSize[1] = {
    
     256 };
	float a[2] = {
    
     0,255 };
	const float * ranges[1] = {
    
     a };
	//直接统计个数的直方图
	calcHist(&img, 1, channels, Mat(), hist, 1, histSize, ranges);//得到的hist是一个单通道,width=1,height=256的Mat变量
	//归一化的直方图
	normalize(hist, hist_nor, 1, 0, NORM_L1, -1);//NORM_L1方法为某个像素对应的数目除以最大值
	double result;
	result = compareHist(hist, hist_nor, 0);//最后一个参数代表比较的方法,不同方法的结果是不一样的,有些靠近1代表越相似,有些则相反
	cout << "两幅图像的相似度为:" << result << endl;
	waitKey(0);
	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_46146657/article/details/120275725