matlab图像处理小结_matlab图像处理例子
matlab图像处理小结由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“matlab图像处理例子”。
1.function [center, r] = solve_circle(pt1, pt2, pt3)
2.%Effect: solve the circle which acro points 'pt1', 'pt2' and 'pt3' 3.%Inputs:
4.%pt1, pt2, pt3: [x, y]
5.%center: the circle center [x0;y0] 6.%r: the radius of the circle 7.%Author: Su dongcai at 2012/1/2 8.A = zeros(2, 2);B = zeros(2, 1);9.[A(1, :), B(1)] = circle2line(pt1, pt2);10.[A(2, :), B(2)] = circle2line(pt2, pt3);11.center = AB;
12.r = norm(pt1'(y2^2 + y2^2)18.%(a-x2)^2 +(b-y2)^2 = r^2 | 19.%Inputs:
20.%pt1, pt2: [x1, y1], [x2, y2] 21.%Outputs:
22.%A: 2[x1-x2, y1-y2]
23.%B:(x1^2 + y1^2)pt2);
26.B = norm(pt1)^2-norm(pt2)^2;
close all;clear;clc;>> i=imread('rice.png');%>> imshow(i);>> background=imopen(i,strel('disk',15));>> i2=imsubtract(i,background);%>> figure,imshow(i2);>> i3=imadjust(i2,stretchlim(i2),[0 1]);%>> figure,imshow(i3);>> level=graythresh(i3);>> bw=im2bw(i3,level);%>> figure,imshow(bw);>> [labeled,numobjects]=bwlabel(bw,4);graindata=regionprops(labeled,'all');
close all;clear;clc;i=imread('rice.png');background=imopen(i,strel('disk',15));i2=imsubtract(i,background);i3=imadjust(i2,stretchlim(i2),[0 1]);level=graythresh(i3);bw=im2bw(i3,level);[labeled,numobjects]=bwlabel(bw,4);data=regionprops(labeled,'all');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2006.6.2 close all;clear;clc;>> i=imread('r.jpg');%>> figure,imshow(i);>> imgray=rgb2gray(i);>> figure,imshow(imgray)>> background=imopen(imgray,strel('disk',15));>> i2=imsubtract(imgray,background);%>> figure,imshow(i2);>> i3=imadjust(i2,stretchlim(i2),[0 1]);%>> figure,imshow(i3);>> level=graythresh(i3);>> bw=im2bw(i3,level);%>> figure,imshow(bw);>> imnobord=imclearborder(bw,4);%>> figure,imshow(imnobord);>> [labeled,numobjects]=bwlabel(bw,4);>> rgb_label=label2rgb(labeled,@spring,'c','shuffle');>> figure,imshow(rgb_label);>> graindata=regionprops(labeled,'all');hold on;for k=1:numobjects lab=sprintf('%d',k);text(graindata(k).Centroid(1),graindata(k).Centroid(2),lab,'Color','k');end hold off;%剔除碎米粒
>> idxdown=find([graindata.Area]
[lab_little,num_little]=bwlabel(little,4);rgb_little=label2rgb(lab_little,@spring,'c','shuffle');figure,imshow(rgb_little);
little_data=regionprops(lab_little,'all');hold on;for k=1:num_little lab=sprintf('%d',k);text(little_data(k).Centroid(1),little_data(k).Centroid(2),lab,'Color','k');end hold off;%>> graindata(idxdown,:)=[];%剔除碎米粒 %剔除连接米粒
>> idxup=find([graindata.Area]>250);%剔除连接米粒 big=ismember(labeled,idxup);figure,imshow(big);
[lab_big,num_big]=bwlabel(big,4);rgb_big=label2rgb(lab_big,@spring,'c','shuffle');figure,imshow(rgb_big);
big_data=regionprops(lab_big,'all');hold on;for k=1:num_big lab=sprintf('%d',k);text(big_data(k).Centroid(1),big_data(k).Centroid(2),lab,'Color','k');end hold off;%>> graindata(numup,:)=[];%剔除连接米粒 %获取完整米粒
idxsuit=find([graindata.Area]>=150&[graindata.Area]
>> graindata >> mean([graindata.Area])>> mean([graindata.Eccentricity])>> mean([graindata.MajorAxisLength])>> mean([graindata.MinorAxisLength])>> mean([graindata.EquivDiameter])>> figure,hist([graindata.Area],20);>> figure,hist([graindata.Eccentricity],20);>> figure,hist([graindata.MajorAxisLength],20);>> figure,hist([graindata.MinorAxisLength],20);>> figure,hist([graindata.EquivDiameter],20);
data=[graindata.Area] data=[graindata.Centroid] data=[graindata.BoundingBox] data=[graindata.SubarrayIdx] data=[graindata.MajorAxisLength] data=[graindata.MinorAxisLength] data=[graindata.Eccentricity] data=[graindata.Orientation] data=[graindata.ConvexHull] data=[graindata.ConvexImage] data=[graindata.ConvexArea] data=[graindata.Image] data=[graindata.FilledImage] data=[graindata.FilledArea] data=[graindata.EulerNumber] data=[graindata.Extrema] data=[graindata.EquivDiameter] data=[graindata.Solidity] data=[graindata.Extent] data=[graindata.PixelIdxList] data=[graindata.PixelList]
Area 计算各个连通区域中的象素总数 BoundingBox 包含相应区域的最小矩形 Centroid 给出每个区域的质心
MajorAxisLength 与区域具有相同标准二阶中心矩(又叫标准差)的椭圆的长轴长度 MinorAxisLength 与区域具有相同标准二阶中心矩的椭圆的短轴长度 Eccentricity 与区域具有相同标准二阶中心矩的椭圆的离心率
Orientation 与区域具有相同标准二阶中心矩的椭圆的长轴与x轴的交角 Image 二值图像,与某区域具有相同大小的逻辑矩阵。
FilledImage 与上相同,唯一区别是这是个做了填充的逻辑矩阵!本例中和上面的没有区别,只有 区域有空洞时才有明显差别。
FilledArea 是标量,填充区域图像中的 on 像素个数
ConvexHull 是p行2列的矩阵,包含某区域的最小凸多边形 ConvexImage 二值图像,用来画出上述的区域最小凸多边形 ConvexArea 是标量,填充区域凸多边形图像中的 on 像素个数 EulerNumber 等于图像中目标个数减去这些目标中空洞的个数 Extrema 8行2列矩阵,八方向区域极值点
EquivDiameter 是标量,等价直径:与区域具有相同面积的圆的直径.计算公式为:sqrt(4*Area/pi)
Solidity 是标量,同时在区域和其最小凸多边形中的像素比例。计算公式为: Area/ConvexArea,这也是个仿射特征,实际上反映出区域的固靠性程度。
Extent 是标量,同时在区域和其最小边界矩形中的像素比例。计算公式为:Area除以边界矩 形面积,这也是个仿射特征,实际上反映出区域的扩展范围程度。
PixelIdxList p元向量,存储区域像素的索引下标
PixelList p行ndims(L)列矩阵,存储上述索引对应的像素坐标 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 基于特定原则的区域选择
当你要基于特定准则条件选择某个区域时,将函数 ismember 和 regionprops 联合使用是很有用处的。例如:创建一个只包含面积大于80的二值图像,用以下命令
idx = find([stats.Area] > 80);BW2 = ismember(L,idx);regionprops函数的扩展思路
在regionprops函数的基础上,你可以使用它提供的基本数据来扩展它的功能,比如我就将区域的曲率数据和骨架数据作为它的另外属性值来开发,从而希望它能用来做更细致的特征提取。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2006.6.2 P221图像粒度测定(雪花)>> i=imread('snowflakes.png');>> figure,imshow(i);>> %(2)>> clahei=adapthisteq(i,'numtiles',[10 10]);>> clahei=imadjust(clahei);>> imshow(clahei);>> gi=imadjust(im2double(i),[],[0 1]);>> figure,imshow(gi),title('adjusted grayscale image');>> %(3)>> se=strel('disk',10);>> topi=imtophat(gi,se);>> figure,imshow(topi),title('top-hat image');>> %(4)>> for counter=0:22 remain=imopen(clahei,strel('disk',counter));intensity_area(counter+1)=sum(remain(:));end >> figure,plot(intensity_area,'m-*'),grid on;>> title('sum of opening(pixels)');>> title('sum of opening values in opened image as a function of radius');>> xlabel('radius of opening(pixels)');>> ylabel('pixel value sum of opened objects(intensity)');>> >> >> >> for counter=0:20 remain=imopen(topi,strel('disk',counter));surfarea(counter+1)=sum(remain(:));end >> figure,plot(surfarea,'m-*'),grid on;>> set(gca,'xtick',[0 2 4 6 8 10 12 14 16 18 20]);>> title('surface area of opened objects as a function of radius');>> xlabel('radius of opening(pixels)');>> ylabel('surface area of opened objects(pixels)');>> %(5)>> intensity_area_prime=diff(intensity_area);>> figure,plot(intensity_area_prime,'m-*'),grid on;>> title('Granulometry(size distrubution)of snowflakes');>> set(gca,'xtick',[0 2 4 6 8 10 12 14 16 18 20 22]);>> xlabel('radius of snowflakes(pixels)');>> ylabel('sum of pixel values in snowflakes as a function of radius');>> derivsurfarea=diff(surfarea);>> figure,plot(derivsurfarea,'m-*'),grid on;>> title('granulometry(size distribution)of stars');>> xlabel('radius of stars(pixels)');>> ylabel('lo of pixels between two succeive openings');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2006.6.2 花椒检测 clc;clear;close all;i=imread('gj.jpg');imshow(i);icanny=edge(i,'canny');imshow(icanny);se90=strel('line',2,90);se0=strel('line',2,0);bwsdil=imdilate(icanny,[se90 se0]);figure,imshow(bwsdil),title('dilated');ifill=imfill(bwsdil,'holes');figure,imshow(ifill);
%bwero=imerode(bwsdil,[se90 se0]);%figure,imshow(bwero);%i2fill=imfill(bwero,'holes');%figure,imshow(bwero);%imshow(i2fill);
%bwnobord=imclearborder(bwsdil,4);%figure,imshow(bwnobord);bwnobord=imclearborder(ifill,4);figure,imshow(bwnobord);se=strel('disk',5);bwc=imclose(bwnobord,se);bwco=imopen(bwnobord,se);figure,imshow(bwc);figure,imshow(bwco);%mask=bwsdil&bwco;%figure,imshow(mask);clc [labeled,numobjects]=bwlabel(bwco);numobjects
jdata=regionprops(labeled,'all');%jdata
jarea=[jdata.Area];mean(jarea)max(jarea)min(jarea)hist(jarea,255)jdata.Eccentricity %std([jdata.Eccentricity])/(Mean([jdata.Eccentricity])jstd=std([jdata.Eccentricity])jmean=Mean([jdata.Eccentricity])jcv=jstd/jmean
>> std([jdata.Area])/ mean([jdata.Area])%面积的变异系数
>> std([jdata.Eccentricity])/ mean([jdata.Eccentricity])%椭圆的变异系数 >> std([jdata.MajorAxisLength])/ mean([jdata.MajorAxisLength])>> std([jdata.MinorAxisLength])/ mean([jdata.MinorAxisLength])>> std([jdata.EquivDiameter])/ mean([jdata.EquivDiameter])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2006.06.06 rice.png close all;clear;clc >> i=imread('rice.png');imshow(i);background=imopen(i,strel('disk',15));figure,imshow(background);i2=imsubtract(i,background);figure,imshow(i2);i3=imadjust(i2,stretchlim(i2),[0 1]);figure,imshow(i3);level=graythresh(i3);bw=im2bw(i3,level);figure,imshow(bw);imnobord=imclearborder(bw);[label,numobjects]=bwlabel(imnobord,4);numobjects rgb_label=label2rgb(label,@spring,'c','shuffle');figure,imshow(rgb_label);graindata=regionprops(label,'all');graindata
>> numdown=find([graindata.Area]> graindata(numdown,:)=[];>> numup=find([graindata.Area]>250);>> graindata(numup,:)=[];>> graindata
>> std([graindata.Area])/ mean([graindata.Area])%面积的变异系数
>> std([graindata.Eccentricity])/ mean([graindata.Eccentricity])%椭圆的变异系数
>> std([graindata.MajorAxisLength])/ mean([graindata.MajorAxisLength])>> std([graindata.MinorAxisLength])/ mean([graindata.MinorAxisLength])>> std([graindata.EquivDiameter])/ mean([graindata.EquivDiameter])%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2006.06.06 rice的垩白度检测 >> clear;close all;clc;>> rgb=imread('r.jpg');>> close all;>> imshow(rgb);>> i=rgb2gray(rgb);>> j=medfilt2(i,[5 5]);>> figure,imshow(i);>> figure,imshow(j);>> imhist(j,256);>> t=0.3;>> v=imadjust(j,[t 1],[],1);>> imhist(v,256);>> t_c=0.6;>> bw_v=im2bw(v,0.01);>> chalk=imadjust(v,[t_c 1],[],1);>> bw_chalk=im2bw(chalk,0.01);>> figure,imshow(v);>> figure,imshow(bw_v);>> figure,imshow(chalk);>> figure,imshow(bw_chalk);>> degree_chalkne=bwarea(bw_chalk)/bwarea(bw_v)*100 >> bw=im2bw(j,t);>> figure,imshow(bw);>> se=(ones(3,3));>> bw1=imerode(bw,se);%两次腐蚀 >> figure,imshow(bw1);>> bw2=imerode(bw1,se);>> figure,imshow(bw2);
>> [l,num]=bwlabel(bw2);%标记腐蚀后的大米图像 >> t_chalk=100;%设置垩白面积的下限 >> compare=(l)&(chalk>t_chalk);%>> compare=(bw2)&(bw_chalk>t_chalk);>> [r,c]=find(compare);%标记垩白米粒的位置 >> result=bwselect(l,c,r);%显示只含有垩白米粒的图像 >> figure,imshow(result);
>> [l_chalk,num_chalk]=bwlabel(result);%标记垩白米粒图像,便于计数 >> rate_chalky_grains=num_chalk/num*100;>> rate_chalky_grains
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2006.6.17 bwmorph函数 >> help bwmorph BWMORPH Perform morphological operations on binary image.BW2 = BWMORPH(BW1,OPERATION)applies a specific morphological operation to the binary image BW1.BW2 = BWMORPH(BW1,OPERATION,N)applies the operation N times.N can be Inf, in which case the operation is repeated until the image no longer changes.OPERATION is a string that can have one of these values: 'bothat' Subtract the input image from its closing 'bridge' Bridge previously unconnected pixels 'clean' Remove isolated pixels(1's surrounded by 0's)'close' Perform binary closure(dilation followed by erosion)'diag' Diagonal fill to eliminate 8-connectivity of background 'dilate' Perform dilation using the structuring element ones(3)'erode' Perform erosion using the structuring element ones(3)'fill' Fill isolated interior pixels(0's surrounded by 1's)'hbreak' Remove H-connected pixels 'majority' Set a pixel to 1 if five or more pixels in its 3-by-3 neighborhood are 1's 'open' Perform binary opening(erosion followed by dilation)'remove' Set a pixel to 0 if its 4-connected neighbors are all 1's, thus leaving only boundary pixels 'shrink' With N = Inf, shrink objects to points;shrink objects with holes to connected rings 'skel' With N = Inf, remove pixels on the boundaries of objects without allowing objects to break apart 'spur' Remove end points of lines without removing small objects completely.'thicken' With N = Inf, thicken objects by adding pixels to the exterior of objects without connected previously unconnected objects 'thin' With N = Inf, remove pixels so that an object without holes shrinks to a minimally connected stroke, and an object with holes shrinks to a ring halfway between the hold and outer boundary 'tophat' Subtract the opening from the input image
Cla Support-------------The input image BW1 can be numeric or logical.It must be 2-D, real and nonsparse.The output image BW2 is logical.Examples--------BW1 = imread('circles.png');imview(BW1)BW2 = bwmorph(BW1,'remove');BW3 = bwmorph(BW1,'skel',Inf);imview(BW2)imview(BW3)
See also erode, dilate, bweuler, bwperim.Reference page in Help browser doc bwmorph
BW1 = imread('circles.png');figure,imshow(BW1)BW2 = bwmorph(BW1,'erode');figure,imshow(BW2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %边界提取 b=bwmorph(bw,'remove');b=bwperim(bw,8);%又叫边界象素测定 b=edge(bw,'canny');%又叫边界提取 %去除孤立象素点
nosinglepixel=bwmorph(bw,'clean');%去除小面积物体
nosmall=bwareaopen(bw,CNN);%阈值处理再取反
bw=~im2bw(i,graythresh(i));
%开运算(消除小物体)与闭运算(填充物体内细小空洞)se=strel('disk',6);iopen=imopen(bw,se);iclose=imclose(bw,se);%腐蚀与膨胀联合操作 %(1)创建结构元素 se=strel('rectangle',[40 30]);%(2)使用结构元素腐蚀图像 bw1=imread('circbw.tif');bw2=imerode(bw1,se);imshow(bw2);%(3)逆操作,回复矩形原来大小 bw3=imdilate(bw2,se);figure,imshow(bw3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2006.6.18花椒子
%直接对灰度图进行canny运算 >> i=imread('nut.bmp');>> figure,imshow(i);>> ig=rgb2gray(i);>> figure,imshow(ig);%igcanny=edge(ig,'canny');%igcfill=imfill(igcanny,'hole');igcanny_thresh=edge(ig,'canny',(graythresh(ig)*.1));igcfill=imfill(igcanny_thresh,'hole');>> figure,imshow(igcfill);
%先对灰度图滤波,再进行canny运算
>> imed=medfilt2(ig);%中值滤波后对图像边界有一定的损伤!!>> imedcanny=edge(imed,'canny');>> imedfill=imfill(imedcanny,'hole');>> figure,imshow(imedfill);>> nosmall=bwareaopen(imedfill,150);>> figure,imshow(nosmall);
%注意:若对灰度图像先拉氏锐化,在canny提取边界,效果不大好!!%结论:无需拉氏锐化,也不必中值滤波,可直接canny提取边界!!>> ifill=igcfill|imedfill;>> figure,imshow(ifill);>> nosmall=bwareaopen(ifill,150);>> figure,imshow(nosmall);
%当t=0.55时,阈值处理再canny运算的效果 >> imhist(ig);>> t=0.55;>> v=imadjust(ig,[0 t],[],1);>> vcanny=edge(v,'canny');>> vfill=imfill(vcanny,'hole');>> figure,imshow(vfill);>> ifill=igcfill|vfill;>> figure,imshow(ifill);>> nosmall=bwareaopen(ifill,150);>> figure,imshow(nosmall);
%当t=0.6时,阈值处理再canny运算的效果的效果 >> t=0.6;>> v=imadjust(ig,[0 t],[],1);>> vcanny=edge(v,'canny');>> vfill=imfill(vcanny,'hole');>> figure,imshow(vfill);>> ifill=igcfill|vfill;>> figure,imshow(ifill);>> nosmall=bwareaopen(ifill,150);>> figure,imshow(nosmall);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %处理花椒子
>> i=imread('nut.bmp');%figure,imshow(i);ig=rgb2gray(i);figure,imshow(ig);>> imed=medfilt2(ig);imedcanny=edge(imed,'canny');imedfill=imfill(imedcanny,'hole');%figure,imshow(imedfill);nosmall=bwareaopen(imedfill,150);>> figure,imshow(nosmall);>> [labeled,numobjects]=bwlabel(nosmall,4);>> rgb_label=label2rgb(labeled,@spring,'c','shuffle');%>> figure,imshow(rgb_label);>> nutdata=regionprops(labeled,'all');>> min([nutdata.Solidity])
>> rectangle('Position', [253.5000 207.5000 26.0000 28.0000])%画矩形
>> rectangle('Position', [250.5000 50.5000 27.0000 26.0000])>> figure,imshow(nutdata(1).Image)%只显示1号物体的图像
>> figure,imshow(nutdata(1).ConvexImage)%画出1号物体的凸多边形 >> std([nutdata.Eccentricity])/ mean([nutdata.Eccentricity])std([nutdata.Area])/ mean([nutdata.Area])std([nutdata.Solidity])/ mean([nutdata.Solidity])>> std([nutdata.Centroid])/ mean([nutdata.Centroid])std([nutdata.MajorAxisLength])/ mean([nutdata.MajorAxisLength])std([nutdata.MinorAxisLength])/ mean([nutdata.MinorAxisLength])std([nutdata.Orientation])/ mean([nutdata.Orientation])std([nutdata.EquivDiameter])/ mean([nutdata.EquivDiameter])std([nutdata.Extent])/ mean([nutdata.Extent])std([nutdata.Extrema])/ mean([nutdata.Extrema])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %处理花椒皮 close all;clc;clear;>> i=imread('p.bmp');imshow(i);ig=rgb2gray(i);figure,imshow(ig);imed=medfilt2(ig);imedcanny=edge(imed,'canny');figure,imshow(imedcanny);>> se90=strel('line',2,90);se0=strel('line',2,0);bwsdil=imdilate(imedcanny,[se90 se0]);figure,imshow(bwsdil),title('dilated');ifill=imfill(bwsdil,'holes');figure,imshow(ifill);>> bwero=imerode(ifill,[se90 se0]);>> figure,imshow(bwero);>> nosmall=bwareaopen(bwero,150,4);>> figure,imshow(nosmall);>> nobord=imclearborder(nosmall,4);>> figure,imshow(nobord);>> [labeled,numobjects]=bwlabel(nobord,4);>> numobjects >> pdata=regionprops(labeled,'all');>> max([pdata.Solidity])>> std([pdata.Solidity])/mean([pdata.Solidity])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %处理混合图像 >> clear;clc;close all;>> i=imread('m.bmp');%>> figure,imshow(i);>> ig=rgb2gray(i);figure,imshow(ig);imed=medfilt2(ig);%>> figure,imshow(imed);imedcanny=edge(imed,'canny');%>> figure,imshow(imedcanny);>> se90=strel('line',2,90);se0=strel('line',2,0);bwsdil=imdilate(imedcanny,[se90 se0]);%figure,imshow(bwsdil),title('dilated');ifill=imfill(bwsdil,'holes');%figure,imshow(ifill);bwero=imerode(ifill,[se90 se0]);%figure,imshow(bwero);>> nosmall=bwareaopen(bwero,150,4);%figure,imshow(nosmall);nobord=imclearborder(nosmall,4);figure,imshow(nobord);>> [labeled,numobjects]=bwlabel(nobord,4);>> numobjects >> rgb_label=label2rgb(labeled,@spring,'c','shuffle');figure,imshow(rgb_label);>> mexdata=regionprops(labeled,'all');hold on;%以下内容画在同一figure中 centr=[mexdata.Centroid];%寻找重心位置 nums=1:numobjects;for k = 1:numobjects soli=mexdata(k).Solidity;soli_string=sprintf('%2.2f',soli);%等价于转字符串 % signal=num2str(nums(k));signal=sprintf('%d',k);%直接使用打印语句打印序号 text(centr(2*k-1),centr(2*k),signal)%按序标记物体
text(centr(2*k-1)-30,centr(2*k)-30,soli_string)%标注每个Solidity值 end
for k=1:numobjects plot(mexdata(k).ConvexHull(:,1),mexdata(k).ConvexHull(:,2),...'b','Linewidth',2)end
%画出1和2号物体的外接矩形
%>> rectangle('position',[9.5000 224.5000 62.0000 63.0000])%>> rectangle('position',[65.5000 141.5000 34.0000 39.0000])%画出每个物体的外接矩形 bb=[mexdata.BoundingBox];for k=1:numobjects rectangle('position',[bb(4*k-3)bb(4*k-2)bb(4*k-1)bb(4*k)])end
%>> figure,imshow(mexdata(1).Image)%只显示1号物体的图像
%>> figure,imshow(mexdata(1).ConvexImage)%画出1号物体的凸多边形 %>> figure,imshow(mexdata(2).Image)%只显示2号物体的图像
%>> figure,imshow(mexdata(2).ConvexImage)%画出2号物体的凸多边形 %画出单个物体的凸多边形的填充图形 for k=1:numobjects figure,imshow(mexdata(k).ConvexImage)end
%只显示Solidity>0.92的物体的图像 >> idx = find([mexdata.Solidity] > 0.92);>> BW2 = ismember(labeled,idx);>> figure,imshow(BW2)
>> mexdata=regionprops(labeled,'all');>> %只显示Solidity
>> numdown=find([mexdata.Solidity]> mexdata
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2006.6.19 %roipoly函数的用法 I = imread('eight.tif');c = [222 272 300 270 221 194];r = [21 21 75 121 121 75];BW = roipoly(I,c,r);imview(I), imview(BW)
%可以使用下面的方法创建相应的向量: regionprops(L,'Area');allArea = [stats.Area];
%创建一个只包含面积大于80的二值图像 idx = find([stats.Area] > 80);BW2 = ismember(L,idx);
%只显示某个下标所对应的物体图像 bw2=ismember(L,N);figure,imshow(bw2);
%在调用regionprops之前必须将二值图像转变为标注矩阵 L = bwlabel(BW);%或者
L = double(BW);
%将matlab数据写到excel中 a=ones(3);succe = xlswrite('c:/matlab/work/myworkbook.xls',a,'A2:C4')%将行矩阵转换为列矩阵 a=[1 2 3 4 5 6];b=transpose(a);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2006.6.22球形物体的检测和标识(循环检测和标识算法)clc;clear;close all;%Step 1: Read image %Step 2: Threshold the image %Step 3: Remove the noise %Step 4: Find the boundaries %Step 5: Determine which objects are round >> RGB = imread('pillsetc.png');imshow(RGB)>> I = rgb2gray(RGB);threshold = graythresh(I);bw = im2bw(I,threshold);imshow(bw)>> % remove all object containing fewer than 30 pixels bw = bwareaopen(bw,30);>> figure,imshow(bw)>> % fill a gap in the pen's cap se = strel('disk',2);bw = imclose(bw,se);>> figure,imshow(bw)>> % fill any holes, so that regionprops can be used to estimate % the area enclosed by each of the boundaries bw = imfill(bw,'holes');>> figure,imshow(bw)>> [B,L] = bwboundaries(bw,'noholes');>> % Display the label matrix and draw each boundary figure,imshow(label2rgb(L, @jet, [.5.5.5]))>> hold on for k = 1:length(B)boundary = B{k};plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)end >> stats = regionprops(L,'Area','Centroid');>> stats = regionprops(L,'Area','Centroid');threshold = 0.94;% loop over the boundaries for k = 1:length(B)% obtain(X,Y)boundary coordinates corresponding to label 'k' boundary = B{k};% compute a simple estimate of the object's perimeter delta_sq = diff(boundary).^2;perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k' area = stats(k).Area;
% compute the roundne metric metric = 4*pi*area/perimeter^2;
% display the results metric_string = sprintf('%2.2f',metric);% mark objects above the threshold with a black circle if metric > threshold centroid = stats(k).Centroid;plot(centroid(1),centroid(2),'ko');end
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y',...'FontSize',14,'FontWeight','bold');end >> title(['Metrics closer to 1 indicate that ',...'the object is approximately round']);