# MATLAB相应一些函数机及其作用

1.applylut

A = applylut(BW,lut)

lut = makelut('sum(x(:)) == 4',2);
BW2 = applylut(BW1,lut);
imshow(BW1)
figure, imshow(BW2)

makelut
2.bestblk

siz = bestblk([m n],k)
[mb,nb] = bestblk([m n],k)

siz = bestblk([640 800],72)
siz =
64 50

blkproc
3.blkproc
MATLAB高级应用——图形及影像处理 320

B = blkproc(A,[m n],fun)
B = blkproc(A,[m n],fun,P1,P2,...)
B = blkproc(A,[m n],[mborder nborder],fun,...)
B = blkproc(A,'indexed',...)

I2 = blkproc(I,[8 8],'std2(x)*ones(size(x))');
imshow(I)
figure, imshow(I2,[]);

colfilt, nlfilter,inline
4.brighten

brighten(beta)
newmap = brighten(beta)
newmap = brighten(map,beta)
brighten(fig,beta)

5.bwarea

total = bwarea(BW)

imshow(BW);

bwarea(BW)
ans =
15799

bweuler, bwperim
6.bweuler.

eul = bweuler(BW,n)

imshow(BW);
bweuler(BW)
ans =
-2

bwmorph, bwperim
7.bwfill

BW2 = bwfill(BW1,c,r,n)
BW2 = bwfill(BW1,n)
[BW2,idx] = bwfill(...)
BW2 = bwfill(x,y,BW1,xi,yi,n)
[x,y,BW2,idx,xi,yi] = bwfill(...)
BW2 = bwfill(BW1,'holes',n)
[BW2,idx] = bwfill(BW1,'holes',n)

MATLAB高级应用——图形及影像处理 322
BW1 =[1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0
1 1 1 1 0 1 1 1
1 0 0 1 1 0 1 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0]
BW2 = bwfill(BW1,3,3,8)
BW2 =
1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 1 0
1 1 1 1 1 1 1 0
1 1 1 1 0 1 1 1
1 0 0 1 1 0 1 0
1 0 0 0 1 0 1 0
1 0 0 0 1 1 1 0
BW3 = ~im2bw(I);
BW4 = bwfill(BW3,'holes');
imshow(BW3)
figure, imshow(BW4)

bwselect, roifill
8.bwlabel

L = bwlabel(BW,n)
[L,num] = bwlabel(BW,n)

BW = [1 1 1 0 0 0 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 0 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 1 1 0
1 1 1 0 0 0 0 0]
L = bwlabel(BW,4)
L =
1 1 1 0 0 0 0 0
1 1 1 0 2 2 0 0
1 1 1 0 2 2 0 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 3 3 0
1 1 1 0 0 0 0 0
[r,c] = find(L==2);
rc = [r c]
rc =
2 5
3 5
2 6
3 6

bweuler, bwselect
9.bwmorph

BW2 = bwmorph(BW1,operation)
BW2 = bwmorph(BW1,operation,n)

MATLAB高级应用——图形及影像处理 324
imshow(BW1);
BW2 = bwmorph(BW1,'remove');
BW3 = bwmorph(BW1,'skel',Inf);
imshow(BW2)
figure, imshow(BW3)

bweuler, bwperim, dilate, erode
10.bwperim

BW2 = bwperim(BW1,n)

BW2 = bwperim(BW1,8);
imshow(BW1)
figure, imshow(BW2)

bwarea, bweuler, bwfill
11.bwselect

BW2 = bwselect(BW1,c,r,n)
BW2 = bwselect(BW1,n)
[BW2,idx] = bwselect(...)

c = [16 90 144];
r = [85 197 247];
BW2 = bwselect(BW1,c,r,4);
imshow(BW1)
figure, imshow(BW2)

bwfill, bwlabel, impixel, roipoly, roifill
12.cmpermute
MATLAB高级应用——图形及影像处理 326

[Y,newmap] = cmpermute(X,map)
[Y,newmap] = cmpermute(X,map,index)

To order a colormap by luminance, use:
ntsc = rgb2ntsc(map);
[dum,index] = sort(ntsc(:,1));
[Y,newmap] = cmpermute(X,map,index);

randperm
13.cmunique

[Y,newmap] = cmunique(X,map)
[Y,newmap] = cmunique(RGB)
[Y,newmap] = cmunique(I)

