当前位置:网站首页>24. histogram calculation
24. histogram calculation
2022-06-26 01:36:00 【Nightmare satiety】
#include <opencv4/opencv2/opencv.hpp>
#include <iostream>
int main(int argc, char** argv){
cv::Mat src = cv::imread("/home/long2double/Desktop/OpenCV_Study/source/lena.png");
if(src.empty()){
std::cout << "could not load image ...." << std::endl;
return -1;
}
cv::namedWindow("test opencv setup");
cv::imshow("test opencv setup", src);
// Split channels
std::vector<cv::Mat> bgr_planes;
cv::split(src, bgr_planes);
// Calculate the histogram
int histSize = 256;
float range[] = {
0, 256};
const float *histRanges = {
range};
cv::Mat b_hist, g_hist, r_hist;
cv::calcHist(&bgr_planes[0], 1, 0, cv::Mat(), b_hist, 1, &histSize, &histRanges, true, false);
cv::calcHist(&bgr_planes[1], 1, 0, cv::Mat(), g_hist, 1, &histSize, &histRanges, true, false);
cv::calcHist(&bgr_planes[2], 1, 0, cv::Mat(), r_hist, 1, &histSize, &histRanges, true, false);
// normalization
int hist_h = 400;
int hist_w = 512;
int bin_w = hist_w / histSize;
cv::normalize(b_hist, b_hist, 0, hist_h, cv::NORM_MINMAX);
cv::normalize(g_hist, g_hist, 0, hist_h, cv::NORM_MINMAX);
cv::normalize(r_hist, r_hist, 0, hist_h, cv::NORM_MINMAX);
// Calculate the histogram
cv::Mat histImage(hist_w, hist_h, CV_8UC3, cv::Scalar(0, 0, 0));
for(int i = 1; i < histSize; i++){
cv::line(histImage, cv::Point((i - 1) * bin_w, hist_h - cvRound(b_hist.at<float>(i - 1))),
cv::Point(i * bin_w, hist_h - cvRound(b_hist.at<float>(i))), cv::Scalar(255, 0, 0), 2, cv::LINE_AA);
cv::line(histImage, cv::Point((i - 1) * bin_w, hist_h - cvRound(g_hist.at<float>(i - 1))),
cv::Point(i * bin_w, hist_h - cvRound(g_hist.at<float>(i))), cv::Scalar(0, 255, 0), 2, cv::LINE_AA);
cv::line(histImage, cv::Point((i - 1) * bin_w, hist_h - cvRound(r_hist.at<float>(i - 1))),
cv::Point(i * bin_w, hist_h - cvRound(r_hist.at<float>(i))), cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
}
cv::imshow("histImage", histImage);
cv::waitKey();
}

边栏推荐
- 2022年育婴员(五级)考试试题及答案
- Multiple interface calls, using promise all、Promise. Race and promise any
- 2022安徽省安全员C证考试练习题模拟考试平台操作
- 从查询数据库性能优化谈到redis缓存-谈一谈缓存的穿透、雪崩、击穿
- vite打包构建时 @charset utf-8警告问题处理;
- Technical introduction - detailed explanation of chip manufacturing process
- **MySQL example 1 (query by multiple conditions according to different problems)**
- 通过电脑获取WIFI密码(只能连接过的WiFi)
- [visual studio code] vscode shortcut keys
- 马斯克 VS 乔布斯,谁是21世纪最伟大的创业家
猜你喜欢

数组中的第K个最大元素

Reading notes on how to connect the network - hubs, routers and routers (III)

ETCD数据库源码分析——集群通信初始化

新库上线 | CnOpenData中国新房信息数据

Obtain WiFi password through computer (only connected WiFi)

Discrete Mathematics - 01 mathematical logic

生信周刊第34期

CityJSON

从查询数据库性能优化谈到redis缓存-谈一谈缓存的穿透、雪崩、击穿

Mpu6050 reads the ID incorrectly and 0xd1 occurs (the correct ID should be 0x68 or 0x69). Solution.
随机推荐
快速生成1~20自然数,并轻松复制
2021 - 1 - 15 notes de pêche Ctrl + C / V
Shengxin weekly issue 33
Quickly generate 1~20 natural numbers and easily copy
Development and monitoring of fusion experiment pulse power supply by LabVIEW
--都市修炼手册之SQL-- 第一章 基础复习
Embedded C second learning notes
Oracle database complete uninstallation steps (no screenshot)
物联网?快来看 Arduino 上云啦
RT thread project engineering construction and configuration - (Env kconfig)
Etcd database source code analysis cluster communication initialization
Technical foreword - metauniverse
Flex & Bison 开始
JSON实例(一)
新库上线 | CnOpenDataA股上市公司IPO申报发行文本数据
Freertos+stm32l+esp8266+mqtt protocol transmits temperature and humidity data to Tencent cloud IOT platform
MySQL book borrowing system project database creation TABLE statement (combined primary key and foreign key settings)
STM32 key development foundation
Simple making of master seal
Embedded C first learning notes