微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用拉格朗日插值对图像进行上采样

如何解决如何使用拉格朗日插值对图像进行上采样

我有一项作业,需要使用一些插值方法对图像进行上采样。我必须自己编写插值方法代码,我认为我不能使用任何特殊的函数来为我神奇地做到这一点。所以首先这里是我的主要内容

I = imread('image1.png');
%figure;
%imshow(I);

R = I(:,:,1);
G = I(:,2);
B = I(:,3);

[w,h,d] = size(I)

scalex = 2;
scaley = 2;

lagrange = @lagrange_interpol

resized_w = scalex*w
resized_h = scaley*h

R_new = zeros([size(R,1)*scalex size(R,2)*scaley]);
R_new(1:scalex:end,1:scaley:end) = R;

% Code for interpolation

G_new = zeros([size(G,1)*scalex size(G,2)*scaley]);
G_new(1:scalex:end,1:scaley:end) = G;

% Code for interpolation

B_new = zeros([size(B,1)*scalex size(B,2)*scaley]);
B_new(1:scalex:end,1:scaley:end) = B;

% Code for interpolation

I = cat(3,R_new,G_new,B_new); % Re-combine the 2D matrices to get an image

figure,imagesc(I);

这是我的拉格朗日插值方法代码

function [y] = lagrange(x,x0,y0)
  % x is the value we want to interpolate
  
  % x0 vector is in the form [x0,x1,x2,...] and contains the points
  
  % y0 vector contains the values. In this case I send the function 
  % the row vectors of R,G and B matrices
  
  n = size(x0,2);
  y = 0
  
  for i=1:n
    res = 1
    for j=1:n
      if i == j
        continue
      endif
      res *= ((x-x0(j)) / (x0(i)-x0(j)));
    endfor
    y += res * y0(i)
  endfor

这里困扰我的是我不知道如何将这两个部分结合起来。更具体地说,我不确定应该如何将函数发送到 x0 向量。我在做这部分的尝试看起来是这样的:

for i=1:w
  for j=1:h
    R_new(i,j) = lagrange(j,[1:h],R_new(i,:));
  endfor
endfor

我知道这是一个非常糟糕的尝试,因为这种方法没有给我任何结果。由于某种原因,代码一直在运行。如果您能帮助我,我将不胜感激。我对 Octave 也有点陌生,所以可能有些事情我不知道。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。