Splitting image into 4 quadrants, up, down, right and left.
4 views (last 30 days)
Show older comments
Hey! I am trying to split an image into 4 quadrants based on the two diagonals through the middle of the image. And then swap the upper quadrant with the lower one. I am having difficulty setting my loop to get the quadrants. I would really appreciate some help!! Thank you so much
0 Comments
Accepted Answer
Matt J
on 29 Oct 2018
Edited: Matt J
on 29 Oct 2018
[m,n]=size(yourImage);
[I,J]=ndgrid(1:m,1:n);
Jf=n+1-I;
Q1=(I<J & J<Jf);
Q2=(I<J & J>Jf);
Q3=(I>J & J>Jf);
Q4=(I>J & J<Jf);
newImage=yourImage;
newImage(Q3)=yourImage(Q1); %flip top and bottom quadrants (Q1 and Q3)
newImage(Q1)=yourImage(Q3);
9 Comments
Image Analyst
on 30 Oct 2018
Edited: Image Analyst
on 30 Oct 2018
Make sure you have a gray scale image, not a color one. And if the number of rows and columns are not the same, use imresize().
Why do you want to do this unusual thing anyway?
More Answers (1)
Image Analyst
on 30 Oct 2018
Here's a start (using some of Matt's code - thanks) that works for gray scale or RGB images, and non-square images (sort of). See if you can fix it (the Q's) so that the center of the diagonals is at the center of the final image for non-square images..
% originalImage = imread('cameraman.tif');
originalImage = imread('peppers.png');
[rows, ncolumns, numberOfColorChannels] = size(originalImage);
[Y,X]=ndgrid(1:rows,1:ncolumns);
Jf=ncolumns+1-Y;
Q1=(Y<X & X<Jf);
Q2=(Y<X & X>Jf);
Q3=(Y>X & X>Jf);
Q4=(Y>X & X<Jf);
flippedImage = flipud(originalImage);
% Mask out Q1 | Q3
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
mask = Q1 | Q3;
maskedImage1 = bsxfun(@times, flippedImage, cast(mask, 'like', originalImage));
mask = Q2 | Q4;
maskedImage2 = bsxfun(@times, originalImage, cast(mask, 'like', originalImage));
finalImage = maskedImage1 + maskedImage2;
imshow(finalImage);


0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!