Matlab讀取帶有地理信息的圖像
目前需要讀取遙感影像并對(duì)像元值進(jìn)行重新計(jì)算后再輸出圖像。
代碼如下:
[A, R]= geotiffread('pathname:\ filename')? ?? ?%得到文件的像元值矩陣A和地理信息R
B= A.* 0.0001? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???%將A進(jìn)行處理,得到所需要的值
geotiffwrite('pathname:\ newfilename', B, R)? ?%重新輸出圖像
但是系統(tǒng)顯示圖片上的錯(cuò)誤。
我看到geotiffwrite參考頁里面的syntax第一行就是這么寫的:geotiffwrite(filename,A,R)。不知道錯(cuò)在哪。


geotiffread
Read GeoTIFF file
Syntax
[A,R] = geotiffread(filename)
[X,cmap,R] = geotiffread(filename)
[A,refmat,bbox] = geotiffread(filename)
[X,cmap,refmat,bbox] = geotiffread(filename)
[___] = geotiffread(filename,idx)
[___] = geotiffread(url,___)
[A,R] = geotiffread(filename)?reads a georeferenced grayscale, RGB, or multispectral image or data grid from the GeoTIFF file specified by?filename?into?A?and creates a spatial referencing object,?R.
MATLAB 遙感影像的讀寫操作,帶空間坐標(biāo)信息!
filepath=''E:\Sciencetific Work\北洛河_狀頭.tif';'
[Data, R] = geotiffread(filepath); ??%讀取帶有空間信息的地理數(shù)據(jù),返回?cái)?shù)據(jù),和地理柵格空間參考信息spatialref.GeoRasterReference

info = geotiffinfo('shanghai_roi_WGS2.tif');???%geotif的信息

