Invert even and odd columns in an image

3 views (last 30 days)
Vitor Neis
Vitor Neis on 19 May 2022
Edited: DGM on 19 May 2022
Hi guys,
How to invert even and odd columns in an image?
Thanks for the help!
  2 Comments
Vitor Neis
Vitor Neis on 19 May 2022
Sorry, by invert, I meant to change the position between the columns

Sign in to comment.

Accepted Answer

DGM
DGM on 19 May 2022
Edited: DGM on 19 May 2022
Depends what you need. "Inverting even and odd column positions" might be interpreted a few different ways. By simply doing a circular shift like Image Analyst shows, the odd columns are now in even positions, and the even columns are now in odd positions. Excepting the edges, the image content remains continuous.
Alternatively, you could interpret it as swapping adjacent columns pairwise like Chunru shows. There's a point of caution to consider though. Simple indexing will have problems if the image width is odd. Consider the simple example:
A = 1:11 % a simple vector for demonstration
A = 1×11
1 2 3 4 5 6 7 8 9 10 11
sz = size(A);
oddidx = 1:2:sz(2) % odd indices
oddidx = 1×6
1 3 5 7 9 11
evenidx = 2:2:sz(2) % even indices
evenidx = 1×5
2 4 6 8 10
Note that the odd and even index vectors aren't the same length, so you can't swap them directly. How you deal with this is up to you.
You could pad the array and then (if you want) trim off the excess afterward:
A = 1:11;
% pad the array if needed
ispadded = false;
if mod(size(A,2),2) == 1
A = padarray(A,[0 1],0,'post');
ispadded = true;
end
% flip adjacent column pairs
sz = size(A);
oddidx = 1:2:sz(2);
evenidx = 2:2:sz(2);
B = zeros(size(A),class(A));
B(:,oddidx,:) = A(:,evenidx,:);
B(:,evenidx,:) = A(:,oddidx,:);
% strip padding
if ispadded
B = B(:,1:end-1,:);
end
B
B = 1×11
2 1 4 3 6 5 8 7 10 9 0
Alternatively, you can simply hold the position of the last column instead of trying to flip it with its nonexistent neighbor:
A = 1:11;
% flip adjacent column pairs
sz = size(A);
oddidx = 1:2:sz(2);
evenidx = min(2:2:ceil(sz(2)/2)*2,sz(2));
B = zeros(size(A),class(A));
B(:,oddidx,:) = A(:,evenidx,:);
B(:,evenidx,:) = A(:,oddidx,:);
B
B = 1×11
2 1 4 3 6 5 8 7 10 9 11
Either of the above examples should work for an image just the same as for the example vector:
A = imread('peppers.png');
% flip adjacent column pairs
sz = size(A);
oddidx = 1:2:sz(2);
evenidx = min(2:2:ceil(sz(2)/2)*2,sz(2));
B = zeros(size(A),class(A));
B(:,oddidx,:) = A(:,evenidx,:);
B(:,evenidx,:) = A(:,oddidx,:);
imshow(B)
There may be other interpretations of the described transformation. If none of these methods do what you want, you'll have to elaborate on how they differ from your needs.

More Answers (2)

Chunru
Chunru on 19 May 2022
a = randn(10, 10, 3);
imshow(a)
% swap even and odd columns
b = zeros(size(a));
b(:, 1:2:end, :) = a(:, 2:2:end, :);
b(:, 2:2:end, :) = a(:, 1:2:end, :);
figure
imshow(b)

Image Analyst
Image Analyst on 19 May 2022
How about just cropping off the first column and tacking it onto the end?
M2 = [M(:, 2:end), M(:, 1)];
You can do this (as long as I didn't do your homework for you) because you can't turn in someone else's solution as your own.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!