二值化轮廓图像轮廓点坐标顺时针获取matlab程序

由一副二值化轮廓图像,经过8邻域扫描,得到顺时针方向各个轮廓点的坐标。

%由一幅2值化的汉字轮廓图像顺时针或逆时针获取轮廓坐标
edgeIm = imread('D:\EdgeIm.bmp');
%figure, imshow(edgeIm);

%edgeIm中非零元素的坐标,即轮廓坐标
[i, j] = find(edgeIm);

%轮廓点数
numPoints = size(i, 1);
curNum = 0;

%初始搜索点坐标
currentR = i(1, 1);
currentC = j(1, 1);

%初始化轮廓点坐标
points = zeros(numPoints, 2);

%开始搜索
curNum = curNum + 1;
points(curNum,:) = [currentR, currentC];
edgeIm(currentR, currentC) = 0;

while curNum ~= numPoints;
    if edgeIm(currentR, currentC-1) == 1;
        curNum = curNum + 1;
        currentC = currentC - 1;
        points(curNum,:) = [currentR, currentC];
        edgeIm(currentR, currentC) = 0;
    
        
        elseif edgeIm(currentR-1, currentC-1) == 1;
            curNum = curNum + 1;
            currentR = currentR - 1;
            currentC = currentC - 1;
            points(curNum,:) = [currentR, currentC];
            edgeIm(currentR, currentC) = 0;
            
        elseif edgeIm(currentR-1, currentC) == 1;
            curNum = curNum + 1;
            currentR = currentR - 1;
            points(curNum,:) = [currentR, currentC];
            edgeIm(currentR, currentC) = 0;
            
        elseif edgeIm(currentR-1, currentC+1) == 1;
            curNum = curNum + 1;
            currentR = currentR - 1;
            currentC = currentC + 1;
            points(curNum,:) = [currentR, currentC];
            edgeIm(currentR, currentC) = 0;
            
        elseif edgeIm(currentR, currentC+1) == 1;
            curNum = curNum + 1;
            currentC = currentC + 1;
            points(curNum,:) = [currentR, currentC];
            edgeIm(currentR, currentC) = 0;
            
        elseif edgeIm(currentR+1, currentC+1) == 1;
            curNum = curNum + 1;
            currentR = currentR + 1;
            currentC = currentC + 1;
            points(curNum,:) = [currentR, currentC];
            edgeIm(currentR, currentC) = 0;
            
        elseif edgeIm(currentR+1, currentC) == 1;
            curNum = curNum + 1;
            currentR = currentR + 1;
            points(curNum,:) = [currentR, currentC];
            edgeIm(currentR, currentC) = 0;
            
        else edgeIm(currentR+1, currentC-1) == 1;
            curNum = curNum + 1;
            currentR = currentR + 1;
            currentC = currentC - 1;
            points(curNum,:) = [currentR, currentC];
            edgeIm(currentR, currentC) = 0;
    end
end


你可能感兴趣的:(数字图像处理)