前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于opencv的图像校正_伽马校正怎么设置

基于opencv的图像校正_伽马校正怎么设置

作者头像
全栈程序员站长
发布2022-11-08 18:14:44
4870
发布2022-11-08 18:14:44
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
#include "stdafx.h"
#include <cmath>
#include <iostream>  
#include <opencv2\core\core.hpp>  
#include <opencv2\highgui\highgui.hpp>  
#include <opencv2\imgproc\imgproc.hpp>  
using namespace cv;
using namespace std;
float get_Gamma_Value(Mat& gray_img);
void create_Gamma_Table(unsigned char* gama_table, float gama_value);
void Gamma_Correction(Mat& gray_img, Mat& dst_img, unsigned char* gama_table);
int _tmain(int argc, _TCHAR* argv[])
{
char input_image_name[100];
char output_image_name[100];
int image_num = 476;
for (int i = 1; i <= image_num; i++)
{
sprintf(input_image_name, "../%s\\%d.jpg","temp", i);
Mat input_image = imread(input_image_name);
if (input_image.empty())
{
cout << "Failed to load image !" << endl;
continue;;
}
Mat gray_image;
cvtColor(input_image, gray_image, CV_BGR2GRAY);
// Start a timer
double duration;
duration = static_cast<double>(cv::getTickCount());
float gama_value = get_Gamma_Value(gray_image);
unsigned char LUT[256];
create_Gamma_Table(LUT, gama_value);
Mat result_image(gray_image.rows, gray_image.cols, gray_image.type());
Gamma_Correction(gray_image, result_image, LUT);
// Calculate the time cost and print
duration = static_cast<double>(cv::getTickCount()) - duration;
duration /= cv::getTickFrequency();
std::cout << duration * 1000 << " ms" << std::endl;
imshow("Source_Image", input_image);
imshow("Gamma_Correction", result_image);
//imwrite("test6.bmp",result_image);
waitKey(1);
}
return 0;
}
/****************************************************
①当Gamma值比1大时,在输入值相同的情况下,输出值减小;
②当Gamma值为1时,输出值不变;
③当Gamma值比1小时,在输入值相同的情况下,输出值增加。
****************************************************/
//公式:gamma = log(y/range)/ log(x/range),x是整幅图像像素的平均值,y是像素值最大范围的一半。
//先计算灰度图像的像素均值mean,将计算出来的均值带入 gammaVal = log(mean/255)/log(0.5) 这个公式中,就可以得到Gamma值了。
float get_Gamma_Value(Mat& gray_img)
{
if (gray_img.empty())
{
return -1.0;
}
cv::Scalar meam_value = cv::mean(gray_img);
float val = meam_value.val[0];
//float gamma_val = (log10(val / 255.0)) / (log10(0.5));
float gamma_val = (log10(0.5)) / (log10(val / 255.0));
return gamma_val;
}
void create_Gamma_Table(unsigned char* gama_table, float gama_value)
{
for (int i = 0; i < 256; i++)
{
float f = (i + 0.5f) / 255.0;
f = (float)(pow(f, gama_value));
gama_table[i] = saturate_cast<uchar>(f * 255.0f - 0.5f);
}
}
void Gamma_Correction(Mat& gray_img, Mat& dst_img, unsigned char* gama_table)
{
if(gray_img.channels() != dst_img.channels() || gray_img.cols != dst_img.cols || gray_img.rows != dst_img.rows)
{
return;
}
for (int i = 0; i < gray_img.rows; i++)
{
for (int j = 0; j < gray_img.cols; j++)
{
dst_img.at<uchar>(i, j) = gama_table[(int)(gray_img.at<uchar>(i, j))];
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月25日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档