convert the binary matrix with correct position

조회 수: 2 (최근 30일)
niranjan v
niranjan v 2019년 1월 29일
댓글: Guillaume 2019년 1월 30일
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.

답변 (1개)

Guillaume
Guillaume 2019년 1월 29일
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
niranjan v
niranjan v 2019년 1월 30일
thank you so much sir,
but
finalimg = reshape(bi2de(binimg), size(orgimg));
after i executed this line i got this error
Error using reshape
To RESHAPE the number of elements must not change.
Error in pro (line 11)
finalimg=reshape(bi2de(binimg),size(orgimg));
Guillaume
Guillaume 2019년 1월 30일
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));

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by