资源简介
局部均值滤波(local statistics neibor filter) 用于人脸美颜
代码片段和文件信息
#include
#include “math.h“
#define MIN2(a b) ((a) < (b) ? (a) : (b))
#define MAX2(a b) ((a) > (b) ? (a) : (b))
#define CLIP3(x a b) MIN2(MAX2(ax) b)
#include
#include
#include
#include
using namespace cv;
using namespace std;
void MeanCovMapCalculate(unsigned char* srcData int width int height float* meanData float* covData int radius)
{
int len = width * height;
int i j;
int sum = 0 count = 0 sum_v = 0 count_v = 0;
float gray = 0gray_v = 0;
unsigned char* tempData = (unsigned char*) malloc(sizeof(unsigned char) * height * width);
memcpy(tempData srcData sizeof(unsigned char) * height * width);
for(j = 0; j < height; j++ )
{
for(i = 0; i < width; i++)
{
len = i + j * width;
gray = tempData[len];
sum = 0;
count = 0;
for(int n = -radius; n <= radius; n++)
{
for(int m = -radius; m <= radius; m++)
{
int x = CLIP3(i + m 0 width - 1);
int y = CLIP3(j + n 0 height - 1);
int pos = x + y * width;
gray = tempData[pos];
sum += gray;
count++;
}
}
gray = sum / MAX2(count 1);
meanData[len] = CLIP3(gray 0 255);
sum_v = 0;
count_v = 0;
for(int n = -radius; n <= radius; n++)
{
for(int m = -radius; m <= radius; m++)
{
int x = CLIP3(i + m 0 width - 1);
int y = CLIP3(j + n 0 height - 1);
int pos = x + y * width;
gray_v = tempData[pos];
sum_v += (gray_v - meanData[len]) * (gray_v - meanData[len]);
count_v++;
}
}
gray_v = sum_v / MAX2(count_v 1);
covData[len] = gray_v;
}
}
free(tempData);
}
int LSNFilterOneChannel(unsigned char* srcData int width int height int radius int delta)
{
int len = sizeof(unsigned int) * width * height;
unsigned char* dstData = (unsigned char*)malloc(len);
float* meanData = (float*) malloc(len);
float* covData = (float*) malloc(len);
memset(meanData 0 len);
memset(covData 0 len);
MeanCovMapCalculate(srcData width height meanData covData radius);
float mean = 0 cov = 0 K = 0;
int i j;
int gray = 0;
for(j = 0; j < height; j++ )
{
for(i = 0; i < width; i++)
{
len = i + j * width;
mean = meanData[len]
- 上一篇:libstdc++.so.6.0.22
- 下一篇:树莓派实时传输图像到pc机
评论
共有 条评论