OpenCV直方图计算

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <vector>

using namespace cv;
using namespace std;

int main()
{
	cout << "直方图calcHist" << endl;

	Mat src = imread("left.png", IMREAD_GRAYSCALE);
	if (src.empty())
	{
		cout << "read image failed." << endl;
		return -1;
	}
	imshow("gray_img", src);
	
	Mat hist;
	int histSize = 256;
	calcHist(&src, 1, 0, Mat(), hist, 1, &histSize, 0);

	double maxVal = 0;
	double minVal = 0;
	minMaxLoc(hist, &minVal, &maxVal, 0, 0);
	int zoom = 2;
	Mat histImg(histSize * zoom, histSize * zoom, CV_8U, Scalar(255));
	int hpt = static_cast<int>(0.9 * histSize);
	for (int h = 0; h < histSize; h++)
	{
		float binVal = hist.at<float>(h);
		if (binVal > 0)
		{
			int intensity = static_cast<int>(binVal * hpt / maxVal);
			line(histImg, Point(h * zoom, histSize * zoom), Point(h * zoom, (histSize - intensity) * zoom), Scalar(0), zoom);
		}
	}
	imshow("hist_img", histImg);

	cout << "直方图均衡化equalizeHist" << endl;
	Mat result;
	equalizeHist(src, result);
	imshow("equalizeHist", result);

	cout << "直方图反向投影calcBackProject" << endl;
	Mat imageROI = src(Rect(400,20,150,80));
	Mat hist2;
	calcHist(&imageROI, 1, 0, Mat(), hist2, 1, &histSize, 0);
	normalize(hist2, hist2, 1.0);
	Mat backP;
	calcBackProject(&src, 1, 0, hist2, backP, 0, 255.0);

	Mat mask;
	threshold(backP, mask, 30, 255, THRESH_BINARY);
	imshow("backProject", mask);

	cout << "积分图像" << endl;
	Mat integralImg;
	integral(src, integralImg, CV_32S);

	waitKey();

	return 0;
}
相关推荐
游客5201 小时前
opencv中的各种滤波器简介
图像处理·人工智能·python·opencv·计算机视觉
小俊俊的博客1 小时前
海康RGBD相机使用C++和Opencv采集图像记录
c++·opencv·海康·rgbd相机
7yewh1 小时前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
Kai HVZ1 小时前
《OpenCV计算机视觉》--介绍及基础操作
人工智能·opencv·计算机视觉
biter00881 小时前
opencv(15) OpenCV背景减除器(Background Subtractors)学习
人工智能·opencv·学习
吃个糖糖1 小时前
35 Opencv 亚像素角点检测
人工智能·opencv·计算机视觉
菜狗woc15 小时前
opencv-python的简单练习
人工智能·python·opencv
西猫雷婶17 小时前
python学opencv|读取图像(十四)BGR图像和HSV图像通道拆分
开发语言·python·opencv
云空17 小时前
《QT 5.14.1 搭建 opencv 环境全攻略》
开发语言·qt·opencv
编码小哥17 小时前
opencv中的色彩空间
opencv·计算机视觉