Help With Scaling Image Without Using Toolbox

I am trying to scale an image using affine transformation. I am using an image of resolution 256x256. I have a code that looks something like as below :
i = imread('DIP/pic.jpg');
R = uint8(zeros(size(i)));
for x = 1:256
for y = 1:256
v = 0.25*x;
w = 0.25*y;
i(x,y) = R(v,w);
end
end
imshow(i);
imshow(R);
However whenever I run the script, I get the error Subscript indices must either be real positive integers or logicals.
I know I could just use the toolbox but I am not doing so to see how can I actually implement affine transformation as it would help me with getting a better understanding of image processing.
Thanks

Answers (2)

Walter Roberson
Walter Roberson on 28 Feb 2018
Edited: Walter Roberson on 28 Feb 2018
Try the case of x=1 y=1. v=0.25*x so v=0.25, and likewise for w. You then try to use those 0.25 as subscripts.

5 Comments

Tried it but still the error persists.
@Yawar Khalid: Of course the error persists. Walter did not mean, that you should try this, but explained, that trying this is the problem. R(0.25, 0.25) is an error, because indices must be positive integers.
Which dimension does R have? Maybe you mean:
v = 4 * x;
w = 4 * y;
i(x,y) = R(v,w);
or
v = ceil(0.25 * x);
w = ceil(0.25 * y);
i(x,y) = R(v,w);
R has dimensions that are 1/4th of the original image, that is it should have a resolution of 64x64 after being resized. I will try the suggestions and post back the results
EDIT: Tried these methods but still no success. the ceiling worked but instead of scaling the image down it modified the intensity value of the pixels
@Yawar: In "i(x,y) = R(v,w);" you overwrite the pixels of "i". It is not clear why you insert data from the zero matrix to the imported image. It it also not clear, if your input or output has 64x64 or 256x256 pixels.
Please post clearly, what you want to achieve:
  1. What are the inputs (size and type)
  2. What is the wanted output (size and type)
  3. By which operation do you want to achieve it?
1. The input is in image called pic.jpg. Its of size 256x256
2. The desired output is the pic.jpg image reduced to 1/4th of its size, that is the pic.jpg now has a size of 64x64 instead of 256x256
3. The operation I want to use is affine transformation for scaling. I want to define a custom function T which transforms the size of the pic.jpg and converts in to 64x64.
Let (x,y) be the original coordinates of pic.jpg and let (v,w) be the transformed coordinates of pic.jpg and let T=(x/4,y/4) and what I want to do is something like :
(x,y) = T(v,w)

Sign in to comment.

img = imread('DIP/pic.jpg');
x = round(linspace(1, size(img, 2), 256));
y = round(linspace(1, size(img, 1), 256));
idx = sub2ind(size(img), y, x);
R = reshape(img(idx), 256, 256);
By the way:
R = zeros(size(img), 'uint8');
is more efficient than creating a double array at first (with 8 bytes per element) and converting it to an uint8 afterwards.

4 Comments

reshape is a built-in function of the toolbox so I would like to explore any other alternatives
round, ceil, linspace, size, imread and imshow are toolbox functions. Implicitly a=b calls the toolbox function subsasgn also and 0.25*x calls the built-in times. reshape does only change the dimensions of the matrix without touching the data in any way. If this is too much for you already, I cannot offer any useful help.
What I meant to say was that I didn't use reshape and that I would be much obliged if you could help me understand why my code doesn't work and what I am doing wrong.
When I do what you have done, it gives me an error :
To RESHAPE the number of elements must not change.
Jan, you missed that .jpg are almost always 3D.

Sign in to comment.

Asked:

on 28 Feb 2018

Commented:

on 1 Mar 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!