bit plane not stored correctly

69 次查看(过去 30 天)
Kees
Kees 2024-11-10,15:29
评论: Kees 2024-11-15,12:56
hello i'm a bit stuck with this program, the goal is to store different slices of 3 different images into a matrix, this works except for slice 16, since the bitvalues were low i have upgraded them otherwise the program sees them as zeros and the image is black. The bits are changed correcly when i load the image seperate but when i use imshow to display the different slices the 16th ( also 8th) slice is black.
What do i do wrong , i also tried ai but to no avail i hope someone can help me
ps im still a greenhorn at this just started..
this is the program
% Load existing 256x256 8bit grayscale images
image1 = imread('baboon.bmp'); % Ensure '8bit_1.bmp' is in the current directory
image2 = imread('Grayscale-Lena-Image.png'); % Ensure '8bit_2.bmp' is in the current directory
image3 = imread('house 256_ 8bit.ppm');
imshow('house 256_ 8bit.ppm');
% Display histogram of the image
figure;
itemp=image3;
imhist(itemp);
title('Histogram of the Image');
% Check if images are grayscale
if size(image1, 3) ~= 1 || size(image2, 3) ~= 1
error('Images must be grayscale.');
end
% Store the image matrices in a cell array
images = {image1, image2, image3};
% Initialize a 3D matrix to hold the bit planes (16 for 8 bits per image)
bit_planes_3d = zeros(size(image1, 1), size(image1, 2), 16);
% Extract bit planes from the first image (1-8)
for i = 1:8
bit_planes_3d(:, :, i) = bitget(images{1}, i);
end
% Extract bit planes from the second image (9-16)
for i = 1:7
bit_planes_3d(:, :, i + 8) = bitget(images{2}, i);
end
% Extract the 8th bit plane and modify the pixel values for i = 1:r for j = 1:c
% Example modification: set the 8th bit to 1 if the current pixel value is above a
% threshold if itemp(i, j)
% > 128 itemp(i, j) = bitset(itemp(i, j), 8, 1); else itemp(i, j) = bitset(itemp(i, j), 8, 0);
% end end end
for i = 8
it= image3; %read the image
itemp2 = it(:,:,1);
[r,c]= size(itemp2); % get the dimensions of image
s = zeros(r,c,8);
% pre allocate a variable to store 8 bit planes of the image % nb X = zeros(2,3,4)= 2x3x4 matrix/ array
if itemp2(r, c) > 128 itemp2(r, c) = bitset(itemp2(r, c), 8, 1); %bit 8 word op 1 gezet
else itemp2(r, c) = bitset(itemp2(r, c), 8, 0);
bit_planes_3d(:, :, i + 8) = bitget(itemp2, i);
end
end
%Display the bit planes
%Open a new figure window
for i = 1:16
subplot(4, 4, i); % 4 rows, 4 columns for 16 bit planes
imshow(bit_planes_3d(:, :, i));
title(['Bit Plane ' num2str(i)]);
end
  1 个评论
DGM
DGM 2024-11-10,16:56
编辑:DGM 2024-11-10,16:57
I'm not sure where this is going, but this is why slice 16 starts out blank
% Extract bit planes from the second image (9-16)
for i = 1:8 % <-- any reason why you're skipping the MSB?
bit_planes_3d(:, :, i + 8) = bitget(images{2}, i);
end

请先登录,再进行评论。

回答(2 个)

Sameer
Sameer 2024-11-11,10:29
The issue you're facing with the 8th and 16th bit planes being black might be due to a few reasons:
  • The loop for extracting bit planes from the second image should go from 1 to 8, not 1 to 7. This might be why the 16th slice is not being processed correctly.
  • Ensure that the bit plane extraction logic is correctly implemented for all images. The current logic for modifying the 8th bit plane seems to be inside a loop that only runs for i = 8, which might not be correct.
Here’s a revised version of your code with these corrections:
image1 = imread('baboon.bmp');
image2 = imread('Grayscale-Lena-Image.png');
image3 = imread('house 256_ 8bit.ppm');
imshow('house 256_ 8bit.ppm');
% Display histogram of the image
figure;
itemp = image3;
imhist(itemp);
title('Histogram of the Image');
% Check if images are grayscale
if size(image1, 3) ~= 1 || size(image2, 3) ~= 1
error('Images must be grayscale.');
end
% Store the image matrices in a cell array
images = {image1, image2, image3};
% Initialize a 3D matrix to hold the bit planes (16 for 8 bits per image)
bit_planes_3d = zeros(size(image1, 1), size(image1, 2), 16);
% Extract bit planes from the first image (1-8)
for i = 1:8
bit_planes_3d(:, :, i) = bitget(images{1}, i);
end
% Extract bit planes from the second image (9-16)
for i = 1:8
bit_planes_3d(:, :, i + 8) = bitget(images{2}, i);
end
% Modify the 8th bit plane of the third image
itemp2 = image3(:,:,1);
[r, c] = size(itemp2);
for i = 1:r
for j = 1:c
if itemp2(i, j) > 128
itemp2(i, j) = bitset(itemp2(i, j), 8, 1);
else
itemp2(i, j) = bitset(itemp2(i, j), 8, 0);
end
end
end
% Extract the modified 8th bit plane from the third image
bit_planes_3d(:, :, 16) = bitget(itemp2, 8);
% Display the bit planes
figure;
for i = 1:16
subplot(4, 4, i); % 4 rows, 4 columns for 16 bit planes
imshow(bit_planes_3d(:, :, i));
title(['Bit Plane ' num2str(i)]);
end
Hope this helps!
  3 个评论
Sameer
Sameer 2024-11-12,12:55
Could you attach the images so that I can check on my end?
Kees
Kees 2024-11-14,13:33
i attached the images, if i use baboon for the 3rd the problem remains

请先登录,再进行评论。


Kees
Kees 2024-11-12,13:59
i might just found the answer the 3rd picture is ppm this is not supported
  4 个评论
DGM
DGM 2024-11-15,6:47
I'm not sure what part isn't working. This specific objection is regarding the reading of a PPM file. I don't have the file, so I can't say why a particular PPM file might not be read correctly. All I'm saying is that it should be possible.
As to why there's a problem with image3, I'm also not sure, since I thought this problem was about the initialization of the working array from the MSB bitplane of image2. Like I said, I don't know where this is going, so I can't comment on what the processing of image3 is intended to produce.
Kees
Kees 2024-11-15,12:56
ok wel thanx for your input, @ sameer i attached the pictures

请先登录,再进行评论。

产品


版本

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by