How do I pass a vector is a parameter in a function?
3 views (last 30 days)
Show older comments
I'm new to matlab so I'm kind of confused on how functions work exactly so correct me if I have the wrong idea, but I'm essentially trying to "rewrite" the nearest neighbor part of the imresize() function. I want my function to take in an image and the size (number of rows and columns) of the new image. I'm confused on how to get the function to take in a vector such as [400 400] as one of the arguments. This is my function:
function resized = myimresize(I1, size)
I1 = imread('image.tif');
M1 = size(I1,1); % Number of rows in I
N1 = size(I1,2); % Number of columns in I
% Pick size of output image %%%%This is where I need help, I know this part is wrong but don't know how to write it correctly %%%%
newSize = [M2, N2];
I2 = zeros(M2,N2); % Allocate output image
cx = N2/N1; % Scale in x
cy = M2/M1; % Scale in y
for x=1:N2
for y=1:M2
% Calculate position in input image
v = x/cx;
w = y/cy;
% Pick the nearest neighbor to (v,w)
v = round(v);
w = round(w);
I2(y,x) = I1(w,v);
end
end
imshow(I2, []);
end
where img is the original 300 rows by 300 column image i'm inputting, and for "size" i want to be able to put in [400 400] so the image is resized to 400 rows by 400 columns, like this:
>> img = imread('image.tif')
>> myimresize(img, [400 400])
But how do I tell the function to take in a vector as the size?
0 Comments
Accepted Answer
KSSV
on 8 Oct 2018
function resized = myimresize(imagename, newsize)
% INPUTS:
% imagename - name of the image which you read with extension
% newsize - the new size you wanted..it should be [M2 N2]
I1 = imread(imagename);
M1 = size(I1,1); % Number of rows in I
N1 = size(I1,2); % Number of columns in I
% Pick size of output image %%%%This is where I need help, I know this part is wrong but don't know how to write it correctly %%%%
M2 = newsize(1) ; N2 = newsize(2) ;
I2 = zeros(M2,N2); % Allocate output image
cx = N2/N1; % Scale in x
cy = M2/M1; % Scale in y
for x=1:N2
for y=1:M2
% Calculate position in input image
v = x/cx;
w = y/cy;
% Pick the nearest neighbor to (v,w)
v = round(v);
w = round(w);
I2(y,x) = I1(w,v);
end
end
imshow(I2, []);
end
More Answers (0)
See Also
Categories
Find more on Encryption / Cryptography in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!