首页 > 其他分享 >Lanczos resampling

Lanczos resampling

时间:2023-01-09 20:56:12浏览次数:55  
标签:const int double h2 w2 resampling Lanczos out

Lanczos resampling - Detailed Pedia

Lanczos resampling is often used also for multivariate interpolation, for example to resize or rotate a digital image. It has been considered the "best compromise" among several simple filters for this purpose.

The filter is named after its inventor, Cornelius Lanczos (匈牙利人).

1.cpp:

#include "bmp24.h"
// https://github.com/avaneev/avir
#include "lancir.h" // 仅头文件即可,支持SIMD和NEON
int main() {
  BMP24 in; in.load("in.bmp");
  int w = in.width, h = in.height, w2 = 286, h2 = 201;
  BMP24 out; out.create(w2, h2);
  avir::CLancIR lir;
  /* void resizeImage( const Tin* const SrcBuf, const int SrcWidth,
    const int SrcHeight, const int SrcSSize, Tout* const NewBuf,
    const int NewWidth, const int NewHeight, const int NewSSize,
    const int ElCount); */
  lir.resizeImage(in.bits, w, h, in.stride, out.bits, w2, h2, out.stride, 3);
  out.save("lir.bmp");
  return 0;
}

 2.cpp有bug,我折腾不动了。

#include <math.h>
#include "bmp24.h"

const double pi = 3.1415926535897932, pipi = pi * pi, a = 2;
double L(double x) {
  if (fabs(x) < 1e-30) return 1.0;
  if (x >= -a && x < a) return a * sin(pi*x) * sin(pi*x/a) / (pipi * x*x);
  return 0.0;
}

int main() {
  BMP24 in; in.load("in.bmp");
  int w = in.width, h = in.height, w2 = 286, h2 = 201;
  BMP24 out; out.create(w2, h2);
  const double ratio = 3.14;
  uint8_t* row2 = out;
  for (int y2 = 0; y2 < h2; y2++) {
    uint8_t* p2 = row2;
    for (int x2 = 0; x2 < w2; x2++) {
      double yr = y2 / ratio, xr = x2 / ratio; // r(eal)
      int yf = int(yr), xf = int(xr); // f(loor)
      double b = 0, g = 0, r = 0;
      for (int y = yf - a + 1; y <= yf + a; y++) {
        for (int x = xf - a + 1; x <= xf + a; x++) {
            if (y < 8 || y >= h - 8 || x < 8 || x >= w - 8) {
              uint8_t* p = in + yf * in.stride + xf * 3;
              b = *p; g = p[1]; r = p[2];
              break;
            }
            uint8_t* p = in + y * in.stride + x * 3;
            const double t = L(xr - x) * L(yr - y);
            b += *p * t; g += p[1] * t; r += p[2] * t;
        }
        *p2 = int(b + 0.5); p2[1] = int(g + 0.5); p2[2] = int(r + 0.5);
      }
      p2 += 3;
    }
    row2 += out.stride;
  }
  out.save("simple.bmp");
  return 0;
}
View Code

The parameter a is a positive integer, typically 2 or 3, which determines the size of the kernel.

1:  2:  全部文件 25KB

标签:const,int,double,h2,w2,resampling,Lanczos,out
From: https://www.cnblogs.com/funwithwords/p/17038497.html

相关文章