baidut
7/27/2017 - 12:16 AM

Find the bug and fix it~

Find the bug and fix it~

Itest = imread('Itest.png'); 
Iones = ones(50,50,3);

for dim3 = 1 : 3 
    for row = 1 : 2*50
        for col = 1 : 2*50
           resize_I(row, col, dim3) = lanczosFilter(3, Itest(:,:,dim3), [col, row], 2, 50, 50);
        end
    end
end

resize_I2 = imresize(Itest, [100,100], 'Lanczos3', 'Antialiasing', false);

figure(1); imshow(Itest); title('source image')

h = figure(2)
subplot(1,2,1); imshow(resize_I); title('lanczosFilter');
subplot(1,2,2); imshow(resize_I2); title('imresize');

ezDump(h);
function [ret_val] = lanczosFilter(lobe, src, texCoord, scale, W, H)
% texCoord: [w, h]    
    % Parameters
    coef = -inf;
    coef_sum = 0;
    res = 0;
    idx_x =0;
    idx_y = 0;
    ret_val = 0;
    
    for n = -lobe : 1 : lobe-1
       for m = -lobe : 1 : lobe-1
          idx_x = floor(texCoord(1) / scale) + m + 1;
          idx_y = floor(texCoord(2) / scale) + n + 1;
          
          coef = lanczos3Coef(texCoord(1) / scale - idx_x, lobe) * lanczos3Coef(texCoord(2) / scale - idx_y, lobe);
          
          % when the neib. pixel is outside the boundary, using the boundary pixels
            if (idx_x < 1)
                idx_x = 1;
            else
                idx_x = idx_x;
            end
            
            if (idx_y < 1)
                idx_y = 1;
            else
                idx_y = idx_y;
            end
            
            if (idx_x > W)
                idx_x = W;
            else
                idx_x = idx_x;
            end
            
            if (idx_y > H)
                idx_y = H;
            else
                idx_y = idx_y;
            end
            
            res =  res + src(idx_y, idx_x) * coef;
            coef_sum = coef_sum + coef;
       end
    end
    
    if (coef_sum ~= 0)
       ret_val = res / coef_sum;
       ret_val = min(max(ret_val, 0), 255);
    end
    
end

function [coef] = lanczos3Coef(x, lobe)
    if (x == 0.0)
        coef = 1.0;
        return;
    end
     s = sin(pi * x) * sin(pi * x / lobe);
     t = pi^2.0 * x^2.0 / lobe;
    if(abs(x) < 3.0)
       coef = s / t;
    else
       coef = 0.0;
    end
end

A friend of mine has trouble with his program. He was going to implement lanczosFilter. He compared his implementation of lanczosFilter with the built-in imresize function and found his results is buggy (See result.jpg).

Try to find the reason causing the result so ugly.

Have fun! Please visit my blog to find the answer.