当前位置:网站首页>First knowledge of opencv4.x --- image histogram drawing
First knowledge of opencv4.x --- image histogram drawing
2022-07-25 09:47:00 【F l e】
// Image histogram rendering
#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);
// Statistics 0~255 The number of pixels of each gray value
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);// Got hist It's a single channel ,width=1,height=256 Of Mat Variable
// Draw histogram , Draw a solid rectangle through the diagonal points of the rectangle
// The width of each rectangle is 2, Of the image width Should be 2*256=512, Graphic height By the maximum number of pixels max_value decision
//i Value 0,2,4...510, Every time you draw a rectangle , The coordinates of the lower left corner are (max_value,i), The coordinates of the upper right corner are (max_value-hist.at(i/2),i+2)
double min_value, max_value;
minMaxLoc(hist, &min_value, &max_value);
max_value = max_value / 100;// By setting the breakpoint, the maximum value is 3000+, narrow 100 times , Prevent the picture from getting too big
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);// Be careful hist The element in is float type
}
namedWindow("hist", WINDOW_NORMAL);
imshow("hist", img_zeros);
waitKey(0);
return 0;
}

// Image normalized histogram rendering
#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);
// Statistics 0~255 The number of pixels of each gray value
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);// Got hist It's a single channel ,width=1,height=256 Of Mat Variable
// normalization hist
normalize(hist, hist, 1, 0, NORM_L1, -1);//NORM_L1 Method divides the corresponding number of a pixel by the maximum
// Draw histogram , Draw a solid rectangle through the diagonal points of the rectangle
// The width of each rectangle is 2, Of the image width Should be 2*256=512, Graphic height By the maximum number of pixels max_value decision
//i Value 0,2,4...510, Every time you draw a rectangle , The coordinates of the lower left corner are (max_value,i), The coordinates of the upper right corner are (max_value-hist.at(i/2),i+2)
double min_value, max_value;
minMaxLoc(hist, &min_value, &max_value);
max_value = max_value * 1000;// By setting the breakpoint, the maximum value is 0.038+, expand 1000 times , Prevent the picture from being too small
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);// Be careful hist The element in is float type
}
namedWindow("hist", WINDOW_NORMAL);
imshow("hist", img_zeros);
waitKey(0);
return 0;
}

// Image histogram comparison
#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);
// Statistics 0~255 The number of pixels of each gray value
const int channels[1] = {
0 };
Mat hist, hist_nor;
const int histSize[1] = {
256 };
float a[2] = {
0,255 };
const float * ranges[1] = {
a };
// Directly count the number of histograms
calcHist(&img, 1, channels, Mat(), hist, 1, histSize, ranges);// Got hist It's a single channel ,width=1,height=256 Of Mat Variable
// Normalized histogram
normalize(hist, hist_nor, 1, 0, NORM_L1, -1);//NORM_L1 Method divides the corresponding number of a pixel by the maximum
double result;
result = compareHist(hist, hist_nor, 0);// The last parameter represents the method of comparison , The results of different methods are different , A little close 1 Represents the more similar , On the contrary, some
cout << " The similarity of the two images is :" << result << endl;
waitKey(0);
return 0;
}

边栏推荐
猜你喜欢
随机推荐
[code source] add brackets to the daily question
Kotlin 实现文件下载
OC--Foundation--字典
Android & Kotlin : 困惑解答
OC--类别 扩展 协议与委托
自定义Dialog 实现 仿网易云音乐的隐私条款声明弹框
初识Opencv4.X----图像直方图均衡
Minkowskiengine installation
Class (2) and protocol
[code source] daily one question non decreasing 01 sequence
Swift简单实现待办事项
[code source] score split of one question per day
OC--Foundation--字符串+日期和时间
@1-1 CCF 2021-04-1 gray histogram
缺陷检测网络--混合监督(kolektor缺陷数据集复现)
[gplt] 2022 popular lover (Floyd)
文件--初识
Redis set structure command
如何将其他PHP版本添加到MAMP
UI prototype resources