gray2ind, rgb2ind
14.col2im

A = col2im(B,[m n],[mm nn],block_type)
A = col2im(B,[m n],[mm nn])

blkproc, colfilt, im2col, nlfilter
15.colfilt

B = colfilt(A,[m n],block_type,fun)
B = colfilt(A,[m n],block_type,fun,P1,P2,...)
B = colfilt(A,[m n],[mblock nblock],block_type,fun,...)
B = colfilt(A,'indexed',...)

blkproc, col2im, im2col, nlfilter
16.colorbar

colorbar('vert')
colorbar('horiz')
colorbar(h)
colorbar
h = colorbar(...)

h = fspecial('log');
I2 = filter2(h,I);
imshow(I2,[]), colormap(jet(64)), colorbar
17.conv2

C = conv2(A,B)
C = conv2(hcol,hrow,A)
C = conv2(...,shape)

A = magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
MATLAB高级应用——图形及影像处理 328
10 12 19 21 3
11 18 25 2 9
B = [1 2 1;0 2 0;3 1 3]
B =
1 2 1
0 2 0
3 1 3
C = conv2(A,B)
C =
17 58 66 34 32 38 15
23 85 88 35 67 76 16
55 149 117 163 159 135 67
79 78 160 161 187 129 51
23 82 153 199 205 108 75
30 68 135 168 91 84 9
33 65 126 85 104 15 27

filter2
18.convmtx2

T = convmtx2(H,m,n)
T = convmtx2(H,[m n])

conv2
19.convn

C = convn(A,B)
C = convn(A,B,shape)

conv2
20.corr2

r = corr2(A,B)

std2
21.dct2

B = dct2(A)
B = dct2(A,m,n)
B = dct2(A,[m n])

I = rgb2gray(RGB);
J = dct2(I);
imshow(log(abs(J)),[]), colormap(jet(64)), colorbar
J(abs(J) < 10) = 0;
K = idct2(J)/255;
imshow(K)

fft2, idct2, ifft2
22.dctmtx

D = dctmtx(n)

dct2
23.dilate

BW2 = dilate(BW1,SE)
BW2 = dilate(BW1,SE,alg)
BW2 = dilate(BW1,SE,...,n)

SE = ones(6,2);
BW2 = dilate(BW1,SE);
imshow(BW1)
figure, imshow(BW2)

bwmorph, erode
24.dither

X = dither(RGB,map)
BW = dither(I)

rgb2ind
25.double

B = double(A)

B = sqrt(double(A));

im2double, im2uint, uint8
26.edge

BW = edge(I,'sobel')
BW = edge(I,'sobel',thresh)
BW = edge(I,'sobel',thresh,direction)
[BW,thresh] = edge(I,'sobel',...)
BW = edge(I,'prewitt')
BW = edge(I,'prewitt',thresh)
BW = edge(I,'prewitt',thresh,direction)
[BW,thresh] = edge(I,'prewitt',...)
BW = edge(I,'roberts')
BW = edge(I,'roberts',thresh)
[BW,thresh] = edge(I,'roberts',...)
BW = edge(I,'log')
BW = edge(I,'log',thresh)
BW = edge(I,'log',thresh,sigma)
[BW,threshold] = edge(I,'log',...)
BW = edge(I,'zerocross',thresh,h)
[BW,thresh] = edge(I,'zerocross',...)
BW = edge(I,'canny')
BW = edge(I,'canny',thresh)
BW = edge(I,'canny',thresh,sigma)
MATLAB高级应用——图形及影像处理 332
[BW,threshold] = edge(I,'canny',...)

BW1 = edge(I,'prewitt');
BW2 = edge(I,'canny');
imshow(BW1);
figure, imshow(BW2)
27.erode

BW2 = erode(BW1,SE)
BW2 = erode(BW1,SE,alg)
BW2 = erode(BW1,SE,...,n)

SE = ones(3,1);
BW2 = erode(BW1,SE);
imshow(BW1)
figure, imshow(BW2)

bwmorph, dilate

28.fft2

B = fft2(A)
B = fft2(A,m,n)

imshow(saturn2)
B = fftshift(fft2(saturn2));
imshow(log(abs(B)),[]), colormap(jet(64)), colorbar

dct2, fftshift, idct2, ifft2
29.fftn

B = fftn(A)
B = fftn(A,siz)

fft2, ifftn
30.fftshift
MATLAB高级应用——图形及影像处理 334

