版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、matlab 圖像分割算法源碼圖像讀取及灰度變換I=imread('cameraman.tif');%讀取圖像subplot(1,2,1),imshow(I) %輸出圖像title('原始圖像') %在原始圖像中加標(biāo)題subplot(1,2,2),imhist(I) %輸出原圖直方圖title('原始圖像直方圖') %在原圖直方圖上加標(biāo)題圖像旋轉(zhuǎn)I = imread('cameraman.tif');figure,imshow(I);theta = 30;K = imrotate(I,theta); % Try varying t
2、he angle, theta.figure, imshow(K)邊緣檢測I = imread('cameraman.tif');J1=edge(I,'sobel');J2=edge(I,'prewitt');J3=edge(I,'log');subplot(1,4,1),imshow(I);subplot(1,4,2),imshow(J1);subplot(1,4,3),imshow(J2);subplot(1,4,4),imshow(J3);1.圖像反轉(zhuǎn)MATLAB程序?qū)崿F(xiàn)如下:I=imread('xian.bmp
3、39;);J=double(I);J=-J+(256-1); %圖像反轉(zhuǎn)線性變換H=uint8(J);subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(H);2.灰度線性變換MATLAB程序?qū)崿F(xiàn)如下:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title('原始圖像');axis(5
4、0,250,50,200);axis on; %顯示坐標(biāo)系I1=rgb2gray(I);subplot(2,2,2),imshow(I1);title('灰度圖像');axis(50,250,50,200);axis on;
5、 %顯示坐標(biāo)系J=imadjust(I1,0.1 0.5,); %局部拉伸,把0.1 0.5內(nèi)的灰度拉伸為0 1subplot(2,2,3),imshow(J);title('線性變換圖像0.1 0.5');axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on; &
6、#160; %顯示坐標(biāo)系K=imadjust(I1,0.3 0.7,); %局部拉伸,把0.3 0.7內(nèi)的灰度拉伸為0 1subplot(2,2,4),imshow(K);title('線性變換圖像0.3 0.7');axis(50,250,50,200);grid on;
7、; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系3.非線性變換MATLAB程序?qū)崿F(xiàn)如下:I=imread('xian.bmp');I1=rgb2gray(I);subplot(1,2,1),imshow(I1);title('灰度圖像');axis(50,250,50,200);grid on;
8、; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系J=double(I1);J=40*(log(J+1);H=uint8(J);subplot(1,2,2),imshow(H);title('對(duì)數(shù)變換圖像'
9、;);axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系4.直方圖均衡化MATLAB程序?qū)崿F(xiàn)如下:I=imread('xian.bmp
10、');I=rgb2gray(I);figure;subplot(2,2,1);imshow(I);subplot(2,2,2);imhist(I);I1=histeq(I);figure;subplot(2,2,1);imshow(I1);subplot(2,2,2);imhist(I1);5.線性平滑濾波器用MATLAB實(shí)現(xiàn)領(lǐng)域平均法抑制噪聲程序:I=imread('xian.bmp');subplot(231)imshow(I)title('原始圖像')I=rgb2gray(I);I1=imnoise(I,'salt & pepper
11、',0.02);subplot(232)imshow(I1)title('添加椒鹽噪聲的圖像')k1=filter2(fspecial('average',3),I1)/255; %進(jìn)行3*3模板平滑濾波k2=filter2(fspecial('average',5),I1)/255; %進(jìn)行5*5模板平滑濾波k3=filte
12、r2(fspecial('average',7),I1)/255; %進(jìn)行7*7模板平滑濾波k4=filter2(fspecial('average',9),I1)/255; %進(jìn)行9*9模板平滑濾波subplot(233),imshow(k1);title('3*3模板平滑濾波');subplot(234),imshow(k2);t
13、itle('5*5模板平滑濾波');subplot(235),imshow(k3);title('7*7模板平滑濾波');subplot(236),imshow(k4);title('9*9模板平滑濾波');6.中值濾波器用MATLAB實(shí)現(xiàn)中值濾波程序如下:I=imread('xian.bmp');I=rgb2gray(I);J=imnoise(I,'salt&pepper',0.02);subplot(231),imshow(I);title('原圖像');subplot(232),ims
14、how(J);title('添加椒鹽噪聲圖像');k1=medfilt2(J); %進(jìn)行3*3模板中值濾波k2=medfilt2(J,5,5); %進(jìn)行5*5模板中值濾波k3=medfilt2(J,7,7); %進(jìn)行7*7模板中值濾波k4=medfilt2(J,9,9);
15、 %進(jìn)行9*9模板中值濾波subplot(233),imshow(k1);title('3*3模板中值濾波');subplot(234),imshow(k2);title('5*5模板中值濾波');subplot(235),imshow(k3);title('7*7模板中值濾波');subplot(236),imshow(k4);title('9*9模板中值濾波');7.用Sobel算子和拉普拉斯對(duì)圖像銳化:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title(
16、39;原始圖像');axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系I1=im2bw(I);subplot(2,2,2),imsho
17、w(I1);title('二值圖像');axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系H=fspecial('so
18、bel'); %選擇sobel算子 J=filter2(H,I1); %卷積運(yùn)算subplot(2,2,3),imshow(J); title('sobel算子銳化圖像');axis(50,250,50,200);grid on; &
19、#160; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系h=0 1 0,1 -4 1,0 1 0; %拉普拉斯算子J1=conv2(I1,h,'same'); %卷積運(yùn)算subp
20、lot(2,2,4),imshow(J1); title('拉普拉斯算子銳化圖像');axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on;
21、60; %顯示坐標(biāo)系8.梯度算子檢測邊緣用MATLAB實(shí)現(xiàn)如下:I=imread('xian.bmp');subplot(2,3,1);imshow(I);title('原始圖像');axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on;
22、; %顯示坐標(biāo)系I1=im2bw(I);subplot(2,3,2);imshow(I1);title('二值圖像');axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on;
23、60; %顯示坐標(biāo)系I2=edge(I1,'roberts');figure;subplot(2,3,3);imshow(I2);title('roberts算子分割結(jié)果');axis(50,250,50,200);grid on;
24、 %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系I3=edge(I1,'sobel');subplot(2,3,4);imshow(I3);title('sobel算子分割結(jié)果');axis(50,250,50,200);grid on;
25、; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系I4=edge(I1,'Prewitt');subplot(2,3,5);imshow(I4);title('Prewitt算子分割結(jié)果');axis(50,250,50,200);grid on;
26、 %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系9.LOG算子檢測邊緣用MATLAB程序?qū)崿F(xiàn)如下:I=imread('xian.bmp');subplot(2,2,1);imshow(I);title('原始圖像'
27、;);I1=rgb2gray(I);subplot(2,2,2);imshow(I1);title('灰度圖像');I2=edge(I1,'log');subplot(2,2,3);imshow(I2);title('log算子分割結(jié)果');10.Canny算子檢測邊緣用MATLAB程序?qū)崿F(xiàn)如下:I=imread('xian.bmp');subplot(2,2,1);imshow(I);title('原始圖像')I1=rgb2gray(I);subplot(2,2,2);imshow(I1);title('
28、灰度圖像');I2=edge(I1,'canny');subplot(2,2,3);imshow(I2);title('canny算子分割結(jié)果');11.邊界跟蹤(bwtraceboundary函數(shù))clcclear allI=imread('xian.bmp');figureimshow(I);title('原始圖像');I1=rgb2gray(I);
29、160; %將彩色圖像轉(zhuǎn)化灰度圖像 threshold=graythresh(I1); %計(jì)算將灰度圖像轉(zhuǎn)化為二值圖像所需的門限BW=im2bw(I1, threshold); %將灰度圖像轉(zhuǎn)化為二值圖像figureimshow(BW);title('二值圖像');dim=size(BW);col=round(dim(2)/2)-90;
30、0; %計(jì)算起始點(diǎn)列坐標(biāo)row=find(BW(:,col),1); %計(jì)算起始點(diǎn)行坐標(biāo)connectivity=8;num_points=180;contour=bwtraceboundary(BW,row,col,'N',connectivity,num_points); %提取邊界figureimshow(I1);hold on;plot(contour(:,2),contour(:,1), 'g','LineWidth
31、' ,2);title('邊界跟蹤圖像');12.Hough變換I= imread('xian.bmp');rotI=rgb2gray(I);subplot(2,2,1);imshow(rotI);title('灰度圖像');axis(50,250,50,200);grid on; axis on; BW=edge(rotI,
32、39;prewitt');subplot(2,2,2);imshow(BW);title('prewitt算子邊緣檢測后圖像');axis(50,250,50,200);grid on; axis on; H,T,R=hough(BW);subplot(2,2,3);imshow(H,'XData',T,'YData',R,
33、9;InitialMagnification','fit');title('霍夫變換圖');xlabel('theta'),ylabel('rho');axis on , axis normal, hold on;P=houghpeaks(H,5,'threshold',ceil(0.3*max(H(:);x=T(P(:,2);y=R(P(:,1);plot(x,y,'s','color','white');lines=houghlines(BW,T,R,P,
34、'FillGap',5,'MinLength',7);subplot(2,2,4);,imshow(rotI);title('霍夫變換圖像檢測');axis(50,250,50,200);grid on; axis on; hold on;max_len=0;for k=1:length(lines)xy=lines(k).point1;
35、lines(k).point2;plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');len=norm(lines(k).point1-lines(k)
36、.point2);if(len>max_len)max_len=len;xy_long=xy;endendplot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');13.直方圖閾值法用MATLAB實(shí)現(xiàn)直方圖閾值法:I=imread('xian.bmp');I1=rgb2gray(I);figure;subplot(2,2,1);imshow(I1);title('灰度圖像')axis(50,250,50,200);grid on;
37、160; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系m,n=size(I1);
38、60; %測量圖像尺寸參數(shù)GP=zeros(1,256); %預(yù)創(chuàng)建存放灰度出現(xiàn)概率的向量for k=0:2
39、55 GP(k+1)=length(find(I1=k)/(m*n); %計(jì)算每級(jí)灰度出現(xiàn)的概率,將其存入GP中相應(yīng)位置endsubplot(2,2,2),bar(0:255,GP,'g') %繪制直方圖title('灰度直方圖')xlabel('灰度值
40、9;)ylabel('出現(xiàn)概率') I2=im2bw(I,150/255); subplot(2,2,3),imshow(I2);title('閾值150的分割圖像')axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on;
41、60; %顯示坐標(biāo)系I3=im2bw(I,200/255); %subplot(2,2,4),imshow(I3);title('閾值200的分割圖像')axis(50,250,50,200);grid on; %顯示網(wǎng)格線
42、axis on; %顯示坐標(biāo)系14. 自動(dòng)閾值法:Otsu法用MATLAB實(shí)現(xiàn)Otsu算法:clcclear allI=imread('xian.bmp');subplot(1,2,1),imshow(I);title('原始圖像')axis(50,250,50,200);grid on; &
43、#160; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系level=graythresh(I); %確定灰度閾值BW=im2bw(I,level);subplot(1,2,2),imshow(BW);title('Otsu
44、法閾值分割圖像')axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系15.膨脹操作I=imread('xian.bmp'
45、;); %載入圖像I1=rgb2gray(I);subplot(1,2,1);imshow(I1);title('灰度圖像') axis(50,250,50,200);grid on; %顯示網(wǎng)格線ax
46、is on; %顯示坐標(biāo)系se=strel('disk',1); %生成圓形結(jié)構(gòu)元素I2=imdilate(I1,se); %用
47、生成的結(jié)構(gòu)元素對(duì)圖像進(jìn)行膨脹subplot(1,2,2);imshow(I2);title('膨脹后圖像');axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on;
48、160; %顯示坐標(biāo)系16.腐蝕操作MATLAB實(shí)現(xiàn)腐蝕操作I=imread('xian.bmp'); %載入圖像I1=rgb2gray(I);subplot(1,2,1);imshow(I1);title('灰度圖像') axis(50,250,50,200);grid on; &
49、#160; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系se=strel('disk',1); %生成圓形結(jié)構(gòu)元素I2=imerode(I1,se);
50、160; %用生成的結(jié)構(gòu)元素對(duì)圖像進(jìn)行腐蝕subplot(1,2,2);imshow(I2);title('腐蝕后圖像');axis(50,250,50,200);grid on; %顯示網(wǎng)格線axis on; &
51、#160; %顯示坐標(biāo)系17.開啟和閉合操作用MATLAB實(shí)現(xiàn)開啟和閉合操作I=imread('xian.bmp'); %載入圖像subplot(2,2,1),imshow(I);title('原始圖像');axis(50,250,50,200);axis on;
52、160; %顯示坐標(biāo)系 I1=rgb2gray(I);subplot(2,2,2),imshow(I1);title('灰度圖像');axis(50,250,50,200);axis on; %顯示坐標(biāo)系
53、; se=strel('disk',1); %采用半徑為1的圓作為結(jié)構(gòu)元素I2=imopen(I1,se); %開啟操作I3=imclose(I1,se); %閉合操作subplot(2,2,3),imshow(I2);title('開啟運(yùn)算后圖像
54、9;);axis(50,250,50,200);axis on; %顯示坐標(biāo)系subplot(2,2,4),imshow(I3);title('閉合運(yùn)算后圖像');axis(50,250,50,200); axis on;
55、160; %顯示坐標(biāo)系18.開啟和閉合組合操作I=imread('xian.bmp'); %載入圖像subplot(3,2,1),imshow(I);title('原始圖像');axis(50,250,50,200);axis on;
56、; %顯示坐標(biāo)系 I1=rgb2gray(I);subplot(3,2,2),imshow(I1);title('灰度圖像');axis(50,250,50,200);axis on; %顯示坐標(biāo)系
57、160; se=strel('disk',1); I2=imopen(I1,se); %開啟操作I3=imclose(I1,se); %閉合操作subplot(3,2,3),imshow(I2);title('開啟運(yùn)算后圖像');axis(50,250,50,
58、200);axis on; %顯示坐標(biāo)系subplot(3,2,4),imshow(I3);title('閉合運(yùn)算后圖像');axis(50,250,50,200);axis on;
59、60; %顯示坐標(biāo)系se=strel('disk',1); I4=imopen(I1,se);I5=imclose(I4,se);subplot(3,2,5),imshow(I5); %開閉運(yùn)算圖像title('開閉運(yùn)算圖像');axis(50,250,50,200);axis on;
60、0; %顯示坐標(biāo)系 I6=imclose(I1,se);I7=imopen(I6,se);subplot(3,2,6),imshow(I7); %閉開運(yùn)算圖像 title('閉開運(yùn)算圖像');axis(50,250,50,200);axis on; %顯示坐標(biāo)系&
61、#160; 19.形態(tài)學(xué)邊界提取利用MATLAB實(shí)現(xiàn)如下:I=imread('xian.bmp'); %載入圖像subplot(1,3,1),imshow(I);title('原始圖像'); axis(50,250,50,200);grid on;
62、 %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系I1=im2bw(I);subplot(1,3,2),imshow(I1);title('二值化圖像');axis(50,250,50,200);grid on;
63、60; %顯示網(wǎng)格線axis on; %顯示坐標(biāo)系I2=bwperim(I1); %獲取區(qū)域的周長subplot(1,3,3),imshow(I2);
64、60;title('邊界周長的二值圖像');axis(50,250,50,200);grid on;axis on; 20.形態(tài)學(xué)骨架提取利用MATLAB實(shí)現(xiàn)如下:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title('原始圖像');axis(50,250,50,200);axis on;
65、; I1=im2bw(I);subplot(2,2,2),imshow(I1);title('二值圖像');axis(50,250,50,200);axis on; I2=bwmorph(I1,'skel
66、39;,1);subplot(2,2,3),imshow(I2);title('1次骨架提取');axis(50,250,50,200);axis on; I3=bwmorph(I1,'skel',2);subplot(2,2,4),imshow(I3);title('2次骨架提取');axis(50,250,50,200);axis
67、on; 21.直接提取四個(gè)頂點(diǎn)坐標(biāo)I = imread('xian.bmp');I = I(:,:,1);BW=im2bw(I); figureimshow(BW)x,y=getpts Matlab求二值圖像的周長 2013-01-21 20:00:21| 分類: matlab | 標(biāo)簽: |字號(hào)大中小 訂閱
68、 方法一,使用8向鏈碼。水平或垂直連通的長度為1,斜向連通長度為1.414(2的平方根)首先給出8向鏈碼的編碼函數(shù)function out=chaincode8(image)%功能:實(shí)現(xiàn)8連通鏈碼%輸入: 二值圖像%輸出:鏈碼的結(jié)果n=0 1;-1 1;-1 0;-1 -1;0 -1;1 -1;1 0;1 1;%設(shè)置標(biāo)志flag=1;%初始輸出的鏈碼串為空cc=;%找到起始點(diǎn)x y=find(image=1);x=min(x);imx=image(x,:);y=find(imx=1, 1 );first=x y;dir=7;while flag=1
69、; tt=zeros(1,8); newdir=mod(dir+7-mod(dir,2),8); for i=0:7 j=mod(newdir+i,8)+1;
70、; tt(i+1)=image(x+n(j,1),y+n(j,2); end d=find(tt=1, 1 ); dir=mod(newdir+d-1,8);
71、; %找到下一個(gè)像素點(diǎn)的方向碼后補(bǔ)充在鏈碼的后面 cc=cc,dir; x=x+n(dir+1,1);y=y+n(dir+1,2); %判別鏈碼的結(jié)束標(biāo)志 if x=first(1)&&y=first(2) flag=0; endendout=cc;下面是主函
72、數(shù)i=bwperim(imread('1.bmp'),8);%求出二值圖像的邊界c8=chaincode8(i);%生成8向鏈碼sum1=0;sum2=0;for k=1:length(c8) if c8(k)=0 |c8(k)=2 |c8(k)=4 |c8(k)=6 sum1=sum1+1; else sum2=sum2+1; &
73、#160; endendl=sum1+sum2*sqrt(2);方法二,利用Matlab提供的函數(shù)i=imread('1.bmp');s=regionprops(i,'Perimeter');1. 代碼matlab函數(shù)實(shí)現(xiàn)圖像銳化 I,map=imread('img.jpg');imshow(I,map);I=double(I);Gx,Gy=gradient(I); % 計(jì)算梯度G=sqrt(Gx.*Gx+
74、Gy.*Gy); % 注意是矩陣點(diǎn)乘J1=G;figure,imshow(J1,map); % 第一種圖像增強(qiáng)J2=I; % 第二種圖像增強(qiáng)K=find(G>=7);J2(K)=G(K);figure,imshow(J2,map);J3=I;
75、160; % 第三種圖像增強(qiáng)K=find(G>=7);J3(K)=255;figure,imshow(J3,map);J4=I; % 第四種圖像增強(qiáng)K=find(G<=7);J4(K)=255;figure,imshow(J4,map);J
76、5=I; % 第五種圖像增強(qiáng)K=find(G<=7);J5(K)=0;Q=find(G>=7);J5(Q)=255;figure,imshow(J5,map);MATLAB示例程序001-OSTU大津法/最大類間方差 Otsu最大類間方差法原理 利用閾值將原圖像分成前景,背景兩個(gè)圖象。 當(dāng)取最佳閾
77、值時(shí),背景應(yīng)該與前景差別最大,即方差最大。otsu算法找的就是這個(gè)最大方差下的閾值。 最大類間方差法(otsu)的公式推導(dǎo): 記t為前景與背景的分割閾值,前景點(diǎn)數(shù)占圖像比例為w0,平均灰度為u0;背景點(diǎn)數(shù)占圖像比例為w1,平均灰度為u1。 則圖像的總平均灰度為:u=w0*u0+w1*u1。 前景和背景圖象的方差:g=w0*(u0-u)*(u0-u)+w1*(u1-u)*(u1-u)=w0*w1*(u0-u1)*(u0-u1),此公式為方差公式。 循環(huán)求取最大方差即可。
78、0;MABLAB代碼及詳細(xì)注釋:function ostuimg = imread('Lena.jpg');I_gray=rgb2gray(img);figure,imshow(I_gray);I_double=double(I_gray); %轉(zhuǎn)化為雙精度,因?yàn)榇蠖鄶?shù)的函數(shù)和操作都是基于double的%figure,imshow(I_double);wid,len=size(I_gray); %wid為行數(shù),len為列數(shù)colorlevel=256; %灰度級(jí)hist=zeros(c
79、olorlevel,1); %直方圖,256×1的0矩陣 %threshold=128; %初始閾值%計(jì)算直方圖,統(tǒng)計(jì)灰度值的個(gè)數(shù)for i=1:wid for j=1:len m=I_gray(i,j)+1; %因?yàn)榛叶葹?-255所以+1 hist(m)=hist(m)+1; endend %直方圖歸一化hist=hist/(wid*len);
80、60; %miuT為總的平均灰度,histm代表像素值為m的點(diǎn)個(gè)數(shù)miuT=0;for m=1:colorlevel miuT=miuT+(m-1)*hist(m);endxigmaB2=0; %用于保存每次計(jì)算的方差,與下次計(jì)算的方差比較大小for mindex=1:colorlevel threshold=mindex-1; omega1=0; %前景點(diǎn)所占比例 omega2=0; %背景點(diǎn)所占比例 &
81、#160; for m=1:threshold-1 omega1=omega1+hist(m); %計(jì)算前景比例 end omega2=1-omega1; %計(jì)算背景比例 miu1=0; %前景平均灰度比例 miu2=0; %背景平均灰度比例
82、 %計(jì)算前景與背景平均灰度 for m=1:colorlevel if m miu1=miu1+(m-1)*hist(m); %前景平均灰度 else miu2=miu2+(m-1)*hist(m); %背景平均灰度 end end
83、160; % miu1=miu1/omega1;% miu2=miu2/omega2; %計(jì)算前景與背景圖像的方差 xigmaB21=omega1*(miu1-miuT)2+omega2*(miu2-miuT)2; xigma(mindex)=xigmaB21; %保留每次計(jì)算的方差 %每次計(jì)算方差后,與上一次計(jì)算的方差比較。保留最大方差為
84、finalT if xigmaB21>xigmaB2 finalT=threshold; xigmaB2=xigmaB21; endend %比較方法兩種閾值的不同fT=finalT/255; %閾值歸一化T=graythresh(I_gray); %matlab函數(shù)求閾值 for i=1:wid for j=1:le
85、n if I_double(i,j)>finalT bin(i,j)=255; else bin(i,j)=0; end endend figure,imshow(bin);figure,plot(1:colorlevel,xigm
86、a); end 運(yùn)行結(jié)果:function main img=imread('lena.jpg'); imshow(img); img=double(img); m n=size(img); Hist=zeros(1,256); for i=1:m for j=1:n Hist(img(i,j)+1)=Hist(img(i,j)+1)+1; %求直方圖 end end p=Hist/(m*n); %直方圖概率分布 uT=sum(1:256).*p(1:256); %圖像亮度均值,其實(shí)比真正的均值要大1,所以后面減了1 sigma_2=zeros(1,256)
87、; for k=1:256 sigma_2(k)=(uT*w(k,p)-u(k,p)2/(w(k,p)*(1-w(k,p); %類間方差 end tmp index=max(sigma_2); %求最大類間方差的索引 index=index-1; %這里索引是1-256,實(shí)際圖像灰度是0-255,所以減1 imgn=img>index; figure; imshow(imgn); function re=w(k,p) %直方圖前k個(gè)亮度級(jí)的0階累積矩 re=sum(p(1:k); end function re=u(k,p) %直方圖前k個(gè)亮度級(jí)的1階累積矩 re=sum(1:k).*p
88、(1:k); end end效果:原圖二值化后bwlabel函數(shù)(二值圖像中元素標(biāo)記) 分類: MATLAB 2012-07-03 10:05 648人閱讀 評(píng)論(0) 收藏 舉報(bào) 圖像處理轉(zhuǎn)至:圖像處理函數(shù)詳解bwlabel功能:對(duì)連通對(duì)象進(jìn)行標(biāo)注,bwlabel主要對(duì)二維二值圖像中各個(gè)分離部分進(jìn)行標(biāo)注(多維用bwlabeln,用法類似)。用法:L = bwlabel(BW,n)L,num = bwlabel(BW,n)L = bwlabel(BW,n)表示返回和BW相同大小的數(shù)組L。L中包含了連通對(duì)象的標(biāo)注。參數(shù)n為4或8,分別對(duì)應(yīng)4鄰域和8鄰域,默認(rèn)值為8。L,num = bwlabel
89、(BW,n)返回連通數(shù)num。 bwlabel用法:L = bwlabel(BW,n)返回一個(gè)和BW大小相同的L矩陣,包含了標(biāo)記了BW中每個(gè)連通區(qū)域的類別標(biāo)簽,這些標(biāo)簽的值為1、2、num(連通區(qū)域的個(gè)數(shù))。n的值為4或8,表示是按4連通尋找區(qū)域,還是8連通尋找,默認(rèn)為8。四連通或八連通是圖像處理里的基本感念:而8連通,是說一個(gè)像素,如果和其他像素在上、下、左、右、左上角、左下角、右上角或右下角連接著,則認(rèn)為他們是聯(lián)通的;4連通是指,如果像素的位置在其他像素相鄰的上、下、左或右,則認(rèn)為他們是連接著的,連通的,在左上角、左下角、右上角或右下角連接,則不認(rèn)為他們連通。L,num = bw
90、label(BW,n)這里num返回的就是BW中連通區(qū)域的個(gè)數(shù)。補(bǔ)充:我聽說過16連通,這應(yīng)該是在三維空間里的概念了吧。舉例說明:BW = 1 1 1 0 0 0 0 0 1 1 1 &
91、#160; 0 1 1 0 0 1 1 1 0 1 1 0 0 1
92、60; 1 1 0 0 0 1 0 1 1 1 0 0 0 1
93、0; 0 1 1 1 0 0 0 1 0 1 1 1 0 0 1
94、60; 1 0 1 1 1 0 0 0 0 0按4連通計(jì)算,方形的區(qū)域,和翻轉(zhuǎn)的L形區(qū)域,有用是對(duì)角連接,不屬于連通,所以分開標(biāo)記,連通區(qū)域個(gè)數(shù)為3 1. L = bwlabel(BW,4)結(jié)果如下:L =
95、60; 1 1 1 0 0 0 0 0 1 1 1 0 2 2 0 &
96、#160; 0 1 1 1 0 2 2 0 0 1 1 1 0 0
97、60; 0 3 0 1 1 1 0 0 0 3 0 1 1 1 0 0 0 3 0
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 保健用品配送合同樣本
- 家具展會(huì)物流分包服務(wù)
- 網(wǎng)球館裝修解約協(xié)議書
- 教育機(jī)構(gòu)裝修合同調(diào)整
- 家居體驗(yàn)館裝修設(shè)計(jì)委托書
- 美食餐廳裝修協(xié)議
- 攀巖館裝修改造合同
- 酒類釀造車間裝修協(xié)議
- 印刷品配送服務(wù)合同模板
- 公路建設(shè)挖掘機(jī)租賃合同
- 網(wǎng)絡(luò)安全服務(wù)項(xiàng)目服務(wù)質(zhì)量保障措施(實(shí)施方案)
- 昆山柏廬新村動(dòng)遷方案
- 《第七課 七巧板》參考課件
- 跨期選擇行為的心理機(jī)制
- 小升初語文 閱讀考點(diǎn)專題 小說(一) 課件 【要點(diǎn)精編+拓展提升】學(xué)年語文六年級(jí)下冊(統(tǒng)編版)
- 老年人心理健康量表(含評(píng)分)
- 消防車駕駛員安全教育課件
- 頂板基本知識(shí)培訓(xùn)課件
- 中國國道簡介
- GB/T 34133-2023儲(chǔ)能變流器檢測技術(shù)規(guī)程
- 電氣自動(dòng)化專業(yè)發(fā)展規(guī)劃
評(píng)論
0/150
提交評(píng)論