Paul Manta
Paul Manta

Reputation: 31567

Oblique stripes resulting from bilinear interpolation

I'm trying to write a function in GNU Octave that does bilinear interpolation on a PGM image. The result isn't that great: I keep getting oblique stripes of different shades all over the image. Also, the rows and columns that are added during interpolation are darker than they should. Could someone help me by pointing out the problem, please?

function bilinear(img)
    data = imread(img);

    for n = 1 : 2 : (rows(data) - 1)  * 2
        average = average_vector(data(n, 1:end), data(n+1:1:end));
        data = [data(1:n, 1:end); average; data(n+1:rows(data), 1:end)];
    end

    for n = 1 : 2 : (columns(data) - 1) * 2
        average = average_vector(data(1:rows(data), n), data(1:rows(data), n+1));
        data = [data(1:rows(data), 1:n) average data(1:rows(data), n+1:end)];
    end

    imwrite(data, strcat("out_bilinear_", img));
end

function res = average_vector(a, b)
    res = zeros(size(a));
    for n = 1 : length(a)
        res(n) = (a(n) + b(n)) / 2;
    end
end

Here's an image showing the problem: enter image description here

Upvotes: 0

Views: 369

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272457

You're iterating through the input image row-by-row (or column-by-column), but inserting new rows (or columns) as you go. I'm pretty sure this will screw your indexing up.

I would suggest creating a new output matrix, rather than modifying the original. This will be considerably faster, too.

Incidentally, your average_vector function can be written simply as res = (a + b) / 2;.

Upvotes: 2

Related Questions