2个回答
展开全部
A=imread('D:\1.jpg'); %读入图像
I1=rgb2gray(A); %灰度处理,自动取值二值化
level=graythresh(I1);
bw=im2bw(I1,level);
[B,L] = bwboundaries(bw,4);
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');
% 循环历遍每个连通域的边界
for k = 1:length(B)
% 获取一条边界上的所有点
boundary = B{k};
% 计算边界周长
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% 获取边界所围面积
area = stats(k).Area;
% 计算匹配度
metric =80*area/perimeter^2;
% 要显示的匹配度字串
metric_string = sprintf('%2.2f',metric);
% 标记出匹配度接近1的连通域
if metric >= 0.8 && metric <= 1.1
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
% 提取该连通域所对应在二值图像中的矩形区域
goalboundary = boundary;
s = min(goalboundary, [], 1);
e = max(goalboundary, [], 1);
%将目标区域分别向两侧延伸7个像素
goal = imcrop(A2,[s(2)-7 s(1) e(2)-s(2)+14 e(1)-s(1)]);
end
% 显示匹配度字串
text(boundary(1,2)-35,boundary(1,1)+13,...
metric_string,'Color','g',...
'FontSize',14,'FontWeight','bold');
end
goal = ~goal; %将目标区域进行反处理
figure,imshow(goal)
I1=rgb2gray(A); %灰度处理,自动取值二值化
level=graythresh(I1);
bw=im2bw(I1,level);
[B,L] = bwboundaries(bw,4);
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');
% 循环历遍每个连通域的边界
for k = 1:length(B)
% 获取一条边界上的所有点
boundary = B{k};
% 计算边界周长
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% 获取边界所围面积
area = stats(k).Area;
% 计算匹配度
metric =80*area/perimeter^2;
% 要显示的匹配度字串
metric_string = sprintf('%2.2f',metric);
% 标记出匹配度接近1的连通域
if metric >= 0.8 && metric <= 1.1
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
% 提取该连通域所对应在二值图像中的矩形区域
goalboundary = boundary;
s = min(goalboundary, [], 1);
e = max(goalboundary, [], 1);
%将目标区域分别向两侧延伸7个像素
goal = imcrop(A2,[s(2)-7 s(1) e(2)-s(2)+14 e(1)-s(1)]);
end
% 显示匹配度字串
text(boundary(1,2)-35,boundary(1,1)+13,...
metric_string,'Color','g',...
'FontSize',14,'FontWeight','bold');
end
goal = ~goal; %将目标区域进行反处理
figure,imshow(goal)
追问
运行你那个的时候这样:
Error in ==> Untitled5 at 2
I1=rgb2gray(A); %灰度处理,自动取值二值化
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询