1. 程式人生 > >MATLAB影象處理(包括影象型別轉換)

MATLAB影象處理(包括影象型別轉換)

Matlab的常見問題

===================================

1).Matlab 6.X在Windows 2000/XP上無法啟動

2).我有一組x,y,z值,非規則排列,如何在Matlab中繪圖?

3).如何在給定控制代碼的axis裡繪圖?

4).由Matlab符號運算得到的公式怎麼才能將資料代進去運算?

5).在Matlab中如何求最值點?如何求一維陣列的極值?

6).Matlab中如何作線性擬合/線性迴歸/多元線性迴歸?

7).Matlab中如何作圓迴歸?

8).Matlab中如何繪製箭頭?

9).Matlab中如何作二維資料的插值?

10).Matlab中如何繪製三維資料陣?

11).Matlab中如何註解一大段程式碼?

12).Matlab中如何計算程式執行的時間?

13).Matlab中如何改變預設的工作路徑?

14).Matlab如何改變預設的圖形字型?

15).如何在Matlab中實現互動操作?

16).Matlab中為什麼只能在小數點後顯示四位?

17).Matlab如何在命令視窗按照格式輸出?

18).如何在Matlab中畫隱函式曲線?

19).Matlab中什麼函式可以刪除矩陣的某一行或列?

20).Matlab中能開的最大陣列是由什麼決定的?

21).如何在Matlab中新增新的工具箱?

22).如何讀寫Matlab的.mat檔案?

23).如何得到contour線上的座標點?

24).如何將Matlab繪製的三維網格圖帖到word裡?

25).請問可以檢視Matlab中函式的原始碼嗎?

26).Matlab有沒有求矩陣行數/列數/維數的函式?

27).Matlab中如何中斷運算?

28).Matlab中有沒有畫圓或橢圓的函式?

29).Matlab下如何定義整形

30).Matlab如何產生均勻分佈的白噪聲?

31).在Matlab中debug的時候能否跟蹤變數的?

32).請問在Matlab中怎樣輸入特殊符號啊或者上標、下標?

33).Matlab中如何後臺執行一個DOS程式?

34).Matlab如何載入輸入檔案(批處理模式). ?

35).Matlab如何啟動時執行規定的檔案?

36).如何在Matlab GUI中使用圖形背景?

37).大量資料點Matlab繪圖為什麼很慢?

38).Matlab中如何求解廣義積分?即積分限到有無窮的或者有歧

異點的積分(瑕積分)?

39).為什麼我的Matlab程式這麼慢?

40).Matlab中如何作線性擬合/線性迴歸/多元線性迴歸?

>*****************************************************************************<

Matlab的常見問題

>*****************************************************************************<

===================================

1)Matlab 6.X在Windows 2000/XP上無法啟動

:#highsun,2001/3/2, SMTH/NewSoftware #

其實也出來很久了,不知大家有沒有注意到.

雖然是針對繁體中文系統的,我試過在簡體

中文系統下一樣可以用。

Solution Number: 26990

Date Last Modified: 2001-01-30

Product: MATLAB 6.0 ==> Current Version

Platform: Windows

Problem Description

Why do I encounter problems when running MATLAB 6.0 (R12) on Hebrew

or

Traditional Chinese (Taiwan) Windows? I try to start MATLAB but after

the splash screen disappears, MATLAB exits.

PLEASE NOTE: This solution only applies to MATLAB 6.0. If you have a

similar problem with MATLAB 5.0 or the Student Edition of MATLAB 5.0,

see solution 7213.

Solution:

This problem is caused by a bug in one of the font properties files we ship

with MATLAB. The font.properties file is used by Java to map the standard

Java font names to system fonts for a particular language operating system.

However, we made a few assumptions that do not hold for the Hebrew or

Traditional Chinese Windows, causing this problem.

We have created a fixed version of the mwt.jar file that you can use to

correct this. To use the fix, first rename your mwt.jar file as mwt.old.

This file is found in the $MATLAB\java\jar directory, where $MATLAB is your

MATLAB root directory. Then download the newer mwt.jar file from:

