matlab 图像分割算法源码
MATLAB纹理分割方法对细胞图片分割

MATLAB纹理分割方法对细胞图片分割%1.读取图像。
代码如下:I = imread('F:\xb\xb.tif');%读取图像figure(1); imshow(I);%显示原图像%2.创建纹理图像。
代码如下:E = entropyfilt(I);%创建纹理图像Eim = mat2gray(E);%转化为灰度图像figure(2),imshow(Eim);%显示灰度图像BW1 = im2bw(Eim, .8);%转化为二值图像figure(3), imshow(BW1);%显示二值图像%3.分别显示图像的底部纹理和顶部纹理。
代码如下:BWao = bwareaopen(BW1,2000);%提取底部纹理figure(4), imshow(BWao);%显示底部纹理图像nhood = true(9);closeBWao = imclose(BWao,nhood);%形态学关操作figure(5), imshow(closeBWao)%显示边缘光滑后的图像roughMask = imfill(closeBWao,'holes');%填充操作figure(6),imshow(roughMask);%显示填充后的图像I2 = I;I2(roughMask) = 0;%底部设置为黑色figure(7), imshow(I2);%突出显示图像的顶部%4.使用entropyfilt进行滤波分割。
代码如下:E2 = entropyfilt(I2);%创建纹理图像E2im = mat2gray(E2);%转化为灰度图像figure(8),imshow(E2im);%显示纹理图像BW2 = im2bw(E2im,graythresh(E2im));%转化为二值图像figure(9), imshow(BW2)%显示二值图像mask2 = bwareaopen(BW2,1000);%求取图像顶部的纹理掩膜figure(10),imshow(mask2);%显示顶部纹理掩膜图像texture1 = I; texture1(~mask2) = 0;%底部设置为黑色texture2 = I; texture2(mask2) = 0;%顶部设置为黑色figure(11),imshow(texture1);%显示图像顶部figure(12),imshow(texture2);%显示图像底部boundary = bwperim(mask2);%求取边界segmentResults = I;segmentResults(boundary) = 255;%边界处设置为白色figure(13),imshow(segmentResults);%显示分割结果%5.使用stdfilt和rangefilt进行滤波分割。
matlab程序代码 关于医学图像分割处理 边缘检测 阈值法

matlab程序代码关于医学图像分割处理边缘检测阈值法图像分割程序:% This is a program for extracting objects from an image. Written for vehicle number plate segmentation and extraction % Authors : Jeny Rajan, Chandrashekar P S % U can use attached test image for testing % input - give the image file name as input. eg :- car3.jpg clc; clear all; k=input('Enter the file name','s'); % input image; color image im=imread(k); im1=rgb2gray(im); im1=medfilt2(im1,[3 3]); %Median filtering the image to remove noise% BW = edge(im1,'sobel'); %finding edges [imx,imy]=size(BW); msk=[0 0 0 0 0; 0 1 1 1 0; 0 1 1 1 0; 0 1 1 1 0;0 0 0 0 0;]; B=conv2(double(BW),double(msk)); %Smoothing imageto reduce the number of connected components L = bwlabel(B,8);% Calculating connected components mx=max(max(L)) % There will be mx connected components.Here U can give a value between 1 and mx for L or in a loop you can extract all connected components % If you are using the attached car image, by giving 17,18,19,22,27,28 to L you can extract the number plate completely. [r,c] = find(L==17); rc = [r c];[sx sy]=size(rc); n1=zeros(imx,imy); for i=1:sx x1=rc(i,1); y1=rc(i,2); n1(x1,y1)=255; end % Storing the extracted image in an array figure,imshow(im); figure,imshow(im1); figure,imshow(B); figure,imshow(n1,[]);边缘检测:I=imread('lena.jpg'); imshow(I); title('原始图像'); BW1= edge(I,'Canny',0.00) ; %edge调用Canny为检测算子判别阈值为0.00 figure,imshow(BW1); title( '阈值为0.00的Canny算子边缘检测图像'); BW2= edge(I,'Canny',0.05) ; %edge调用Canny为检测算子判别阈值为0.05 figure,imshow(BW2); title( '阈值为0.05的Canny算子边缘检测图像'); BW20= edge(I,'Canny',0.1) ; %edge调用Canny为检测算子判别阈值为0.1 figure,imshow(BW20); title( '阈值为0.1的Canny算子边缘检测图像'); BW21= edge(I,'Canny',0.2) ; %edge调用Canny为检测算子判别阈值为0.2 figure,imshow(BW21); title( '阈值为0.2的Canny算子边缘检测图像'); BW22= edge(I,'Canny',0.3) ; %edge调用Canny为检测算子判别阈值为0.3 figure,imshow(BW22); title( '阈值为0.3的Canny算子边缘检测图像 ');。
基于Matlab的彩色图像分割

