opencv 调节图像对比度及亮度的人脸识别

opencv 调节图像对比度及亮度的人脸识别,第1张

opencv 调节图像对比度及亮度的人脸识别
// cascade.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include 
#include 
#include 

using namespace cv;
using namespace std;

void creatTable(vector &table, int alpha, int beta)
{
	for (int i = 0; i < 255; i++)
	{
		table.push_back(alpha * i + beta);
		if (table[i] > 255)
		{
			table[i] = 255;
		}
		if(table[i] < 0)
		{
			table[i] = 0;
		}
	}
}

void BrightnessAdjustment(Mat& src, Mat& dst, vector table)
{
	for (int i = 0; i < src.rows; i++)
	{
		uchar* data1 = src.ptr(i);
		uchar* data2 = dst.ptr(i);
		for (int j = 0; j < src.cols; j++)
		{
			data2[j] = table[data1[j]];
		}
	}
}

int main(int argc, char** argv) {
	
	String fileName = "D:\temp\cascade\model\haarcascade_frontalface_alt.xml";//设置文件路径
	CascadeClassifier face_classifier;//创建分类器

	if (!face_classifier.load(fileName))
	{//加载分类数据
		printf("could not load face feature data...n");
		return -1;
	}
	//string imagePath = "D:\temp\cascade\test";
	string imagePath = "D:\temp\cascade\image\train";
	vector vecImage;
	glob(imagePath, vecImage);
	for (int i = 0; i < vecImage.size(); i++)
	{
		Mat src = imread(vecImage[i]);
		if (src.empty())
		{
			printf("could not load image...n");
			return -1;
		}
		//imshow("input image", src);
		Mat gray;
		cvtColor(src, gray, COLOR_BGR2GRAY);//转成灰度图
		equalizeHist(gray, gray);//直方图均衡化,提高对比度

		vector table;
		creatTable(table, 1.5, 10);
		BrightnessAdjustment(gray, gray, table);
		imshow("input image", gray);
		waitKey(0);
		vector faces;
		face_classifier.detectMultiScale(gray, faces, 1.2, 3, 0, Size(24, 24));//在多尺度上检测
		for (size_t t = 0; t < faces.size(); t++)
		{
			rectangle(src, faces[static_cast(t)], Scalar(0, 0, 255), 2, 8, 0);
		}

		imshow("detect faces", src);
		waitKey(0);
	}
	
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5713957.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存