MATLAB: I had partitioned the image (‘cameraman.tif’ )into 8-bit map images ( C1, C2, C3, C4, C5, C6, C7, C8) but unable to rejoin to get back the original image ‘C’ as shown below in the related code in the Body Section.

partitioning the questions

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');

Best Answer

The first thing where you've gone wrong is to create all these C* images. If you're numbering variables, you're doing something wrong. Matlab already has a very effective way of storing things with a numbering index, it's called a matrix or cell array. So:
Cbit{1} = bitget(C, 1); figure, ...
Cbit{2} = ...
%etc
And, since you're now using a cell array, you can avoid retyping/ copy-pasting all this repeating code simply by replacing it with a loop:
Cbit = cell(1, 8); %always a good idea to preallocate although in your case won't make a difference
for bit = 1:8
Cbit{bit} = bitget(C, bit); figure, imshow(logical(Cbit{bit})), title(sprintf('Bit plane %d', bit));
end
Isn't that easier?
As for putting the image back together, simply use the inverse operation. You use bitget to get the bits, use bitset to set them back:
newC = zeros(size(C), class(C));
for bit = 1:8
newC = bitset(newC, bit, Cbit{bit});
end
Another option to reconstruct that image is to do the arithmetic yourself. One of the many ways (in R2016):
newC = sum(cat(3, Cbit{:}) .* permute(2.^(0:7), [1 3 2]), 3)
Related Question