and place it in your $MATLAB\java\jar directrory. Then restart MATLAB;

this should correct the problem you're seeing.

2)我有一組x,y,z值,非規則排列,如何在Matlab中繪圖?

:#FangQ([email protected]),2002/6/12, BigGreen/MathTools #

參見第一節問題7)

3)如何在給定控制代碼的axis裡繪圖?

:#FangQ([email protected]),2002/6/12, SMTH/MathTools #

plot(data,'parent',haxis);

或者

hbar=bar(data);

set(hbar,'parent',haxis);

4)由Matlab符號運算得到的公式怎麼才能將資料代進去運算?

:#ramjet (德芙)2002/3/3, SMTH/MathTools #

使用subs(),或先將值賦予一個符號變數,然後用eval()

5)在Matlab中如何求最值點?如何求一維陣列的極值?

:#FangQ([email protected]),2002/6/18, SMTH/MathTools#

最值:

一維或多維陣列最值用max(data()

如果想返回最值所在的位置,用[Y,I]=max(data)

:#FangQ([email protected]), 2001/4/21,UESTC/Math#

極值:

data是你的資料,

find(diff(sign(diff(data)))==-2)+1

找到極大值的位置

find(diff(sign(diff(data)))==2)+1

找到極小值的位置

data(find(diff(sign(diff(data)))==-2)+1)和

data(find(diff(sign(diff(data)))==2)+1)

返回的是極大值和極小值

6)Matlab中如何作線性擬合/線性迴歸/多元線性迴歸?

:#FangQ([email protected]),2002/6/21, BigGreen/MathTools #

即用y=a*x+b來擬合一組資料{{x1,y1},{x2,y2}…{xn,yn}}

matlab中使用polyfit

x=data(:,1);

y=data(:,2);

p=polyfit(x,y,1);

p(1)為斜率a,p(2)為截距b

多元線性迴歸即用y=a1*x1+a2*x2+..+am*xm來擬合數據點{x1i,x2i,…xmi,yi}

(i=1~n)

|x11,x21,…xm1|

A=|x12,x22,…xm2|

|…………… |

|x1n,x2n,…xmn|

Y={y1,y2,y3,…,yn}'

則係數{a1,a2,…,am}'=pinv(A)*Y

在matlab中使用

coeff=A\Y

則可以得到最小二乘意義上的擬合係數

7)Matlab中如何作圓迴歸?

:#Peter Boettcher ([email protected]),2002/5/16, comp.soft-sys.matlab#

Q5.5: How can I fit a circle to a set of XY data?

=================================================

An elegant chunk of code to perform least-squares circle fitting was

written by Bucher Izhak and has been floating around the newgroup for

some time. The first reference to it that I can find is in:

function [xc,yc,R,a] = circfit(x,y)

%CIRCFIT Fits a circle in x,y plane

%

% [XC, YC, R, A] = CIRCFIT(X,Y)

% Result is center point (yc,xc) and radius R.A is an optional

% output describing the circle's equation:

%

% x^2+y^2+a(1)*x+a(2)*y+a(3)=0

% by Bucher izhak 25/oct/1991

n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;

A=[sum(x) sum(y) n;sum(xy) sum(yy) sum(y);sum(xx) sum(xy) sum(x)];

B=[-sum(xx+yy) ; -sum(xx.*y+yy.*y) ; -sum(xx.*x+xy.*y)];

a=A\B;

xc = -.5*a(1);

yc = -.5*a(2);

R = sqrt((a(1)^2+a(2)^2)/4-a(3));

Tom Davis provided a more sophisticated approach that works for more

cases in and Code included.

8)Matlab中如何繪製箭頭?

:#FangQ([email protected]),2002/6/21, SMTH/MathTools #

2-D Plotting and Graphics中查詢arrow.m,或者

9)Matlab中如何作二維資料的插值?

:#FangQ([email protected]),2002/6/21, BigGreen/MathTools #

對於一維、二維、三維規則資料點陣使用interp1/interp2/interp3,

二維、三維非規則資料用griddata/griddata3

10)Matlab中如何繪製三維資料陣?

