opencv 如何将曲线拟合到 backbone 化的图像?

2exbekwf  于 2023-06-24  发布在  其他
关注(0)|答案(3)|浏览(101)

backbone 化的过程产生大致表示形状的 backbone 的图像,但它不是轮廓。如何将 backbone 转换为轮廓?

使用以下OpenCV/Python代码,您可以完成以下's.png'图像的 backbone 化过程:

import numpy as np
import cv2, cv 
img = cv2.imread("s.png",0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)

ret,img = cv2.threshold(img,127,255,0)
element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
done = False

img =cv2.bitwise_not(img)
original = img

while( not done):
    eroded = cv2.erode(img,element)
    temp = cv2.dilate(eroded,element)
    temp = cv2.subtract(img,temp)
    skel = cv2.bitwise_or(skel,temp)
    img = eroded.copy()

    zeros = size - cv2.countNonZero(img)
    if zeros==size:
        done = True

cv2.imshow("original", original)
cv2.imshow("skeleton",skel)
cv2.imshow("dilate-skeleton",cv2.dilate(skel, element))
mu0hgdu0

mu0hgdu01#

#!/usr/bin/env python

import pylab as pl
import numpy as np
import cv2

def skeletonize(image):
    img = image
    size = np.size(img)
    skel = np.zeros(img.shape,np.uint8)

    ret,img = cv2.threshold(img,127,255,0)
    element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
    done = False

    img =cv2.bitwise_not(img)
    # original = img

    while( not done):
        eroded = cv2.erode(img,element)
        temp = cv2.dilate(eroded,element)
        temp = cv2.subtract(img,temp)
        skel = cv2.bitwise_or(skel,temp)
        img = eroded.copy()

        zeros = size - cv2.countNonZero(img)
        if zeros==size:
            done = True

    return skel, cv2.dilate(skel, element)

def collapse_contours(contours):
    contours = np.vstack(contours)
    contours = contours.squeeze()
    return contours

def get_rough_contour(image):
    """Return rough contour of character.
    image: Grayscale image.
    """
    skeleton, dskeleton = skeletonize(image)
    contours, hierarchy = cv2.findContours(skeleton,
                                           cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
    contour = collapse_contours(contours)
    return contour

def draw_contour(image, contour, colors=(255, 0, 0)):
    for p in contour:
        cv2.circle(image, tuple(p), 1, colors, -1)

def main():
    image = cv2.imread("s.png", 0)
    contour = get_rough_contour(image)
    # You will probably want to do some spline fitting here to
    # smooth the contour
    draw_contour(image, contour)
    pl.imshow(image, cmap=pl.cm.gray)

if __name__ == '__main__':
    main()

结果:

46scxncf

46scxncf3#

您正在寻找findcontours()函数
http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=find%20contour#findcontours

相关问题