欢迎来到天天文库
浏览记录
ID:43482773
大小:43.51 KB
页数:11页
时间:2019-10-07
《部分图像分割的方法(matlab)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、大津法:functiony1=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('afteradjust');count=imhist(gray);[r,t]=size(gray);n=r*t;l=256;count=count/n;%各级灰度出现的概率fori=2:l
2、 ifcount(i)~=0 st=i-1; break endend%以上循环语句实现寻找出现概率不为0的最小灰度值fori=l:-1:1 ifcount(i)~=0; nd=i-1; break endend%实现找出出现概率不为0的最大灰度值f=count(st+1:nd+1);p=st;q=nd-st;%p和分别是灰度的起始和结束值u=0;fori=1:q; u=u+f(i)*(p+i-1); u
3、a(i)=u;end%计算图像的平均灰度值fori=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;ifth4、(gray(i,j)); endendfori=1:r forj=1:t if(x1(i,j)>th) y1(i,j)=x1(i,j); else y1(i,j)=0; end endend%上面一段代码实现分割%figure,imshow(y1);%title('灰度门限分割的图像');程序二:clc; clear; cd 'D:My DocumentsMATLAB' time = now; I = i5、mread('qr4.bmp'); figure(1),imshow(I),title('p1_1.bmp'); % show the picture I2 = 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 = 6、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) % 7、T1 = edge(T,'sobel'); % figure,imshow(T1); % BW = edge(T,'sobel'); % figure,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,25
4、(gray(i,j)); endendfori=1:r forj=1:t if(x1(i,j)>th) y1(i,j)=x1(i,j); else y1(i,j)=0; end endend%上面一段代码实现分割%figure,imshow(y1);%title('灰度门限分割的图像');程序二:clc; clear; cd 'D:My DocumentsMATLAB' time = now; I = i
5、mread('qr4.bmp'); figure(1),imshow(I),title('p1_1.bmp'); % show the picture I2 = 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 =
6、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) %
7、T1 = edge(T,'sobel'); % figure,imshow(T1); % BW = edge(T,'sobel'); % figure,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,25
此文档下载收益归作者所有