f=imread('demo.png');
figure(1)
imshow(f)
f=f(:,:,3);
f=histeq(f,256); %增强对比度
f=im2bw(f,0.386);
rowhigh=102+276-1; %提取有用部分
colhigh=193+277-1;
f=f(102:rowhigh,193:colhigh);
se=strel('square',14); %开闭运算
fo=imopen(f,se);
f2=imclose(fo,se);
f2=~f2;
figure(2)
imshow(f2);
g=edge(f2,'sobel',0.47);
[B,L] = bwboundaries(f2,'noholes');
hold on
for k = 1:length(B) % 标记边界
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 2)
end
hold on
[L1,n]=bwlabel(g); %标定质心
ff=imread('demo.png');
for k=1:n
[r,c]=find(L1==k);
rbar=mean(r);
cbar=mean(c);
plot(cbar,rbar,'Marker','*','MarkerEdgeColor','blue');
fprintf('行坐标为%9.2f , ',cbar)
fprintf('列坐标为%9.2f\n',rbar);
rgb=ff(floor(cbar)+193,floor(rbar)+102,:) %读取RGB值
end
axis on,axis normal;
[H,theta,rho]=hough(g);%判断直线
peak=houghpeaks(H,11);
lines=houghlines(g,theta,rho,peak,'FillGap',10,'MinLength',31);%
hold on
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2.5,'Color','blue');
end
l=regionprops(L,'Perimeter','Area'); %求周长,面积
l.Perimeter
l.Area
stats = regionprops(L,'Area','Centroid','Perimeter');
threshold = 0.95;
for k = 1:length(B)
boundary = B{k};
area = stats(k).Area;
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2))); %利用4pi*面积/周长^2=1判断圆
metric = 4*pi*area/perimeter^2;
metric_string = sprintf('%2.2f',metric) ;
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','g', 'FontSize',14,'FontWeight','bold');
if metric > threshold
centroid = stats(k).Centroid;
text(centroid(1)-10,centroid(2)-10,'圆','Color','b','FontSize',10,'FontWeight','bold');
end
end
阿里matlab建模师
- 粉丝: 3778
- 资源: 2812
最新资源
- enca-1.9.tar
- 在小程序中使用formdata上传数据,可实现多文件上传.zip
- 图书盒子Pro小程序-JMU图书馆.zip
- 回答问题类微信小程序完整源码.zip
- redis - 5.0.5 - 1.el7.aarch64.rpm
- 只需放置一个dll 简单方便的hook微信强制打开小程序 devtool.zip
- 前端mpvue后端nodejs+thinkjs+mysql微信小程序商城(准备用uniapp重构并适配多端).zip
- Weakly-Supervised-Video-Emotion-Detection-and-Prediction-via-Cross-Modal-Temporal-Erasing-Network笔记
- 初试小程序之仿探探.zip
- 入门第一个小程序简单的电影推荐小程序.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