Resizing and repeating an image

I am having a problem with images. I am trying to take an image and render it 1 / 4th from its original size and then repeat it as a 2x2 matrix. View like this:

Given an input image:
           ---------------------
           |                   |
           |                   |
           |       IMG         | 
           |                   |
           |                   |
           ---------------------

   Firstly, shrink it to 1/4 of its original size:
           -----------
           |   img   | 
           |         | 
           |---------|

   Then concatenate it into a "2x2 array" of the shrunken image:
           ---------------------
           |   img   |   img   |
           |         |         |
           |---------|---------| 
           |   img   |   img   |
           |         |         |
           ---------------------

      

What I'm struggling with is the fact that I don't know how to turn it into a 2x2 array. Any suggestions? This time it is not really HW :) This is for educational purposes. Here's what I've tried so far:

function[newImg] = immultiply(picture)

Image = imread(picture); %// Reads in the picture
[r, c, l] = size(Image); %// Finds the images rows, columns and layers
rows = round(r ./ 2); %// Divides up the rows
columns = round(c ./ 2); %// Divides up the columns

newImg = cat(3,rows,columns,3); %// Creates my image, but just gives me a blank thing
imshow(newImg)


end

      

I will update as I work further. Thank!

+3


source to share


3 answers


The previous answer covers perfectly what you asked,

however, if you do not want to use imresize

and repmat

,



im = imread('peppers.png');
subplot(1,2,1),imshow(im) 
im1 = im(1 : 2 : end, 1 : 2 : end,:); 
im2 = [im1 im1; im1 im1]; 
subplot(1,2,2),imshow(im2);

      

enter image description here

+2


source


The previous two answers are correct and I originally worked on it just by playing around and not going to post it, but with minor changes I think it touches on some of Luis Mendo's comments about Cammal's answer.

My original thought was, why throw data away? If you downsample to 1/4 the size but built 4 of them, you have room for all the data:

img1 = imread('myimage.png');
subplot(1,2,1),imshow(img1) 
img2 = img1([1:2:end, 2:2:end], [1:2:end, 2:2:end]); 
subplot(1,2,2),imshow(img2);

      

The resulting top-left quadrant img2

will be exactly what Kamtal's answer gives: interpolating the nearest neighbor of pixels with odd x and y coordinates. The other 3 will be (even / odd), (odd / even), (even / even). Each pixel in img1

appears in img2

, and each sub-image may be slightly different.

If instead we want to combine data from each of the 4 images into one image without throwing away all the data, we can change that a bit. We just take the average of 4 images. Note that img2

it's the same here as above, I'm just breaking out the calculations to make it obvious.

img1 = imread('myimage.png');
subplot(1,3,1),imshow(img1) 
img2a = img1(1:2:end, 1:2:end);
img2b = img1(1:2:end, 2:2:end);
img2c = img1(2:2:end, 1:2:end);
img2d = img1(2:2:end, 2:2:end);
img2 = [img2a img2b; img2c img2d];
subplot(1,3,2),imshow(img2);
img3a = (img2a + img2b + img2c + img2d)/4;
img3 = [img3a img3a; img3a img3a];
subplot(1,3,3),imshow(img3);

      

Here img3

shows four identical images, all of downsampling an original image using the filter medium.



To do this with a filter, you must use the kernel:

[0.25 0.25]
[0.25 0.25]

      

It just takes the average of 4 elements in the neighborhood. With the kernel start at (1,1), your downsampled interpolated image will be in odd numbered rows / columns:

img1 = imread('myimage.png');
subplot(1,2,1),imshow(img1) 
h = [0.25, 0.25; 0.25, 0.25]   //% define the mean filter kernel
img2a = imfilter(img1, h);     //% filter before applying Kamtal solution
img2b = img2a(1:2:end, 1:2:end);
img2 = [img2b img2b; img2b img2b]; 
subplot(1,2,2),imshow(img2);

      

The resulting image should be the same as img3

above.

(By the way, average filtering with a 2x2 kernel and then sampling to 1/4 size is essentially bilinear interpolation. imresize

Uses bicubic interpolation by default, so its results will be slightly different.)

+4


source


Just use the instructions imresize

andrepmat

i = imread('lena.png');

figure(1),subplot(1,2,1),imshow(i) 
[n,m,d] = size(i);

newI = imresize(i,0.5); 
finalI = repmat(newI,2,2); 
figure(1),subplot(1,2,2),imshow(finalI);

      

newI is the image resized to 1/2. This is equivalent to shrinking the image to 1/4. finalI is the last image that is repeated 4 times. repmat

is a 2x2 matrix concatenation.

I highly recommend you check the documentation for these two functions: http://uk.mathworks.com/help/images/ref/imresize.html http://uk.mathworks.com/help/matlab/ref/repmat.html

+3


source







All Articles