Zhang-Suen 图像骨架提取算法的原理和OpenCV实现

记录一下图像骨架提取算法,转载至 两种图像骨架提取算法的研究(1)原理部分

基于OpenCV的实现代码如下,代码部分参考 opencv骨架提取/图像细化

void thinImage(Mat & srcImg) {
    vector<Point> deleteList;
    int neighbourhood[9];
    int nl = srcImg.rows;
    int nc = srcImg.cols;
    bool inOddIterations = true;
    while (true) {
        for (int j = 1; j < (nl - 1); j++) {
            uchar* data_last = srcImg.ptr<uchar>(j - 1);
            uchar* data = srcImg.ptr<uchar>(j);
            uchar* data_next = srcImg.ptr<uchar>(j + 1);
            for (int i = 1; i < (nc - 1); i++) {
                if (data[i] == 255) {
                    int whitePointCount = 0;
                    neighbourhood[0] = 1;
                    if (data_last[i] == 255) neighbourhood[1] = 1;
                    else  neighbourhood[1] = 0;
                    if (data_last[i + 1] == 255) neighbourhood[2] = 1;
                    else  neighbourhood[2] = 0;
                    if (data[i + 1] == 255) neighbourhood[3] = 1;
                    else  neighbourhood[3] = 0;
                    if (data_next[i + 1] == 255) neighbourhood[4] = 1;
                    else  neighbourhood[4] = 0;
                    if (data_next[i] == 255) neighbourhood[5] = 1;
                    else  neighbourhood[5] = 0;
                    if (data_next[i - 1] == 255) neighbourhood[6] = 1;
                    else  neighbourhood[6] = 0;
                    if (data[i - 1] == 255) neighbourhood[7] = 1;
                    else  neighbourhood[7] = 0;
                    if (data_last[i - 1] == 255) neighbourhood[8] = 1;
                    else  neighbourhood[8] = 0;
                    for (int k = 1; k < 9; k++) {
                        whitePointCount += neighbourhood[k];
                    }
                    if ((whitePointCount >= 2) && (whitePointCount <= 6)) {
                        int ap = 0;
                        if ((neighbourhood[1] == 0) && (neighbourhood[2] == 1)) ap++;
                        if ((neighbourhood[2] == 0) && (neighbourhood[3] == 1)) ap++;
                        if ((neighbourhood[3] == 0) && (neighbourhood[4] == 1)) ap++;
                        if ((neighbourhood[4] == 0) && (neighbourhood[5] == 1)) ap++;
                        if ((neighbourhood[5] == 0) && (neighbourhood[6] == 1)) ap++;
                        if ((neighbourhood[6] == 0) && (neighbourhood[7] == 1)) ap++;
                        if ((neighbourhood[7] == 0) && (neighbourhood[8] == 1)) ap++;
                        if ((neighbourhood[8] == 0) && (neighbourhood[1] == 1)) ap++;
                        if (ap == 1) {
                            if (inOddIterations && (neighbourhood[3] * neighbourhood[5] * neighbourhood[7] == 0)
                                && (neighbourhood[1] * neighbourhood[3] * neighbourhood[5] == 0)) {
                                deleteList.push_back(Point(i, j));
                            }
                            else if (!inOddIterations && (neighbourhood[1] * neighbourhood[5] * neighbourhood[7] == 0)
                                && (neighbourhood[1] * neighbourhood[3] * neighbourhood[7] == 0)) {
                                deleteList.push_back(Point(i, j));
                            }
                        }
                    }
                }
            }
        }
        if (deleteList.size() == 0)
            break;
        for (size_t i = 0; i < deleteList.size(); i++) {
            Point tem;
            tem = deleteList[i];
            uchar* data = srcImg.ptr<uchar>(tem.y);
            data[tem.x] = 0;
        }
        deleteList.clear();

        inOddIterations = !inOddIterations;
    }
}

原图和细化结果如下:

         



  • 12
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
以下是Python实现的Zhang-Suen骨架算法的示例代码: ``` python import numpy as np def ZhangSuen(image): """ Implementation of Zhang-Suen thinning algorithm for binary images. """ # Define the 8-neighborhood of a pixel neighbors = ((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)) # Initialize the image to be thinned thinned_image = image.copy() # Initialize flag to indicate if a pixel was removed pixel_removed = True while pixel_removed: pixel_removed = False # Step 1: Mark pixels to be removed marked_pixels = [] for x in range(1, image.shape[0]-1): for y in range(1, image.shape[1]-1): if image[x,y] == 0: continue neighbors_count = sum([image[x+i,y+j] == 255 for i,j in neighbors]) if neighbors_count < 2 or neighbors_count > 6: continue transitions_count = sum([1 for i in range(8) if image[x+neighbors[i][0],y+neighbors[i][1]] == 0 and image[x+neighbors[i+1 if i<7 else 0][0],y+neighbors[i+1 if i<7 else 0][1]] == 255]) if transitions_count != 1: continue marked_pixels.append((x,y)) # Step 2: Remove marked pixels for x,y in marked_pixels: thinned_image[x,y] = 0 pixel_removed = True # Step 3: Mark pixels to be removed marked_pixels = [] for x in range(1, image.shape[0]-1): for y in range(1, image.shape[1]-1): if image[x,y] == 0: continue neighbors_count = sum([image[x+i,y+j] == 255 for i,j in neighbors]) if neighbors_count < 2 or neighbors_count > 6: continue transitions_count = sum([1 for i in range(8) if image[x+neighbors[i][0],y+neighbors[i][1]] == 0 and image[x+neighbors[i+1 if i<7 else 0][0],y+neighbors[i+1 if i<7 else 0][1]] == 255]) if transitions_count != 1: continue marked_pixels.append((x,y)) # Step 4: Remove marked pixels for x,y in marked_pixels: thinned_image[x,y] = 0 pixel_removed = True return thinned_image ``` 解释一下上面的代码: 首先,我们定义了一个8邻域的元组neighbors,表示一个像素的8个邻居像素; 然后,我们初始化一个待细化的二值图像thinned_image,复制输入的二值图像; 接着,我们开始迭代细化过程,直到没有像素可以被删除为止; 在每一轮迭代中,我们执行两个步骤:标记像素和删除像素。标记像素的过程先扫描一遍图像,找到满足一定条件的像素,并将它们加入到一个列表marked_pixels中。删除像素的过程再扫描一遍图像,将marked_pixels中的像素删除。这两个步骤分别执行两次,以保证算法的正确性; 标记像素的条件包括:它周围有2到6个前景像素;它周围的前景像素中只有一个像素的背景像素是它的邻居;它不是端点像素(即它的周围不是一个完整的线段); 删除像素的条件与标记像素的条件相同。 最后,我们返回细化后的二值图像

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值