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;
}
相关推荐
ctrey_11 小时前
2024-11-1 学习人工智能的Day20 openCV(2)
人工智能·opencv·学习
绕灵儿12 小时前
OpenCV通过指针裁剪图像
人工智能·opencv·计算机视觉
决战春招17 小时前
人工智能之人脸识别(人脸采集人脸识别)
人工智能·opencv·学习·计算机视觉
千秋1000019 小时前
OpenCV—calcHist()函数
人工智能·opencv·计算机视觉
爱写代码的小朋友19 小时前
使用 Python 和 OpenCV 实现实时人脸识别
开发语言·python·opencv
小负不负1 天前
使用kalibr_calibration标定相机(realsense)和imu(h7min)
数码相机·opencv·计算机视觉
凤枭香1 天前
python opencv灰度变换
图像处理·人工智能·python·opencv
maxruan1 天前
cv::Mat初始化、赋值初始化与访问方式
人工智能·opencv·计算机视觉
jndingxin1 天前
OpenCV视觉分析之目标跟踪(11)计算两个图像之间的最佳变换矩阵函数findTransformECC的使用
opencv·目标跟踪·矩阵
joker_man11 天前
使用Python和OpenCV实现火焰检测
开发语言·python·opencv