C语言实现积分图实现ace算法
时间: 2023-09-22 07:06:42 浏览: 107
图算法C语言实现
以下是使用C语言实现积分图实现ACE算法的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
using namespace cv;
void integral_image(Mat &img, Mat &integral)
{
int h = img.rows;
int w = img.cols;
integral.create(h+1, w+1, CV_32S);
integral.setTo(0);
for(int i=1; i<=h; i++)
{
for(int j=1; j<=w; j++)
{
integral.at<int>(i, j) = img.at<uchar>(i-1, j-1) + integral.at<int>(i-1, j) + integral.at<int>(i, j-1) - integral.at<int>(i-1, j-1);
}
}
}
void local_mean_and_variance(Mat &integral, int w_size, Mat &local_mean, Mat &local_variance)
{
int h = integral.rows - 1;
int w = integral.cols - 1;
int half_w_size = w_size / 2;
local_mean.create(h, w, CV_32F);
local_variance.create(h, w, CV_32F);
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
int top = i - half_w_size;
int left = j - half_w_size;
int bottom = i + half_w_size;
int right = j + half_w_size;
top = top < 0 ? 0 : top;
left = left < 0 ? 0 : left;
bottom = bottom >= h ? h-1 : bottom;
right = right >= w ? w-1 : right;
int area = (bottom - top + 1) * (right - left + 1);
int sum = integral.at<int>(bottom+1, right+1) + integral.at<int>(top, left) - integral.at<int>(bottom+1, left) - integral.at<int>(top, right+1);
float mean = (float)sum / area;
local_mean.at<float>(i, j) = mean;
int sum2 = integral.at<int>(bottom+1, right+1) + integral.at<int>(top, left) - integral.at<int>(bottom+1, left) - integral.at<int>(top, right+1);
float variance = (float)sum2 / area - mean * mean;
local_variance.at<float>(i, j) = variance;
}
}
}
void contrast_score(Mat &local_mean, Mat &local_variance, float k, float alpha, Mat &contrast)
{
contrast.create(local_mean.rows, local_mean.cols, CV_32F);
for(int i=0; i<local_mean.rows; i++)
{
for(int j=0; j<local_mean.cols; j++)
{
float mean = local_mean.at<float>(i, j);
float variance = local_variance.at<float>(i, j);
float score = k * fabs(mean) / sqrt(variance + alpha * alpha);
contrast.at<float>(i, j) = score;
}
}
}
void contrast_enhancement(Mat &img, Mat &contrast, Mat &img_enhanced)
{
img.convertTo(img_enhanced, CV_32F);
float *pImg = img_enhanced.ptr<float>(0);
float *pContrast = contrast.ptr<float>(0);
int size = img_enhanced.rows * img_enhanced.cols;
for(int i=0; i<size; i++)
{
pImg[i] = pImg[i] + pContrast[i] * (pImg[i] - mean(pImg, size));
pImg[i] = pImg[i] < 0 ? 0 : pImg[i];
pImg[i] = pImg[i] > 255 ? 255 : pImg[i];
}
img_enhanced.convertTo(img_enhanced, CV_8U);
}
int main(int argc, char **argv)
{
if(argc < 2)
{
printf("Usage: %s image_path\n", argv[0]);
return -1;
}
Mat img = imread(argv[1], IMREAD_GRAYSCALE);
if(img.empty())
{
printf("Failed to load image: %s\n", argv[1]);
return -1;
}
Mat integral, local_mean, local_variance, contrast, img_enhanced;
integral_image(img, integral);
local_mean_and_variance(integral, 21, local_mean, local_variance);
contrast_score(local_mean, local_variance, 0.4, 1.0, contrast);
contrast_enhancement(img, contrast, img_enhanced);
imshow("original", img);
imshow("enhanced", img_enhanced);
waitKey(0);
destroyAllWindows();
return 0;
}
```
其中,img_path 是待处理的图像路径,w_size 是局部均值和局部方差计算时的窗口大小,k 和 alpha 是对比度得分计算时的参数。
阅读全文