
Display a bit plane
16 ビュー (過去 30 日間)
古いコメントを表示
How can I write a function that displays the b-th bit-plane of an 8-bit grayscale image?
The input image variable imagepath must be the file name of a .bmp, .jpg, or other grayscale image permitted in Matlab , and is allowed to be a path name such as C:\Math535\image.bmp.
BitImage is a binary image, having either the value 0 or the value 1, and is extracted from the original image data and stored in a variable whose type is a double array, with the same size domain X as the input image.
The output image BitImage should be a MXN binary image and displayed using imshow.
%sample code
function BitImage = ShowBitImageGray(imagepath, b)
imagepath=imread('bag.png');
imagepath=double(imagepath);
[~,~] = size(imagepath);
imshow (imagepath/255); title('Original Image');
BitImage=bitget(imagepath,1); figure, imshow(logical(BitImage));title('Bit plane 1');
BitImage=bitget(imagepath,2); figure, imshow(logical(BitImage));title('Bit plane 2');
BitImage=bitget(imagepath,3); figure, imshow(logical(BitImage));title('Bit plane 3');
BitImage=bitget(imagepath,4); figure, imshow(logical(BitImage));title('Bit plane 4');
BitImage=bitget(imagepath,5); figure, imshow(logical(BitImage));title('Bit plane 5');
BitImage=bitget(imagepath,6); figure, imshow(logical(BitImage));title('Bit plane 6');
BitImage=bitget(imagepath,7); figure, imshow(logical(BitImage));title('Bit plane 7');
BitImage=bitget(imagepath,8); figure, imshow(logical(BitImage));title('Bit plane 8');
end
0 件のコメント
回答 (1 件)
DGM
2023 年 12 月 29 日
The given code more or less does what's requested, though it's not generalized.
I'm just going to throw this out there.
I'm claiming liberty to ignore the filename-as-input requirement, as I feel that it is poorly-generalized and promotes disk thrashing and unnecessary requantization as part of an acceptable workflow.
I'm ignoring the uint8-only and grayscale-only requirements, as they're presumptuous artifacts of prevailing IPT conventions and general naivete.
% a test image (probably uint8)
inpict = imread('peppers.png');
% for the sake of testing
% spread the data away from the original uint8 quant levels
inpict = im2double(inpict);
inpict = imgaussfilt(inpict,0.5);
% recast as any particular test class
% MIMT imcast() supports float classes and 8,16,32b signed/unsigned integer classes
inpict = imcast(inpict,'int32'); % MIMT-only
% get this bitplane image
outpict = getbitplane(inpict,5); % logical-class
% display the result
% IPT imshow() does not support valid RGB logical or signed integer images
% MIMT imshow2() does
imshow2(outpict)
function outpict = getbitplane(inpict,thisbit)
% OUTPICT = GETBITPLANE(INPICT,BIT)
% Get a logical image representing the specified bitplane from a logical image.
%
% INPICT is a logical or integer-class image of any number of channels.
% BIT specifies the bitplane (integer scalar between 1 and the width of the integer class)
%
% OUTPICT is a logical image of the same size as INPICT.
% Be aware that imshow() will not display correct logical images unless
% they are strictly 2D.
thisclass = class(inpict);
switch thisclass
case 'logical'
nbits = 1;
case {'single','double'}
error('Image must be logical or integer numeric class')
otherwise
nbits = str2double(regexp(thisclass,'\d*','match'));
end
if thisbit < 1 || thisbit > nbits
error('Specified bitplane must be between 1 and %d',nbits)
end
outpict = bitget(inpict,thisbit);
outpict = logical(outpict);
end

As noted, this uses MIMT tools for sake of demonstration, though they're not necessary for the conversion itself.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!