Invert even and odd columns in an image

조회 수: 1 (최근 30일)
Vitor Neis
Vitor Neis 2022년 5월 19일
편집: DGM 2022년 5월 19일
Hi guys,
How to invert even and odd columns in an image?
Thanks for the help!
  댓글 수: 2
Chunru
Chunru 2022년 5월 19일
Are you referrring to binary image? Attach an example image and indicate what "invert" means.
Vitor Neis
Vitor Neis 2022년 5월 19일
Sorry, by invert, I meant to change the position between the columns

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2022년 5월 19일
편집: 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
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.

추가 답변 (2개)

Chunru
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)

Image Analyst
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.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by