geotiffwrite('shanghai_roi_WGS5.tif',Y,R, 'GeoKeyDirectoryTag',
info.GeoTIFFTags.GeoKeyDirectoryTag);??%寫出帶有地理信息的geotif文件
MATLAB中使用Inf和-Inf (infinite)分別代表正無窮量和負(fù)無窮量,NaN表示非數(shù)值量?(Not a Number)
Matlab中提示:Maximum recursion
limit of 500 reached. Warning:Function tic has the same name as MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
解決方法:You accidentally named one of your own files tic.m 重命名即可
Matlab中讀取圖片灰度值:
imread函數(shù):Read image from graphics file
example:??I=imread(‘C:\Users\Administrators\Desktop\beforeclass’);
?????????D=rgb2gray(I);??將彩色圖像轉(zhuǎn)換為灰度圖像
?????????D包含的就是灰度值
imcrop函數(shù):Crop Image 裁剪圖像
I=imread('圖像名字(帶擴(kuò)展名及路徑)');%首先讀入一幅待裁剪的圖像
I2=imcrop(I,[a b c d]);%利用裁剪函數(shù)裁剪圖像,其中,(a,b)表示裁剪后左上角像素在原圖像中的位置;c表示裁剪后圖像的寬,d表示裁剪后圖像的高
關(guān)于矩形區(qū)域[Xmin Ymin Width Height],Xmin是橫向第Xmin個(gè)像素,Ymin是縱向第Ymin個(gè)像素;
imhist函數(shù):顯示區(qū)域的像素值和灰度值的關(guān)系
功能:顯示圖像的直方圖。
輸入數(shù)據(jù)類型可以是無符號(hào)8位、16位、有符號(hào)16位、單精浮點(diǎn)、字符型、邏輯型等數(shù)據(jù)。
用法:??
imhist(I,n)? 計(jì)算和顯示圖像I的直方圖,n為指定的灰度級(jí)數(shù)目,默認(rèn)為256。如果I是二值圖像,那么n僅有兩個(gè)值。
imhist(X,map)? 計(jì)算和顯示索引圖像x的直方圖,map為調(diào)色板。
[counts,x]= imhist(...)? 返回直方圖數(shù)據(jù)向量counts或相應(yīng)的色彩值向量x。
舉例:
? ? ? ? I=imread('rice.tif')
? ?? ???imshow(I)
? ? ? ? figure,imhist(I)
imhist只能對(duì)灰度圖像畫直方圖,所以那樣直接畫是不行的,我給你改了一下,你再試試
L=imread('lily.tif');
L=rgb2gray(L);
subplot(1,2,1);
imhist(L);
This example illustrates how to use impixel to get pixel values.
? ?1.? Display an image.
? ?? ?imshow canoe.tif
? ?2. Call impixel. When called with no input arguments, impixel associates itself with the image in the current axes.
? ?? ?vals = impixel
? ?3. Select the points you
want to examine in the image by clicking the mouse. impixel places a star at
each point you select.
? ?4. When you are finished selecting points, press Return. impixel returns the pixel values in an n-by-3 array, where n is the number of points you selected. The stars used to indicate selected points disappear from the image.
? ?? ?pixel_values =
? ?? ?0.1294? ?0.1294? ? 0.1294
? ?? ?0.5176? ? ? ? 0? ?? ?? ?0
? ? ? 0.7765? ?0.6118? ? 0.4196
1、使用imcrop做圖像剪切,或用矩形塊提取圖像塊區(qū)域;
2、可以使用pixval on(低版本),或impixelinfo;
clc; clear all; close all;
figure;
imshow('rice.png');
impixelinfo
3、使用Figure窗口的“Data cursor”圖標(biāo)來查看;
4、使用imview來查看;
clc; clear all; close all;
imview('rice.png');
strcmp:Compare strings withcase sensitivity
C/C++函數(shù),比較兩個(gè)字符串
設(shè)這兩個(gè)字符串為str1,str2,
若str1==str2,則返回零;
若str1<str2,則返回負(fù)數(shù)
若str1>str2,則返回正數(shù)。
matlab中函數(shù),strcmp(s1,s2) 判斷兩個(gè)字符串s1和s2是否相同,相同返回true ,不同返回false
strcat 即 Strings Catenate,橫向連接字符串。
語法
combinedStr= strcat(s1,s2, ...,sN)
描述
將數(shù)組s1,s2,...,sN水平地連接成單個(gè)字符串,并保存于變量combinedStr中。如果任一參數(shù)是元胞數(shù)組,那么結(jié)果combinedStr?是一個(gè)元胞數(shù)組,否則,combinedStr是一個(gè)字符數(shù)組。
fread函數(shù)可從文件中讀取二進(jìn)制數(shù)據(jù)
A = fread(fid, count)
A = fread(fid, count, precision)
其中fid為指針?biāo)肝募械漠?dāng)前位置,count指讀取的數(shù)據(jù)個(gè)數(shù), precision表示以什么格式的數(shù)據(jù)類型讀取數(shù)據(jù)。
fclose(fid); closes an open file. fileID
is an integer file identifier obtained from fopen
matlab中這兩個(gè)字符串查找的函數(shù)findstr(),
strfind()表明上看起來用法相似,效果也相似。一.
findstr(s1,s2)--在較長的字符串中查找較短的字符串出現(xiàn)的次數(shù),并返回其位置,因此無論s1,s2哪個(gè)為長字符串,位置在前在后都沒有關(guān)系。例:
s = 'Find the starting indices of the shorter string.';
findstr(s, 'the')
ans = 6 30
findstr('the', s)
ans = 6 30
二. strfind(s1,s2)--or strfind(s1,pattern),因此其意思在s1中搜索pattern。
例:
S = 'Find the starting indices of the pattern string';
strfind(S, 'in')
ans = 2 15 19 45
strfind(S, 'In')
ans = []
注意,第一個(gè)函數(shù)findstr函數(shù)中,不能識(shí)別元胞數(shù)組,該函數(shù)只能用于字符串中。而第二個(gè)函數(shù)strfind中,S可以是元胞數(shù)組格式的字符串。通過cell2mat()可以把cell結(jié)構(gòu)轉(zhuǎn)成數(shù)據(jù),findstr(),strfind()函數(shù)只能對(duì)一維字符串?dāng)?shù)據(jù)進(jìn)行操作,若是二維字符串則得使用別的函數(shù)。
在matlab中,無論是內(nèi)建函數(shù)還是工具箱函數(shù),2很常見, 這可能是因?yàn)?英文two和to發(fā)音相同。而2寫起來也比較簡單。 所以很多轉(zhuǎn)換類函數(shù)都用2來命名而非to。
比如string to number, 不是命名為strTonum而是str2num。
Convert string to number
Matlab中讀取txt文件
textread函數(shù):[ID,X,Y,DEPTH]=textread(‘C:\Users\Administrator\Desktop\robertisland1.txt’,’%d%f %f %f’,’headerlines’,1);
headerlines表示開頭跳過幾行(有的文件開頭有說明文字)
如何將MATLAB運(yùn)行結(jié)果寫入txt文件
a = [1,2,3,4,5,6,7,8,9];
fp = fopen('D:\ztq.txt','wt');
for i =1 : 9
???fprintf(fp, '%d', a(i));
end
fclose(fp);
將數(shù)組a導(dǎo)入到D盤中的ztq.txt中。
矩陣的合并:
C=[A B] 在水平方向上合并矩陣A和B
C=[A;B] 在豎直方向上合并A和B
matlab中生成斐波那契數(shù)列:
function a=fib(n)
%
生成長度為n的斐波那契數(shù)列
if n==1
a=1;
elseif n==2
a=[1 1];
else
b=fib(n-1);
a=[b,b(end-1)+b(end)];
end
例子
fib(10)
ans =
???? 1????1???? 2???? 3????5???? 8??? 13???21??? 34??? 55
Matlab中判斷一個(gè)值發(fā)生變化
比如判斷的變量為X。循環(huán)前設(shè)兩個(gè)變量,VarA=0和VarB。在循環(huán)中:VarB=VarA,VarA=X(這兩句順序很重要),然后用if語句判斷VarA是否等于VarB。如果相等說明X和上一個(gè)X相同。
MATLAB顯示輸出數(shù)據(jù)主要有三種方式,一種是直接在命令行輸入數(shù)據(jù)項(xiàng)并且不加“;”符號(hào),另外兩種是利用disp函數(shù)和fprintf函數(shù)實(shí)現(xiàn) 總的來說,使用fprintf最為靈活方便,可以輸出任何格式
Matlab矩陣分割:
索引矩陣的一種特殊形式,不僅可以獲得某個(gè)元素,也可獲得某一行或某一列
I = A(i : j, m : n)?%輸出i到j(luò)行的m到n列
B1=Band(1:1000000;:)
Matlab中把1-100這100個(gè)整數(shù)排成一個(gè)矩陣:
x=1:100;
X=reshape(x,m,n);%m
和n分別為要生成矩陣的行數(shù)和列數(shù)
或者通過linspace函數(shù)創(chuàng)建一維數(shù)組
y = linspace(1,100)
&&和||是短路版本的邏輯與和邏輯或,稱為short-circuit運(yùn)算符。短路邏輯運(yùn)算符的兩邊必須是邏輯標(biāo)量值。比如單個(gè)邏輯變量,邏輯表達(dá)式都可以。
Matlab文件中出現(xiàn)asv格式文件
在MATLAB中編輯某個(gè)文件時(shí),如果較長時(shí)間(系統(tǒng)默認(rèn)是5分鐘,可自行修改)沒有對(duì)已經(jīng)變動(dòng)的文件進(jìn)行保存,那么系統(tǒng)會(huì)自動(dòng)保存,這就是asv文件,即automatically saved file或autosave的意思,只是備份文件。用記事本打開,和.m文件內(nèi)容相同。
Matlab讀取txt文件行數(shù)
越是底層的函數(shù)效率是越高的,只是使用不方便而已。MATLAB有一個(gè)fread函數(shù),不過這是默認(rèn)處理的是二進(jìn)制文件,不過沒有關(guān)系,文本文件只是一個(gè)編碼而已,我們還是可以使用fread進(jìn)行讀取的。
tic;
fid=fopen('data.csv','rt');
row=0;
while ~foef(fid)
? ? ? ? row=row+sum(fread(fid,'*char')==char(10));?
? ? %計(jì)算其中的回車個(gè)數(shù),其中10是回車的ASCII編碼
? ? %'*char'表示每次讀取一個(gè)字符,*表示輸出也是字符
? ? %放心fread現(xiàn)在已經(jīng)可以自動(dòng)識(shí)別中文了,萬一還是識(shí)別不了,
? ? % 請(qǐng)?jiān)趂open中指定文件編碼格式,比如gbk
end;
fclose(fid);
row;
toc;
matlab輸出矩陣
如矩陣a??
a 不加分號(hào)即輸出
disp(a) 即輸出