# 线性插值 c代码.....

xiaoxiao2021-03-25  31

int clamp_s(int value, int m_min, int m_max) { int mm_ret = value; if (value < m_min) { mm_ret = m_min; } else if (value > m_max) { mm_ret = m_max; } return mm_ret; } // 上面函数可以用一个宏代替 #define CLAMP(a, s, m) ((a) < (s)? (s) : ((a) > (m) ? (m) : (a))) // 参数说明依次是，原始图数据，目的图数据，原图宽高， 目的宽高，通道数 int LinearImage(unsigned char * srcImage, unsigned char * destImage, int srcW, int srcH, int destW, int destH, int channels) { float timeX = 0.0f; float timeY = 0.0f; timeX = srcW * 1.0f / destW; timeY = srcH * 1.0f / destH; int i = 0; int j = 0; int k = 0; for (i = 0; i < destH; i++) { for (j = 0; j < destW; j++) { int srcIdX = 0; int srcIdY = 0; float srcfIdX = 0.0f; float srcfIdY = 0.0f; float weightX[2] = { 0.0f }; float weightY[2] = { 0.0f }; srcfIdX = j * timeX; srcfIdY = i * timeY; srcIdX = (int)(srcfIdX); srcIdY = (int)(srcfIdY); weightX[1] = srcfIdX - srcIdX; weightX[0] = 1.0f - weightX[1]; weightY[1] = srcfIdY - srcIdY; weightY[0] = 1.0f - weightY[1]; for (k = 0; k < channels; k++) { destImage[(i * destW + j) * channels + k] = (srcImage[(srcIdY * srcW + srcIdX) * channels + k] * weightX[0] + srcImage[(srcIdY * srcW + clamp_s(srcIdX + 1, 0, srcW - 1)) * channels + k] * weightX[1]) * weightY[0] + (srcImage[(clamp_s(srcIdY + 1, 0, srcH - 1) * srcW + srcIdX) * channels + k] * weightX[0] + srcImage[(clamp_s(srcIdY + 1, 0, srcH - 1) * srcW + clamp_s(srcIdX + 1, 0, srcW - 1)) * channels + k] * weightX[1]) * weightY[1]; } } } return 0; } // 抠图后直接插值 int cutImageAndLinear(unsigned char * src, unsigned char * desData, int srcW, int srcH, int linearW, int linearH, int x0, int y0, int x1, int y1, int channels) { float timeX = 0.0f; float timeY = 0.0f; int destW = x1 - x0 + 1; int destH = y1 - y0 + 1; timeX = destW * 1.0f / linearW; timeY = destH * 1.0f / linearH; int i = 0; int j = 0; int k = 0; for (i = 0; i < linearH; i++) { for (j = 0; j < linearW; j++) { int srcIdX = 0; int srcIdY = 0; float srcfIdX = 0.0f; float srcfIdY = 0.0f; float weightX[2] = { 0.0f }; float weightY[2] = { 0.0f }; srcfIdX = j * timeX + x0; srcfIdY = i * timeY + y0; srcIdX = (int)(srcfIdX); srcIdY = (int)(srcfIdY); weightX[1] = srcfIdX - srcIdX; weightX[0] = 1.0f - weightX[1]; weightY[1] = srcfIdY - srcIdY; weightY[0] = 1.0f - weightY[1]; for (k = 0; k < channels; k++) { float temp = (src[(srcIdY * srcW + srcIdX) * channels + k] * weightX[0] + src[(srcIdY * srcW + CLAMP(srcIdX + 1, 0, srcW - 1)) * channels + k] * weightX[1]) * weightY[0] + (src[(CLAMP(srcIdY + 1, 0, srcH - 1) * srcW + srcIdX) * channels + k] * weightX[0] + src[(CLAMP(srcIdY + 1, 0, srcH - 1) * srcW + CLAMP(srcIdX + 1, 0, srcW - 1)) * channels + k] * weightX[1]) * weightY[1]; desData[(i * linearW + j) * channels + k] = (unsigned char)temp; } } } return 0; }

最新回复(0)