B = fftshift(A)

B = fftn(A);
C = fftshift(B);

fft2, fftn, ifftshift
31.filter2

B = filter2(h,A)
B = filter2(h,A,shape)

A = magic(6)
A =
35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11
h = fspecial('sobel')
h =
1 2 1
0 0 0
-1 -2 -1
B = filter2(h,A,'valid')
B =
-8 4 4 -8
-23 -44 -5 40
-23 -50 1 40
-8 4 4 -8

conv2, roifilt2
32.freqspace

[f1,f2] = freqspace(n)
[f1,f2] = freqspace([m n])
[x1,y1] = freqspace(...,'meshgrid')
f = freqspace(N)
f = freqspace(N,'whole')

fsamp2, fwind1, fwind2
33.freqz2

[H,f1,f2] = freqz2(h,n1,n2)
[H,f1,f2] = freqz2(h,[n2 n1])
[H,f1,f2] = freqz2(h,f1,f2)
[H,f1,f2] = freqz2(h)
[...] = freqz2(h,...,[dx dy])
[...] = freqz2(h,...,dx)
freqz2(...)

Hd = zeros(16,16);
Hd(5:12,5:12) = 1;
Hd(7:10,7:10) = 0;
h = fwind1(Hd,bartlett(16));
colormap(jet(64))
freqz2(h,[32 32]); axis ([-1 1 -1 1 0 1])
34.fsamp2
MATLAB高级应用——图形及影像处理 336

h = fsamp2(Hd)
h = fsamp2(f1,f2,Hd,[m n])

[f1,f2] = freqspace(21,'meshgrid');
Hd = ones(21);
r = sqrt(f1.^2 + f2.^2);
Hd((r0.5)) = 0;
colormap(jet(64))
mesh(f1,f2,Hd)

conv2, filter2, freqspace, ftrans2, fwind1, fwind2
35.fspecial

h = fspecial(type)
h = fspecial(type,parameters)

h = fspecial('unsharp',0.5);
I2 = filter2(h,I)/255;
imshow(I)
figure, imshow(I2)

conv2, edge, filter2, fsamp2, fwind1, fwind2
36.ftrans2

h = ftrans2(b,t)
h = ftrans2(b)

colormap(jet(64))
b = remez(10,[0 0.05 0.15 0.55 0.65 1],[0 0 1 1 0 0]);
[H,w] = freqz(b,1,128,'whole');
plot(w/pi-1,fftshift(abs(H)))

conv2, filter2, fsamp2, fwind1, fwind2
37.fwind1

h = fwind1(Hd,win)
h = fwind1(Hd,win1,win2)
h = fwind1(f1,f2,Hd,...)

[f1,f2] = freqspace(21,'meshgrid');
Hd = ones(21);
r = sqrt(f1.^2 + f2.^2);
Hd((r0.5)) = 0;
colormap(jet(64))
mesh(f1,f2,Hd)

conv2, filter2, fsamp2, freqspace, ftrans2, fwind2
38.fwind2

h = fwind2(Hd,win)
h = fwind2(f1,f2,Hd,win)

[f1,f2] = freqspace(21,'meshgrid');
Hd = ones(21);
r = sqrt(f1.^2 + f2.^2);
Hd((r0.5)) = 0;
colormap(jet(64))
mesh(f1,f2,Hd)
MATLAB高级应用——图形及影像处理 338

conv2, filter2, fsamp2, freqspace, ftrans2, fwind1
39.getimage

A = getimage(h)
[x,y,A] = getimage(h)
[...,A,flag] = getimage(h)
[...] = getimage

imshow rice.tif
I = getimage;
40.gray2ind

[X,map] = gray2ind(I,n)

ind2gray
41.grayslice

X = grayslice(I,n)
X = grayslice(I,v)

X = grayslice(I,16);
imshow(I)
figure, imshow(X,jet(16))

gray2ind
42.histeq

J = histeq(I,hgram)
J = histeq(I,n)
[J,T] = histeq(I,...)

J = histeq(I);
imshow(I)
figure, imshow(J)
imhist(I,64)
figure; imhist(J,64)

43.hsv2rgb

rgbmap = hsv2rgb(hsvmap)
RGB = hsv2rgb(HSV)

MATLAB高级应用——图形及影像处理 340
rgb2hsv, rgbplot
44.idct2

