views:

399

answers:

2

I am particularly stuck in this case

I = imread('liftingbody.png');
S = qtdecomp(I,.27);
blocks = repmat(uint8(0),size(S));
for dim = [512 256 128 64 32 16 8 4 2 1];
    numblocks = length(find(S==dim));
    if (numblocks > 0)
     values = repmat(uint8(1),[dim dim numblocks]);
     values(2:dim,2:dim,:) = 0;
     blocks = qtsetblk(blocks,S,dim,values);
    end
end
blocks(end,1:end) = 1;
blocks(1:end,end) = 1;
imshow(I), figure, imshow(blocks,[])

( The example above is from the MATLAB help )

If I try to write the image i.e blocks using imwrite(blocks) then the whole image appears to be black. This happens for any input images. But I want to write exactly the output that imshow shows here. Can anyone help ?

+6  A: 

You created blocks as a uint8 matrix. By convention, MATLAB and Image Processing Toolbox treat a uint8 grayscale as having a range of values from 0 to 255. That is, 0 is black and 255 is white. So your blocks matrix, which contains only 0s and 1s, would normally be displayed as black and almost-black.

When you displayed blocks using:

imshow(blocks,[])

You used the "auto-ranging" syntax of imshow, which displays the minimum value of blocks as black and the maximum value of blocks as white.

But then when you saved blocks using imwrite, it made the normal assumption of 0 as black and 255 as white.

Try initializing blocks as a logical matrix instead, like this:

blocks = logical(size(S));

MATLAB and Image Processing Toolbox treat a logical matrix as a binary image and will display 0 as black and 1 and white. If you pass a logical matrix to imwrite, it will create a 1-bit-depth binary image file.

Steve Eddins
thanks a lot :D
Tasbeer
A: 

a generic answer is to normalize the image to be in the range for imwrite(blocks):

imwrite((blocks-min(blocks))/(max(blocks)-min(blocks)))
meduz