Clear Filters
Clear Filters

Exchange bit-planes

8 views (last 30 days)
Brahim Aboumouadine
Brahim Aboumouadine on 24 Dec 2020
hey everyone
how to Exchange the third and the fifth bit-planes of a gray-scale image using bitget and bitset ??
thanks in advance

Accepted Answer

Image Analyst
Image Analyst on 24 Dec 2020
Edited: Image Analyst on 24 Dec 2020
Here's one way:
% Demo to extract and swap bitplanes in a gray scale image.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 1, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Get the 3rd bitplane
bitPlane3Image = bitget(grayImage, 3);
% Get the 5th bitplane
bitPlane5Image = bitget(grayImage, 5);
% Make the 3rd bitplane the 5th one.
grayImage = bitset(grayImage, 3, bitPlane5Image);
% Make the 5th bitplane the 3rd one.
grayImage = bitset(grayImage, 5, bitPlane3Image);
% Display the original gray scale image.
subplot(2, 1, 2);
imshow(grayImage, []);
title('Modified Grayscale Image', 'FontSize', fontSize);
fprintf('Done running %s.m.\n', mfilename);
msgbox('Done with demo');
  1 Comment
Brahim Aboumouadine
Brahim Aboumouadine on 24 Dec 2020
thank you , this demo is really helpful :)

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!