当前位置:文档之家› 部分图像分割的方法(matlab)

部分图像分割的方法(matlab)

大津法:function y1=OTSU(image,th_set)image=imread('color1.bmp');gray=rgb2gray(image);%原图像的灰度图low_high=stretchlim(gray);%增强图像,似乎也不是一定需要gray=imadjust(gray,low_high,[]);% subplot(224);imshow(gray);title('after adjust');count=imhist(gray);[r,t]=size(gray);n=r*t;l=256;count=count/n;%各级灰度出现的概率for i=2:lif count(i)~=0st=i-1;breakendend%以上循环语句实现寻找出现概率不为0的最小灰度值for i=l:-1:1if count(i)~=0;nd=i-1;breakendend%实现找出出现概率不为0的最大灰度值f=count(st+1:nd+1);p=st;q=nd-st;%p和分别是灰度的起始和结束值u=0;for i=1:q;u=u+f(i)*(p+i-1);ua(i)=u;end%计算图像的平均灰度值for i=1:q;w(i)=sum(f(1:i));end%计算出选择不同k的时候,A区域的概率d=(u*w-ua).^2./(w.*(1-w));%求出不同k值时类间方差[y,tp]=max(d);%求出最大方差对应的灰度级th=tp+p;if th<th_setth=tp+p;elseth=th_set; %根据具体情况适当修正门限endy1=zeros(r,t);for i=1:rfor j=1:tx1(i,j)=double(gray(i,j));endendfor i=1:rfor j=1:tif (x1(i,j)>th)y1(i,j)=x1(i,j);elsey1(i,j)=0;endendend%上面一段代码实现分割% figure,imshow(y1);% title('灰度门限分割的图像');程序二:clc; clear;cd 'D:\My Documents\MATLAB' time = now;I = imread('qr4.bmp');figure(1),imshow(I),title('p1_1.bmp'); % show the pictureI2 = rgb2gray(I);figure(2),imshow(I2),title('I2.bmp'); %ÖÐÖµÂ˲¨J = medfilt2(I2); figure(3),imshow(J);imwrite(J,'J.bmp'); [M N] = size(J);J1 = J(1:M/2,1:fix(N/2)); J2 = J(1:M/2,fix(N/2)+1:N); J3 = J(M/2+1:M, 1:fix( N/2)); J4 = J(M/2+1:M, fix(N/2)+1:N); % figure(4), img = J1;T1 = test_gray2bw( img ); % figure(5), img = J2;T2 = test_gray2bw( img ); % figure(6), img = J3;T3 = test_gray2bw( img ); % figure(7), img = J4;T4 = test_gray2bw( img ); T = [T1,T2;T3,T4]; figure,imshow(T)% T1 = edge(T,'sobel'); % figure,imshow(T1); % BW = edge(T,'sobel'); % f igure,imshow(BW);function [bw_img] = test_gray2bw( img ) %大津法[row_img col_img ] = size( img ) all_pix = row_img * col_img% get probability of each pixel(ÏñËØ). count_pix = zeros(1,256) % pro_pix = []for i = 1 : 1 : row_img for j = 1 : 1 : col_imgcount_pix(1,img(i,j)+1) = count_pix(1,img(i,j)+1) + 1 %ͳ¼Æ´ÎÊý end en dpro_pix = count_pix / all_pix% choose k value; max_kesi = -1 T = 0for k = 1 : 1 :while( i <= k )wa = wa + pro_pix(1,i+1) %ǰk¸öi£¬Ã¿¸öÏñËØµÄ»Ò¶È¸ÅÂÊ£¬¸ÅÂÊºÍ ua = ua + i * pro_pix(1,i+1) i = i + 1 endif ( wa == 0.0 ) continue; elseua = ua / wa endub = 0 wb = 0 i = k + 1while( i <= 255 )wb = wb + pro_pix( 1 , i + 1 )ub = ub + i * pro_pix( 1 , i + 1 ) i = i + 1 endif ( wb == 0.0 ) continue; elseub = ub / wb endu = wa * ua + wb * ub% kesi = wa * ( ua - u ) * ( ua - u ) + wb * ( ub - u ) * ( ub -u ) % %ÉÏÏÂÕâÁ½¸ö¹«Ê½Êǵȼ۵Äkesi = wa * wb * (ua - ub)^2; if( kesi > max_kesi ) max_kesi = kesi T = k end end% get bw img bw_img = imgfor i = 1 : 1 : row_img for j = 1 : 1 : col_img if ( img(i,j) <= T ) bw_img(i,j) = 0elsebw_img( i,j ) = 255 end end endimwrite(bw_img,'bw_img.bmp')figure(),imshow('bw_img.bmp')%,title('bw_ing')区域生长法:close all;clear all;clc;A=dicomread('im.dcm');%读入图像(医学CT图像)% seed=[200,220];%选择起始位置thresh=6.3;%相似性选择阈值%A=rgb2gray(A0);%A=A0;%灰度化%A=imadjust(A,[min(min(double(A)))/255,max(max(double(A)))/255],[]); figure,imshow(A,[]);A=double(A); %将图像灰度化[y,x]=getpts; %获得区域生长起始点x1=round(x); %横坐标取整y1=round(y); %纵坐标取整seed=A(x1,y1);B=A;%将A赋予B[r,c]=size(B);%图像尺寸r为行数,c为列数n=r*c;%计算图像所包含点的个数pixel_seed=seed;%原图起始点灰度值q=[x1 y1];%q用来装载起始位置top=1;%循环判断flagM=zeros(r,c);%建立一个与原图形同等大小的矩阵M(x1,y1)=1;%将起始点赋为1,其余为0count=1;%计数器while top~=0%也可以写成top!=0 循环结束条件r1=q(1,1);%起始点行位置c1=q(1,2);%起始点列位置p=A(r1,c1);%起始点灰度值dge=0;for i=-1:1%周围点的循环判断for j=-1:1if r1+i<=r&r1+i>0&c1+j<=c&c1+j>0%保证在点周围范围之内if abs(A(r1+i,c1+j)-p)<=thresh&M(r1+i,c1+j)~=1%判定条件?top=top+1;%满足判定条件top加1,top为多少,则q的行数有多少行q(top,:)=[r1+i c1+j];%将满足判定条件的周围点的位置赋予q,q记载了满足判定的每一外点M(r1+i,c1+j)=1;%满足判定条件将M中相对应的点赋为1count=count+1;%统计满足判定条件的点个数,其实与top此时的值一样B(r1+i,c1+j)=1;%满足判定条件将B中相对应的点赋为1endif M(r1+i,c1+j)==0;%如果M中相对应点的值为0将dge赋为1,也是说这几个点不满足条件dge=1;%将dge赋为1endelsedge=1;%点在图像外将dge赋为1endendend%此时对周围几点判断完毕,在点在图像外或不满足判定条件则将dge赋为1,满足条件dge为0if dge~=1%最后判断的周围点(i=1,j=1)是否满足条件,如dge=0,满足。

