user867204
user867204

Reputation: 265

upsampling in matlab

I try to write a matlab function that upsamples me a picture (matrix of grey values). It is actually nothing overwhelmingly complicated, but I yet manage to do it wrong. My objective is it to resize it by factor 2 and for the start I just want to see my upscaled picture. I want to fill the gaps with zeros, hence every 2nd row/column is a filled with zeros. When I am done, I wonder why I see nothing but a grey ocean of pixels. I would have expected to be able to recognize at least some stuff in my picture.

Here is my function, does anyone see my mistake?

function [upsampled] = do_my_upsampling(image)
    [X Y] = size(image);
    upsampled = zeros(X*2, Y*2);
    upsampled(1:2:end, 1:2:end) = image(1:1:end, 1:1:end);
end

Upvotes: 1

Views: 8308

Answers (2)

user2853257
user2853257

Reputation: 1

Try imshow(image,[])

or, as your image is a double, convert it into uint8 first and then show i.e

imshow(uint8(image))

Upvotes: -1

Jonas
Jonas

Reputation: 74940

Your code works fine for me (with image = rand(100);. However, it's not a very Matlab-way to achieve the result.

If you just want to spread out your pixels, why don't you do direct indexing?

[nRows,nCols] = size(image);
upsampled = zeros(2*nRows,2*nCols);
upsampled(1:2:end,1:2:end) = image;

Upvotes: 5

Related Questions