程序如下:
clear;clc
I=imread('G:\桌面备份\2017.10.2备份\等高线\等高线程序备份\6.jpg');
img=rgb2gray(I);
[c,h] =imcontour(img);
% 等高线标注
h_clabel=clabel(c,h);
set(h_clabel,'BackgroundColor',[1 1 .6])
s = getcontourlines(c);
调用函数如下:
function s = getcontourlines(c)
sz = size(c,2); %# Size of the contour matrix c
ii = 1; %# Index to keep track of current location
jj = 1; %# Counter to keep track of # of contour lines
while ii < sz %# While we haven't exhausted the array
n = c(2,ii); %# How many points in this contour?
s(jj).v = c(1,ii); %# Value of the contour
s(jj).x = c(1,ii+1:ii+n); %# X coordinates
s(jj).y = c(2,ii+1:ii+n); %# Y coordinates
ii = ii + n + 1; %# Skip ahead to next contour line
jj = jj + 1; %# Increment number of contours
end
end
其中,输出的s为一个结构体,存放每个一个像素值轮廓的坐标。上面函数同样适用于计算等高线的函数contourf。截图如下,其中v为像素值,x和y为对应的轮廓的横纵坐标。