资源简介
演示了如何对一幅灰度图片进行二值化,边缘提取,边缘连接,边缘跟踪
代码片段和文件信息
function region_seg(imgdataiframe)
%------------------------------------------------------------------------
% Author : sun
% Date : 2010.5.21
% Function : sky segment
%------------------------------------------------------------------------
%
% constant defines
%
file = ‘test5.bmp‘;
left_bound = 65;
right_bound = 648;
top_bound = 15;
bot_bound = 525;
line_width = 3;
connect_size = 3;
close_size = 3;
med_vect_size = [10 2];
canny_thresh = [0.01 0.03];
canny_sigma = 1.5;
%
% read image and pre-operation
%
if nargin == 0 % no input arg
imgdata = imread(file);
I = rgb2gray(imgdata);
elseif ischar(imgdata)
file = imgdata;
I = rgb2gray(imread(file));
else
I = rgb2gray(imgdata);
end
figure;imshow(I);
if nargin==2
title(‘frame num:‘+int2str(iframe));
else
title(file);
end
%
% make covers
%
s = size(I);
% black box
cov_box = zeros(s(1)s(2));
cov_box(top_bound:bot_boundleft_bound:right_bound) = 1;
% top white lines
top_lines = zeros(s(1)s(2));
top_lines(top_bound:top_bound+line_widthleft_bound:right_bound)=1; % horizon
top_lines(top_bound:s(2)/4left_bound:left_bound+line_width) =1; % left
top_lines(top_bound:s(2)/4right_bound-line_width:right_bound) =1; % right
%
% denosing
%
denois = I;
denois = medfilt2(denoismed_vect_size);
figure;imshow(denois);
title(‘中值滤波去噪‘);
%
% edge detection
%
edgeim = edge(denois‘canny‘canny_threshcanny_sigma);
figure;imshow(edgeim);
title(‘边缘检测‘);
%
% morphologically imdilate
%
se = strel(‘disk‘connect_size);
edgeim = imdilate(edgeimse);
figure;imshow(edgeim);
title(‘扩充线条‘);
%
% morphologically close twice
%
se = strel(‘disk‘close_size);
imgclosed=imclose(edgeimse);
figure;imshow(imgclosed);
title(‘闭操作‘);
% cover
imgclosed = ~imgclosed;
imgclosed = imgclosed&cov_box;
imgclosed = imgclosed|top_lines;
figure;imshow(imgclosed);
title(‘方框掩模‘);
%
% get bound
%
dim = size(imgclosed);
col = round(dim(2)/2);
row = min(find(imgclosed(:col)));
boundary = bwtraceboundary(imgclosed[row col]‘S‘);
%
% display result
%
figure;imshow(I);
hold on;
plot(boundary(:2)boundary(:1)‘g‘‘LineWidth‘2);
% title(‘处理后效果‘);
title(file);
hold off;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1622070 2010-05-22 16:22 test5.bmp
文件 2739 2010-05-24 09:51 region_seg.m
----------- --------- ---------- ----- ----
1624809 2
评论
共有 条评论