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;
}
相关推荐
Dev7z26 分钟前
基于OpenCV和MATLAB的椭圆检测系统的设计与实现
人工智能·opencv·matlab
雍凉明月夜8 小时前
视觉opencv学习笔记Ⅲ
笔记·opencv·学习
深蓝海拓2 天前
opencv的模板匹配(Template Matching)学习笔记
人工智能·opencv·计算机视觉
滨HI02 天前
C++ opencv简化轮廓
开发语言·c++·opencv
技术支持者python,php4 天前
训练模型,物体识别(opencv)
人工智能·opencv·计算机视觉
深蓝海拓4 天前
OpenCV学习笔记之:调整ORB算法的参数以适应不同的图像
笔记·opencv·学习
Mrliu__4 天前
Opencv(十六) : 图像边缘检测
人工智能·opencv·计算机视觉
柳鲲鹏5 天前
OpenCV:文件视频防抖,python版
python·opencv·音视频
千里飞刀客5 天前
aruco位姿检测
人工智能·opencv·计算机视觉
weixin_457760006 天前
OpenCV 图像处理基础算法详解(一)
图像处理·opencv·算法