需求:需要对tiff格式的sar遥感图像进行检测,因此通过matlab进行预处理,将tiff格式转化为jpg格式,并且提取其中的经纬度信息到txt中。最后将检测完毕的图像写回遥感图像。
实现:利用matlab完成
1.tiffToJPG.m 作用:将tiff转为jpg并提取信息到txt
%This function is used to extract remote sensing image information into a
%text file,and get a jpg type img from tiff remote sensing image.
%author:maqy 2017.09.05
%param:
% geoTiffpath:the path of geotiff img.
% jpgOutPath: the path of output jpg img.
% txtInfoOutPath:the path of output txt file.
function tiffToJPG(geoTiffPath,jpgOutPath,txtInfoOutPath)
%filename='/home/hadoop/DocumentMaqy/test.tiff'; the path of geotiff img.
%outpath='/home/hadoop/DocumentMaqy/a.jpg'; the path of output jpg img.
%'/home/hadoop/DocumentMaqy/a.txt' the path of output txt file.
filename=geoTiffPath;
out=jpgOutPath;
txtpath=txtInfoOutPath;
[I,R]=geotiffread(filename);%I is the normal img and R is the geo information.
info=geotiffinfo(filename);
I=uint8(I);%The default format of the photo is 16 bit, translate it to 8 bit.
imwrite(I,out,'jpg');
fid = fopen(txtpath,'w');
%fprintf(fid,'%s\n',info.FileModDate);
fprintf(fid,'%f\n',info.CornerCoords.Lat);
fprintf(fid,'%f\n',info.CornerCoords.Lon);
fclose(fid);
2.tiffToJPGDir.m 作用:一次处理一个文件夹
%Convert remote sensing images in a folder.
%author:maqy 2017.09.06
%param:
% geoTiffpath:the path of geotiff img.
% jpgOutPath: the path of output jpg img.
% txtInfoOutPath:the path of output txt file.
function tiffToJPGDir(geoTiffPath,jpgOutPath,txtInfoOutPath)
Files = dir(fullfile(geoTiffPath,'*.tiff'));
LengthFiles = length(Files);
for i = 1:LengthFiles
filename = strcat(geoTiffPath,Files(i).name);
tmp=Files(i).name;% such as 1.tiff
tmp(end-4:end)=[];% get name without format '.tiff'
out=strcat(tmp,'.jpg');% such as 1.jpg
out=strcat(jpgOutPath,out);% concat /jpgOutPath/1.jpg
txtpath=strcat(tmp,'_info.txt');% get 1_info.txt
txtpath=strcat(txtInfoOutPath,txtpath);% get /txtInfoOutPath/1_info.txt
[I,R]=geotiffread(filename);
info=geotiffinfo(filename);
I=uint8(I);
imwrite(I,out,'jpg');
fid = fopen(txtpath,'w');
%fprintf(fid,'%s\n',info.FileModDate); this line used to
%output Date information to txt
fprintf(fid,'%f\n',info.CornerCoords.Lat);
fprintf(fid,'%f\n',info.CornerCoords.Lon);
fclose(fid);
end
3.writeBackToTiff.m 作用:将检测完毕的jpg图像写回tiff
%write the detected ship jpg img back to tiff
%author:maqy 2017.09.07
%param:
% geoTiffInPath:the path of geotiff img.
% detectedImgPath: the detected ship img
% geoTiffOutPath:the path of output geotiff file.
function writeBackToTiff(geoTiffInPath,detectedImgPath,geoTiffOutPath)
filename=geoTiffInPath;
out=geoTiffOutPath;
[~,R]=geotiffread(filename);
info=geotiffinfo(filename);
img=imread(detectedImgPath);
%imshow(T);
img_size=size(img);
dimension=numel(img_size);
if dimension>2
img=rgb2gray(img);
end
geotiffwrite(out, img, R,'GeoKeyDirectoryTag',info.GeoTIFFTags.GeoKeyDirectoryTag);
4.writeBackToTiffTrans.m 作用:在3的基础上,去除黑边,并修改一下存储的文件信息
%write the detected ship jpg img back to tiff,and remove the black edges in
%the tif.
%author:maqy 2017.10.07
%param:
% geoTiffInPath:the path of geotiff img.
% detectedImgPath: the detected ship img
% geoTiffOutPath:the path of output geotiff file. attention: this param
% can't end with '.tiff'.
function writeBackToTiffTrans(geoTiffInPath,detectedImgPath,geoTiffOutPath)
filename=geoTiffInPath;
out=geoTiffOutPath;
[I,R]=geotiffread(filename);%get tiff img
info=geotiffinfo(filename);
%test
%info.GeoTIFFTags.GeoKeyDirectoryTag.GTCitationGeoKey='GCS_WGS_1984';
%info.GeoTIFFTags.GeoKeyDirectoryTag.GeogCitationGeoKey='';
s1=info.GeoTIFFTags.GeoKeyDirectoryTag;
s2=struct('GTModelTypeGeoKey',s1.GTModelTypeGeoKey,...
'GTRasterTypeGeoKey',s1.GTRasterTypeGeoKey,...
'GeographicTypeGeoKey',s1.GeographicTypeGeoKey,...
'GTCitationGeoKey','WGS84 / Google Mercator',...
'GeogEllipsoidGeoKey',s1.GeogEllipsoidGeoKey,...
'GeogSemiMajorAxisGeoKey',s1.GeogSemiMajorAxisGeoKey,...
'GeogSemiMinorAxisGeoKey',s1.GeogSemiMinorAxisGeoKey,...
'ProjectedCSTypeGeoKey',s1.ProjectedCSTypeGeoKey,...
'ProjectionGeoKey',s1.ProjectionGeoKey,...
'ProjCoordTransGeoKey',s1.ProjCoordTransGeoKey,...
'ProjLinearUnitsGeoKey',s1.ProjLinearUnitsGeoKey,...
'ProjNatOriginLongGeoKey',s1.ProjNatOriginLongGeoKey,...
'ProjNatOriginLatGeoKey',s1.ProjNatOriginLatGeoKey,...
'ProjFalseEastingGeoKey',s1.ProjFalseEastingGeoKey,...
'ProjFalseNorthingGeoKey',s1.ProjFalseNorthingGeoKey,...
'ProjScaleAtNatOriginGeoKey',s1.ProjScaleAtNatOriginGeoKey);
img=imread(detectedImgPath); %get detected img
%if the img has more than one channel, convert to gray.
img_size=size(img);
dimension=numel(img_size);
if dimension>2
img=rgb2gray(img);
end
%get the Alpha matrix
[dim1,dim2]=size(I);
Alpha=255*ones(dim1,dim2);
for i=1:dim1
for j=1:dim2
if(I(i,j)==0)
Alpha(i,j)=0;
end
end
end
%imshow(T);
%set result
result=ones(dim1,dim2,2);
result(:,:,1)=img(:,:,1);
result(:,:,2)=Alpha(:,:,1);
result=uint8(result);
%geotiffwrite(out, result, R,'GeoKeyDirectoryTag',info.GeoTIFFTags.GeoKeyDirectoryTag);
geotiffwrite(out, result, R,'GeoKeyDirectoryTag',s2);
PS:可以将其打成jar包利用java来调用,参考:
https://blog.csdn.net/cs_fang_dn/article/details/50239115