当前位置:网站首页>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;
}

边栏推荐
猜你喜欢
随机推荐
@2-1 safety index predicted by CCF at the end of December 1, 2020
OC--Foundation--数组
Prim minimum spanning tree (diagram)
How many regions can a positive odd polygon be divided into
Flutter Rive 多状态例子
Customize dialog to realize the pop-up box of privacy clause statement imitating Netease cloud music
cf #785(div2) C. Palindrome Basis
Dream set sail (the first blog)
[code source] daily question farmland Division
CUDA 解释 - 深度学习为何使用 GPU
How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)
Learn redis Linux and install redis
【Android studio】批量数据导入到android 本地数据库
OC--类别 扩展 协议与委托
如何安装pytorch?—— 一种最简单有效的方法!
Operation 7.19 sequence table
How to import a large amount of data in MATLAB
In depth interpretation of C language random number function and how to realize random number
如何将其他PHP版本添加到MAMP
OC -- Foundation -- dictionary


![[gplt] 2022 popular lover (Floyd)](/img/30/c96306ca0a93f22598cec80edabd6b.png)






