MSTAR数据转换成图片

来源:互联网 发布:caffe官网教程 编辑:程序博客网 时间:2024/06/14 19:02
最近做毕业设计需要用到MSTAR数据集,比较尴尬的是拿到的数据集是二进制文件,说明中给了一个地址可以下载到转换工具(https://www.sdms.afrl.af.mil,需翻墙),不过不怎么好用。
README中对MSTAR数据格式说明如下:


FILE FORMAT

Each file is constructed with a prepended, variable-length, 
Phoenix formatted (ASCII) header which contains detailed ground 
truth and sensor information for the specific chip.  Following 
the Phoenix header is the data block.  The data block is written 
in Sun floating point format and is divided into two blocks, a 
magnitude block followed by a phase block.


用记事本任意打开一个文件可以看到文件的PhoenixHeader,中间我们需要的信息是NumberOfColumns和NumberOfRows这两个变量,PhoenixHeader以单独一行“[EndofPhoenixHeader]”结束,后面跟的是模值块与相位块(SAR图像是一个复矩阵),我们需要提取模值块并保存为图片。

下面是代码:

clearReadPath = 'D:\Document\Graduation Project\MSTAR\DAA7B02AA\TARGETS\TEST\15_DEG\T72\SN_S7\';SavePath = 'D:\Document\Graduation Project\MSTAR\DAA7B02AA\TARGETSJPG\TEST\15_DEG\T72\SN_S7\';FileType = '*.017';Files = dir([ReadPath FileType]);NumberOfFiles = length(Files);for i = 1 : NumberOfFiles    FileName = Files(i).name;    NameLength = length(FileName);    FID = fopen([ReadPath FileName],'rb','ieee-be');    ImgColumns = 0;    ImgRows = 0;    while ~feof(FID)                                % 在PhoenixHeader找到图片尺寸大小        Text = fgetl(FID);        if ~isempty(strfind(Text,'NumberOfColumns'))            ImgColumns = str2double(Text(18:end));            Text = fgetl(FID);            ImgRows = str2double(Text(15:end));            break;        end    end    while ~feof(FID)                                 % 跳过PhoenixHeader        Text = fgetl(FID);        if ~isempty(strfind(Text,'[EndofPhoenixHeader]'))            break        end    end    Mag = fread(FID,ImgColumns*ImgRows,'float32','ieee-be');    Img = reshape(Mag,[ImgColumns ImgRows]);    imwrite(uint8(imadjust(Img)*255),[SavePath FileName(1:NameLength-3) 'jpg']); % 调整对比度后保存    fclose(FID);end



更新一下


最近又用到了这个小脚本,不过要自己建文件夹挺麻烦的,把它改了一下,写成了函数形式。当目标文件夹不存在时可以自动建立目标文件夹,并且支持递归建立子文件夹并将生成文件保存至对应的子文件夹。

以下是代码:

function MSTAR2JPG(sourcePath, targetPath)if ~exist(targetPath,'dir')    mkdir(targetPath);endFiles = dir(sourcePath);for i = 1:length(Files)    if Files(i).isdir == 0        FID = fopen([sourcePath '\' Files(i).name],'rb','ieee-be');        while ~feof(FID)                                % 在PhoenixHeader找到图片尺寸大小            Text = fgetl(FID);            if ~isempty(strfind(Text,'NumberOfColumns'))                ImgColumns = str2double(Text(18:end));                Text = fgetl(FID);                ImgRows = str2double(Text(15:end));                break;            end        end        while ~feof(FID)                                 % 跳过PhoenixHeader            Text = fgetl(FID);            if ~isempty(strfind(Text,'[EndofPhoenixHeader]'))                break            end        end        Mag = fread(FID,ImgColumns*ImgRows,'float32','ieee-be');        Img = reshape(Mag,[ImgColumns ImgRows]);        imwrite(uint8(imadjust(Img)*255),[targetPath '\' Files(i).name(1:end-3) 'JPG']); % 调整对比度后保存        clear ImgColumns ImgRows        fclose(FID);    else        if strcmp(Files(i).name,'.') ~= 1 && strcmp(Files(i).name,'..') ~= 1            if ~exist([targetPath '\' Files(i).name],'dir')                mkdir([targetPath '\' Files(i).name]);            end            MSTAR2JPG([sourcePath '\' Files(i).name],[targetPath '\' Files(i).name]);        end    endendend


1 0
原创粉丝点击