簡體   English   中英

如何將曲線擬合到骨架化圖像?

[英]How do you fit a curve to a skeletonized image?

骨架化過程產生的圖像大致代表形狀的骨架,但不是輪廓。 如何將骨骼轉換為輪廓?

在此處輸入圖片說明


使用以下OpenCV / Python代碼,可以完成以下's.png'圖像的框架化過程:

在此處輸入圖片說明

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))
#!/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()

結果: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM