Stretch the middle of image

2 views (last 30 days)
Fat Man
Fat Man on 27 Jun 2019
Answered: DGM on 7 Nov 2022
Suppose I have a .png
How can I use Matlab to stretch the middle third of the image in horizontal direction by a factor of 2?
img = imread('dough.png');%Open the image input
image(img)
[rows,columns,~] = size(img)
Thank you for your help!!!

Accepted Answer

KSSV
KSSV on 27 Jun 2019
img = imread('dough.png');%Open the image input
image(img)
[rows,columns,~] = size(img)
iwant = imresize(img,[2*rows columns]) ;
  2 Comments
Fat Man
Fat Man on 27 Jun 2019
Thank you for replying
But, the rows represents the Y-axis in Matlab figure. Please take a look at the output image that I would like to get
KSSV
KSSV on 27 Jun 2019
The code just resizes the matrix..you will not get the effect you want as shown.

Sign in to comment.

More Answers (1)

DGM
DGM on 7 Nov 2022
You could do this literally as the question describes
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/226538/dough.png');
[h,w,~] = size(inpict);
% split the image
w3 = round(w/3);
L = inpict(:,1:w3,:);
M = inpict(:,w3+1:2*w3,:);
R = inpict(:,2*w3+1:end,:);
% resize the middle bit, concatenate
M = imresize(M,[h 2*w3]);
outpict = [L M R];
imshow(outpict)
Or you could perhaps ease the transitions by interpolating. Here, the query curve xqn has slope 1 at its ends and slope 1/2 in the middle, but the transitions are eased in a PWL manner.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/226538/dough.png');
[h,wi,c,~] = size(inpict);
% set up the query vector for the interpolation
wo = wi*4/3;
xout = rescale([0 0.246 0.329 0.671 0.754 1],1,wo);
xin = rescale([0 0.328 0.403 0.597 0.672 1],1,wi);
xqn = interp1(xout,xin,1:wo);
plot(xqn)
xlabel('output x-coordinates')
ylabel('input x-coordinates')
% do the interpolation
inpict = im2double(inpict);
outpict = zeros(h,wo,c);
for cx = 1:c
outpict(:,:,cx) = interp2(inpict(:,:,cx),xqn,(1:h)');
end
imshow(outpict)
I'm sure you could also use the PWL image transformation tools and imwarp() as well. Feel free to figure that out.

Tags

Community Treasure Hunt

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

Start Hunting!