当前位置:网站首页>Explain in detail why the number of pixels with a gray value of 255 calculated by the opencv histogram calculation function calchist() is 0
Explain in detail why the number of pixels with a gray value of 255 calculated by the opencv histogram calculation function calchist() is 0
2022-04-22 12:57:00 【Haohong algorithm】
In the use of OpenCV Histogram calculation function calcHist() when , Gray value found to be 255 The number of pixels is always 0.
Even if the gray value in the image is 255 The number of pixels of is not 0, Use OpenCV Histogram calculation function calcHist() The calculated result is also 0.
An example is as follows :
//OpenCV edition 3.0
// The author WeChat /QQ 2487872782
// If you have any questions, you can contact the author
// Welcome to the image processing exchange group , Group number 271891601
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main( )
{
// Read the source image and convert it into gray image
cv::Mat srcImage = cv::imread("F:/material/images/P0027-coin-01.png");
// Judge whether the file is read correctly
if( !srcImage.data )
return 1;
cv::Mat ImageGray;
cv::cvtColor(srcImage,ImageGray,CV_BGR2GRAY);
// Define histogram parameters
const int channels[1]={
0};
const int histSize[1]={
256};
float pranges[2]={
0,255};
const float* ranges[1]={
pranges};
cv::MatND hist;
cv::calcHist(&ImageGray,1,channels,cv::Mat(),hist,1,histSize,ranges);
std::cout<<hist<<std::endl;
// Find the maximum value of image pixels
double maxVal=0;
cv::minMaxLoc(ImageGray, 0, &maxVal, 0, 0);
std::cout<<" The maximum value of image pixels is :"<<maxVal<<std::endl;
return 0;
}
Pictures used in the program “P0027-coin-01.png” Download link :
link :https://pan.baidu.com/s/1k13r2DdhEXuXWlxV-IbxNA Extraction code :kwgo
The results of the program are shown in the following figure :

As can be seen from the screenshot above , function calcHist() The calculated gray value is 255 The number of pixels is 0, But from “ The maximum value of image pixels is :255” It can be seen that , The number is definitely not 0, At least greater than or equal to 1 Of .
How could this happen ? Look up the function calcHist() Official documents of , The prototype is as follows :
void cv::calcHist (const Mat * images,
int nimages,
const int * channels,
InputArray mask,
OutputArray hist,
int dims,
const int * histSize,
const float ** ranges,
bool uniform = true,
bool accumulate = false
)
Notice the 8 Parameters “ranges” Explanation :
“ranges Array of the dims arrays of the histogram bin boundaries in each dimension. When the histogram is uniform ( uniform =true), then for each dimension i it is enough to specify the lower (inclusive) boundary of the 0-th histogram bin and the upper (exclusive) boundary”
Notice the words I marked in red in the above paragraph . From this passage we can see , This ranges The determined gray statistical range interval is the left closed right open interval .
Why? OpenCV This function of does not put this ranges The determined gray statistical range interval is set to Left and right closed What about the interval ?
This starts with the drawing of histogram .
Let's start with a simple gray histogram .
Now let's count the gray histogram parameters drawn as follows :
Number of statistical intervals :2, In the code above histSize[1]={2};
Statistical range of gray value :0~4, In the code above pranges[2]={0,4};
In the case of such parameters , Our histogram puts 0~4 The gray range of is divided into two statistical intervals , Respectively 0~2 and 2~4, As shown in the figure below :

