Im trying to slice an image and switch its quadrants so that I switch the bottom right quadrant and the top left quadrant and the bottom left quadrant with the top right quad

2 views (last 30 days)
function sliceNDice(fn)
img = imread(fn);
imshow(fn)
figure
topLeft = img(1:end/2,1:end/2);
topRight = img(1:end/2,end/2+1:end);
botLeft = img(end/2+1:end,1:end/2);
botRight = img(end/2+1:end,end/2+1:end);
newImg = img(3,botRight,botLeft,topRight,topLeft);
imgshow(newImg)
end

Answers (2)

Walter Roberson
Walter Roberson on 11 Apr 2023
newImg = img(3,botRight,botLeft,topRight,topLeft);
That would try to index the array img using 3 as the row index, using the entire array botRight as the column indices, using the entire array botLeft as the plane index, and up through the hyper dimensions.
You should be using
newImg = cat(2,botRight,botLeft,topRight,topLeft);

DGM
DGM on 11 Apr 2023
Edited: DGM on 11 Apr 2023
img = imread('peppers.png');
topLeft = img(1:end/2,1:end/2,:); % specify enough dimensions
topRight = img(1:end/2,end/2+1:end,:);
botLeft = img(end/2+1:end,1:end/2,:);
botRight = img(end/2+1:end,end/2+1:end,:);
newImg = [botRight,botLeft; topRight,topLeft]; % concatenate
imshow(newImg)
Note that the output image size only matches that of the input image if the input image has even geometry.
FWIW, if you were using MIMT, this reduces to one line.
outpict = shuffle(inpict,[2 2],[4 3 2 1]);

Categories

Find more on Image Processing Toolbox 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!