3 Matlab编程实现3.1 Matlab编程过程用Matlab来分割彩色图像的过程如下:1)获取图像的RGB颜色信息。
通过与用户的交互操作来提示用户输入待处理的彩色图像文件路径;2)RGB彩色空间到lab彩色空间的转换。
通过函数makecform()和applycform()来实现; 3)对ab分量进行Kmean聚类。
调用函数kmeans()来实现;4)显示分割后的各个区域。
用三副图像分别来显示各个分割目标,背景用黑色表示。
3.2 Matlab程序源码%文件读取clear;clc;file_name = input('请输入图像文件路径:','s');I_rgb = imread(file_name); %读取文件数据figure();imshow(I_rgb); %显示原图title('原始图像');%将彩色图像从RGB转化到lab彩色空间C = makecform('srgb2lab'); %设置转换格式I_lab = applycform(I_rgb, C);%进行K-mean聚类将图像分割成3个区域ab = double(I_lab(:,:,2:3)); %取出lab空间的a分量和b分量nrows = size(ab,1);ncols = size(ab,2);ab = reshape(ab,nrows*ncols,2);nColors = 3; %分割的区域个数为3[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',3); %重复聚类3次pixel_labels = reshape(cluster_idx,nrows,ncols);figure();imshow(pixel_labels,[]), title('聚类结果');%显示分割后的各个区域segmented_images = cell(1,3);rgb_label = repmat(pixel_labels,[1 1 3]);for k = 1:nColorscolor = I_rgb;color(rgb_label ~= k) = 0;segmented_images{k} = color;endfigure(),imshow(segmented_images{1}), title('分割结果——区域1'); figure(),imshow(segmented_images{2}), title('分割结果——区域2'); figure(),imshow(segmented_images{3}), title('分割结果——区域3');。
如何在Matlab中进行图像分割

如何在Matlab中进行图像分割图像分割是图像处理中十分重要的一项技术,它能够将图像划分为多个具有独立意义的区域,有助于进一步的图像分析和处理。
在Matlab中进行图像分割,我们可以利用许多现成的函数和工具箱,使得整个过程更加高效和便捷。
本文将介绍如何在Matlab中进行图像分割,包括基于阈值的分割方法、基于边缘的分割方法以及基于区域的分割方法。
首先,基于阈值的分割方法是最简单和常用的图像分割方法之一。
它基于图像的亮度或颜色信息,将图像分为不同的区域。
在Matlab中,我们可以使用im2bw 函数将彩色图像转换为二值图像,然后使用graythresh函数或multithresh函数确定适当的阈值。
例如,下面的代码演示了如何使用阈值进行图像分割:```matlabimg = imread('image.jpg');grayImg = rgb2gray(img);threshold = graythresh(grayImg);binaryImg = im2bw(grayImg, threshold);```其次,基于边缘的分割方法是通过检测图像中的边缘信息来实现图像分割。
在Matlab中,我们可以使用一系列边缘检测算法,如Sobel算子、Canny算子等。
这些算法可以提取图像中的边缘信息,并将其转化为二值图像。
下面的代码演示了如何使用Canny算子进行图像分割:```matlabimg = imread('image.jpg');grayImg = rgb2gray(img);edgeImg = edge(grayImg, 'canny');```最后,基于区域的分割方法是将图像分为具有相似纹理、颜色或形状特征的区域。
在Matlab中,我们可以使用基于区域的分割算法,如分水岭算法、区域生长算法等。
这些算法可以通过对图像进行区域合并或区域分裂来实现图像分割。
btd分解算法matlab代码

btd分解算法matlab代码BTD(Binary Tree Decomposition)分解算法是一种用于图像处理和计算机视觉领域的算法,用于对图像进行分割和分解。
在MATLAB中,你可以使用以下代码来实现BTD分解算法:matlab.function [segments, tree] = btd_decomposition(image)。
% 参数设置。
minSegmentSize = 100; % 最小分割尺寸。
maxLevels = 10; % 最大分解层数。
% 初始化二叉树。
tree = binary_tree_initialize(image);% 递归分解。
tree = recursive_decomposition(tree, 1, maxLevels, minSegmentSize);% 提取分割结果。
segments = extract_segments(tree, image);function tree = binary_tree_initialize(image)。
% 创建二叉树结构。
tree = struct('level', 0, 'segment', [], 'children', []);% 初始化根节点。
tree.level = 1;tree.segment = struct('bbox', [1, 1, size(image, 2), size(image, 1)], 'meanColor', mean(image(:)), 'size',numel(image));function tree = recursive_decomposition(tree, level, maxLevels, minSegmentSize)。
% 检查是否达到最大层数或者分割尺寸。
if level >= maxLevels || tree.segment.size <= minSegmentSize.return;end.% 在当前节点进行分割。
部分图像分割的方法(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,满足。
图像分块matlab程序

图像分块matlab程序看到不少⽹友,浏览了之前写读的图像分块简单版,现在将之前写的复杂版也贡献与⼤家⼀起学习、讨论。
clear,clc;[filename,filepath] = uigetfile('*.*','Select the image');if isequal(filename,0)||isequal(filepath,0)return;elsefilefullpath=[filepath,filename];end%得到⽂件名,以便建⽴⼀个⽂件夹保存分块图像[pathstr,name,ext] = fileparts(filename);Im=imread(filefullpath);% imshow(Im)L = size(Im);%分块⼤⼩height=300;width=400;%重叠x=0.25;h_val=height*(1-x);w_val=width*(1-x);max_row = (L(1)-height)/h_val+1;max_col = (L(2)-width)/w_val+1;%判断能否完整分块if max_row==fix(max_row)%判断是否能够整分max_row=max_row;elsemax_row=fix(max_row+1);endif max_col==fix(max_col)%判断是否能够整分max_col=max_col;elsemax_col=fix(max_col+1);endseg = cell(max_row,max_col);for row = 1:max_rowfor col = 1:max_colif ((width+(col-1)*w_val)>L(2)&&((row-1)*h_val+height)<=L(1))%判断最右边不完整的部分seg(row,col)= {Im((row-1)*h_val+1:height+(row-1)*h_val,(col-1)*w_val+1:L(2),:)};elseif((height+(row-1)*h_val)>L(1)&&((col-1)*w_val+width)<=L(2))%判断最下边不完整的部分seg(row,col)= {Im((row-1)*h_val+1:L(1),(col-1)*w_val+1:width+(col-1)*w_val,:)};elseif((width+(col-1)*w_val)>L(2)&&((row-1)*h_val+height)>L(1))%判断最后⼀张seg(row,col)={Im((row-1)*h_val+1:L(1),(col-1)*w_val+1:L(2),:)};elseseg(row,col)= {Im((row-1)*h_val+1:height+(row-1)*h_val,(col-1)*w_val+1:width+(col-1)*w_val,:)}; %其余完整部分 endendendimshow(Im);hold onsystem(['mkdir ',name]);%创建与图⽚名相同的⽂件⽤来保存图⽚paths=[pwd,'\',name]; %获取指定⽂件夹⽬录%保存⼦图for i=1:max_rowfor j=1:max_colimwrite(seg{i,j},[paths,'\',strcat(int2str(i),'⾏',int2str(j),'列','.bmp')]); %把第i帧的图⽚写为'mi.bmp'endend% 画框显⽰for row = 1:max_rowfor col = 1:max_colc=rand(1,3);%随机颜⾊rectangle('Position',[w_val*(col-1),h_val*(row-1),width,height],...'LineWidth',2,'LineStyle','-','EdgeColor',c);end end。
图像分割技术的matlab实现

f=rgb2gray(f); % 将彩色图像转换为灰度图像f=im2double(f); % 转换为双精度,便于后面的计算figure, imshow(f),title('Original Image'),PF=edge(f,'prewitt'); % 边缘探测,算子为prewitt figure,imshow(PF),title('Prewitt Filter');RF=edge(f,'roberts'); % 边缘探测,算子为roberts figure,imshow(RF),title('Roberts Filter');LF=edge(f,'log'); % 边缘探测,算子为logfigure,imshow(LF),title('Laplacian of Gaussian (LoG) Filter');CF=edge(f,'canny'); % 边缘探测,算子为canny figure,imshow(CF),title('Canny Filter');f=rgb2gray(f); % 灰度转换f=im2double(f); % 数据类型转换% 使用垂直Sobel算子,自动选择阈值[VSFAT Threshold]=edge(f,'sobel','vertical'); % 边缘探测figure, imshow(f),title('Original Image'), % 显示原始图像figure,imshow(VSFAT),title('Sobel Filter - Automatic Threshold'); % 显示边缘探测图像%使用水平和垂直Sobel算子,自动选择阈值SFST=edge(f,'sobel',Threshold);figure,imshow(SFST),title('Sobel Filter (Horizontal and Vertical)'); % 显示边缘探测图像%使用指定45度角Sobel算子滤波器,指定阈值s45=[-2 -1 0;-1 0 1;0 1 2];SFST45=imfilter(f,s45,'replicate');SFST45=SFST45>=Threshold;figure,imshow(SFST45),title('Sobel Filter (45 Degree)'); % 显示边缘探测图像%使用指定-45度角Sobel算子滤波器,指定阈值sm45=[0 1 2;-1 0 1;-2 -1 0];SFSTM45=imfilter(f,sm45,'replicate');SFSTM45=SFSTM45>=Threshold;figure,imshow(SFSTM45),title('Sobel Filter (-45 Degree)'); % 显示边缘探测图像I = imread('circuit.tif');rotI = imrotate(I,33,'crop'); % 图像旋转,该函数具体用法在本书13.3.3有介绍。
如何使用MATLAB进行图像分割处理

如何使用MATLAB进行图像分割处理图像分割是计算机视觉领域中的一项重要任务,它可以将图像中的不同区域分割出来,为后续的图像分析和理解提供基础。
MATLAB作为一种强大的数学计算工具和编程语言,提供了丰富的图像处理函数和工具箱,可以方便地进行图像分割处理。
本文将介绍如何使用MATLAB进行图像分割处理。
首先,我们需要加载图像。
MATLAB提供了imread函数用于读取图像文件。
例如,我们可以使用以下代码加载一张名为"image.jpg"的图像:```matlabimage = imread('image.jpg');```加载图像后,我们可以对图像进行预处理。
预处理的目的是为了减少噪声和增强图像的对比度,从而更好地进行分割。
MATLAB提供了丰富的图像预处理函数,如imresize、imadjust、imnoise等。
我们可以根据实际需求选择适当的函数进行预处理。
例如,以下代码使用imadjust函数对图像进行对比度增强:```matlabimage = imadjust(image);```接下来,我们可以选择合适的分割算法对图像进行分割。
MATLAB提供了多种图像分割算法,如阈值分割、区域生长、边缘检测等。
我们可以根据图像的特点和需求选择适合的算法。
以下是一种常用的阈值分割算法的示例代码:```matlabthreshold = graythresh(image);binaryImage = imbinarize(image, threshold);```在上述代码中,graythresh函数计算出一个合适的阈值,然后imbinarize函数将图像转化为二值图像。
通过调整阈值的大小,我们可以控制分割的精度和效果。
除了阈值分割,MATLAB还提供了更复杂的分割算法,如基于区域的分割算法。
这些算法可以根据图像中的区域特征进行分割,例如颜色、纹理、形状等。
以下是一种基于区域的分割算法的示例代码:```matlabsegmented = regiongrowing(image, seed);```在上述代码中,regiongrowing函数根据种子点对图像进行区域生长分割。
如何进行图像分割的Matlab实现

如何进行图像分割的Matlab实现引言:图像分割是计算机视觉领域的一项基础技术,它将图像中的像素点分为不同的区域,使得具有相似特征的像素被聚类到一起。
在图像分析、目标检测、图像处理等任务中,图像分割起着至关重要的作用。
本文将介绍如何使用Matlab实现图像分割算法,包括传统的阈值分割、基于区域的分割以及基于深度学习的分割等。
一、传统的阈值分割1.1 简介阈值分割是最简单和常用的图像分割方法之一,它根据像素的灰度值与阈值的比较结果将像素分为两类:前景和背景。
在Matlab中,可以使用函数`im2bw`实现二值化分割任务。
1.2 实现步骤(1)加载图像:使用`imread`函数读取待分割的图像,并将其转换为灰度图像。
(2)确定阈值:根据图像的灰度直方图,可以通过分析波峰和波谷来确定一个适合的阈值。
(3)二值化分割:使用`im2bw`函数将灰度图像二值化,得到分割后的图像。
(4)结果显示:使用`imshow`函数将原图像和分割结果进行显示。
二、基于区域的分割2.1 简介基于区域的分割方法将图像划分为具有一定连续性和相似性质的区域,其基本思想是将图像中相似的像素组成区域,并对区域进行合并或分裂,以达到分割的目的。
2.2 实现步骤(1)加载图像:同样使用`imread`函数读取待分割的图像。
(2)图像预处理:可选的预处理步骤包括噪声去除、图像增强等,以提供更好的分割效果。
(3)区域生长:选择一个适当的种子点作为起始点,在附近的像素中根据一定的准则来判断是否属于同一区域,并逐步生长扩展区域,直至满足停止准则。
(4)结果显示:使用`imshow`函数将原图像和分割结果进行显示。
三、基于深度学习的分割3.1 简介基于深度学习的分割方法是近年来发展起来的一种高效且准确的分割技术,主要基于深度卷积神经网络(CNN)和全卷积网络(FCN)。
深度学习模型通过学习大量标注的图像,能够学习到图像的高级特征,从而实现更准确的图像分割。
图像分割代码

Canny边缘分割clc;a = imread('1.bmp');a=rgb2gray(a); % 选取的是jpg格式的图片,试用要进行灰度处理imshow(a);title('灰度图');ffta = fft2(a); % 获取2维离散傅里叶变化后的图像,保存到fftIsffta = fftshift(ffta); % 将傅里叶变化的中心移到图像中心,保存到sfftIRR = real(sffta); % 取实部II = imag(sffta); % 取虚部A = sqrt(RR.^2 + II.^2); % 计算频谱幅值A = (A - min(min(A)))/(max(max(A)) - min(min(A)))*225; % 灰度拉升,将变换后的图像拉升到0~255区间b=edge(a,'canny',[0.03,0.06]); %灰度图的边缘提取c=edge(a,'canny',[0.05,0.1]);d=edge(a,'canny',[0.05,0.1],2);figure;subplot(1,3,1),imshow(b), axis on;title('canny 阈值=0.02');subplot(1,3,2),imshow(c), axis on;title('canny 阈值=0.07');subplot(1,3,3),imshow(d),axis on;title('默认');figure;imshow(A);title('频谱图');Soble算子-阈值不同时的边缘分割a = imread('1.bmp');a=rgb2gray(a); % 灰度处理b=edge(a,'sobel',0.02);c=edge(a,'sobel',0.07);[d,e]=edge(a,'sobel');subplot(1,3,1),imshow(b), axis on;title('sobel 阈值=0.02');subplot(1,3,2),imshow(c), axis on;title('sobel 阈值=0.07')subplot(1,3,3),imshow(d),axis on;title('默认');四种算子的边缘分割I=imread('1.bmp');I=rgb2gray(I);imshow(I);title('原始图像');BW1=edge(I,'Roberts ',0.3); %edge调用Roberts为检测算子判别阈值为0.3 figure,imshow(BW1);title( '阈值为0.3的Roberts算子边缘检测图像');BW2=edge(I, 'sobel ',0.3); %edge调用sobel为检测算子判别阈值为0.3 figure,imshow(BW2);title( '阈值为0.3的sobel算子边缘检测图像');BW3=edge(I,'Prewitt ',0.3); %edge调用Prewitt为检测算子判别阈值为0.3 figure,imshow(BW3);title( '阈值为0.3的Prewitt算子边缘检测图像');BW4= edge(I,'Canny',0.3) ;%edge调用Canny为检测算子判别阈值为0.3 figure,imshow(BW4);title( '阈值为0.3的Canny算子边缘检测图像');A=BW1(x,y);B=BW2(x,y);C=BW3(x,y);D=BW4(x,y);E=A+B+C+D;figure,imshow(E);title( 'jia');LOG算子边缘分割I=imread ('1.bmp');I=rgb2gray(I);BW1=edge(I,'log',0.00);figure,imshow(BW1);title('阈值为0.00的LOG算子边缘检测图像');BW11=edge(I,'log',0.01);figure,imshow(BW11);title('阈值为0.01的LOG算子边缘检测图像');BW2= edge(I,'log',0.03);figure,imshow(BW2);title('阈值为0.03的LOG算子边缘检测图像');BW22= edge(I,'log',0.05);figure,imshow(BW22);title('阈值为0.05的LOG算子边缘检测图像');加噪后均值滤波clc;img=imread('1.bmp');img_0=rgb2gray(img);img_1=imnoise(img_0,'salt & pepper',0.02);img_2=medfilt2(img_1);subplot(2,2,1);imshow(img);title('原始图像');subplot(2,2,2);imshow(img_1);title('加入噪声后图像'); subplot(2,2,3);imshow(img_2);title('中值滤波后图像');加噪声后中值滤波clc;img=imread('1.bmp');img_0=rgb2gray(img);img_1=imnoise(img_0,'salt & pepper',0.02);img_2=medfilt2(img_1);subplot(2,2,1);imshow(img);title('原始图像');subplot(2,2,2);imshow(img_1);title('加入噪声后图像'); subplot(2,2,3);imshow(img_2);title('中值滤波后图像');阈值分割clc;A=imread('1.bmp');figuresubplot(1,5,1),imshow(A);title('原图像')B=im2bw(A,91/255);subplot(1,5,2),imshow(B);title('阈值91的图像')C=im2bw(A,71/255);subplot(1,5,3),imshow(C);title('阈值71的图像')双重处理A=imread('1.bmp');figure,B=rgb2gray(A);C=imnoise(B,'salt & pepper',0.02);D=medfilt2(B);subplot(2,2,1)imshow(A)title('原始图像')subplot(2,2,2)imshow(D)title('中值滤波')E=im2bw(D,91/255);subplot(2,2,3),imshow(E);title('阈值91的中值滤波图像'), F=im2bw(D,71/255);subplot(2,2,4),imshow(F);title('阈值71的中值滤波图像'),。
数字图像处理代码Ch7《图像分割》

来自《数字图像处理MATLAB版》书中示例,代码复制与运行结果。
例7.1 点检测。
代码:clc,clear,close all;f = imread('Fig1002(a).tif');imshow(f),title('(a)原始图像');w=[-1 -1 -1;-1 8 -1; -1 -1 -1 ]; %一个合适的点检测模板g=abs(imfilter(tofloat(f),w)); %abs操作不接受整数数据T=max(g(:));g=g>=T; %找到满足g>=T的所有点,赋值给g figure; imshow(g);title('(b)检测出的孤立点');运行结果:例7.2 检测指定方向的线。
代码:clc;clear,close all;f = imread('Fig1004(a).tif');subplot 231; imshow(f);title('(a)连线模板图像')w = [2 -1 -1;-1 2 -1; -1 -1 2]; %+45度线检测模板g = imfilter(tofloat(f),w);subplot 232 ,imshow(g,[]),title('(b)使用 +45度检测器处理后的图像') gtop = g(1:150,1:150); %左上角部分gtop = pixeldup(gtop,4); %enlarge by pixel duplication(就是维度*4) subplot 233; imshow(gtop,[])title('(c)+45度检测器处理后的图像左上角') gbot = g(end-119:end,end-119:end);gbot = pixeldup(gbot,4);subplot 234 ;imshow(gbot,[]),title('(d)-45度检测器处理后的图像右下角') g = abs(g);subplot 235; imshow(g,[]),title('(e)图(b)的绝对值')T = max(g(:)); g = g>=T;subplot 236 ;imshow(g),title('(f)满足条件[g>=T]的所有点(白色点)[其中g是图(e)]')运行结果:例7.3 使用Sobel边缘检测器。
matlab二值化分割代码

matlab二值化分割代码
以下是一个简单的 MATLAB 代码示例,用于对图像进行二值化分割:
matlab.
% 读取图像。
img = imread('your_image.jpg');
% 将图像转换为灰度图像。
gray_img = rgb2gray(img);
% 使用Otsu方法进行图像二值化。
level = graythresh(gray_img);
bw_img = imbinarize(gray_img,level);
% 显示原始图像和二值化图像。
subplot(1,2,1), imshow(gray_img), title('Original Image');
subplot(1,2,2), imshow(bw_img), title('Binary Image');
在这个示例中,首先使用 `imread` 函数读取图像,然后使用`rgb2gray` 函数将图像转换为灰度图像。
接下来,使用
`graythresh` 函数确定阈值,并使用 `imbinarize` 函数将灰度图像转换为二值化图像。
最后,使用 `subplot` 和 `imshow` 函数显示原始图像和二值化图像。
需要注意的是,这只是一个简单的二值化分割示例。
实际应用中可能需要根据具体的图像特征和要求进行参数调整和算法优化。
图像分割部分源代码

1.图像反转MATLAB程序实现如下:I=imread('xian.bmp');J=double(I);J=-J+(256-1); %图像反转线性变换H=uint8(J);subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(H);2.灰度线性变换MATLAB程序实现如下:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title('原始图像');axis([50,250,50,200]);axis on; %显示坐标系I1=rgb2gray(I);subplot(2,2,2),imshow(I1);title('灰度图像');axis([50,250,50,200]);axis on; %显示坐标系J=imadjust(I1,[0.1 0.5],[]); %局部拉伸,把[0.1 0.5]内的灰度拉伸为[0 1] subplot(2,2,3),imshow(J);title('线性变换图像[0.1 0.5]');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系K=imadjust(I1,[0.3 0.7],[]); %局部拉伸,把[0.3 0.7]内的灰度拉伸为[0 1] subplot(2,2,4),imshow(K);title('线性变换图像[0.3 0.7]');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系3.非线性变换MATLAB程序实现如下:I=imread('xian.bmp');I1=rgb2gray(I);subplot(1,2,1),imshow(I1);title('灰度图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系J=double(I1);J=40*(log(J+1));H=uint8(J);subplot(1,2,2),imshow(H);title('对数变换图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系4.直方图均衡化MATLAB程序实现如下:I=imread('xian.bmp');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实现领域平均法抑制噪声程序:I=imread('xian.bmp');subplot(231)imshow(I)title('原始图像')I=rgb2gray(I);I1=imnoise(I,'salt & pepper',0.02);subplot(232)imshow(I1)title('添加椒盐噪声的图像')k1=filter2(fspecial('average',3),I1)/255; %进行3*3模板平滑滤波k2=filter2(fspecial('average',5),I1)/255; %进行5*5模板平滑滤波k3=filter2(fspecial('average',7),I1)/255; %进行7*7模板平滑滤波k4=filter2(fspecial('average',9),I1)/255; %进行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模板平滑滤波');6.中值滤波器用MATLAB实现中值滤波程序如下:I=imread('xian.bmp');I=rgb2gray(I);J=imnoise(I,'salt&pepper',0.02);subplot(231),imshow(I);title('原图像');subplot(232),imshow(J);title('添加椒盐噪声图像');k1=medfilt2(J); %进行3*3模板中值滤波k2=medfilt2(J,[5,5]); %进行5*5模板中值滤波k3=medfilt2(J,[7,7]); %进行7*7模板中值滤波k4=medfilt2(J,[9,9]); %进行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算子和拉普拉斯对图像锐化:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title('原始图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I1=im2bw(I);subplot(2,2,2),imshow(I1);title('二值图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系H=fspecial('sobel'); %选择sobel算子J=filter2(H,I1); %卷积运算subplot(2,2,3),imshow(J);title('sobel算子锐化图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系h=[0 1 0,1 -4 1,0 1 0]; %拉普拉斯算子J1=conv2(I1,h,'same'); %卷积运算subplot(2,2,4),imshow(J1);title('拉普拉斯算子锐化图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系8.梯度算子检测边缘用MATLAB实现如下:I=imread('xian.bmp');subplot(2,3,1);imshow(I);title('原始图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I1=im2bw(I);subplot(2,3,2);imshow(I1);title('二值图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I2=edge(I1,'roberts');figure;subplot(2,3,3);imshow(I2);title('roberts算子分割结果');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I3=edge(I1,'sobel');subplot(2,3,4);imshow(I3);title('sobel算子分割结果');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I4=edge(I1,'Prewitt');subplot(2,3,5);imshow(I4);title('Prewitt算子分割结果');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系9.LOG算子检测边缘用MATLAB程序实现如下:I=imread('xian.bmp');subplot(2,2,1);imshow(I);title('原始图像');I1=rgb2gray(I);subplot(2,2,2);imshow(I1);title('灰度图像');I2=edge(I1,'log');subplot(2,2,3);imshow(I2);title('log算子分割结果');10.Canny算子检测边缘用MATLAB程序实现如下:I=imread('xian.bmp');subplot(2,2,1);imshow(I);title('原始图像')I1=rgb2gray(I);subplot(2,2,2);imshow(I1);title('灰度图像');I2=edge(I1,'canny');subplot(2,2,3);imshow(I2);title('canny算子分割结果');11.边界跟踪(bwtraceboundary函数)clcclear allI=imread('xian.bmp');figureimshow(I);title('原始图像');I1=rgb2gray(I); %将彩色图像转化灰度图像threshold=graythresh(I1); %计算将灰度图像转化为二值图像所需的门限BW=im2bw(I1, threshold); %将灰度图像转化为二值图像figureimshow(BW);title('二值图像');dim=size(BW);col=round(dim(2)/2)-90; %计算起始点列坐标row=find(BW(:,col),1); %计算起始点行坐标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' ,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,'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,'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,'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;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).point2);if(len>max_len)max_len=len;xy_long=xy;endendplot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');13.直方图阈值法用MATLAB实现直方图阈值法:I=imread('xian.bmp');I1=rgb2gray(I);figure;subplot(2,2,1);imshow(I1);title('灰度图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系[m,n]=size(I1); %测量图像尺寸参数GP=zeros(1,256); %预创建存放灰度出现概率的向量for k=0:255GP(k+1)=length(find(I1==k))/(m*n); %计算每级灰度出现的概率,将其存入GP中相应位置endsubplot(2,2,2),bar(0:255,GP,'g') %绘制直方图title('灰度直方图')xlabel('灰度值')ylabel('出现概率')I2=im2bw(I,150/255);subplot(2,2,3),imshow(I2);title('阈值150的分割图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I3=im2bw(I,200/255); %subplot(2,2,4),imshow(I3);title('阈值200的分割图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系14. 自动阈值法:Otsu法用MATLAB实现Otsu算法:clcclear allI=imread('xian.bmp');subplot(1,2,1),imshow(I);title('原始图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系level=graythresh(I); %确定灰度阈值BW=im2bw(I,level);subplot(1,2,2),imshow(BW);title('Otsu法阈值分割图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系15.膨胀操作I=imread('xian.bmp'); %载入图像I1=rgb2gray(I);subplot(1,2,1);imshow(I1);title('灰度图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系se=strel('disk',1); %生成圆形结构元素I2=imdilate(I1,se); %用生成的结构元素对图像进行膨胀subplot(1,2,2);imshow(I2);title('膨胀后图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系16.腐蚀操作MATLAB实现腐蚀操作I=imread('xian.bmp'); %载入图像I1=rgb2gray(I);subplot(1,2,1);imshow(I1);title('灰度图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系se=strel('disk',1); %生成圆形结构元素I2=imerode(I1,se); %用生成的结构元素对图像进行腐蚀subplot(1,2,2);imshow(I2);title('腐蚀后图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系17.开启和闭合操作用MATLAB实现开启和闭合操作I=imread('xian.bmp'); %载入图像subplot(2,2,1),imshow(I);title('原始图像');axis([50,250,50,200]);axis on; %显示坐标系I1=rgb2gray(I);subplot(2,2,2),imshow(I1);title('灰度图像');axis([50,250,50,200]);axis on; %显示坐标系se=strel('disk',1); %采用半径为1的圆作为结构元素I2=imopen(I1,se); %开启操作I3=imclose(I1,se); %闭合操作subplot(2,2,3),imshow(I2);title('开启运算后图像');axis([50,250,50,200]);axis on; %显示坐标系subplot(2,2,4),imshow(I3);title('闭合运算后图像');axis([50,250,50,200]);axis on; %显示坐标系18.开启和闭合组合操作I=imread('xian.bmp'); %载入图像subplot(3,2,1),imshow(I);title('原始图像');axis([50,250,50,200]);axis on; %显示坐标系I1=rgb2gray(I);subplot(3,2,2),imshow(I1);title('灰度图像');axis([50,250,50,200]);axis on; %显示坐标系se=strel('disk',1);I2=imopen(I1,se); %开启操作I3=imclose(I1,se); %闭合操作subplot(3,2,3),imshow(I2);title('开启运算后图像');axis([50,250,50,200]);axis on; %显示坐标系subplot(3,2,4),imshow(I3);title('闭合运算后图像');axis([50,250,50,200]);axis on; %显示坐标系se=strel('disk',1);I4=imopen(I1,se);I5=imclose(I4,se);subplot(3,2,5),imshow(I5); %开—闭运算图像title('开—闭运算图像');axis([50,250,50,200]);axis on; %显示坐标系I6=imclose(I1,se);I7=imopen(I6,se);subplot(3,2,6),imshow(I7); %闭—开运算图像title('闭—开运算图像');axis([50,250,50,200]);axis on; %显示坐标系19.形态学边界提取利用MATLAB实现如下:I=imread('xian.bmp'); %载入图像subplot(1,3,1),imshow(I);title('原始图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I1=im2bw(I);subplot(1,3,2),imshow(I1);title('二值化图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I2=bwperim(I1); %获取区域的周长subplot(1,3,3),imshow(I2);title('边界周长的二值图像');axis([50,250,50,200]);grid on;axis on;20.形态学骨架提取利用MATLAB实现如下:I=imread('xian.bmp'); subplot(2,2,1),imshow(I); title('原始图像');axis([50,250,50,200]); axis on;I1=im2bw(I);subplot(2,2,2),imshow(I1); title('二值图像');axis([50,250,50,200]); axis on;I2=bwmorph(I1,'skel',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 on;21.直接提取四个顶点坐标I = imread('xian.bmp');I = I(:,:,1);BW=im2bw(I);figureimshow(~BW)[x,y]=getpts。
FCM图像分割算法MATLAB源代码

FCM图像分割算法function fcmapp(file, cluster_n)% FCMAPP% fcmapp(file, cluter_n) segments a image named file using the algorithm% FCM.% [in]% file: the path of the image to be clustered.% cluster_n: the number of cluster for FCM.eval(['info=imfinfo(''',file, ''');']);switch info.ColorTypecase 'truecolor'eval(['RGB=imread(''',file, ''');']);% [X, map] = rgb2ind(RGB, 256);I = rgb2gray(RGB);clear RGB;case 'indexed'eval(['[X, map]=imread(''',file, ''');']);I = ind2gray(X, map);clear X;case 'grayscale'eval(['I=imread(''',file, ''');']);end;I = im2double(I);filename = file(1 : find(file=='.')-1);data = reshape(I, numel(I), 1);tic[center, U, obj_fcn]=fcm(data, cluster_n);elapsedtime = toc;%eval(['save(', filename, int2str(cluster_n),'.mat'', ''center'', ''U'', ''obj_fcn'', ''elapsedtime'');']); fprintf('elapsedtime = %d', elapsedtime);maxU=max(U);temp = sort(center, 'ascend');for n = 1:cluster_n;eval(['cluster',int2str(n), '_index = find(U(', int2str(n), ',:) == maxU);']);index = find(temp == center(n));switch indexcase 1color_class = 0;case cluster_ncolor_class = 255;otherwisecolor_class = fix(255*(index-1)/(cluster_n-1));endeval(['I(cluster',int2str(n), '_index(:))=', int2str(color_class),';']);end;filename = file(1:find(file=='.')-1);I = mat2gray(I);%eval(['imwrite(I,', filename,'_seg', int2str(cluster_n), '.bmp'');']);imwrite(I, 'temp\tu2_4.bmp','bmp');imview(I);function fcmapp(file, cluster_n)% FCMAPP% fcmapp(file, cluter_n) segments a image named file using the algorithm% FCM.% [in]% file: the path of the image to be clustered.% cluster_n: the number of cluster for FCM.eval(['info=imfinfo(''',file, ''');']);switch info.ColorTypecase 'truecolor'eval(['RGB=imread(''',file, ''');']);% [X, map] = rgb2ind(RGB, 256);I = rgb2gray(RGB);clear RGB;case 'indexed'eval(['[X, map]=imread(''',file, ''');']);I = ind2gray(X, map);clear X;case 'grayscale'eval(['I=imread(''',file, ''');']);end;I = im2double(I);filename = file(1 : find(file=='.')-1);data = reshape(I, numel(I), 1);tic[center, U, obj_fcn]=fcm(data, cluster_n);elapsedtime = toc;%eval(['save(', filename, int2str(cluster_n),'.mat'', ''center'', ''U'', ''obj_fcn'', ''elapsedtime'');']); fprintf('elapsedtime = %d', elapsedtime);maxU=max(U);temp = sort(center, 'ascend');for n = 1:cluster_n;eval(['cluster',int2str(n), '_index = find(U(', int2str(n), ',:) == maxU);']);index = find(temp == center(n));switch indexcase 1color_class = 0;case cluster_ncolor_class = 255;otherwisecolor_class = fix(255*(index-1)/(cluster_n-1));endeval(['I(cluster',int2str(n), '_index(:))=', int2str(color_class),';']); end;filename = file(1:find(file=='.')-1);I = mat2gray(I);%eval(['imwrite(I,', filename,'_seg', int2str(cluster_n), '.bmp'');']); imwrite(I, 'r.bmp');imview(I);主程序1ImageDir='.\';%directory containing the images%path('..') ;%cmpviapath('..') ;img=im2double(imresize(imread([ImageDir '12.png']),2)) ;figure(1) ; imagesc(img) ; axis image[ny,nx,nc]=size(img) ;imgc=applycform(img,makecform('srgb2lab')) ;d=reshape(imgc(:,:,2:3),ny*nx,2) ;d(:,1)=d(:,1)/max(d(:,1)) ; d(:,2)=d(:,2)/max(d(:,2)) ;%d=d ./ (repmat(sqrt(sum(d.^2,2)),1,3)+eps()) ;k=4 ; % number of clusters%[l0 c] = kmeans(d, k,'Display','iter','Maxiter',100);[l0 c] = kmeans(d, k,'Maxiter',100);l0=reshape(l0,ny,nx) ;figure(2) ; imagesc(l0) ; axis image ;%c=[ 0.37 0.37 0.37 ; 0.77 0.73 0.66 ; 0.64 0.77 0.41 ; 0.81 0.76 0.58 ; ...%0.85 0.81 0.73 ] ;%c=[0.99 0.76 0.15 ; 0.55 0.56 0.15 ] ;%c=[ 0.64 0.64 0.67 ; 0.27 0.45 0.14 ] ;%c=c ./ (repmat(sqrt(sum(c.^2,2)),1,3)+eps()) ;% Data termDc=zeros(ny,nx,k) ;for i=1:k,dif=d-repmat(c(i,:),ny*nx,1) ;Dc(:,:,i)= reshape(sum(dif.^2,2),ny,nx) ;end ;% Smoothness termSc=(ones(k)-eye(k)) ;% Edge termsg = fspecial('gauss', [13 13], 2);dy = fspecial('sobel');vf = conv2(g, dy, 'valid');Vc = zeros(ny,nx);Hc = Vc;for b=1:nc,Vc = max(Vc, abs(imfilter(img(:,:,b), vf, 'symmetric')));Hc = max(Hc, abs(imfilter(img(:,:,b), vf', 'symmetric'))); endgch=char;gch = GraphCut('open', 1*Dc, Sc,exp(-5*Vc),exp(-5*Hc)); [gch l] = GraphCut('expand',gch);gch = GraphCut('close', gch);label=l(100,200) ;lb=(l==label) ;lb=imdilate(lb,strel('disk',1))-lb ;figure(3) ; image(img) ; axis image ; hold on ;contour(lb,[1 1],'r') ; hold off ; title('no edges') ;figure(4) ; imagesc(l) ; axis image ; title('no edges') ;gch = GraphCut('open', Dc, 5*Sc,exp(-10*Vc),exp(-10*Hc)); [gch l] = GraphCut('expand',gch);gch = GraphCut('close', gch);lb=(l==label) ;lb=imdilate(lb,strel('disk',1))-lb ;figure(5) ; image(img) ; axis image ; hold on ;contour(lb,[1 1],'r') ; hold off ; title('edges') ;figure(6) ; imagesc(l) ; axis image ; title('edges') ;主程序2I = imread( '12.png' );I = rgb2gray(I);subplot(5,3,1),imshow(I);k=medfilt2(I,[5,5]);subplot(5,3,2),imshow(k);title('5*5中值滤波图像');%f=imread('tuxiang1.tif');%subplot(1,2,1),imshow(f);%title('原图像');g1=histeq(k,256);subplot(5,3,3),imshow(g1);title('直方图匹配');%g2=histeq(k2,256);%subplot(2,2,2),imshow(g2);%title('5*5直方图匹配');%k=medfilt2(f,[5,5]);%k2=medfilt2(f,[5,5]);%j=imnoise(f,'gaussian',0,0.005);%subplot(1,3,3),imshow(k2);%title('5*5中值滤波图像');hy = fspecial( 'sobel' );hx = hy;Iy = imfilter(double(g1), hy, 'replicate' );Ix = imfilter(double(g1), hx, 'replicate' );gradmag = sqrt(Ix.^2 + Iy.^2);subplot(5,3,4), imshow(gradmag,[ ]), title( 'gradmag' );L = watershed(gradmag);Lrgb = label2rgb(L);subplot(5,3,5), imshow(Lrgb), title( 'Lrgb' );se = strel( 'disk' , 9);Io = imopen(g1, se);subplot(5,3,6), imshow(Io), title( 'Io' )Ie = imerode(g1, se);Iobr = imreconstruct(Ie, g1);subplot(5,3,7), imshow(Iobr), title( 'Iobr' );Ioc = imclose(Io, se);subplot(5,3,8), imshow(Ioc), title( 'Ioc' );Iobrd = imdilate(Iobr, se);Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr)); Iobrcbr = imcomplement(Iobrcbr);subplot(5,3,9), imshow(Iobrcbr), title( 'Iobrcbr' );fgm = imregionalmax(Iobrcbr);subplot(5,3,10), imshow(fgm), title( 'fgm' );I2 = g1; I2(fgm) = 255;subplot(5,3,11),imshow(I2), title( 'fgm superimposed on original image' );se2 = strel(ones(5,5)); I3 = g1; I3(fgm) = 255;subplot(5,3,12) ,imshow(I3);title( 'fgm4 superimposed on original image' );bw = im2bw(Iobrcbr, graythresh(Iobrcbr));subplot(5,3,13) , imshow(bw), title( 'bw' );D = bwdist(bw); DL = watershed(D);bgm = DL == 0;subplot(5,3,14) , imshow(bgm), title( 'bgm' );gradmag2 = imimposemin(gradmag, bgm | fgm);L = watershed(gradmag2);I4 = g1;I4(imdilate(L == 0, ones(3, 3)) | bgm | fgm) = 255;figure, imshow(I4);title( 'Markers and object boundaries superimposed on original image' ); Lrgb = label2rgb(L, 'jet' , 'w' , 'shuffle' );figure, imshow(Lrgb);title( 'Lrgb' );figure, imshow(I), hold onhimage = imshow(Lrgb);set(himage, 'AlphaData' , 0.3);title( 'Lrgb superimposed transparently on original image' );。
matlab图像分割算法源码

matlab图像分割算法源码1.图像反转MATLAB 程序实现如下:I=imread('xian.bmp');J=double(I);J=-J+(256-1);% 图像反转线性变换H=uint8(J);subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(H);2.灰度线性变换MATLAB 程序实现如下:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title(' 原始图像 ');axis([50,250,50,200]);axis on;% 显示坐标系I1=rgb2gray(I);subplot(2,2,2),imshow(I1);title(' 灰度图像 ');axis([50,250,50,200]);axis on;% 显示坐标系J=imadjust(I1,[0.1 0.5],[]); %局部拉伸,把[0.1 0.5]内的灰度拉伸为[0 1] subplot(2,2,3),imshow(J);title(' 线性变换图像 [0.1 0.5]');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系K=imadjust(I1,[0.3 0.7],[]); %局部拉伸,把[0.3 0.7]内的灰度拉伸为[0 1] subplot(2,2,4),imshow(K);title(' 线性变换图像 [0.3 0.7]');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系3.非线性变换MATLAB 程序实现如下:I=imread('xian.bmp');I1=rgb2gray(I);subplot(1,2,1),imshow(I1);title(' 灰度图像 ');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系J=double(I1);J=40*(log(J+1));H=uint8(J);subplot(1,2,2),imshow(H);title(' 对数变换图像 ');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系4.直方图均衡化MATLAB 程序实现如下:I=imread('xian.bmp');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实现领域平均法抑制噪声程序:I=imread('xian.bmp');subplot(231)imshow(I)title(' 原始图像 ')I=rgb2gray(I);I1=imnoise(I,'salt & pepper',0.02);subplot(232)imshow(I1)title(' 添加椒盐噪声的图像 ')k1=filter2(fspecial('average',3),I1)/255;% 进行 3*3模板平滑滤波k2=filter2(fspecial('average',5),I1)/255;% 进行 5*5模板平滑滤波k3=filter2(fspecial('average',7),I1)/255;% 进行 7*7模板平滑滤波k4=filter2(fspecial('average',9),I1)/255;% 进行 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模板平滑滤波 ');6.中值滤波器用 MATLAB实现中值滤波程序如下:I=imread('xian.bmp');I=rgb2gray(I);J=imnoise(I,'salt&pepper',0.02);subplot(231),imshow(I);title(' 原图像 ');subplot(232),imshow(J);title(' 添加椒盐噪声图像');k1=medfilt2(J);% 进行 3*3模板中值滤波k2=medfilt2(J,[5,5]);% 进行 5*5模板中值滤波k3=medfilt2(J,[7,7]);% 进行 7*7模板中值滤波k4=medfilt2(J,[9,9]);% 进行 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 算子和拉普拉斯对图像锐化:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title(' 原始图像 ');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系I1=im2bw(I);subplot(2,2,2),imshow(I1);title(' 二值图像');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系H=fspecial('sobel');% 选择sobel算子J=filter2(H,I1);% 卷积运算subplot(2,2,3),imshow(J);title('sobel算子锐化图像');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系h=[0 1 0,1 -4 1,0 1 0];% 拉普拉斯算子J1=conv2(I1,h,'same');% 卷积运算subplot(2,2,4),imshow(J1);title(' 拉普拉斯算子锐化图像');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系8.梯度算子检测边缘用MATLAB 实现如下:I=imread('xian.bmp');subplot(2,3,1);imshow(I);title(' 原始图像 ');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系imshow(I1);title(' 二值图像 ');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系figure;subplot(2,3,3);imshow(I2);');title('roberts 算子分割结果axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系I3=edge(I1,'sobel');subplot(2,3,4);imshow(I3);');title('sobel 算子分割结果axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系subplot(2,3,5);imshow(I4);');title('Prewitt 算子分割结果axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系9.LOG 算子检测边缘用 MATLAB程序实现如下:I=imread('xian.bmp');subplot(2,2,1);imshow(I);title(' 原始图像 ');imshow(I1);title(' 灰度图像 ');I2=edge(I1,'log');subplot(2,2,3);imshow(I2);title('log 算子分割结果 ');10.Canny算子检测边缘用 MATLAB程序实现如下:I=imread('xian.bmp');subplot(2,2,1);imshow(I);title(' 原始图像 ')I1=rgb2gray(I);subplot(2,2,2);imshow(I1);title(' 灰度图像 ');I2=edge(I1,'canny');subplot(2,2,3);imshow(I2);title('canny算子分割结果');11. 边界跟踪( bwtraceboundary函数)clcclear allI=imread('xian.bmp');figureimshow(I);title(' 原始图像 ');I1=rgb2gray(I);% 将彩色图像转化灰度图像threshold=graythresh(I1);% 计算将灰度图像转化为二值图像所需的门限BW=im2bw(I1, threshold);% 将灰度图像转化为二值图像figureimshow(BW);title(' 二值图像 ');col=round(dim(2)/2)-90;% 计算起始点列坐标row=find(BW(:,col),1);% 计算起始点行坐标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' ,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,'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,'InitialMagnification','fit');title(' 霍夫变换图 ');xlabel('\theta'),ylabel('\rho');axis on , axis normal, hold on;x=T(P(:,2));y=R(P(:,1));plot(x,y,'s','color','white');lines=houghlines(BW,T,R,P,'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;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).point2);if(len>max_len)max_len=len;xy_long=xy;endendplot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');13.直方图阈值法用 MATLAB实现直方图阈值法:I=imread('xian.bmp');I1=rgb2gray(I);figure;subplot(2,2,1);imshow(I1);title(' 灰度图像 ')axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系[m,n]=size(I1);% 测量图像尺寸参数for k=0:255GP(k+1)=length(find(I1==k))/(m*n);% 计算每级灰度出现的概率,将其存入GP 中相应位置endsubplot(2,2,2),bar(0:255,GP,'g')% 绘制直方图title(' 灰度直方图 ')xlabel(' 灰度值 ')ylabel(' 出现概率 ')I2=im2bw(I,150/255);subplot(2,2,3),imshow(I2);title(' 阈值 150 的分割图像 ')axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系I3=im2bw(I,200/255);%subplot(2,2,4),imshow(I3);title(' 阈值 200 的分割图像 ')axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系14.自动阈值法: Otsu 法用 MATLAB 实现 Otsu 算法:clcclear allI=imread('xian.bmp');subplot(1,2,1),imshow(I);title(' 原始图像 ')axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系level=graythresh(I);% 确定灰度阈值BW=im2bw(I,level);subplot(1,2,2),imshow(BW);title('Otsu法阈值分割图像')axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系15.膨胀操作I=imread('xian.bmp');% 载入图像I1=rgb2gray(I);subplot(1,2,1);imshow(I1);title(' 灰度图像 ')axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系se=strel('disk',1);%生成圆形结构元素I2=imdilate(I1,se);% 用生成的结构元素对图像进行膨胀subplot(1,2,2);imshow(I2);title(' 膨胀后图像');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系16.腐蚀操作MATLAB 实现腐蚀操作I=imread('xian.bmp');% 载入图像I1=rgb2gray(I);subplot(1,2,1);imshow(I1);title(' 灰度图像 ')axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系se=strel('disk',1);% 生成圆形结构元素I2=imerode(I1,se);% 用生成的结构元素对图像进行腐蚀subplot(1,2,2);imshow(I2);title(' 腐蚀后图像');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系17.开启和闭合操作用 MATLAB实现开启和闭合操作I=imread('xian.bmp');% 载入图像subplot(2,2,1),imshow(I);title(' 原始图像');axis([50,250,50,200]);axis on;% 显示坐标系I1=rgb2gray(I);subplot(2,2,2),imshow(I1);title(' 灰度图像');axis([50,250,50,200]);axis on;% 显示坐标系se=strel('disk',1);% 采用半径为 1 的圆作为结构元素I2=imopen(I1,se);% 开启操作I3=imclose(I1,se);%闭合操作subplot(2,2,3),imshow(I2);title(' 开启运算后图像');axis([50,250,50,200]);axis on;% 显示坐标系subplot(2,2,4),imshow(I3);title(' 闭合运算后图像');axis([50,250,50,200]);axis on;% 显示坐标系18.开启和闭合组合操作I=imread('xian.bmp');% 载入图像subplot(3,2,1),imshow(I);title(' 原始图像 ');axis([50,250,50,200]);axis on;% 显示坐标系I1=rgb2gray(I);subplot(3,2,2),imshow(I1);title(' 灰度图像 ');axis([50,250,50,200]);axis on;% 显示坐标系se=strel('disk',1);I2=imopen(I1,se);% 开启操作I3=imclose(I1,se);%闭合操作subplot(3,2,3),imshow(I2);title(' 开启运算后图像');axis([50,250,50,200]);axis on;% 显示坐标系subplot(3,2,4),imshow(I3);title(' 闭合运算后图像');axis([50,250,50,200]);axis on;% 显示坐标系se=strel('disk',1);I4=imopen(I1,se);I5=imclose(I4,se);subplot(3,2,5),imshow(I5);% 开—闭运算图像title(' 开—闭运算图像 ');axis([50,250,50,200]);axis on;% 显示坐标系I6=imclose(I1,se);I7=imopen(I6,se);subplot(3,2,6),imshow(I7);% 闭—开运算图像title(' 闭—开运算图像 ');axis([50,250,50,200]);axis on;% 显示坐标系19.形态学边界提取利用 MATLAB 实现如下:I=imread('xian.bmp');% 载入图像subplot(1,3,1),imshow(I);title(' 原始图像 ');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系I1=im2bw(I);subplot(1,3,2),imshow(I1);title(' 二值化图像 ');axis([50,250,50,200]);grid on;% 显示网格线axis on;% 显示坐标系I2=bwperim(I1);% 获取区域的周长subplot(1,3,3),imshow(I2);title(' 边界周长的二值图像');axis([50,250,50,200]);grid on;axis on;20.形态学骨架提取利用 MATLAB 实现如下:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title(' 原始图像 ');axis([50,250,50,200]);axis on;I1=im2bw(I);subplot(2,2,2),imshow(I1);title(' 二值图像 ');axis([50,250,50,200]);axis on;I2=bwmorph(I1,'skel',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 on;21.直接提取四个顶点坐标I = imread('xian.bmp');I = I(:,:,1);BW=im2bw(I);figureimshow(~BW)[x,y]=getpts。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
matlab 图像分割算法源码1.图像反转MATLAB程序实现如下:I=imread('xian.bmp');J=double(I);J=-J+(256-1); %图像反转线性变换H=uint8(J);subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(H);2.灰度线性变换MATLAB程序实现如下:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title('原始图像');axis([50,250,50,200]);axis on; %显示坐标系I1=rgb2gray(I);subplot(2,2,2),imshow(I1);title('灰度图像');axis([50,250,50,200]);axis on; %显示坐标系J=imadjust(I1,[0.1 0.5],[]); %局部拉伸,把[0.1 0.5]内的灰度拉伸为[0 1] subplot(2,2,3),imshow(J);title('线性变换图像[0.1 0.5]');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系K=imadjust(I1,[0.3 0.7],[]); %局部拉伸,把[0.3 0.7]内的灰度拉伸为[0 1] subplot(2,2,4),imshow(K);title('线性变换图像[0.3 0.7]');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系3.非线性变换MATLAB程序实现如下:I=imread('xian.bmp');I1=rgb2gray(I);subplot(1,2,1),imshow(I1);title('灰度图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系J=double(I1);J=40*(log(J+1));H=uint8(J);subplot(1,2,2),imshow(H);title('对数变换图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系4.直方图均衡化MATLAB程序实现如下:I=imread('xian.bmp');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实现领域平均法抑制噪声程序:I=imread('xian.bmp');subplot(231)imshow(I)title('原始图像')I=rgb2gray(I);I1=imnoise(I,'salt & pepper',0.02);subplot(232)imshow(I1)title('添加椒盐噪声的图像')k1=filter2(fspecial('average',3),I1)/255; %进行3*3模板平滑滤波k2=filter2(fspecial('average',5),I1)/255; %进行5*5模板平滑滤波k3=filter2(fspecial('average',7),I1)/255; %进行7*7模板平滑滤波k4=filter2(fspecial('average',9),I1)/255; %进行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模板平滑滤波');6.中值滤波器用MATLAB实现中值滤波程序如下:I=imread('xian.bmp');I=rgb2gray(I);J=imnoise(I,'salt&pepper',0.02);subplot(231),imshow(I);title('原图像');subplot(232),imshow(J);title('添加椒盐噪声图像');k1=medfilt2(J); %进行3*3模板中值滤波k2=medfilt2(J,[5,5]); %进行5*5模板中值滤波k3=medfilt2(J,[7,7]); %进行7*7模板中值滤波k4=medfilt2(J,[9,9]); %进行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算子和拉普拉斯对图像锐化:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title('原始图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I1=im2bw(I);subplot(2,2,2),imshow(I1);title('二值图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系H=fspecial('sobel'); %选择sobel算子J=filter2(H,I1); %卷积运算subplot(2,2,3),imshow(J);title('sobel算子锐化图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系h=[0 1 0,1 -4 1,0 1 0]; %拉普拉斯算子J1=conv2(I1,h,'same'); %卷积运算subplot(2,2,4),imshow(J1);title('拉普拉斯算子锐化图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系8.梯度算子检测边缘用MATLAB实现如下:I=imread('xian.bmp');subplot(2,3,1);imshow(I);title('原始图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I1=im2bw(I);imshow(I1);title('二值图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I2=edge(I1,'roberts');figure;subplot(2,3,3);imshow(I2);title('roberts算子分割结果'); axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I3=edge(I1,'sobel');subplot(2,3,4);imshow(I3);title('sobel算子分割结果');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I4=edge(I1,'Prewitt');subplot(2,3,5);imshow(I4);title('Prewitt算子分割结果'); axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系9.LOG算子检测边缘用MATLAB程序实现如下:I=imread('xian.bmp');subplot(2,2,1);imshow(I);title('原始图像');I1=rgb2gray(I);imshow(I1);title('灰度图像');I2=edge(I1,'log');subplot(2,2,3);imshow(I2);title('log算子分割结果');10.Canny算子检测边缘用MATLAB程序实现如下:I=imread('xian.bmp');subplot(2,2,1);imshow(I);title('原始图像')I1=rgb2gray(I);subplot(2,2,2);imshow(I1);title('灰度图像');I2=edge(I1,'canny');subplot(2,2,3);imshow(I2);title('canny算子分割结果');11.边界跟踪(bwtraceboundary函数)clcclear allI=imread('xian.bmp');figureimshow(I);title('原始图像');I1=rgb2gray(I); %将彩色图像转化灰度图像threshold=graythresh(I1); %计算将灰度图像转化为二值图像所需的门限BW=im2bw(I1, threshold); %将灰度图像转化为二值图像figureimshow(BW);title('二值图像');dim=size(BW);col=round(dim(2)/2)-90; %计算起始点列坐标row=find(BW(:,col),1); %计算起始点行坐标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' ,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,'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,'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,'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;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).point2);if(len>max_len)max_len=len;xy_long=xy;endendplot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');13.直方图阈值法用MATLAB实现直方图阈值法:I=imread('xian.bmp');I1=rgb2gray(I);figure;subplot(2,2,1);imshow(I1);title('灰度图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系[m,n]=size(I1); %测量图像尺寸参数GP=zeros(1,256); %预创建存放灰度出现概率的向量for k=0:255GP(k+1)=length(find(I1==k))/(m*n); %计算每级灰度出现的概率,将其存入GP中相应位置endsubplot(2,2,2),bar(0:255,GP,'g') %绘制直方图title('灰度直方图')xlabel('灰度值')ylabel('出现概率')I2=im2bw(I,150/255);subplot(2,2,3),imshow(I2);title('阈值150的分割图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I3=im2bw(I,200/255); %subplot(2,2,4),imshow(I3);title('阈值200的分割图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系14. 自动阈值法:Otsu法用MATLAB实现Otsu算法:clcclear allI=imread('xian.bmp');subplot(1,2,1),imshow(I);title('原始图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系level=graythresh(I); %确定灰度阈值BW=im2bw(I,level);subplot(1,2,2),imshow(BW);title('Otsu法阈值分割图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系15.膨胀操作I=imread('xian.bmp'); %载入图像I1=rgb2gray(I);subplot(1,2,1);imshow(I1);title('灰度图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系se=strel('disk',1); %生成圆形结构元素I2=imdilate(I1,se); %用生成的结构元素对图像进行膨胀subplot(1,2,2);imshow(I2);title('膨胀后图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系16.腐蚀操作MATLAB实现腐蚀操作I=imread('xian.bmp'); %载入图像I1=rgb2gray(I);subplot(1,2,1);imshow(I1);title('灰度图像')axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系se=strel('disk',1); %生成圆形结构元素I2=imerode(I1,se); %用生成的结构元素对图像进行腐蚀subplot(1,2,2);imshow(I2);title('腐蚀后图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系17.开启和闭合操作用MATLAB实现开启和闭合操作I=imread('xian.bmp'); %载入图像subplot(2,2,1),imshow(I);title('原始图像');axis([50,250,50,200]);axis on; %显示坐标系I1=rgb2gray(I);subplot(2,2,2),imshow(I1);title('灰度图像');axis([50,250,50,200]);axis on; %显示坐标系se=strel('disk',1); %采用半径为1的圆作为结构元素I2=imopen(I1,se); %开启操作I3=imclose(I1,se); %闭合操作subplot(2,2,3),imshow(I2);title('开启运算后图像');axis([50,250,50,200]);axis on; %显示坐标系subplot(2,2,4),imshow(I3);title('闭合运算后图像');axis([50,250,50,200]);axis on; %显示坐标系18.开启和闭合组合操作I=imread('xian.bmp'); %载入图像subplot(3,2,1),imshow(I);title('原始图像');axis([50,250,50,200]);axis on; %显示坐标系I1=rgb2gray(I);subplot(3,2,2),imshow(I1);title('灰度图像');axis([50,250,50,200]);axis on; %显示坐标系se=strel('disk',1);I2=imopen(I1,se); %开启操作I3=imclose(I1,se); %闭合操作subplot(3,2,3),imshow(I2);title('开启运算后图像');axis([50,250,50,200]);axis on; %显示坐标系subplot(3,2,4),imshow(I3);title('闭合运算后图像');axis([50,250,50,200]);axis on; %显示坐标系se=strel('disk',1);I4=imopen(I1,se);I5=imclose(I4,se);subplot(3,2,5),imshow(I5); %开—闭运算图像title('开—闭运算图像');axis([50,250,50,200]);axis on; %显示坐标系I6=imclose(I1,se);I7=imopen(I6,se);subplot(3,2,6),imshow(I7); %闭—开运算图像title('闭—开运算图像');axis([50,250,50,200]);axis on; %显示坐标系19.形态学边界提取利用MATLAB实现如下:I=imread('xian.bmp'); %载入图像subplot(1,3,1),imshow(I);title('原始图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I1=im2bw(I);subplot(1,3,2),imshow(I1);title('二值化图像');axis([50,250,50,200]);grid on; %显示网格线axis on; %显示坐标系I2=bwperim(I1); %获取区域的周长subplot(1,3,3),imshow(I2);title('边界周长的二值图像');axis([50,250,50,200]);grid on;axis on;20.形态学骨架提取利用MATLAB实现如下:I=imread('xian.bmp');subplot(2,2,1),imshow(I);title('原始图像');axis([50,250,50,200]);axis on;I1=im2bw(I);subplot(2,2,2),imshow(I1);title('二值图像');axis([50,250,50,200]);axis on;I2=bwmorph(I1,'skel',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 on;21.直接提取四个顶点坐标I = imread('xian.bmp');I = I(:,:,1);BW=im2bw(I);figureimshow(~BW)[x,y]=getpts。