:#FangQ([email protected]),2002/6/21, BigGreen/MathTools #

如果使用matlab,開啟幫助視窗,在目錄樹上找到

MATLAB\Using Matlab\3-D Visualization: Volume Visualization Techniques

如果圖形複雜,建議使用Tecplot,參見Tecplot手冊中資料格式,將你

的三維資料讀入Tecplot,雙擊zone,可以設定mesh/contour/surface

transparency等。

在Field選單中有3D Iso-surface Details和3D Slice Details,可以繪製等值

面和任意平面的截面圖。

11)Matlab中如何註解一大段程式碼?

:#misc,2002/6/21, SMTH/MathTools #

if(0)

大段的程式碼

end

12)Matlab中如何計算程式執行的時間?

:#misc,2002/6/21, SMTH/MathTools #

tic

your_code;

toc

或者使用

t=cputime;

your_operation;

cputime-t

13)Matlab中如何改變預設的工作路徑?

:#SindyGong, 2002/4/7, SMTH/MathTools #

編輯一個startup.m檔案,其中cd yourpath

或者在X:\matlab\toolbox\local\matlabrc.m的最後新增cd yourpath

參見:

14)Matlab如何改變預設的圖形字型?

:#comp.soft-sys.matlab FAQ#

編輯一個startup.m檔案,其中

set(0,'DefaultObjectnamePropertyName',value)

或者在X:\matlab\toolbox\local\matlabrc.m的最後新增

set(0,'DefaultObjectnamePropertyName',value)

15)如何在Matlab中實現互動操作?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

如果只在命令視窗進行互動操作,請參見demo中的例子,主要是

通過input命令和pause/clear/disp等實現的,還有一些視窗資源可以使

用:

uigetfile,uiputfile,uiwait,uisetcolor,uisetfont, uiopen,uisave

inputdlg,msgbox,helpdlg,questdlg,warndlg,errordlg

16)Matlab中為什麼只能在小數點後顯示四位?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

用format命令來改變命令視窗數字的顯示格式和精度,但不會影

響matlab的計算精度,matlab的矩陣運算預設都是雙精度浮點型運算。

17)Matlab如何在命令視窗按照格式輸出?

:#FangQ([email protected]),2002/6/21,SMTHTools #

fprintf(1,"your_format_string",var1,var2,…);

18)如何在Matlab中畫隱函式曲線?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

查詢implicit,會找到一個Arthur Jutan寫的implot.m

Mathematica中繪製隱函式用ImplicitPlot[]

或者ImplicitPlot3D[]

Maple中為implicitplot(),implicitplot3d()

參見

Plot3D.htm

19)Matlab中什麼函式可以刪除矩陣的某一行或列?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

A(j,=[]; %刪除A的第j行

A(:,i)=[]; %刪除A的第i列

20)Matlab中能開的最大陣列是由什麼決定的?

:# chenft (mike),2002/6/1, SMTH/MathTools #

I have had similar problems. Below is an explanation I received from Ian

Boyd

from Mathworks (just giving credit where credit is due) that explains

what's happening. You solution is to run matlab with the -nojvm mode.

"The heap memory system in J***A consists of data and handle elements.

When you allocate a variable you get a handle and data. As long as data

has an

associated handle, the JVM considers it valid and will not clean it up.

However, when you call the clear function in MATLAB, all handles are

destroyed, and the data associated is now invalid. This means that the

J***A

engine can free up that data (garbage collection), but does not mean

that it will clean it up at that moment.

Calling the PACK command encourages J***A to run the garbage collector

and de-fragment the memory. But it does not force it to (This is part

of the J***A design). Even though the memory is 'freed' on the heap,

it is not actually free to the OS, it is only free to the JVM. Here

is one way to think of it:

[MATLAB]

[J***A]

[OS]

MATLAB runs on J***A (virtual machine), and Java runs on the OS (physical

machine). So when MATLAB is running in J***A mode memory allocations

are requested from the JRE, not the OS.

One problem you may be running into is that the default maximum J***A heap