B = idct2(A)
B = idct2(A,m,n)
B = idct2(A,[m n])

dct2, dctmtx, fft2, ifft2
45.ifft2

B = ifft2(A)
B = ifft2(A,m,n)

fft2, fftshift, idct2
46.ifftn

B = ifftn(A)
B = ifftn(A,siz)

fft2, fftn, ifft2
47.sim2bw

BW = im2bw(I,level)
BW = im2bw(X,map,level)
BW = im2bw(RGB,level)

BW = im2bw(X,map,0.4);
imshow(X,map)

figure, imshow(BW)

ind2gray, rgb2gray
48.im2col

B = im2col(A,[m n],block_type)
B = im2col(A,[m n])
B = im2col(A,'indexed',...)

blkproc, col2im, colfilt, nlfilter
49.im2double

I2 = im2double(I1)
RGB2 = im2double(RGB1)
BW2 = im2double(BW1)
X2 = im2double(X1,'indexed')

double, im2uint8, uint8
50.im2uint8

I2 = im2uint8(I1)
RGB2 = im2uint8(RGB1)
BW2 = im2uint8(BW1)
X2 = im2uint8(X1,'indexed')
MATLAB高级应用——图形及影像处理 342

im2uint16, double, im2double, uint8, imapprox, uint16
51.im2uint16

I2 = im2uint16(I1)
RGB2 = im2uint16(RGB1)
X2 = im2uint16(X1,'indexed')

im2uint8, double, im2double, uint8, uint16, imapprox

imshow(I)
figure, imshow(J)

brighten, histeq
53.imapprox

[Y,newmap] = imapprox(X,map,n)

[Y,newmap] = imapprox(X,map,tol)
Y = imapprox(X,map,newmap)
[...] = imapprox(...,dither_option)

cmunique, dither, rgb2ind
54.imcontour

imcontour(I,n)
imcontour(I,v)
imcontour(x,y,...)
imcontour(...,LineSpec)
[C,h] = imcontour(...)

imcontour(I,3)

clabel, contour, LineSpec
55.imcrop

I2 = imcrop(I)
X2 = imcrop(X,map)
RGB2 = imcrop(RGB)
I2 = imcrop(I,rect)
X2 = imcrop(X,map,rect)
RGB2 = imcrop(RGB,rect)
MATLAB高级应用——图形及影像处理 344
[...] = imcrop(x,y,...)
[A,rect] = imcrop(...)
[x,y,A,rect] = imcrop(...)

I2 = imcrop(I,[60 40 100 90]);
imshow(I)
figure, imshow(I2)

zoom
56.imfeature

stats = imfeature(L,measurements)
stats = imfeature(L,measurements,n)

L = bwlabel(BW);
stats = imfeature(L,'all');
stats(23)
ans =
Area: 89
Centroid: [95.6742 192.9775]
BoundingBox: [87.5000 184.5000 16 15]
MajorAxisLength: 19.9127
MinorAxisLength: 14.2953
Eccentricity: 0.6961
Orientation: 9.0845
ConvexHull: [28x2 double]

ConvexImage: [15x16 uint8 ]
ConvexArea: 205
Image: [15x16 uint8 ]
FilledImage: [15x16 uint8 ]
FilledArea: 122
EulerNumber: 0
Extrema: [ 8x2 double]
EquivDiameter: 10.6451
Solidity: 0.4341
Extent: 0.3708
PixelList: [89x2 double]

bwlabel
57.imfinfo

info = imfinfo(filename,fmt)
info = imfinfo(filename)

info = imfinfo('canoe.tif')
info =
Filename:'canoe.tif'
FileModDate: '25-Oct-1996 22:10:39'
FileSize: 69708
Format: 'tif'
FormatVersion: []
Width: 346
Height: 207
BitDepth: 8
ColorType: 'indexed'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubfileType: 0
BitsPerSample: 8
Compression: 'PackBits'
PhotometricInterpretation: 'RGB Palette'
MATLAB高级应用——图形及影像处理 346
StripOffsets: [ 9x1 double]
SamplesPerPixel: 1
RowsPerStrip: 23
StripByteCounts: [ 9x1 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: [256x3 double]
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSamplue: 255
MinSamplue: 0
Thresholding: 1

58.imhist

imhist(I,n)
imhist(X,map)
[counts,x] = imhist(...)

imhist(I)

histeq
59.immovie

mov = immovie(X,map)