【问题标题】:how can visualizing binary file like image with matlab?如何用matlab可视化二进制文件,如图像?
【发布时间】:2017-04-11 08:56:29
【问题描述】:

我需要像图像一样可视化二进制文件(例如 .exe)
这段代码将字节打包成 c# 语言的图像:

var width = (int)Math.Sqrt(fi.Length * 8);
width = width + 8 - (width % 8);
var length = (int)(fi.Length * 8 / width);

Func<byte, int, Color> getcolor =
        (b, m) => (b & m) == m ? Color.Black : Color.White;

using (var bitmap = new Bitmap(width, length + 1))
{
    var buffer = File.ReadAllBytes(exefile);

    int x = 0, y = 0;
    foreach (var @byte in buffer)
    {
        bitmap.SetPixel(x + 0, y, getcolor(@byte, 0x80));
        bitmap.SetPixel(x + 1, y, getcolor(@byte, 0x40));
        bitmap.SetPixel(x + 2, y, getcolor(@byte, 0x20));
        bitmap.SetPixel(x + 3, y, getcolor(@byte, 0x10));

        bitmap.SetPixel(x + 4, y, getcolor(@byte, 0x8));
        bitmap.SetPixel(x + 5, y, getcolor(@byte, 0x4));
        bitmap.SetPixel(x + 6, y, getcolor(@byte, 0x2));
        bitmap.SetPixel(x + 7, y, getcolor(@byte, 0x1));

        x += 8;
        if (x >= width)
        {
            x = 0;
            y++;
        }
    }

    bitmap.Save(Path.ChangeExtension(exefile, ".tif"), ImageFormat.Tiff);
}

此代码将二进制文件转换为图像,如下所示:

谁能给我这段代码的 Matlab 实现?

【问题讨论】:

标签: c# matlab image-processing


【解决方案1】:

How can I convert a binary file to another binary representation, like an image

%Matlab 有函数 bitget 可以做你想做的事。然后你需要把所有的位放在一个方阵中。

[f,d]=uigetfile('*.*');
fid=fopen([d,filesep,f],'r');
d = fread(fid,inf,'*uint8'); %load all data as bytes.
fclose(fid);
width = sqrt(length(d)*8);
width = width+8-mod(width,8); %make sure width is a multiple of 8
IM = false(width); %binary matrix
x=1;y=1;
for ct = 1:length(d)
    v=bitget(d(ct),[1:8]);
    IM(x:x+7,y)=v;
    x=x+8;
    if x>width
        x=1;y=y+1;
    end
end
imagesc(IM) %display image

【讨论】:

  • 它的工作,但为什么生成的图像在 c# 和 matlab 中不同?
  • 会不会是旋转了90度?试试这个:imagesc(IM')
  • 如何改变背景和像素颜色?图像背景可能为黑色,像素为白色。
  • 两个选项。更改颜色图,或更改位值。最简单的是 imagesc(~IM) (波浪号是非运算符,所以它翻转位)
猜你喜欢
  • 2013-06-17
  • 2011-08-06
  • 2011-07-13
  • 1970-01-01
  • 2019-09-29
  • 2016-03-04
  • 2011-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多