MATLAB图像分析

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

matlab图像分析批量读取某文件夹下的图像pth=uigetdir();%获取文件路径pth=[pth'\'];files=dir([pth'*.jpg']);%获得指定路径pth下所有的jpg图像文件名nfile=length(files);%文件数if~nfile,return;end%若文件数为0则返回fort=1:nfileImg=imread([pthfiles(t).name]);%读取图像imshow(Img)%显示图像pause%暂停直到按任意键继续end图像边缘提取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)[BW,threshold]=edge(I,'canny',...)例:对某文件夹下的图像进行边缘提取pth=uigetdir();%获取文件路径pth=[pth'\'];files=dir([pth'*.jpg']);%获得指定路径pth下所有的jpg图像文件名nfile=length(files);%文件数if~nfile,return;end%若文件数为0则返回fort=1:nfileImg=imread([pthfiles(t).name]);%读取图像ifsize(Img,3)1,Img=rgb2gray(Img);end%将彩色图像转化为灰度图像Eg=edge(Img,'canny');%使用canny算子进行边缘提取figure(1),imshow(Img);figure(2),imshow(Eg);%显示图像pause%暂停直到按任意键继续end二值图像轮廓跟踪bwboundaries•B=bwboundaries(BW)B=bwboundaries(BW,conn)•B=bwboundaries(BW,conn,options)[B,L]=bwboundaries(...)[B,L,N,A]=bwboundaries(...)例:去掉二值图像E中的过长或过短轮廓线,绘制轮廓线Bdr=bwboundaries(E,4,'noholes');%4连通轮廓跟踪;Thr_min_lth=20;%轮廓长度阈值下界Thr_max_lth=400;%轮廓长度阈值Nbdr=length(Bdr);%轮廓数imshow(E),holdonfornb=1:Nbdrlth=length(Bdr{nb})iflthThr_max_lth||lthThr_min_lthidx=Bdr{nb}(:,1)+(Bdr{nb}(:,2)-1)*row;E(idx)=0;elseplot(Bdr{nb}(:,2),Bdr{nb}(:,1),'r','LineWidth',2);endend数学形态学处理•膨胀:imdilate,腐蚀:imerode,开运算:imopen,闭运算:imclose•如:imclose(I,se)•创建形态学结构元:strel,如se=strel('rectangle',[35])•开运算:先腐蚀后膨胀闭运算:先膨胀后腐蚀•二值图像形态学运算函数bwmorph•bw2=bwmorph(bw1,operation)OPERATIONisastringthatcanhaveoneofthesevalues:'bothat'Subtracttheinputimagefromitsclosing'branchpoints'Findbranchpointsofskeleton'bridge'Bridgepreviouslyunconnectedpixels'clean'Removeisolatedpixels(1'ssurroundedby0's)'close'Performbinaryclosure(dilationfollowedbyerosion)'diag'Diagonalfilltoeliminate8-connectivityofbackground'dilate'Performdilationusingthestructuringelementones(3)'endpoints'Findendpointsofskeleton'erode'Performerosionusingthestructuringelementones(3)'fill'Fillisolatedinteriorpixels(0'ssurroundedby1's)'hbreak'RemoveH-connectedpixels'majority'Setapixelto1iffiveormorepixelsinits3-by-3neighborhoodare1's'open'Performbinaryopening(erosionfollowedbydilation)'remove'Setapixelto0ifits4-connectedneighborsareall1's,thusleavingonlyboundarypixels'shrink'WithN=Inf,shrinkobjectstopoints;shrinkobjectswithholestoconnectedrings'skel'WithN=Inf,removepixelsontheboundariesofobjectswithoutallowingobjectstobreakapart'spur'Removeendpointsoflineswithoutremovingsmallobjectscompletely'thicken'WithN=Inf,thickenobjectsbyaddingpixelstotheexteriorofobjectswithoutconnectedpreviouslyunconnectedobjects'thin'WithN=Inf,removepixelssothatanobjectwithoutholesshrinkstoaminimallyconnectedstroke,andanobjectwithholesshrinkstoaringhalfwaybetweentheholeandouterboundary'tophat'Subtracttheopeningfromtheinputimage连通区域提取与分析•CC=bwconncomp(bw);%连通区域提取;•Stats=regionprops(CC,'BoundingBox');%确定各连通区域的最小外接矩形•Stats=regionprops(bw,'BoundingBox');•例子:绘制bw各连通区域的最小外接矩形bw=imread('circles.png');Stats=regionprops(bw,'BoundingBox');nregion=length(Stats);figure(1),imshow(bw),forc=1:nregionb=round(Stats(c).BoundingBox);subbw=bw(b(2):b(2)+b(4)-1,b(1):b(1)+b(3)-1);figure(1),holdonrectangle('Position',b,'EdgeColor','red')figure(2)imshow(subbw)pause(1)end图像与直线I=imread('circuit.tif');%绘制过图像中心,斜率为k的直线[row,col]=size(I);r0=fix(row/2);c0=fix(col/2);%斜率为k的直线:r=r0+k*(c-c0)k=0.5;c=1:col;r=round(r0+k*(c-c0));imshow(I),holdon,plot(c,r,'r.')%获取直线所经像素idx=r+(c-1)*row;p=I(idx);

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功