MSTAR 資料轉格式
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影象是一個復矩陣),我們需要提取模值塊並儲存為圖片。
下面是程式碼:
-
clear
-
ReadPath =
'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)
;
-
end
-
Files = 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
-
end
-
end
-
end