How do I pass a vector is a parameter in a function?

2 次查看(过去 30 天)
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?

采纳的回答

KSSV
KSSV 2018-10-8
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Geometric Transformation and Image Registration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by