convert the binary matrix with correct position
2 views (last 30 days)
Show older comments
I converted 256*256 decimal matrix into binary matrix and i read only 5 bits into my binary matrix
using the code:
% reading original image
orgimg=imread('img.jpg');
% converting rgb to gray
grayimg=rgb2gray(orgimg);
% converting an image into binary image
binimg=de2bi(grayimg,'left-msb');
% removing 3 lsbs from the image
binimg=de2bi(binimg,5);
After converting im getting the result into one matrix like 52716*5 but i want those binary matrix into 256*256 matrix can you plz help me how to do it
thanks in advance.
0 Comments
Answers (1)
Guillaume
on 29 Jan 2019
Your code:
% removing 3 lsbs from the image
binimg=de2bi(binimg,5);
That comment is completely wrong. binimg is already a binary array. You're then treating it as a decimal array and converting that to a new binary array. Since the input is going to be decimal 1 or 0, the rows are always going to be either [1 0 0 0 0] or [0 0 0 0 0].
It's not clear what you're actually trying to do. If you want to remove the 3 least significant bits
binimg = binimg(:, 1:end-3);
This makes bit 4 the least significant and in effect divides orgimg by 8 (no need to convert to binary to do that):
finalimg = floor(orgimg / 8); %much simpler that going through de2bi and bi2de
Alternatively, you may want to set the least 3 significant bits to 0:
binimg(:, end-2:end) = 0;
Again, that can be achieved a lot more simply:
finalimg = bitand(orgimg, bitcmp(3, 'uint8'));
Note that if you want to convert your binary matrix back to the image size, you'll have to use
finalimg = reshape(bi2de(binimg), size(orgimg));
2 Comments
Guillaume
on 30 Jan 2019
You will get this error if you've done your double de2bi conversion or some other silly operation. As I explained, calling bi2de on a binary image is wrong.
The number of rows in binimg must be equal to the number of pixels in your image if you want to get back an image of the same size.
assert(size(binimg, 1) == numel(orgimg), 'You have made a mistake in your algorithm! Cannot get back an image the same size as the original')
finalimg=reshape(bi2de(binimg),size(orgimg));
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!