Invert even and odd columns in an image

4 vues (au cours des 30 derniers jours)
Vitor Neis
Vitor Neis le 19 Mai 2022
Modifié(e) : DGM le 19 Mai 2022
Hi guys,
How to invert even and odd columns in an image?
Thanks for the help!
  2 commentaires
Chunru
Chunru le 19 Mai 2022
Are you referrring to binary image? Attach an example image and indicate what "invert" means.
Vitor Neis
Vitor Neis le 19 Mai 2022
Sorry, by invert, I meant to change the position between the columns

Connectez-vous pour commenter.

Réponse acceptée

DGM
DGM le 19 Mai 2022
Modifié(e) : DGM le 19 Mai 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.

Plus de réponses (2)

Chunru
Chunru le 19 Mai 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 le 19 Mai 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.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by