如何从轮廓CHAIN_APPROX_SIMPLE转到轮廓CHAIN_APPROX_NONE

时间:2019-07-08 09:45:44

标签: python numpy opencv contour

使用cv2.findContours()可以创建“稀疏”(CHAIN_APPROX_SIMPLE)或“满”(CHAIN_APPROX_NONE)的轮廓。如何将“稀疏”轮廓转换为“完整”轮廓?

我没有轮廓的源图像(形状是已知的),只有轮廓,它们是“稀疏的”(CHAIN_APPROX_SIMPLE)。从这个“稀疏”表示中,我想解析“完整”(CHAIN_APPROX_NONE)表示,以便可以使用它从其他图像访问轮廓强度。

我的临时解决方案(请参见代码段)是使用cv2.drawContours(),它从“稀疏”轮廓表示中绘制所有轮廓像素。结果是一张图片,我能够从生成的图片中提取索引,例如使用np.argwhere()

但是,考虑到cv2.drawContours()在绘制结果图像之前可能已经在内部具有这些索引,因此这一额外步骤似乎没有必要。我想我想要的是cv2.drawContours()的不带绘图部分的变体,或者是输出“完整”轮廓表示而不是图像的选项。

我的临时解决方案的另一个问题是它不保留原始轮廓中的点的顺序。我想知道cv2.drawContours()是否能够在将结果展平为图像之前在内部重新创建完整的有效轮廓?

opencv的其他功能是否可以使用此功能,也许是cv2.drawContours()内部使用的更基本的功能?

import numpy as np
import cv2

# shape (Y,X)
shape = np.array((5, 5))

# sparse contour (X,Y)
sparse_contours = [np.array(
    [[[1, 0]],
     [[1, 4]],
     [[3, 4]],
     [[3, 0]]], dtype=np.int32)]

def full_contour_from_contour(shape, contour):
    # switch shape from y,x to x,y
    shp = shape[[1,0]]
    arr = np.zeros(shp, dtype=np.int32)
    cv2.drawContours(arr, [contour], 0, 1, 1)
    idx = np.argwhere(arr==1)
    # reorder Y,X -> X,Y
    idx = idx[:, [1, 0]]
    # reshape to contour layout
    rows, cols = idx.shape
    idx = idx.reshape(rows, 1, cols)
    return idx.astype(np.int32)

full_contour = full_contour_from_contour(shape, sparse_contour)

# output
# these are correct pixels, with pixels in sparse contour also
# part of the full contour. However, the full contour is not 
# necessarily correct or even valid due to 
# lost information regarding point sequence along the contour)

[[[1 0]]

 [[2 0]]

 [[3 0]]

 [[1 1]]

 [[3 1]]

 [[1 2]]

 [[3 2]]

 [[1 3]]

 [[3 3]]

 [[1 4]]

 [[2 4]]

 [[3 4]]]






2 个答案:

答案 0 :(得分:0)

当您查看文档时:https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours 它指出它们之间的区别在于CHAIN_APPROX_NONE存储每个像素,而CHAIN_APPROX_SIMPLE仅存储形成轮廓线的端点。因此,您可以简单地构造连接轮廓中每对连续顶点的线,以获得近似表示的近似值。属于该线的每个像素也属于轮廓。

答案 1 :(得分:0)

suggestion一致,这是一个似乎可以解决我的问题的代码段。

def full_from_sparse(contour):
    horizontal = np.array([1, 0], 'int')
    vertical = np.array([0, 1], 'int')
    diagonal = np.array([1, 1], 'int')
    def _get_points(p0, p1):
        # find all points on line connecting p0 and p1,
        # including p0, excluding p1
        # line must be horizontal, vertical or diagonal
        diff = p1-p0
        if np.max(np.abs(diff)) <= 1:
            # p0 and p1 are neighbor points
            # or duplicate points, i.e.g no in-between points
            return [p0]
        if diff[0] == 0:
            # vertical
            fac = diff[1]
            inc = vertical
        elif diff[1] == 0:
            # horizontal
            fac = diff[0]
            inc = horizontal
        elif diff[0] == diff[1]:
            # diagonal
            fac = diff[0]
            inc = diagonal
        else:
            raise Exception("points not connected", p0, p1)
        return [p0 + _fac*inc for _fac in range(0, fac, np.sign(fac))]

    full = []
    points = contour[:, 0, :]
    for i in range(len(points)-1):
        _points = _get_points(points[i], points[i+1])
        full.extend(_points)

    # add points from last segment, endpoint to startpoint
    _points = _get_points(points[-1], points[0])
    full.extend(_points)

    # reshape as contour
    full = np.array(full, dtype='int')
    rows, cols = full.shape
    return full.reshape(rows, 1, cols)