Write a MATLAB function that has an input argument for the scaling factor S. The
ID: 2084835 • Letter: W
Question
Write a MATLAB function that has an input argument for the scaling factor S. The function should read in an image, and then scale the image by the scaling factor to form a thumbnail image. First, perform a simple scaling - keep one out of S2 pixels. Since this is a 2D scaling, you can keep the center pixel in each square of S2 pixels, when S is an odd number and one of the 4 center pixels when S is even. Next, you are going to perform a more advanced scaling operation. Instead of keeping the center pixel in each square of S2 pixels, keep the average of all of the pixels in this square. Apply scaling to the images, and notice whether any aspects are more sensitive to scaling than others or better suited to different methods of scaling. Save the resulting images for presentation to the T/AExplanation / Answer
%scaling pixel in three different codes i have written before test them
%image stabilization for the scaling factor
************
%simplex scaling code:
%first test code
inputI = imread('your image');
[r,c] = size(inputI);
scale = [2 2]; % you could scale each dimension differently
outputI = zeros(scale(1)*r,scale(2)*c, class(inputI));
for i=1:scale(1)*r
for j=1:scale(2)*c
% map from output image location to input image location
ii = round( (i-1)*(r-1)/(scale(1)*r-1)+1 );
jj = round( (j-1)*(c-1)/(scale(2)*c-1)+1 );
% assign value
outputI(i,j) = inputI(ii,jj);
end
end
figure(1), imshow(inputI)
figure(2), imshow(outputI)
************
%formula for caluclting xloc and yloc
xloc = (j * (newwidth+1)) / (x+1);
yloc = (i * (newheight+1)) / (y+1);
**************************
%second test code
function imzoom = nnbr(image,zoom);
[r c d] = size(image); % dimensions of image data
%% zoom
rn = floor(zoom*r);
cn = floor(zoom*c);
s = zoom;
im_zoom = zeros(rn,cn,d);
%% nearest neighbour
for i = 1:rn;
x = i/s;
near_i = cast(round(x),'uint16');
if near_i == 0
near_i = 1;
end
for j = 1:cn;
y = j/s;
near_j = cast(round(y),'uint16');
if near_j == 0
near_j = 1;
end
im_zoom(i,j,:) = image(near_i,near_j,:);
end
end
imzoom = im_zoom;
*******************
function image_zoom = blnrm2(image, zoom)
[r c d] = size(image);
rn = floor(zoom*r);
cn = floor(zoom*c);
s = zoom;
im_zoom = zeros(rn,cn,d);
for i = 1:rn;
x1 = cast(floor(i/s),'uint32');
x2 = cast(ceil(i/s),'uint32');
if x1 == 0
x1 = 1;
end
x = rem(i/s,1);
for j = 1:cn;
y1 = cast(floor(j/s),'uint32');
y2 = cast(ceil(j/s),'uint32');
if y1 == 0
y1 = 1;
end
ctl = image(x1,y1,:);
cbl = image(x2,y1,:);
ctr = image(x1,y2,:);
cbr = image(x2,y2,:);
y = rem(j/s,1);
tr = (ctr*y)+(ctl*(1-y));
br = (cbr*y)+(cbl*(1-y));
im_zoom(i,j,:) = (br*x)+(tr*(1-x));
end
end
image_zoom = cast(im_zoom,'uint8');
****************
%third test code
function im_zoom = bicubic_m2(image,zoom);
[r c d] = size(image);
rn = floor(zoom*r);
cn = floor(zoom*c);
s = zoom;
im_zoom = cast(zeros(rn,cn,d),'uint8');
im_pad = zeros(r+4,c+4,d);
im_pad(2:r+1,2:c+1,:) = image;
im_pad = cast(im_pad,'double');
for m = 1:rn
x1 = ceil(m/s); x2 = x1+1; x3 = x2+1;
p = cast(x1,'uint16');
if(s>1)
m1 = ceil(s*(x1-1));
m2 = ceil(s*(x1));
m3 = ceil(s*(x2));
m4 = ceil(s*(x3));
else
m1 = (s*(x1-1));
m2 = (s*(x1));
m3 = (s*(x2));
m4 = (s*(x3));
end
X = [ (m-m2)*(m-m3)*(m-m4)/((m1-m2)*(m1-m3)*(m1-m4)) ...
(m-m1)*(m-m3)*(m-m4)/((m2-m1)*(m2-m3)*(m2-m4)) ...
(m-m1)*(m-m2)*(m-m4)/((m3-m1)*(m3-m2)*(m3-m4)) ...
(m-m1)*(m-m2)*(m-m3)/((m4-m1)*(m4-m2)*(m4-m3))];
for n = 1:cn
y1 = ceil(n/s); y2 = y1+1; y3 = y2+1;
if (s>1)
n1 = ceil(s*(y1-1));
n2 = ceil(s*(y1));
n3 = ceil(s*(y2));
n4 = ceil(s*(y3));
else
n1 = (s*(y1-1));
n2 = (s*(y1));
n3 = (s*(y2));
n4 = (s*(y3));
end
Y = [ (n-n2)*(n-n3)*(n-n4)/((n1-n2)*(n1-n3)*(n1-n4));...
(n-n1)*(n-n3)*(n-n4)/((n2-n1)*(n2-n3)*(n2-n4));...
(n-n1)*(n-n2)*(n-n4)/((n3-n1)*(n3-n2)*(n3-n4));...
(n-n1)*(n-n2)*(n-n3)/((n4-n1)*(n4-n2)*(n4-n3))];
q = cast(y1,'uint16');
sample = im_pad(p:p+3,q:q+3,:);
im_zoom(m,n,1) = X*sample(:,:,1)*Y;
if(d~=1)
im_zoom(m,n,2) = X*sample(:,:,2)*Y;
im_zoom(m,n,3) = X*sample(:,:,3)*Y;
end
end
end
im_zoom = cast(im_zoom,'uint8');
*******************
scaling factor :
tic;imshow(nnbr(im,2.5));toc
tic;imshow(blnr(im,2.5));toc
tic;imshow(bicubic_m2(im,2.5));toc
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.