本博客将存放一些常用的Matlab代码片段,整理成博客,并持续更新,以便写代码可以调用。
Matlab写函数的时候,输入输出个数经常是不固定的,
- narginchk(1,3); %输入参数个数必须是1-3个,否则宝座
- nargoutchk(0,1); %输出参数个数必须是0-2个,否则报错
- % 不同输入个数参数情况
- if nargin == 1
- Var2=2; % 无第二个参数输入,则第二个入参赋值
- end
遥感图像中,经常会有NAN值,显示的时候会有黑边,影响美观
- clear;close all;clc;
- Image = imread('D:\Code\Matlab\遥感图像变化检测\Area\202201121.tif');
- Image(Image<0) = nan;
- him = imshow(Image);
- set(him, 'AlphaData', ~isnan(Image))
有的数据需要显示出更好的效果,需要对数据进行先对数,然后归一化。例如SAR图像散射值大的能达到2^16=65536,直接看图片是一片黑,因此需要调亮暗部细节。
- InImage(InImage < 0) = NaN;
- Image = 10*log10(InImage);
- %% 第一种
- Value_max = max(Image(:));
- Value_min = min(Image(:));
- % 像素归一化
- OutImage = uint8((255/(Value_max-Value_min))*Image); %图像(像素)归一化
也可以用拉伸显示的方式进行显示, 可以参考我的博客《利用matlab实现SAR 图像线性拉伸显示》
https://blog.csdn.net/weixin_41649786/article/details/118404909?spm=1001.2014.3001.5501
经纬度坐标有两种形式保存:一种是度度度(小数形式,例如117.649)形式的数据,一种是度分秒(117-38-57.98东、117-38-57.98E)形式的数据,因此经常需要将两种数据进行转换。
第一种:度度度转化为度分秒,核心:度数=取整,分数=取整((值-度数)*60),秒数=((值-度数)*60-分数)*60
- Str = abs(Str); % 排除负号的影响
- degree = fix(Str); % 度
- minute = fix((Str-degree)*60); % 分
- second = ((Str-degree)*60-minute)*60; % 秒
第二种:度分秒转化为度度度,核心:度分秒=度+分/60+秒/3600=度
- second = str2double(Str(temp1(2)+1:Leng)); %秒"
- minute = str2double(Str(temp1(1)+1:temp1(2)-1)); %分'
- degree = str2double(Str(1:temp1(1)-1)); %度°
- coor = PN*(((second/60)+minute)/60+degree); %经纬度(十进制)
- day1 = datetime(datestr(now,'yyyy-mm-dd'))+caldays(1) % 明天
- day0 = datetime(datestr(now,'yyyy-mm-dd')) % 今天
- day_1 = datetime(datestr(now,'yyyy-mm-dd'))-caldays(1) % 昨天
弹出文件选择框,选择想要的数据
- % 选择需要处理的tif文件
- [FileName,PathName,index] = uigetfile({'*.shp'},'tif文件读取','MultiSelect','on');
- % 判断是或选择
替换图片中的某一种(类)的颜色,可以参考我的博客《利用Matlab替换图片部分颜色》
https://blog.csdn.net/weixin_41649786/article/details/125291724?spm=1001.2014.3001.5501,
- image = imread('测试.JPG'); % 读取图像
- figure('Name','原图')
- imshow(image); % 显示
- R = image(:,:,1); % 红色
- G = image(:,:,2); % 绿色
- B = image(:,:,3); % 蓝色
-
- index = find(R<20 & G<20 & B<20); % 索引,找出被替换颜色的范围
- R(index) = 255; % 红色通道赋值
- G(index) = 255; % 绿色通道赋值
- B(index) = 255; % 蓝色通道赋值
- Out_Image(:,:,1) = R; % 输出图像红色通道
- Out_Image(:,:,2) = G; % 输出图像绿色通道
- Out_Image(:,:,3) = B; % 输出图像蓝色通道
给图片添加地理信息需要用到:GeographicCellsReference、GeographicPostingsReference、MapCellsReference、MapPostingsReference等其中的某一个类(对象),通过创建这些类(对象),来生成包含地理信息的R,代码如下:
- % Read a JPEG image from a file.
- Image = imread('_J6A5783.JPG');
- % 定义图片经纬度范围
- lonlim = [-20 20]; % 经度范围[max min]
- latlim = [-10 10]; % 纬度范围[max min]
- rasterSize = size(Image); % 栅格(图片、数据)大小(行、列)
- R = georefcells(latlim,lonlim,rasterSize,'ColumnsStartFrom','north');
- % R.RowsStartFrom = 'east'
-
- % 生成Geotiff文件
- filename = 'Geo-Hlz' + ".tif";
- geotiffwrite(filename, Image, R)
- % 创建地图并显示数据
- figure('Name','效果图')
- usamap(Image,R)
- geoshow(filename)
例如:经度、纬度、高度以及数量,利用不同颜色表示不同数据;
- close all;clear;clc;
-
- long =[120 121 122 119 118 115 130 129 128 127]; %经度
- lat =[ 60 61 60 59 57 56 55 61 68 64]; %维度
- Hight =[ 12 15 16 17 11 15 15 19 9 21]; %高度
- Num =[ 1 2 5 3 4 6 1 0 8 4]; %数量
-
- figure('Name','3D数据可视化')
- scatter(long,lat,40,Num,'filled');
- ax=gca;
- ax.XDir='reverse';
- %view(-31,14)
- xlabel('经度')
- ylabel('维度')
- zlabel('高程/m')
- cb=colorbar;
- cb.Label.String='人数/人';
-
- figure('Name','4D数据可视化')
- scatter3(long,lat,Hight,40,Num,'filled')
- ax=gca;
- ax.XDir='reverse';
- view(-31,14)
- xlabel('经度')
- ylabel('维度')
- zlabel('高程/m')
- cb=colorbar;
- cb.Label.String='人数/人';