dge=1,不满足。

B(r1,c1)=A(x1,y1);%将原图像起始位置灰度值赋予Bendif count>=n%如果满足判定条件的点个数大于等于ntop=1;endq=q(2:top,:);top=top-1;end%subplot(1,2,1),figure,imshow(M,'displayrange',[]);figure,%subplot(1,2,2),imshow(B,[]);%最后是做的一个例子迭代法:clear all;I=imread('rice.png');ZMAX=max(max(I)); %取出最大灰度值ZMIN=min(min(I)); %取出最小灰度值TK=(ZMAX+ZMIN)/2;bcal=1;ISIZE=size(I); %读出图像大小while(bcal)iForeground=0; %定义前景和背景数iBackground=0;ForegroundSum=0; %定义前景和背景灰度总和BackgroundSum=0;for i=1:ISIZE(1) %循环部分求解读下%for j=1:ISIZE(2)tmp=I(i,j);if(tmp>=TK)iForeground=iForeground+1;ForegroundSum=ForegroundSum+double(tmp); %前景灰度值elseiBackground=iBackground+1;BackgroundSum=BackgroundSum+double(tmp);endendendZO=ForegroundSum/iForeground; %计算前景和背景的平均值ZB=BackgroundSum/iBackground;TKTmp=uint8(ZO+ZB)/2;if(TKTmp==TK )bcal=0;elseTK=TKTmp;end %当阈值不再变化的时候,说明迭代结束enddisp(strcat('迭代后的阀值:',num2str(double(TK)))); %显示迭代计算后阈值TKnewI=im2bw(I,double(TK)/255);subplot(1,2,1);imshow(I);xlabel('(a)原始图像');subplot(1,2,2);imshow(newI);xlabel('(b)迭代法分割效果图') %程序OK 没问题。

相关主题