当前位置:网站首页>初识Opencv4.X----图像直方图绘制
初识Opencv4.X----图像直方图绘制
2022-07-25 09:22:00 【F l e】
//图像直方图绘制
#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;
}

边栏推荐
猜你喜欢

微信小程序初步了解及实现底部导航栏

Voice chat app source code - produced by NASS network source code

@1-1 CCF 2021-04-1 gray histogram
![[gplt] 2022 popular lover (Floyd)](/img/30/c96306ca0a93f22598cec80edabd6b.png)
[gplt] 2022 popular lover (Floyd)
![[code source] daily question - queue](/img/79/79570529445e7fbb86fb63af8dcf50.jpg)
[code source] daily question - queue

Singleton mode

*6-1 CCF 2015-03-2 numerical sorting

Assignment 7.21 Joseph Ring problem and decimal conversion

*6-2 CCF 2015-03-3 Festival

正奇边形可划分成多少区域
随机推荐
初识Opencv4.X----均值滤波
*6-2 CCF 2015-03-3 Festival
Install MySQL in Ubuntu and create new users
How to configure SSH after changing the computer
Browser access to swagger failed with error err_ UNSAFE_ PORT
OC--Foundation--字符串+日期和时间
Redis string structure command
OC -- Foundation -- string + date and time
最短路问题 Bellman-Ford(单源最短路径)(图解)
STM32+HC05串口蓝牙设计简易的蓝牙音箱
cell的定义
OC--Foundation--集合
Voice chat app source code - produced by NASS network source code
@2-1 safety index predicted by CCF at the end of December 1, 2020
[code source] add brackets to the daily question
How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)
[GPLT] 2022 大众情人(floyd)
【代码源】每日一题 非递减01序列
[code source] daily question farmland Division
单例模式(Singleton)