Stretch the middle of image
2 views (last 30 days)
Show older comments
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!!!
0 Comments
Accepted Answer
More Answers (1)
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!