Transpose does not support N-D arrays error
11 views (last 30 days)
Show older comments
Hi, I am performing image morphing using interp2, however i keep on getting the error :
Error using .'
TRANSPOSE does not support N-D arrays. Use
PAGETRANSPOSE/PAGECTRANSPOSE to transpose pages or
PERMUTE to reorder dimensions of N-D arrays.
Any idea how should I modify my code so that I can get the dimension right ?
A = imread('1.png');
B = imread('3.jpeg');
[height,width,channels] = size(A);
[X,Y] = meshgrid(1:width, 1:height);
number_of_frames = 10;
for i=1:number_of_frames
interp_x = (i - 1) / (number_of_frames - 1);
interp_y = (number_of_frames - i) / (number_of_frames - 1);
X_interp = interp_x * X + (1-interp_x) * Y;
Y_interp = interp_y * Y + (1-interp_y) * X;
results = interp2(A,X_interp,Y_interp,'linear',0);
imshow(results)
imwrite(result, sprintf('morph_%03d.jpg', i));
pause(0.1);
end
2 Comments
Stephen23
on 3 Jan 2023
Please show us the complete error message. This means all of the red text.
The code you show does not incude the line where the error occurs.
Accepted Answer
DGM
on 3 Jan 2023
Edited: DGM
on 3 Jan 2023
There are a couple problems. First, the error is because you're feeding a MxNx3 array to interp2(). You're also feeding an integer-class array to interp2().
A = imread('peppers.png');
%B = imread('3.jpeg');
A = im2double(A); % images fed to interp2() must be floating-point
[height,width,channels,~] = size(A);
[X,Y] = meshgrid(1:width, 1:height);
number_of_frames = 10;
results = zeros(height,width,channels); % preallocate
for i=1:number_of_frames
interp_x = (i - 1) / (number_of_frames - 1);
interp_y = (number_of_frames - i) / (number_of_frames - 1);
X_interp = interp_x * X + (1-interp_x) * Y;
Y_interp = interp_y * Y + (1-interp_y) * X;
% one way is to just process it one channel at a time
for c = 1:channels
results(:,:,c) = interp2(A(:,:,c),X_interp,Y_interp,'linear',0);
end
imshow(results)
%imwrite(result, sprintf('morph_%03d.jpg', i));
pause(0.1);
end
I don't know what the rest is actually supposed to do, but it does something.
3 Comments
DGM
on 5 Jan 2023
Edited: DGM
on 5 Jan 2023
If you want the frames to display quicker, you can reduce the pause, but if that's not enough, then it's probably a good idea to rearrange things:
A = imread('peppers.png');
A = im2double(A); % images fed to interp2() must be floating-point
[height,width,channels,~] = size(A);
[X,Y] = meshgrid(1:width, 1:height);
% generate all the frames
frames = 10;
thisframe = zeros(height,width,channels);
allframes = cell(frames,1);
for f = 1:frames
interp_x = (f - 1) / (frames - 1);
interp_y = (frames - f) / (frames - 1);
X_interp = interp_x * X + (1-interp_x) * Y;
Y_interp = interp_y * Y + (1-interp_y) * X;
% one way is to just process it one channel at a time
for c = 1:channels
thisframe(:,:,c) = interp2(A(:,:,c),X_interp,Y_interp,'linear',0);
end
allframes{f} = thisframe;
% if you want to write outputs, they can be done here
%imwrite(thisframe, sprintf('morph_%03d.jpg', i));
end
% addressing a cell array may be slightly faster than a giant 4D array
% for faster display, keep everything else out of the display loop
% this includes all the axes setup that imshow does when called
hi = imshow(zeros(height,width,3)); % set up the axes with a dummy image
for f = 1:frames
hi.CData = allframes{f}; % then update the image object's content
pause(0.01);
end
At least on my machine, with this image, these changes reduced the execution time of the loop contents (excluding the pause) by about a factor of 500.
More Answers (1)
Steven Lord
on 5 Jan 2023
I'd probably adapt the "Interpolate Multiple Sets of Values on Same Grid" example from the griddedInterpolant documentation page. This uses the functionality introduced in release R2021a to interpolate multiple data sets simultaneously. See the Version History section of the documentation page linked above for more information.
A = imread('peppers.png');
A = im2double(A);
whos
Let's interpolate A to a coarser grid.
[x, y] = ndgrid(1:384, 1:512);
G = griddedInterpolant(x, y, A);
[xx, yy] = ndgrid(1:20:384, 1:20:512);
B = G(xx, yy);
Now display the two images.
subplot(1, 2, 1)
imshow(A)
title('Original')
subplot(1, 2, 2)
imshow(B)
title('Interpolated')
0 Comments
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
