how to swap bit plane???

it is bitplane decomposition code..i want to swap them.. how to do it???
g=imread('girl.png');
g=rgb2gray(I_scramble);
g1=bitget(g,1);
g2=bitget(g,2);
g3=bitget(g,3);
g4=bitget(g,4);
g5=bitget(g,5);
g6=bitget(g,6);
g7=bitget(g,7);
g8=bitget(g,8);
figure,imshow(g);
title('original image');
subplot(4,4,1),imshow(logical(g1)),title('Bit 1');
subplot(4,4,2),imshow(logical(g2)),title('Bit 2');
subplot(4,4,3),imshow(logical(g3)),title('Bit 3');
subplot(4,4,4),imshow(logical(g4)),title('Bit 4');
subplot(4,4,5),imshow(logical(g5)),title('Bit 5');
subplot(4,4,6),imshow(logical(g6)),title('Bit 6');
subplot(4,4,7),imshow(logical(g7)),title('Bit 7');
subplot(4,4,8),imshow(logical(g8)),title('Bit 8');
%L=imread('monarch.png');
L=rgb2gray(I1_scramble);
L1=bitget(L,1);
L2=bitget(L,2);
L3=bitget(L,3);
L4=bitget(L,4);
L5=bitget(L,5);
L6=bitget(L,6);
L7=bitget(L,7);
L8=bitget(L,8);
figure,imshow(L);
title('original image');
subplot(4,4,1),imshow(logical(L1)),title('Bit 1');
subplot(4,4,2),imshow(logical(L2)),title('Bit 2');
subplot(4,4,3),imshow(logical(L3)),title('Bit 3');
subplot(4,4,4),imshow(logical(L4)),title('Bit 4');
subplot(4,4,5),imshow(logical(L5)),title('Bit 5');
subplot(4,4,6),imshow(logical(L6)),title('Bit 6');
subplot(4,4,7),imshow(logical(L7)),title('Bit 7');
subplot(4,4,8),imshow(logical(L8)),title('Bit 8');
how to swap this ???

 Accepted Answer

gall = cat(3, g1, g2, g3, g4, g5, g6, g7, g8);
gall_scramble = gall(:, :, randperm(8));
gscramble = zeros(size(g));
for p = 1 : 8
gscramble = bitset(gscramble, p, gall_scramble(:,:,p));
end

4 Comments

if ~isequal(size(g1), size(L1))
error('Images are different sizes, cannot intermix bit panes');
end
pall = cat(3, g1, g2, g3, g4, g5, g6, g7, g8, L1, L2, L3, L4, L5, L6, L7, L8);
pall_scramble = pall(:, :, randperm(size(pall,3)));
gscramble = zeros(size(g), class(g));
Lscramble = zeros(size(g), class(g));
for p = 1 : 8
gscramble = bitset(gscramble, p, pall_scramble(:,:,p));
end
for p = 1 : 8
Lscramble = bitset(Lscramble, p, pall_scramble(:,:,p+8));
end
Surya's "Answer" moved here since it's not an answer to the original question:
I'm a beginner... This code works only one image of bitplane.... I want to swap bit planes of two images ....
Why? Just for curiosity to see what it looks like? Or do you have an actual real-world "use case"?
I rather suspect that "image encryption" will form part of the answer.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!