The key here is to understand the number of gray values in the two statistical intervals .
In the ① Within two intervals , The statistics shows that the gray value is 0 and 1 The number of pixels , Please note that , Excluding the right border 2 The number of pixels .
In the ② Within two intervals , The statistics shows that the gray value is 2 and 3 The number of pixels , Please note that , Excluding the right border 4 The number of pixels .
After understanding the contents marked in red above , You should understand , If my grayscale range is 0~4, So when I draw the histogram, I don't need the right boundary 4 Number of pixels , So I don't need to count the corresponding number of pixels .
Empathy , If my grayscale range is 0~255, So when I draw the histogram, I don't need the right boundary 255 Number of pixels , So I don't need to count the corresponding number of pixels . That's why functions calcHist() Calculated 255 The number of gray value pixels is 0 Why .
Sometimes we need to know that the gray value is 255 The number of pixels , Then what shall I do? ?
It's simple , Write the gray value as 0~266 That's it , In the code above
float pranges[2]={
0,255};
Written as
float pranges[2]={
0,256};
Let's change the above code to the following code :
//OpenCV edition 3.0
// The author WeChat /QQ 2487872782
// If you have any questions, you can contact the author
// Image processing development materials 、 Please add... For the exchange of image processing technology QQ Group , Group number 271891601
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main( )
{
// Read the source image and convert it into gray image
cv::Mat srcImage = cv::imread("F:/material/images/P0027-coin-01.png");
// Judge whether the file is read correctly
if( !srcImage.data )
return 1;
cv::Mat ImageGray;
cv::cvtColor(srcImage,ImageGray,CV_BGR2GRAY);
// Define histogram parameters
const int channels[1]={
0};
const int histSize[1]={
256};
float pranges[2]={
0,256};
const float* ranges[1]={
pranges};
cv::MatND hist;
cv::calcHist(&ImageGray,1,channels,cv::Mat(),hist,1,histSize,ranges);
std::cout<<hist<<std::endl;
// Find the maximum value of image pixels
double maxVal=0;
cv::minMaxLoc(ImageGray, 0, &maxVal, 0, 0);
std::cout<<" The maximum value of image pixels is :"<<maxVal<<std::endl;
return 0;
}

Here we are. , I believe you will understand “ Why? OpenCV Histogram calculation function calcHist() The calculated gray value is 255 The number of pixels is 0”. If you still don't understand , You can add the blogger's wechat /QQ 2487872782 communication .
版权声明
本文为[Haohong algorithm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221255140780.html
边栏推荐
- Leetcode 206, reverse linked list
- C#窗体的淡入淡出功能(项目工程源码)
- R语言绘制小提琴图geom_violin,如何添加额外的点geom_point?geom_violin + geom_boxplot + geom_point组合使用
- MATLAB中的数据从double类型强制转化为uint8其舍入用的是四舍五入,附MATLAB基本数据类型
- Leetcode 1768, alternate merge string
- let、const、var的区别
- 利用OpenCV的函数threshold()对图像作基于OTSU的阈值化处理---并附比较好的介绍OTSU原理的博文链接
- Digital business cloud: how can enterprises achieve local breakthroughs and run fast in small steps under the wave of digital procurement
- Use R language_ Xlsx function export and write dataframe data to excel file
- Leetcode 695. Maximum area of the island
猜你喜欢

书城项目注册页面和邮箱验证

Novartis Nebula updated its prospectus and continued its listing process. Is the "academic school" gaining momentum step by step?

3D application rotation album

Leetcode 617. Merge binary tree

Shopping form making

Simple deployment of microservices

IDE導入項目

Is this open source project going to kill me?

让cpolar获得固定TCP地址

Chinese characters and screenshots suitable for the test boundary are selected
随机推荐
396. Rotation function
Leetcode 118, Yanghui triangle
摆脱 AI 生产“小作坊”:如何基于 Kubernetes 构建云原生 AI 平台
R语言使用scale函数标准化缩放dataframe数据(设置scale参数、scale参数设置除以标准差)
Embedded development: three techniques for verifying sensors and communication data in embedded systems
Baidu map combines VR panorama, and panorama map mode makes you feel more at ease when traveling
Excel字符串拼接
数商云电子招投标系统解决方案丨规范政采流程,提高工作效率
R语言使用dhyper函数生成超几何分布密度数据、使用plot函数可视化超几何分布密度数据(Hypergeometric Distribution)
Detailed explanation of C language preprocessing
R language ggplot2 visualizes the custom sorting of x-axis factor variables instead of sorting in the default alphabetical order
R语言使用rnbinom函数生成符合负二项分布的随机数、使用plot函数可视化符合负二项分布的随机数(Negative binomial distribution)
R语言使用qnorm函数生成正太分布分位数函数数据、使用plot函数可视化正太分布分位数函数数据(Normal distribution)
JS foundation 10
Give yourself the possibility of success
JS foundation 14
JS foundation 5
Practice of importing SAS files with R language
NoSQL调查 PART3:开放源码的失败
Leetcode 34. Find the first and last positions of elements in the sorted array