MATLAB图像处理实例+代码文档格式.docx

上传人:b****6 文档编号:21632613 上传时间:2023-01-31 格式:DOCX 页数:10 大小:19.32KB
下载 相关 举报
MATLAB图像处理实例+代码文档格式.docx_第1页
第1页 / 共10页
MATLAB图像处理实例+代码文档格式.docx_第2页
第2页 / 共10页
MATLAB图像处理实例+代码文档格式.docx_第3页
第3页 / 共10页
MATLAB图像处理实例+代码文档格式.docx_第4页
第4页 / 共10页
MATLAB图像处理实例+代码文档格式.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

MATLAB图像处理实例+代码文档格式.docx

《MATLAB图像处理实例+代码文档格式.docx》由会员分享,可在线阅读,更多相关《MATLAB图像处理实例+代码文档格式.docx(10页珍藏版)》请在冰豆网上搜索。

MATLAB图像处理实例+代码文档格式.docx

%Store(X,Y)offsetsforlateruse;

subtract1sothateachoffsetwill

%correspondtothelastpixelbeforetheregionofinterest

offsetX=start_col-1;

offsetY=start_row-1;

Thresholdtheimage

I=rgb2gray(cropRGB);

threshold=graythresh(I);

BW=im2bw(I,threshold);

BW=~BW;

 

%complementtheimage(objectsofinterestmustbewhite)

figure,imshow(BW)

dim=size(BW);

%horizontalbeam

col1=4;

row1=min(find(BW(:

col1)));

%angledbeam

row2=12;

col2=min(find(BW(row2,:

)));

boundary1=bwtraceboundary(BW,[row1,col1],'

N'

8,70);

%setthesearchdirectiontocounterclockwise,inordertotracedownward.

