Converting image to bits and vice versa

48 ビュー (過去 30 日間)
kaano1
kaano1 2020 年 5 月 20 日
編集済み: Walter Roberson 2022 年 1 月 19 日
Hi all. I am designing convolutional encoder by using Matlab. To see the result clearly, I will use some pictures. I need to convert the picture pixels into binary code and after I encode it, I need to convert these binary codes into pixels and plot it. Can you help me for convertion part?
  1 件のコメント
Rik
Rik 2020 年 5 月 20 日
If you are planning to store them in 8 bit bytes you can use the uint8 data type and use the normal image displaying tools.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2020 年 5 月 20 日
bits = reshape((dec2bin(typecast(YourImage, 'uint8'), 8) - '0').', 1, []);
orig_class = class(YourImage);
orig_size = size(YourImage);
reconstructed = reshape(typecast(uint8(bin2dec(char(reshape(bits, 8, [])+'0').')), orig_class), orig_size);
In the special case that you are willing to restrict your images to be uint8, you can simplify these steps.
Unless you are willing to restrict yourself to one fixed image size, you need to convey the original image size to the remote end somehow.
  2 件のコメント
Andy Paulo Ureña
Andy Paulo Ureña 2022 年 1 月 18 日
Hello Walter, can you help me with the error that im having with your code ? I just put the variable that store the image that i want to convert. Thanks
Error using typecast
The first input argument must be a vector.
Error in imageprocessing (line 37)
bits = reshape((dec2bin(typecast(img, 'uint8'), 8) - '0').', 1, []);
Walter Roberson
Walter Roberson 2022 年 1 月 18 日
編集済み: Walter Roberson 2022 年 1 月 19 日
YourImage = imread('peppers.png');
imshow(YourImage); title('original');
bits = reshape((dec2bin(typecast(YourImage(:), 'uint8'), 8) - '0').', 1, []);
orig_class = class(YourImage);
orig_size = size(YourImage);
reconstructed = reshape(typecast(uint8(bin2dec(char(reshape(bits, 8, [])+'0').')), orig_class), orig_size);
imshow(reconstructed); title('reconstructed')
max(abs(double(YourImage(:)) - double(reconstructed(:))))
ans = 0

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by