当前位置: 首页 > news >正文

Matlab 根据 shp 裁剪矩阵/图像 函数

关于这个函数,我最近(2020.11.1)又修改了一下,之前的版本第二步通用性不强,这波应该可以通用了。现将几个版本命个名:
2020-8-24 V1.0 有内切圆外切圆,第二步不通用
2020-11-1 V2.0 有内切圆外切圆,第二步通用
2020-11-1 V2.1 无内切圆外切圆,第二步通用

1、全代码

1.1 V1.0

function varargout=tailorsheng(varargin)
%% 此函数用于根据 shp 裁剪矩阵/图像
% 输入:
%       P2file shpfile
%       TDMSPfile tiffile,可以不是tif
%       str  要裁剪谁
%       mode 10国家
% 输出:
%       sheng shp
%       photo mat图像
%       ex    经纬度极值
% 调用:
%       TDMSPfile='D:\复习资料\自学\7_科研\夜光遥感\data\DMSP\F121999.v4\F121999.v4b_web.stable_lights.avg_vis.tif';
%       P2file='D:\下载\Useful\shp\国家基础地理数据\bou2_4m\bou2_4p.shp';%省界多边形
%       str='北京市';
%       mode=1;
%       [sheng,photo,ex]=tailorsheng(P2file,TDMSPfile,str);
%       [sheng,photo,ex]=tailorsheng(P2file,TDMSPfile,str,mode);
%-------------------------------------------------------------------
    %%%%    Authors:   Bill O'Hanlon
    %%%%    EMAIL:     ohanlon@qq.com
    %%%%    DATE:      24-08-2020
