How to construct a binary image based on the most significant bit of the pixels?

The brightness intensity of the image fractal.jpg takes values between 0 and 255.Thus each pixel of the image can be represented with 8 bits.Since we have the image, how can i construct a binary image based on the most significant bit of the pixels.Specifically , if the MSB of a pixel is 1 , the pixel in the new image will take 255 , while if it is 0 ,the pixel in the new image will take value 0 .Repeat for the second most significant bit and the least significant bit (least significant bit, LSB). I have to implement this in Octave.

 Accepted Answer

For a gray scale image (not color), you can use bitget:
% Compute the bit plane image.
bitPlaneImage = bitget(grayImage, bp+1);
For a full demo, see my attached demo.

More Answers (1)

% Load image from specified file
img1=imread('fractal.jpg');
% Display initial image
figure (1);
imshow(img1);
% Scan through all rows in the image
for y = 1:size(img1, 1)
% Scan col
for x = 1:size(img1, 2)
% img1 - MSB
if img1(y,x) > 127
% Turn one light pixel white
img1(y,x) = 255;
else
% Turn one dark pixel black
img1(y,x) = 0;
end
end
end
% Display image
figure (2);
imshow(img1);

1 Comment

Not sure what that code is for, but you can do the same thing as that double "for" loop, vectorized by doing:
img1 = uint8(255 * img1>127);
Of course thresholding gets far more complicated for the lower bits because you won't have just one range like here, you'll have 2, 4, ... 128 ranges depending on what bit you want to visualize. That's why I recommended bitget() instead of thresholding or dividing.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!