boundary2=bwtraceboundary(BW,[row2,col2],'

E'

8,90,'

counter'

figure,imshow(RGB);

holdon;

%applyoffsetsinordertodrawintheoriginalimage

plot(offsetX+boundary1(:

2),offsetY+boundary1(:

1),'

g'

'

LineWidth'

2);

plot(offsetX+boundary2(:

2),offsetY+boundary2(:

ab1=polyfit(boundary1(:

2),boundary1(:

1),1);

ab2=polyfit(boundary2(:

2),boundary2(:

vect1=[1ab1

(1)];

%createavectorbasedonthelineequation

vect2=[1ab2

(1)];

dp=dot(vect1,vect2);

%computevectorlengths

length1=sqrt(sum(vect1.^2));

length2=sqrt(sum(vect2.^2));

%obtainthelargerangleofintersectionindegrees

angle=180-acos(dp/(length1*length2))*180/pi

intersection=[1,-ab1

(1);

1,-ab2

(1)]\[ab1

(2);

ab2

(2)];

%applyoffsetsinordertocomputethelocationintheoriginal,

%i.e.notcropped,image.

intersection=intersection+[offsetY;

offsetX]

Plottheresults.

inter_x=intersection

(2);

inter_y=intersection

(1);

%drawan"

X"

atthepointofintersection

plot(inter_x,inter_y,'

yx'

text(inter_x-60,inter_y-30,[sprintf('

%1.3f'

angle),'

{\circ}'

],...

'

Color'

y'

FontSize'

14,'

FontWeight'

bold'

interString=sprintf('

(%2.1f,%2.1f)'

inter_x,inter_y);

text(inter_x-10,inter_y+20,interString,...

%检测一段圆弧的半径(圆形拟合)

Readimage 

Extractinitialboundarypointlocation 

Fitacircletotheboundary

%2006.6.23基于特征的与运算

>

loadimdemosdotsbox

imshow(dots)

figure,imshow(box)

logical_and=box&

dots;

imshow(logical_and);

[r,c]=find(logical_and);

feature_and=bwselect(dots,c,r);

figure,imshow(feature_and);

%利用逻辑运算,提取含有细胞核的细胞

loadimdemosbacteria;

imshow(bacteria);

bact_bw=(bacteria>

=100);

%figure,imshow(bact_bw);

bact_bw=~bact_bw;

figure,imshow(bact_bw);

%直接二值取反

figure,imshow(bact_bw)

%细胞和细胞核重叠在一起,无法区分

%进行Laplace算子滤波,突出核与胞的区别

filtered=filter2(fspecial('

laplacian'

),bacteria);

figure,imshow(filtered) 

%先拉氏滤波

bact_granules=(filtered>

-4);

figure,imshow(bact_granules);

%“-4”运算

bact_granules=bact_granules&

bact_bw;

%特征与运算

erode_bw=erode(bact_bw);

%

%figure,imshow(erode_bw);

nobord=imclearborder(erode_bw);

fill_bw=imfill(nobord,'

holes'

figure,imshow(fill_bw);

%fill_bw=bwfill(erode_bw,'

%figure,imshow(fill_bw);

bact_granules_0=(bact_granules==0);

%判0运算的结果,等价与

figure,imshow(bact_granules_0);

%下边的取反运算!

%bact_granules_anti=~bact_granules;

%figure,imshow(bact_granules_anti);

granules=fill_bw&

bact_granules_0;

%尽可能多地把“核”提取出来

figure,imshow(granules)

[r,c]=find(granules);

result=bwselect(bact_bw,c,r);

figure,imshow(bacteria);

figure,imshow(result)

%2006.7.15

%按顺序标记每一个连通区域

Removethenoise 

Findtheboundaries 

Determinewhichobjectsareround

RGB=imread('

pillsetc.png'

imshow(RGB)

I=rgb2gray(RGB);

threshold=graythresh(I);

bw=im2bw(I,threshold);

bw=bwareaopen(bw,30);

se=strel('

disk'

bw=imclose(bw,se);

bw=imfill(bw,'

[B,L]=bwboundaries(bw,'

noholes'

figure,imshow(label2rgb(L,@jet,[.5.5.5]))

holdon

data=regionprops(L,'

all'

centr=[data.Centroid];

%标记在重心上

nums=1:

length(B);

%需要标记的物体个数

fork=1:

length(B)

boundary=B{k};

plot(boundary(:

2),boundary(:

1),'

w'

'

2)

signal=num2str(nums(k));

text(centr(2*k-1),centr(2*k),signal)%按序标记物体

end

fork=1:

length(B) 

%画出每个区域的最小凸边形

plot(data(k).ConvexHull(:

1),data(k).ConvexHull(:

2),'

b'

2)

stats=regionprops(L,'

Area'

Centroid'

threshold=0.94;

delta_sq=diff(boundary).^2;

perimeter=sum(sqrt(sum(delta_sq,2)));

area=stats(k).Area;

metric=4*pi*area/perimeter^2;

metric_string=sprintf('

%2.2f'

metric);

ifmetric>

threshold

centroid=stats(k).Centroid;

plot(centroid

(1),centroid

(2),'

ko'

end

text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'

...

title(['

Metricscloserto1indicatethat'

theobjectisapproximatelyround'

]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%2006.7.17

%果穗梗、花椒籽分离

%d:

\matlab7\toolbox\images\images\truesize.m*(308行)

clc

i=imread('

huaj_600_rgb.tif'

figure,imshow(i);

ig=rgb2gray(i);

imed=medfilt2(ig);

imedcanny=edge(imed,'

canny'

se90=strel('

line'

2,90);

se0=strel('

2,0);

bwsdil=imdilate(imedcanny,[se90se0]);

%>

figure,imshow(bwsdil)

ifill=imfill(bwsdil,'

bwero=imerode(ifill,[se90se0]);

nosmall=bwareaopen(bwero,3000,4);

nobord=imclearborder(nosmall,4);

figure,imshow(nobord);

[labeled,numobjects]=bwlabel(nobord,4);

rgb_label=label2rgb(labeled,@spring,'

c'

shuffle'

figure,imshow(rgb_label);

mexdata=regionprops(labeled,'

holdon;

centr=[mexdata.Centroid];

%寻找重心位置

nums=1:

numobjects;

numobjects

soli=mexdata(k).Solidity;

soli_string=sprintf('

soli);

%等价于转字符串

signal=sprintf('

%d'

k);

%直接使用打印语句打印序号

text(centr(2*k-1)-30,centr(2*k)-30,soli_string)%标注每个Solidity值

%画最小凸多边形

fork=1:

plot(mexdata(k).ConvexHull(:

1),mexdata(k).ConvexHull(:

2),...

'

Linewidth'

holdoff;

%作出Solidity值的分布图,以之选定阈值

figure,hist([mexdata.Solidity],255)

%只显示Solidity>

0.92的物体的图像

idx=find([mexdata.Solidity]>

0.95);

nut=ismember(labeled,idx);

figure,imshow(nut)

[labnut,numnut]=bwlabel(nut,4);

numnut%数出花椒籽的粒数

%只显示Solidity<

idx=find([mexdata.Solidity]<

0.75);

peduncle=ismember(labeled,idx);

figure,imshow(peduncle)

[labped,numped]=bwlabel(peduncle,4);

numped%数出含果穗梗的花椒数

%只显示无果穗梗的花椒图像

idx=find([mexdata.Solidity]>

=0.75&

[mexdata.Solidity]<

=0.95);

pure=ismember(labeled,idx);

figure,imshow(pure)

[labpure,numpure]=bwlabel(pure,4);

numpure%纯净花椒的粒数

%分别对籽,皮,梗使用regionprops函数

nutdata=regionprops(labnut,'

peddata=regionprops(labped,'

puredata=regionprops(labpure,'

%封装成一个函数

functionhuaj();

%对花椒图像进行处理

imname=input('

inputanimagename:

s'

i=imread(imname);

figure,imshow(i);

ig=rgb2gray(i);

imed=medfilt2(ig);

imedcanny=edge(imed,'

se90=strel('

%figure,imshow(bwsdil)

ifill=imfill(bwsdil,'

bwero=imerode(ifill,[se90se0]);

nosmall=bwareaopen(bwero,3000,4);

nobord=imclearborder(nosmall,4);

[labeled,numobjects]=bwlabel(nobord,4);

figure,hist([mexdata.Solidity],255)

idx=find([mexdata.Solidity]>

0.92);

nut=ismember(labeled,idx);

figure,imshow(nut)

[labnut,numnut]=bwlabel(nut,4);

numnut%数出花椒籽的粒数

%只显示Solidity<

0.75的果穗梗图像

[labped,numped]=bwlabel(peduncle,4);

numped%数出含果穗梗的花椒数

%只显示无果穗梗的花椒果皮图像

=0.92);

pure=ismember(labe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 韩语学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1