# Invert even and odd columns in an image

3 ビュー (過去 30 日間)
Vitor Neis 2022 年 5 月 19 日

Hi guys,
How to invert even and odd columns in an image?
Thanks for the help!
##### 2 件のコメント表示非表示 1 件の古いコメント
Vitor Neis 2022 年 5 月 19 日
Sorry, by invert, I meant to change the position between the columns

サインインしてコメントする。

### 採用された回答

DGM 2022 年 5 月 19 日

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
if mod(size(A,2),2) == 1
end
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,:);
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;
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:
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.

サインインしてコメントする。

### その他の回答 (2 件)

Chunru 2022 年 5 月 19 日
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)
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

Image Analyst 2022 年 5 月 19 日
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.

サインインしてコメントする。

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by