size is relatively low ( <= 64 M, so that is all the memory one session

of MATLAB will ever get on your system.

The good news is that you can increase this value. You will need to create

a java.opts file in $MATLAB/bin/$ARCH (or in the current directory when

you

start MATLA and put the following command:

%%%BEGIN CODE%%%

maxHeapSize = 268435456

%%%END CODE%%%

This will give you 256MB of JVM memory and you can adjust the parameter

as needed.

Note: $MATLAB is the root directory and $ARCH is your system

architecture. This solution works on Windows as well as Solaris, Linux,

Alpha, and SGI. A similar operation is possible on IBM and HPUX, but with

a different syntax.

For the 1.1.8 JVM (Windows, Linux, Solaris, Alpha, SGI) our defaults are:

minHeapSize = 16000000

maxHeapSize = 64000000

These are the structure field names in that correspond to -ms and

-mx, and the settings above are roughly 16MB and 64MB.

To investigate the Java heap a bit, ask via the following:

>> java.lang.Runtime.getRuntime.totalMemory

>> java.lang.Runtime.getRuntime.freeMemory

When the free memory hits zero, Java will double the heap size (up to the

maximum setting).

If you choose to run without Java, you will remove the overhead of the

middle man, but you will also lose some MATLAB functionality (mostly

graphics and the Editor). You will still have most of the computational

power though.

Without J***A, memory management will come directly from the OS, and a

CLEAR operation will result in memory being freed back to the OS.

21)如何在Matlab中新增新的工具箱?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

如果是Matlab安裝光碟上的工具箱,重新執行安裝程式,選中即可。

如果是單獨下載的工具箱,一般情況下僅需要把新的工具箱解壓到某

個目錄,然後用addpath(對於多個目錄的使用genpath())或者pathtool添

加工具箱的路徑,然後用which newtoolbox_command.m來檢驗是否可

以訪問。如果能夠顯示新設定的路徑,則表明該工具箱可以使用了。

具體請看工具箱自己代的README檔案。

22)如何讀寫Matlab的.mat檔案?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

檔案結構參見:

ormat.pdf

建議使用matlab自己提供的函式來讀寫簡單安全,或者參考:

.txt

來自matlab的c math library

23)如何得到contour線上的座標點?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

lcount=5;

[c,h]=contour(peaks,lcount);

x=get(h,'xdata');

y=get(h,'ydata');

這裡得到的x和y都是cell陣列,用x{1}/y{1}來得到每條線上的座標對,

注意,每條線的最後一個數據是NaN

24)如何將Matlab繪製的三維網格圖帖到word裡?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

如果需要點陣圖,好處是所見即所得,壞處是影象精度差,不能放縮:

1.用拷屏 Alt+PrintScreen

2.在圖形視窗選單Edit\Copy Options….\選擇Bitmap,可以選擇透

明背景,然後Edit\Copy Figure

如果需要拷貝向量圖:

在圖形視窗選單Edit\Copy Options….\選擇Metafile,然後

Edit\Copy Figure,在Word中貼上

經常地,按照Metafile方式貼上的圖片曲線會出現鋸齒,最好的方式是

使用eps檔案:

1.將需要拷貝的圖作為當前視窗

2.再轉換到matlab命令視窗,print -deps filename.eps

3.-deps還可以用depsc,deps2,depsc2

4.在word中插入圖片,選中該eps,如果是word 2000以前版本

,不會顯示圖片內容,但可以列印,word XP即可顯示,又可列印。

5.如果不滿意,可以在word中雙擊編輯,如果安裝有Adobe

Illustrator等向量影象編輯軟體,也可以進行編輯。

25)請問可以檢視Matlab中函式的原始碼嗎?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

Matlab除了buildin函式和mex/dll檔案看不到原碼,其他如工具箱等都可

以直接看到程式碼,首先確認該檔案安裝在matlab中,即which

filename.m存在,然後可以edit filename.m

26)Matlab有沒有求矩陣行數/列數/維數的函式?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

ndims(A)返回A的維數

size(A)返回A各個維的最大元素個數

length(A)返回max(size(A))

[m,n]=size(A)如果A是二維陣列,返回行數和列數

