【问题标题】:How to transmit Image to bits and back in MATLAB如何在 MATLAB 中将图像传输到位并返回
【发布时间】:2017-12-14 23:24:24
【问题描述】:

我正在尝试传输一个小的 .jpg 图。我正在使用以下行将图片转换为位:

pic = imread('****.jpg');
x = reshape((dec2bin(typecast(pic(:),'uint8'),8)-'0').',1,[]);

然后,我正在尝试以下方法来重建图像:

n = 250;
m = 250;
s = num2cell(reshape(x,8,[])',2);
b = cellfun(@(x) bin2dec(strrep(num2str(x),' ','')), s);
out = reshape(b,n,m);  

我收到此错误消息:

Error using reshape
To RESHAPE the number of elements must not change.
Error in transmit_pic (line 13)
out = reshape(b,n,m);

我做错了什么?

【问题讨论】:

  • n 没有被你的代码定义,你写了两次 m
  • @Veltz 你是对的。我刚刚更正了代码。

标签: image matlab


【解决方案1】:

经过一番修改后,我找到了一种解决方法。问题在于位深度。

这是正确的代码:

pic = imread('***.png');

x = reshape((dec2bin(pic,8)-'0').',1,[]);
[m, n] = size(pic);

s = num2cell(reshape(x,8,[])',2);
b = cellfun(@(x) bin2dec(strrep(num2str(x),' ','')), s);
out=reshape(b,m,n);  

image(out)

【讨论】:

  • 我猜你的问题是图像是 RGB(即它是 M×N×3),而你没有考虑到第三维。使用[m, n] = size(pic); 行可以为您做到这一点,因为n 将包含所有尾随非单一维度的乘积(即N*3)。更多解释可以找到here
  • @gnovice 你是绝对正确的。谢谢你的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-24
  • 2020-02-19
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
相关资源
最近更新 更多