float类型图片降采样差值方法

时间:2021-10-25 20:31:48

1,本文代码主要是对float类型的图片进行的降采样的差值方法
2, 对外的函数接口只有一个
对单通道float 类型的数据进行的降采样差值 (在opencv 中是Mat 类型是CV_32FC1)
src 输入图片数据指针 (如果是Mat类型,实参为(float)Mat.data)
des 输出图片的数据指针(如果是Mat类型,实参为(float)Mat.data)
srcW 输入图片的宽
srcW 输出图片宽
srcH 输入图片的高
int ImageResize(float * src, float * des, int srcW, int srcH, int desW, int desH);

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
//纵向差值
/* src 输入图片数据指针
des 输出图片的数据指针
srcW 输入图片的宽
srcH 输入图片的高
desH 输出图片的高 */

int BilinearInterpolationCol(float * src, float * des, int srcW, int srcH, int desH)
{
float scale = (float)desH / (float)srcH;
float invscale = (float)srcH / (float)desH;
int i, j, z;
float fsy1;
float fsy2;
int sy1;
int sy2;
int k = 0;
int m = 0;
int skip = (int)(ceil(invscale) + 1.0f);
float * weight = (float *)malloc(skip * sizeof(float));
int * srcIndex = (int *)malloc(skip * sizeof(int));

for(i = 0; i < desH; i ++)
{
memset(weight, 0, sizeof(float) * skip);
memset(srcIndex, 0, sizeof(int) * skip);
k = 0;
m = 0;
fsy1 = (float) i * invscale;
fsy2 = fsy1 + invscale;

sy1 = (int)ceil(fsy1);

sy2 = (int)floor(fsy2);

if((float)sy1 - fsy1 > 0.001f)
{
srcIndex[k] = (sy1 - 1);
weight[k] = ((float)sy1 - fsy1) * scale;
k ++;
}
for(m = sy1; m < sy2; m ++)
{
srcIndex[k] = m;
weight[k] = scale;
k ++;
}
if(fsy2 - (float)(sy2) > 0.001f)
{
srcIndex[k] = sy2;
weight[k] = (fsy2 - (float)(sy2)) * scale;
k ++;
}
for(j = 0; j < srcW; j ++)
{
des[(i * srcW + j)] = 0.0f;
for(z = 0; z < skip; z ++)
{
srcIndex[z] = min(srcIndex[z], (srcH - 1));
des[(i * srcW + j)] += (src[(srcIndex[z] * srcW + j)]) * weight[z];
}
}
}
free(weight);
free(srcIndex);
return 0;

}
/* 横向插值
src 输入图片数据指针
des 输出图片的数据指针
srcW 输入图片的宽
srcH 输入图片的高
desW 输出图片的宽 */

int BilinearInterpolationRow(float * src, float * des, int srcW, int srcH, int desW)
{
float scale = (float)desW / (float)srcW;
float invscale = (float)srcW / (float)desW;
int i, j, z;
float fsx1;
float fsx2;
int sx1;
int sx2;
int k = 0;
int m = 0;
int skip = (int)(ceil(invscale) + 1.0f);
float * weight = (float *)malloc(skip * sizeof(float));
int * srcIndex = (int *)malloc(skip * sizeof(int));


for(j = 0; j < desW; j ++)
{

memset(weight, 0, sizeof(float) * skip);
memset(srcIndex, 0, sizeof(int) * skip);

k = 0;
m = 0;
fsx1 = (float) j * invscale;
fsx2 = fsx1 + invscale;

sx1 = (int)ceil(fsx1);
sx2 = (int)floor(fsx2);

if((float)sx1 - fsx1 > 0.001f)
{
srcIndex[k] = (sx1 - 1);
weight[k] = ((float)sx1 - fsx1) * scale;
k ++;
}
for(m = sx1; m < sx2; m ++)
{
srcIndex[k] = m;
weight[k] = scale;
k ++;
}
if(fsx2 - (float)(sx2) > 0.001f)
{
srcIndex[k] = sx2;
weight[k] = (fsx2 - (float)(sx2)) * scale;
k ++;
}

for(i = 0; i < srcH; i ++)
{
des[(i * desW + j)] = 0.0f;

for(z = 0; z < skip; z ++)
{
srcIndex[z] = min(srcIndex[z], (srcW -1));

des[(i * desW + j)] += (src[(i * srcW + srcIndex[z])]) * weight[z];
}

}
}
free(weight);
free(srcIndex);
return 0;

}
/* 对外提供的接口
对单通道float 类型的数据进行的降采样差值 (在opencv 中是Mat 类型是CV_32FC1)
src 输入图片数据指针 (如果是Mat类型,实参为(float)Mat.data)
des 输出图片的数据指针(如果是Mat类型,实参为(float)Mat.data)
srcW 输入图片的宽
srcW 输出图片宽
srcH 输入图片的高

*/

int ImageResize(float * src, float * des, int srcW, int srcH, int desW, int desH)
{
// 纵向差值
float * temp = (float *)malloc(srcW * desH * sizeof(float) * 4);
BilinearInterpolationCol(src, temp, srcW, srcH, desH);
//横向差值
BilinearInterpolationRow(temp, des, srcW, desH, desW);
free(temp);
return 0;
}