%% 输入
disp('-------function tailorsheng------');
tic %开始计时
mode=1;
if nargin==3
    P2file=varargin{1}; %注意,这里是{, (会是元胞
    TDMSPfile=varargin{2};
    str=varargin{3};
elseif nargin==4
    P2file=varargin{1}; %注意,这里是{, (会是元胞
    TDMSPfile=varargin{2};
    str=varargin{3};
    mode=varargin{4};%这里不做变换的话会是元胞
else
    disp('参数不合要求!');
    return;
end
%% 第一步,提取目标省份的 shp,注意看其范围
[henan,ex]=drawsheng(P2file,str,mode);
disp('shp文件搞定!');
%% 第二步,把图像大致范围剪出来,这步根据图像种类不同,代码也不一样,Attention!!
TDMSP=imread(TDMSPfile); %DMSP范围 180°W-180°E,65°S-75°N
Xmax=ex(1);              %X是经度,Y是纬度
Ymax=ex(2);              %这些已经取整了。
Xmin=ex(3);
Ymin=ex(4);
Xmax1=Xmax+180;          %这是裁剪时用的
Ymax1=75-Ymax;
Xmin1=Xmin+180;
Ymin1=75-Ymin;
Henan=TDMSP(Ymax1*120:Ymin1*120,Xmin1*120:Xmax1*120);%根据自己需要裁减
disp('粗裁剪完成!');
%% 第三步,计算不规则边界的内切圆和外接圆
Y=Ymin:0.0083333333:Ymax; %分辨率0.0083333333°  1°=120
X=Xmin:0.0083333333:Xmax; %求逻辑矩阵用到
[a,b]=size(Henan);
Y2=repmat(Y',1,b);
X2=repmat(X,a,1);
Y2=flipud(Y2);                     %这个纬度得上下翻转一下。
xv=henan.X;yv=henan.Y;             %提取边界
xv = xv(1:end-1); yv = yv(1:end-1);%把最后一个NaN去掉
disp('需要计算逻辑的区域为 Figure 2 红色区域');
bianjie=[xv;yv];
[zhongxin1,zhongxin2,smallR,bigR]=getZhongxin(bianjie,X2,Y2);
%% 第四步,根据内切圆和外接圆+边界进行裁剪
disp('开始计算逻辑矩阵..');
photo=zeros(a,b);
photo(:)=nan;
for i=1:a
    for j=1:b
        if mod(i*b+j,10000)==0
            disp([int2str(i*b+j),' / ',int2str(a*b), ' OK!']);
        end
        x=X2(i,j);
        y=Y2(i,j);
        if norm(zhongxin1-[x;y])<=smallR
            photo(i,j)=Henan(i,j);%内接圆里面的都在
            continue;
        elseif norm(zhongxin2-[x;y])>=bigR
            continue;%外接圆外面的都不在
        elseif inpolygon(x,y,xv,yv)
            photo(i,j)=Henan(i,j);
        end
    end
end
disp('细裁剪完成!');
%% 第五步,画图,裁剪后的
[x,x1,y,y1] = getxy(X,Y);
x=(x-Xmin).*120; 
y=(y-Ymin).*120;
photo=flipud(photo);%contourf 上下翻转一下,才变成imshow
figure;
contourf(photo,'LineStyle','none');
colormap(gray);colorbar %jet
set(gca,'XTick',x,'XTicklabel',x1);   %设置x,y轴
set(gca,'YTick',y,'YTicklabel',y1);
title([str,'夜光遥感影像']);
%% 输出
if nargout==3
    varargout{1}=henan; 
    varargout{2}=photo;
    varargout{3}=ex;
elseif nargout==2
    varargout{1}=henan; 
    varargout{2}=photo;
elseif nargout==1
    varargout{1}=henan; 
elseif nargout==0
    return;
else
    disp('参数不合要求!');
    return;
end
disp('--------Finished!--------');
toc  %展示运行时间
end

1.2 V2.0

function varargout=tailorsheng(varargin)
%% 此函数用于根据 shp 裁剪矩阵/图像
% 输入:
%       P2file shpfile
%       TDMSPfile tiffile,可以不是tif
%       str  要裁剪谁
%       mode 1省0国家
% 输出:
%       sheng shp
%       photo mat图像
%       ex    经纬度极值
% 调用:
%       TDMSPfile='D:\复习资料\自学\7_科研\夜光遥感\data\DMSP\F121999.v4\F121999.v4b_web.stable_lights.avg_vis.tif';
%       P2file='D:\下载\Useful\shp\国家基础地理数据\bou2_4m\bou2_4p.shp';%省界多边形
%       str='北京市';
%       mode=1;
%       [sheng,photo,ex]=tailorsheng(P2file,TDMSPfile,str);
%       [sheng,photo,ex]=tailorsheng(P2file,TDMSPfile,str,mode);
%-------------------------------------------------------------------
    %%%%    Authors:   Bill O'Hanlon
    %%%%    EMAIL:     ohanlon@qq.com
    %%%%    DATE:      01-11-2020
%% 输入
disp('-------function tailorsheng------');
tic %开始计时
mode=1;
if nargin==3
    P2file=varargin{1}; %注意,这里是{, (会是元胞
    TDMSPfile=varargin{2};
    str=varargin{3};
elseif nargin==4
    P2file=varargin{1}; %注意,这里是{, (会是元胞
    TDMSPfile=varargin{2};
    str=varargin{3};
    mode=varargin{4};%这里不做变换的话会是元胞
else
    disp('参数不合要求!');
    return;
end
%% 第一步,提取目标省份的 shp,注意看其范围
[henan,ex]=drawsheng(P2file,str,mode);
disp('shp文件搞定!');
%% 第二步,把图像大致范围剪出来,这步根据图像种类不同,代码也不一样,Attention!!
TDMSP=imread(TDMSPfile); %DMSP范围 73.49781527°E-135.0894573°E,3.83544513°N-53.56042524°N
Xmax=ex(1);              %X是经度,Y是纬度
Ymax=ex(2);              %这些已经取整了。
Xmin=ex(3);
Ymin=ex(4);
Info=geotiffinfo(TDMSPfile); %读取图像信息,比如经纬度,分辨率
global PixelScale; %定义了几个全局变量,
global Pixel_;
global XBound;
global YBound;
PixelScale=Info.PixelScale(1,1);
Pixel_=1/PixelScale;
XBound=Info.BoundingBox(1,1);
YBound=Info.BoundingBox(2,2);
[a,b]=size(TDMSP);
if(Xmax>Info.BoundingBox(2,1)) Xmax=Info.BoundingBox(2,1); end
if(Xmin<Info.BoundingBox(1,1)) Xmin=Info.BoundingBox(1,1); end
if(Ymax>Info.BoundingBox(2,2)) Ymax=Info.BoundingBox(2,2); end
if(Ymin<Info.BoundingBox(1,2)) Ymin=Info.BoundingBox(1,2); end
Xmax1=Xmax-XBound;          %这是裁剪时用的
Ymax1=YBound-Ymax;
Xmin1=Xmin-XBound;
Ymin1=YBound-Ymin;
hangs=round(Ymax1*Pixel_); %上界
hangx=round(Ymin1*Pixel_); %下界
liez=round(Xmin1*Pixel_);  %左界
liey=round(Xmax1*Pixel_);  %右
if(hangs<1) hangs=1; end
if(hangx>a) hangx=b;end
if(liez<1) liez=1; end
if(liey>b) liey=b; end

Henan=TDMSP(hangs:hangx,...
    liez:liey);%根据自己需要裁减
sprintf('粗裁剪完成! %d,%d,%d,%d',hangs,hangx,liez,liey);
%% 第三步,计算不规则边界的内切圆和外接圆
Y=Ymin:PixelScale:Ymax; %分辨率0.0083333333°  1°=120
X=Xmin:PixelScale:Xmax; %求逻辑矩阵用到
%[a,b]=size(Henan);
a=size(Y,2);
b=size(X,2);
Y2=repmat(Y',1,b);
X2=repmat(X,a,1);
Y2=flipud(Y2);                     %这个纬度得上下翻转一下。
xv=henan.X;yv=henan.Y;             %提取边界
xv = xv(1:end-1); yv = yv(1:end-1);%把最后一个NaN去掉
disp('需要计算逻辑的区域为 Figure 2 红色区域');
bianjie=[xv;yv];
[zhongxin1,zhongxin2,smallR,bigR]=getZhongxin(bianjie,X2,Y2);
%% 第四步,根据内切圆和外接圆+边界进行裁剪
disp('开始计算逻辑矩阵..');
photo=zeros(a,b);
photo(:)=nan;
for i=1:a
    for j=1:b
        if mod(i*b+j,10000)==0
            disp([int2str(i*b+j),' / ',int2str(a*b), ' OK!']);
        end
        x=X2(i,j);
        y=Y2(i,j);
        if norm(zhongxin1-[x;y])<=smallR
            photo(i,j)=Henan(i,j);%内接圆里面的都在
            continue;
        elseif norm(zhongxin2-[x;y])>=bigR
            continue;%外接圆外面的都不在
        elseif inpolygon(x,y,xv,yv)
            photo(i,j)=Henan(i,j);
        end
    end
end
disp('细裁剪完成!');
%% 第五步,画图,裁剪后的
[x,x1,y,y1] = getxy(X,Y);
x=(x-Xmin).*Pixel_; 
y=(y-Ymin).*Pixel_;
photo=flipud(photo);%contourf 上下翻转一下,才变成imshow
figure;
contourf(photo,'LineStyle','none');
colormap(gray);colorbar %jet
set(gca,'XTick',x,'XTicklabel',x1);   %设置x,y轴
set(gca,'YTick',y,'YTicklabel',y1);
title([str,'夜光遥感影像']);
%% 输出
photo(photo>=0)=1;
if nargout==3
    varargout{1}=henan; 
    varargout{2}=photo;
    varargout{3}=ex;
elseif nargout==2
    varargout{1}=henan; 
    varargout{2}=photo;
elseif nargout==1
    varargout{1}=henan; 
elseif nargout==0
    return;
else
    disp('参数不合要求!');
    return;
end
disp('--------Finished!--------');
toc  %展示运行时间
end

1.3 V2.1

function varargout=tailorsheng(varargin)
%% 此函数用于根据 shp 裁剪矩阵/图像
% 输入:
%       P2file shpfile
%       TDMSPfile tiffile,可以不是tif
%       str  要裁剪谁
%       mode 1省0国家
% 输出:
%       sheng shp
%       photo mat图像
%       ex    经纬度极值
% 调用:
%       TDMSPfile='D:\复习资料\自学\7_科研\夜光遥感\data\DMSP\F121999.v4\F121999.v4b_web.stable_lights.avg_vis.tif';
%       P2file='D:\下载\Useful\shp\国家基础地理数据\bou2_4m\bou2_4p.shp';%省界多边形
%       str='北京市';
%       mode=1;
%       [sheng,photo,ex]=tailorsheng(P2file,TDMSPfile,str);
%       [sheng,photo,ex]=tailorsheng(P2file,TDMSPfile,str,mode);
%-------------------------------------------------------------------
    %%%%    Authors:   Bill O'Hanlon
    %%%%    EMAIL:     ohanlon@qq.com
    %%%%    DATE:      24-08-2020
%% 输入
disp('-------function tailorsheng------');
tic %开始计时
mode=1;
if nargin==3
    P2file=varargin{1}; %注意,这里是{, (会是元胞
    TDMSPfile=varargin{2};
    str=varargin{3};
elseif nargin==4
    P2file=varargin{1}; %注意,这里是{, (会是元胞
    TDMSPfile=varargin{2};
    str=varargin{3};
    mode=varargin{4};%这里不做变换的话会是元胞
else
    disp('参数不合要求!');
    return;
end
%% 第一步,提取目标省份的 shp,注意看其范围
[henan,ex]=drawsheng(P2file,str,mode);
disp('shp文件搞定!');
%% 第二步,把图像大致范围剪出来,这步根据图像种类不同,代码也不一样,Attention!!
TDMSP=imread(TDMSPfile); %DMSP范围 73.49781527°E-135.0894573°E,3.83544513°N-53.56042524°N
Xmax=ex(1);              %X是经度,Y是纬度
Ymax=ex(2);              %这些已经取整了。
Xmin=ex(3);
Ymin=ex(4);
Info=geotiffinfo(TDMSPfile); %读取图像信息,比如经纬度,分辨率
global PixelScale; %定义了几个全局变量,
global Pixel_;
global XBound;
global YBound;
PixelScale=Info.PixelScale(1,1);
Pixel_=1/PixelScale;
XBound=Info.BoundingBox(1,1);
YBound=Info.BoundingBox(2,2);
[a,b]=size(TDMSP);
if(Xmax>Info.BoundingBox(2,1)) Xmax=Info.BoundingBox(2,1); end
if(Xmin<Info.BoundingBox(1,1)) Xmin=Info.BoundingBox(1,1); end
if(Ymax>Info.BoundingBox(2,2)) Ymax=Info.BoundingBox(2,2); end
if(Ymin<Info.BoundingBox(1,2)) Ymin=Info.BoundingBox(1,2); end
Xmax1=Xmax-XBound;          %这是裁剪时用的
Ymax1=YBound-Ymax;
Xmin1=Xmin-XBound;
Ymin1=YBound-Ymin;
hangs=round(Ymax1*Pixel_); %上界
hangx=round(Ymin1*Pixel_); %下界
liez=round(Xmin1*Pixel_);  %左界
liey=round(Xmax1*Pixel_);  %右
if(hangs<1) hangs=1; end
if(hangx>a) hangx=b;end
if(liez<1) liez=1; end
if(liey>b) liey=b; end

Henan=TDMSP(hangs:hangx,...
    liez:liey);%根据自己需要裁减
sprintf('粗裁剪完成! %d,%d,%d,%d',hangs,hangx,liez,liey);
%% 第三步,计算不规则边界的内切圆和外接圆
Y=Ymin:PixelScale:Ymax; %分辨率0.0083333333°  1°=120
X=Xmin:PixelScale:Xmax; %求逻辑矩阵用到
%[a,b]=size(Henan);
a=size(Y,2);
b=size(X,2);
Y2=repmat(Y',1,b);
X2=repmat(X,a,1);
Y2=flipud(Y2);                     %这个纬度得上下翻转一下。
xv=henan.X;yv=henan.Y;             %提取边界
xv = xv(1:end-1); yv = yv(1:end-1);%把最后一个NaN去掉
disp('需要计算逻辑的区域为 Figure 2 红色区域');
bianjie=[xv;yv];
%[zhongxin1,zhongxin2,smallR,bigR]=getZhongxin(bianjie,X2,Y2);
%% 第四步,根据内切圆和外接圆+边界进行裁剪
disp('开始计算逻辑矩阵..');
photo=zeros(a,b);
photo(:)=nan;
for i=1:a
    for j=1:b
        if mod(i*b+j,10000)==0
            disp([int2str(i*b+j),' / ',int2str(a*b), ' OK!']);
        end
        x=X2(i,j);
        y=Y2(i,j);
        if inpolygon(x,y,xv,yv)
            photo(i,j)=Henan(i,j);
        end
    end
end
disp('细裁剪完成!');
%% 第五步,画图,裁剪后的
[x,x1,y,y1] = getxy(X,Y);
x=(x-Xmin).*Pixel_; 
y=(y-Ymin).*Pixel_;
photo=flipud(photo);%contourf 上下翻转一下,才变成imshow
figure;
contourf(photo,'LineStyle','none');
colormap(gray);colorbar %jet
set(gca,'XTick',x,'XTicklabel',x1);   %设置x,y轴
set(gca,'YTick',y,'YTicklabel',y1);
title([str,'夜光遥感影像']);
%% 输出
photo(photo>=0)=1;
if nargout==3
    varargout{1}=henan; 
    varargout{2}=photo;
    varargout{3}=ex;
elseif nargout==2
    varargout{1}=henan; 
    varargout{2}=photo;
elseif nargout==1
    varargout{1}=henan; 
elseif nargout==0
    return;
else
    disp('参数不合要求!');
    return;
end
disp('--------Finished!--------');
toc  %展示运行时间
end

依赖:

内切圆和外接圆 :https://blog.csdn.net/Gou_Hailong/article/details/108206335
扣shp:https://blog.csdn.net/Gou_Hailong/article/details/108209395
缩放矩阵或图像:https://blog.csdn.net/Gou_Hailong/article/details/108206521
画地图注释:https://blog.csdn.net/Gou_Hailong/article/details/108208442

注:这个函数裁剪小点的边界还好,如果边界太大或者图像太大,运行时间会超级长的,这酸爽被我记录在了下面博客中:

https://blog.csdn.net/Gou_Hailong/article/details/108147268

2、调用

调用代码:

clc
clear
TDMSPfile='D:\复习资料\自学\7_科研\夜光遥感\data\DMSP\F121999.v4\F121999.v4b_web.stable_lights.avg_vis.tif';
P2file='D:\下载\Useful\shp\国家基础地理数据\bou2_4m\bou2_4p.shp';%省界多边形
str='北京市';
mode=1;
[sheng,photo,ex]=tailorsheng(P2file,TDMSPfile,str,mode);

结果:

在这里插入图片描述

CSDN 脑子秀逗了,说图片违规,dd,
博客园链接:https://www.cnblogs.com/Gou-Hailong/p/13559163.html

相关文章:

  • 误差理论与平差基础学习笔记(Ⅱ)
  • Linux bash 编程笔记(基础篇)
  • 基于C语言 的实现数学上常用的功能
  • C++ 编程笔记【1】(基础篇)
  • 对各种单位的汇总
  • Linux Vim 编辑器的使用笔记
  • GNSS 常用缩略语汇总
  • Origin 使用笔记
  • 对 VIIRS/NPP 夜光数据的解读
  • matlab 对数组/矩阵 的一些常用操作+如何判断两个含有nan的矩阵是否相等?
  • Matlab 计算年积日
  • Matlab 填补缺失数据
  • Linux 中 awk 与 sed 操作
  • Linux Shell 及常用命令
  • Linux grep之正则表达式
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 【面试系列】之二:关于js原型
  • android图片蒙层
  • Apache的80端口被占用以及访问时报错403
  • canvas 高仿 Apple Watch 表盘
  • Git 使用集
  • Git初体验
  • Java基本数据类型之Number
  • PHP变量
  • Promise面试题,控制异步流程
  • Python_网络编程
  • Python学习笔记 字符串拼接
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • Spring-boot 启动时碰到的错误
  • ViewService——一种保证客户端与服务端同步的方法
  • vue+element后台管理系统,从后端获取路由表,并正常渲染
  • 安装python包到指定虚拟环境
  • 普通函数和构造函数的区别
  • 巧用 TypeScript (一)
  • 驱动程序原理
  • 如何用vue打造一个移动端音乐播放器
  • 入手阿里云新服务器的部署NODE
  • 使用common-codec进行md5加密
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​力扣解法汇总946-验证栈序列
  • ${ }的特别功能
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • .md即markdown文件的基本常用编写语法
  • .net core 6 集成 elasticsearch 并 使用分词器
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .NET 设计模式—简单工厂(Simple Factory Pattern)
  • [ C++ ] STL---仿函数与priority_queue
  • [BIZ] - 1.金融交易系统特点
  • [CareerCup] 14.5 Object Reflection 对象反射