How can I combine bit map images to get back the original grey scale image?

4 views (last 30 days)
C=imread('cameraman.tif'); C1=bitget(C,1); figure, imshow(logical(C1));title('Bit plane 1'); C2=bitget(C,2); figure, imshow(logical(C2));title('Bit plane 2'); C3=bitget(C,3); figure, imshow(logical(C3));title('Bit plane 3'); C4=bitget(C,4); figure, imshow(logical(C4));title('Bit plane 4'); C5=bitget(C,5); figure, imshow(logical(C5));title('Bit plane 5'); C6=bitget(C,6); figure, imshow(logical(C6));title('Bit plane 6'); C7=bitget(C,7); figure, imshow(logical(C7));title('Bit plane 7'); C8=bitget(C,8); figure, imshow(logical(C8));title('Bit plane 8');

Accepted Answer

Image Analyst
Image Analyst on 8 Sep 2016
Multiply bit plane 7 by 2^7, bitplane 6 by 2^6, and so on. Then add them all up.

More Answers (2)

Rakesh Ahuja
Rakesh Ahuja on 9 Dec 2016
As per your reply ' Multiply bit plane 7 by 2^7, bit-plane 6 by 2^6, and so on. Then add them all up. The result is that I am getting a blank image instead of getting the original image. Kindly rectify the problem. The following code I have used. Kindly correct or suggest me another one.
C=imread('cameraman.tif');
C1=bitget(C,1); figure, imshow(logical(C1));title('Bit plane 1'); C1=im2double(C1); C2=bitget(C,2); figure, imshow(logical(C2));title('Bit plane 2'); C2=im2double(C2); C3=bitget(C,3); figure, imshow(logical(C3));title('Bit plane 3'); C3=im2double(C3); C4=bitget(C,4); figure, imshow(logical(C4));title('Bit plane 4'); C4=im2double(C4); C5=bitget(C,5); figure, imshow(logical(C5));title('Bit plane 5'); C5=im2double(C5); C6=bitget(C,6); figure, imshow(logical(C6));title('Bit plane 6'); C6=im2double(C6); C7=bitget(C,7); figure, imshow(logical(C7));title('Bit plane 7'); C7=im2double(C7); C8=bitget(C,8); figure, imshow(logical(C8));title('Bit plane 8'); C8=im2double(C8);
C1=C1*.2^0; C2=C2*.2^1; C3=C3*.2^3; C4=C4*.2^4; C5=C5*.2^5; C6=C6*.2^6; C7=C7*.2^7; C8=C8*.2^8;
C_Rejoin1= imadd(C1,C2); C_Rejoin1=imadd(C_Rejoin1, C3); C_Rejoin1=imadd(C_Rejoin1, C4); C_Rejoin1=imadd(C_Rejoin1,C5); C_Rejoin1=imadd(C_Rejoin1,C6); C_Rejoin1=imadd(C_Rejoin1,C7); C_Rejoin1=imadd(C_Rejoin1,C8);
figure, imshow(C_Rejoin1);title('Joined Image');
  2 Comments
Image Analyst
Image Analyst on 9 Dec 2016
Several problems with that. For one, you created c9 but then never added it in. I changed it to c8. Then you multiplied by zero point two to the power instead of two to the power. This is because you used "*.2" instead of "* 2". Fixed code is below.
A demo of mine is also attached.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
clearvars;
close all;
format long g;
format compact;
fontSize = 20;
grayImage = imread('cameraman.tif');
C1 = bitget(grayImage,1);
subplot(3, 3, 1);
imshow(logical(C1));
title('Bit plane 1');
C1 = im2double(C1);
C2 = bitget(grayImage,2);
subplot(3, 3, 2);
imshow(logical(C2));
title('Bit plane 2');
C2 = im2double(C2);
C3 = bitget(grayImage,3);
subplot(3, 3, 3);
imshow(logical(C3));
title('Bit plane 3');
C3 = im2double(C3);
C4 = bitget(grayImage,4);
subplot(3, 3, 4);
imshow(logical(C4));
title('Bit plane 4');
C4 = im2double(C4);
C5 = bitget(grayImage,5);
subplot(3, 3, 5);
imshow(logical(C5));
title('Bit plane 5');
C5 = im2double(C5);
C6 = bitget(grayImage,6);
subplot(3, 3, 6);
imshow(logical(C6));
title('Bit plane 6');
C6 = im2double(C6);
C7 = bitget(grayImage,7);
subplot(3, 3, 7);
imshow(logical(C7));
title('Bit plane 7');
C7 = im2double(C7);
C8 = bitget(grayImage,8);
subplot(3, 3, 8);
imshow(logical(C8));
title('Bit plane 8');
C8 = im2double(C8);
C1 = C1 * 2^0;
C2 = C2 * 2^1;
C3 = C3 * 2^3;
C4 = C4 * 2^4;
C5 = C5 * 2^5;
C6 = C6 * 2^6;
C7 = C7 * 2^7;
C8 = C8 * 2^8;
C_Rejoin1 = imadd(C1, C2);
C_Rejoin1 = imadd(C_Rejoin1, C3);
C_Rejoin1 = imadd(C_Rejoin1, C4);
C_Rejoin1 = imadd(C_Rejoin1, C5);
C_Rejoin1 = imadd(C_Rejoin1, C6);
C_Rejoin1 = imadd(C_Rejoin1, C7);
C_Rejoin1 = imadd(C_Rejoin1, C8);
subplot(3, 3, 9);
imshow(C_Rejoin1, []);
title('Joined Image');
DGM
DGM on 20 Jun 2022
Edited: DGM on 20 Jun 2022
@Image Analyst Minor error here:
C1 = C1 * 2^0;
C2 = C2 * 2^1;
C3 = C3 * 2^3; % 2^2
C4 = C4 * 2^4; % 2^3
C5 = C5 * 2^5; % 2^4
C6 = C6 * 2^6; % 2^5
C7 = C7 * 2^7; % 2^6
C8 = C8 * 2^8; % 2^7
I'm assuming that's not in the attached mfile, since the screenshot looks correct. I haven't checked though.

Sign in to comment.


DGM
DGM on 20 Jun 2022
Edited: DGM on 20 Jun 2022
Using a bunch of numbered variables forces you into writing unnecessarily repetitive code. This can be simplified.
One way would be to simply store the images as pages in a 3D array. Just multiply this array by an appropriately-oriented vector of the corresponding powers of 2.
% using a bunch of numbered variables is a great way to waste time
% but i'm going to assume these as given
C = imread('cameraman.tif');
C1 = bitget(C,1);
C2 = bitget(C,2);
C3 = bitget(C,3);
C4 = bitget(C,4);
C5 = bitget(C,5);
C6 = bitget(C,6);
C7 = bitget(C,7);
C8 = bitget(C,8);
% rearrange the given variables into a single array
bpstack = cat(3,C1,C2,C3,C4,C5,C6,C7,C8);
% simply multiply and add
factors = uint8(2.^(0:7)); % powers of 2 (same class as bpstack!)
%bpstack = bsxfun(@times,bpstack,permute(factors,[1 3 2])); % prior to R2016b
bpstack = bpstack.*permute(factors,[1 3 2]); % R2016b and newer
outpict = uint8(sum(bpstack,3));
% show that the result matches the original
immse(outpict,C)
ans = 0
% show the image
imshow(outpict)
Note that the method used for multiplication depends on the version. When this question was asked, bsxfun() would have been appropriate, but for recent versions, either can be used.
The above method takes less time to write and about half to a third of the time to execute when compared to a flat repetitive approach using a pile of numbered variables.

Community Treasure Hunt

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

Start Hunting!