Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
Em Bus
Em Bus le 8 Oct 2018
Commenté : Em Bus le 8 Oct 2018
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?

Réponse acceptée

KSSV
KSSV le 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
  1 commentaire
Em Bus
Em Bus le 8 Oct 2018
I see, I didn't realize you could just put (1) and (2). Thanks so much

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Geometric Transformation and Image Registration dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by