nnz(A)返回A中非0元素的個數

27)Matlab中如何中斷運算?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

在命令視窗按Ctrl+C,在UNIX/LINUX會立即中斷運算,在Windows可

能由於作業系統的原因,有時會出現宕機和等待的情況。

28)Matlab中有沒有畫圓或橢圓的函式?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

沒有,Matlab沒有提供直接繪圓的圖元函式,需要自己寫程式碼,其實

就兩句:

sita=0:pi/20:2*pi;

plot(r*cos(sita),r*sin(sita)); %半徑為r的圓

plot(a*cos(sita+fi),b *sin(sita+fi)); %橢圓

如果是單位圓,可以使用rectangle('Curvature', [1 1])

29)Matlab下如何定義整形

:#修改:fhorse (馬不停蹄),2002/6/21,SMTH/MathTools #

Matlab預設的矩陣資料結構都是雙精度浮點型,即64位來表示一個數

字,大多數的函式和操作都定義在double資料結構,如果你需要

把double的資料轉換為整形,然後再參與運算,需要使用

double(int32(x))或者floor/round/ceil等函式

如果為了節省記憶體,只進行賦值、列印等簡單操作,可以參

見uint8/uint16/uint32命令的幫助

30)Matlab如何產生均勻分佈的白噪聲?

:#misc,2002/6/21,SMTH/MathTools #

help rand 均勻分佈百噪聲

help randn高斯分佈百噪聲

31)在Matlab中debug的時候能否跟蹤變數的?

:#FangQ([email protected]),2002/6/21,BigGreen/MathTools #

可以,如果使用medit,設定斷點後可以用滑鼠移到所看的變數上,顯

示當前的值,或者在命令視窗打該變數名直接回車。如果在程式碼中實

現除錯斷點等功能,參

見dbstop,dbcont,dbstep,dbclear,dbtype,dbstack,dbup,dbdown,dbstatus,

dbquit

32)請問在Matlab中怎樣輸入特殊符號啊或者上標、下標?

:#FangQ([email protected]),southerner(笑著),2002/6/6,SMTH/MathTools#

matlab的text/title/xlabel/ylabel物件支援簡單的TeX排版語法,如希臘字

母,上下標等例如

text(0.5,0.5,'\alpha^\beta_2');

33)Matlab中如何後臺執行一個DOS程式?

:#FangQ([email protected]), 2002/6/4. BigGreen/en_Matlab#

這裡是一個後臺執行一個需要外部輸入的DOS命令的例子,需要的輸

入實事先都寫在同目錄下的input.txt檔案中:

dos('myexe < input.txt &')

34)Matlab如何載入輸入檔案(批處理模式) ?

:#翻譯自:comp.sys-soft.Matlab FAQ. BigGreen/en_Matlab#

PC上可以使用matlab /r引數來在matlab啟動的時候直接載入執行m檔案

,在UNIX上,使用

matlab < MyMFile > MyOutputFile

來外部執行MyMFile,

以上執行方式都可以通過指令碼檔案實現批處理

35)Matlab如何啟動時執行規定的檔案?

:#FangQ([email protected]), 2002/5/29.BigGreen/en_Matlab#

參見上一個問題的回答

36)如何在Matlab GUI中使用圖形背景?

:#FangQ([email protected]), 2002/5/29.BigGreen/en_Matlab#

這是一個簡單的例子:

[A,map]=imread('yourimg.gif');

imagesc(A)

colormap(map)

set(gca,'position',[0 0 1 1])

axis off

ax2=axes('position',[0.2,0.2,0.6,0.6]);

plot(rand(1,10),'parent',ax2);

set(ax2,'color','none')

37)大量資料點Matlab繪圖為什麼很慢?

:#FangQ([email protected]), 2002/6/22.BigGreen/en_Matlab#

1.首先看能否用已有函式對整個矩陣繪圖,比

如mesh/plot3/trimesh等

2.如果必須一點一點/或者一條線一條線的新增,最好作如下

設定:

doublebuffer=on

erasemode=none

backingstore=off

renderer=opengl

以及參考MathWorks對於高速繪圖