Dimension issue when recreating bmp image using imshow()

Hi, I am trying to read a 3*5 24-bit bmp image, divide the channels and recreate the original image from the divided channels. While recreating the image, I used the following code:
reconstruct = red + green + blue;
recons = cast(reconstruct,'uint8');
recon = zeros(width_dec, height_dec, 3,'uint8');
u= 1;
for row = height_dec:-1:1
for column = 1:1:width_dec
recon(row,column,3) = recons(u,:);
fprintf('%d\t',u);
recon(row,column,2) = recons(u+1,:);
fprintf('%d\t',u+1);
recon(row,column,1) = recons(u+2,:);
fprintf('%d\t\n',u+2);
u = u+3;
end
end
% CHECKING THE RECONSTRUCTED IMAGE
figure;
imshow(recon);
When I tried running the code, the dimensions of the reconstructed image changed to 5*5. Actual width of the image is 3 pixels. But in the reconstructed image, the remaining width of 2 pixel is appended with black colour. Why is it happening? Could I get some help regarding this issue?
Thanks in advance.

3 Commenti

Why waste time writing loops for this: just use reshape, possibly with permute if required.
Thank you Stephen. Your tip was helpful. Could you help me with the dimension issue too? Thanks in advance
@aravind telidevara: what sizes are red, green, and blue ?
What size should the output be?

Accedi per commentare.

 Risposta accettata

Jan
Jan il 11 Mag 2018
Modificato: Jan il 11 Mag 2018
I'm sure that replacing the loops by reshape and/or permute would simplify the code (see Stephen's comment). But the actual problem of your code is, that you have swapped the 1st and 2nd dimension of recon:
recon = zeros(width_dec, height_dec, 3,'uint8');
for row = height_dec:-1:1
for column = 1:1:width_dec
recon(row, column, 3) = recons(u,:);
...
end
end
You meant either:
recon(column, row, 3) = recons(u,:);
or alternatively:
recon = zeros(height_dec, width_dec, 3, 'uint8');

Più risposte (0)

Tag

Modificato:

Jan
il 11 Mag 2018

Community Treasure Hunt

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

Start Hunting!

Translated by