Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MATLAB bitplane extraction problem

7 views
Skip to first unread message

Erry Sutisna

unread,
Sep 2, 2010, 4:15:42 PM9/2/10
to
Hi guys,

Currently I'm doing a project about bit plane extraction and I'm told
to display all 8-bit plane images from a given gray scale image. I'm
given a [448 x 188] matrix with values from 0 to 255.

I want to convert the decimal to an 8 bit binary, so that if the
number is 255 I get 11111111 and so on. I did that using binary =
dec2bin(mymatrix,8). When I did this I get all my binaries that I
wanted but the result turn out to be in string of size 84228x8 char.
Is there a way for me to convert the string into a matrix of size 448
x 118 while preserving all the binaries? I tried to use
str2num(binary) but when I did this I lost a quite a lot of
information. For example if I user str2num for the character
00000101, I only get 101 yet I actually need the 0s for the bit plane
extraction.

Secondly, if I were able to put all the binaries into a 448 x 188
matrix, I have to be able to separate the 8 bits into 8 matrices. For
example, lets say binary matrix is [11111111, 0010000, 00001011] I
want to use this matrix to make 8 other matrices so that matrix 1 =
[1,0,0] matrix 2 = [1,0,0], matrix 3 =[1,1,0] matrix 4 = [1,0,1] -----
matrix 8 = [1,0,1]. Notice how matrix 1 corresponds to the LSB of
column 1, 2,3 from the binary matrix. Do you guys have any tips on how
to do this efficiently?

I really appreciate your help!
Erry

Walter Roberson

unread,
Sep 2, 2010, 4:24:22 PM9/2/10
to
On 10-09-02 03:15 PM, Erry Sutisna wrote:

> Currently I'm doing a project about bit plane extraction and I'm told
> to display all 8-bit plane images from a given gray scale image. I'm
> given a [448 x 188] matrix with values from 0 to 255.
>
> I want to convert the decimal to an 8 bit binary, so that if the
> number is 255 I get 11111111 and so on. I did that using binary =
> dec2bin(mymatrix,8). When I did this I get all my binaries that I
> wanted but the result turn out to be in string of size 84228x8 char.
> Is there a way for me to convert the string into a matrix of size 448
> x 118 while preserving all the binaries?

No.

> I tried to use
> str2num(binary) but when I did this I lost a quite a lot of
> information. For example if I user str2num for the character
> 00000101, I only get 101 yet I actually need the 0s for the bit plane
> extraction.

If you subtract '0' (the quoted character of the digit 0) from your string
array, you will get a 84228 x 8 numeric array. You can then reshape that to be
448 x 188 x 8 .

> Secondly, if I were able to put all the binaries into a 448 x 188
> matrix, I have to be able to separate the 8 bits into 8 matrices. For
> example, lets say binary matrix is [11111111, 0010000, 00001011] I
> want to use this matrix to make 8 other matrices so that matrix 1 =
> [1,0,0] matrix 2 = [1,0,0], matrix 3 =[1,1,0] matrix 4 = [1,0,1] -----
> matrix 8 = [1,0,1]. Notice how matrix 1 corresponds to the LSB of
> column 1, 2,3 from the binary matrix. Do you guys have any tips on how
> to do this efficiently?

After the transformation I described above,

matrix1 = TheNumericMatrix(:,:,1);
matrix2 = TheNumericMatrix(:,:,2);

and so on.

But unless you have a good reason to do otherwise, you probably do not need to
split them out into different variables. For example,

imagesc(TheNumericMatrix(:,:,5))

would display the 5th bit plane without needing a different variable for it.

David Young

unread,
Sep 2, 2010, 4:29:21 PM9/2/10
to
I think you can avoid use strings entirely. See the function bitget, which if I've understood you correctly, will do all the work for you, with one function call for each plane.

Erry Sutisna

unread,
Sep 2, 2010, 11:09:06 PM9/2/10
to

I'm sorry but what do you mean by subtracting the string array by 0?
For example if I have a simple code such as this:
a = [5 120; 255 50];
b = dec2bin(a,8);
c = str2num(b);
Do i do c = c-0;?
When I do that I get an array filled with the number of 48 and 49
instead of 0 and 1.

Steven_Lord

unread,
Sep 3, 2010, 9:59:57 AM9/3/10
to

"Erry Sutisna" <erry...@gmail.com> wrote in message
news:8b8b0333-0ccd-489f...@u5g2000prn.googlegroups.com...


> On Sep 2, 1:24 pm, Walter Roberson <rober...@hushmail.com> wrote:
>> On 10-09-02 03:15 PM, Erry Sutisna wrote:

*snip*

> I'm sorry but what do you mean by subtracting the string array by 0?

That's not what Walter told you to do.

> For example if I have a simple code such as this:
> a = [5 120; 255 50];
> b = dec2bin(a,8);
> c = str2num(b);
> Do i do c = c-0;?
> When I do that I get an array filled with the number of 48 and 49
> instead of 0 and 1.

Compare x1 and x2:

x = '101010101'
x1 = x - 0
x2 = x - '0'

As a hint to why this works, consider that the ASCII value of the characters
'0' and '1' are 48 and 49 respectively.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Jan Simon

unread,
Sep 3, 2010, 11:38:14 AM9/3/10
to
Dear Erry,

I'd avoid to convert numbers to strings and back to numbers again.
The already mentioned BITGET is more direct. BITGET get extract either a vector of bits from a scalar or a single bit from a vector of values. But you need a vector of bits from a vector of values.

The method used inside DEC2BIN (open the function in the editor...) extracts the bits using REM(X / 2^bit, 2). For your case:
x = floor(rand(448, 188) * 255); % Test data
bitsv = rem(floor(x(:) ./ 2.^[7:-1:0]), 2);
bits = reshape(bitsv, 448, 188, 8);

Kind regards, Jan

0 new messages