20060221-Tracing George

来源:互联网 发布:手机棋牌麻将软件 编辑:程序博客网 时间:2024/06/14 07:48

原文:http://blogs.mathworks.com/steve/2006/02/21/tracing-george/

url = 'http://blogs.mathworks.com/images/steve/36/george.jpg';I = imread(url);imshow(I)
The first step is to threshold the image. The Image Processing Toolbox functiongraythresh computes binarization thresholds automatically. It works well for a variety of images.
threshold = graythresh(I);bw = im2bw(I, threshold);imshow(bw)
Toolbox function bwmorph has a thinning option, but it works on white (foreground) pixels instead of black pixels. So complement the image:
bw2 = ~bw;
and then thin it. Specify the number of iterations to be Inf so thatbwmorph will keep thinning until the lines are a single pixel wide.
bw3 = bwmorph(bw2, 'thin', inf);imshow(bw3)
Now trace the line using toolbox function bwboundaries.
boundaries = bwboundaries(bw3);
boundaries is a cell array containing one P-by-2 matrix for each object boundary. This image contains only one object, soboundaries contains only one P-by-2 matrix. The matrix has twice as many rows as we need, becausebwboundaries traces all the way around the object.
b = boundaries{1};b = b(1:floor(end/2), :);
Finally we are ready to plot the curve.
x = b(:,2);y = b(:,1);plot(x,y)axis ij     % Place the origin at upper left, with y values increasing from            % top to bottom.axis equal  % Set the aspect ratio so that the data units are the same in            % every direction.title('George P. Burdell')

0 0
原创粉丝点击