您的位置:首页 > 编程语言 > MATLAB

matlab 提取目标 并进项任意轮廓最小外接矩形代码及其实现

2017-12-25 22:30 579 查看

matlab 任意轮廓外接矩形代码及其实现;

clc;clear;close all;
I=imread('00121.bmp');
I=rgb2gray(I);
subplot(3,3,1);imshow(I);
% M=im2double(I);%将unit8转成double型
subplot(3,3,2),imhist(I);
M1=im2bw(I,25/255);%二值图level取值0~1.
subplot(3,3,3);imshow(M1);
BW1=1-M1;%取反操作
subplot(3,3,4);imshow(BW1);
BW2=bwareaopen(BW1,100000,4);%形态学操作,删除二值图像BW中面积小于10000的对象,4邻域
subplot(3,3,5);imshow(BW2);
BW3=1-BW2;
subplot(3,3,6);imshow(BW3);
BW=bwareaopen(BW3,90000);%形态学操作,删除二值图像BW中面积小于10000的对象,8邻域
subplot(3,3,7);imshow(BW);xlabel('除去背景后的完整二值图');
se=strel('disk',4);
% se1=strel('disk',4);
J=imerode(BW,se);
% J1=imerode(BW,se1);
J=BW-J;
subplot(3,3,8);imshow(J);xlabel('4直径');
% subplot(3,3,8);imshow(J1);xlabel('4直径');
J=edge(BW,'canny',0.3,2);
subplot(3,3,9);imshow(J);
整理好的下面。 clc;clear;close all;
I=imread('00073.bmp');
I=rgb2gray(I);
subplot(3,3,1);imshow(I);
%%
% M=im2double(I);%将unit8转成double型
subplot(3,3,2),imhist(I);
M1=im2bw(I,25/255);%二值图level取值0~1.
subplot(3,3,3);imshow(M1);
BW1=1-M1;%取反操作
subplot(3,3,4);imshow(BW1);
BW2=bwareaopen(BW1,100000,4);%形态学操作,删除二值图像BW中面积小于10000的对象,4邻域
subplot(3,3,5);imshow(BW2);
BW3=1-BW2;
subplot(3,3,6);imshow(BW3);
BW=bwareaopen(BW3,90000);%形态学操作,删除二值图像BW中面积小于10000的对象,8邻域
subplot(3,3,7);imshow(BW);xlabel('除去背景后的完整二值图');
%%
se=strel('disk',4);
% se1=strel('disk',4);
J=imerode(BW,se);
% J1=imerode(BW,se1);
J=BW-J;
J=bwmorph(J,'clean',10);%清除杂点,孤立点
J=bwmorph(J,'spur',Inf);%去除小分支,去毛刺
subplot(3,3,8);imshow(J);xlabel('4直径');
%%
[x,y]=find(J==1);%%返回的是行列像素不为0的点的纵坐标,横坐标
xmi=min(x(:));
xma=max(x(:));
ymi=min(y(:));
yma=max(y(:));
dx=xma-xmi; %%矩形宽
dy=yma-ymi; %%矩形长
rectangle('Position',[ ymi xmi dy dx],'EdgeColor','g', 'LineWidth',1);
s=dy*dx;
% [BW,thresh] = edge(BW,'canny');
Jj=edge(BW,'canny',[0.0063,0.0156],1.4);%灵敏度,高斯滤波标准差1.4
j=bwmorph(Jj,'clean',10);
subplot(3,3,9);imshow(j); %%
L=bwlabel(j,8);%标记连通区域,选择8邻域则可以标记成功,
stats=regionprops(L,'BoundingBox');
temp = stats.BoundingBox;
rectangle('position',temp,'edgecolor','r');
clc;clear;close all;
I=imread('00121.bmp');
I=rgb2gray(I);
subplot(3,3,1);imshow(I);
% M=im2double(I);%将unit8转成double型
subplot(3,3,2),imhist(I);
M1=im2bw(I,25/255);%二值图level取值0~1.
subplot(3,3,3);imshow(M1);
BW1=1-M1;%取反操作
subplot(3,3,4);imshow(BW1);
BW2=bwareaopen(BW1,100000,4);%形态学操作,删除二值图像BW中面积小于10000的对象,4邻域
subplot(3,3,5);imshow(BW2);
BW3=1-BW2;
subplot(3,3,6);imshow(BW3);
BW=bwareaopen(BW3,90000);%形态学操作,删除二值图像BW中面积小于10000的对象,8邻域
subplot(3,3,7);imshow(BW);xlabel('除去背景后的完整二值图');
se=strel('disk',4);
% se1=strel('disk',4);
J=imerode(BW,se);
% J1=imerode(BW,se1);
J=BW-J;
J=bwmorph(J,'clean',10);
subplot(3,3,8);imshow(J);xlabel('4直径');
[x,y]=find(J==1);%%返回的是行列像素不为0的点的纵坐标,横坐标
xmi=min(x(:));
xma=max(x(:));
ymi=min(y(:));
yma=max(y(:));
dx=xma-xmi; %%矩形宽
dy=yma-ymi; %%矩形长
rectangle('Position',[ ymi xmi dy dx],'EdgeColor','g', 'LineWidth',1);
s=dy*dx;
[BW,thresh] = edge(BW,'canny');
Jj=edge(BW,'canny',[0.0063,0.0156],1.4);%灵敏度,高斯滤波标准差1.4
j=bwmorph(Jj,'clean',10);
j=bwmorph(Jj,'spur',10);%去除小分支,去毛刺
subplot(3,3,9);imshow(j); %%
L=bwlabel(j,8);
stats=regionprops(L,'BoundingBox');
temp = stats.BoundingBox;
rectangle('position',temp,'edgecolor','r');

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: