简体   繁体   中英

Convert OpenCV contour to Lines and Curves

Is there a way to convert the contours extracted using OpenCV to convert/approximate them into Lines and Curves, probably forming a shape?

I am not that good in mathematics, any suggestion will be highly appreciated

Bazier Curve for 3 points

import cv2
import numpy as np

im = np.zeros((480,640,3),dtype="uint8")

x1=100
y1=100
x2=300
y2=250
x3=200
y3=400

cv2.circle(im,(x1,y1),13,(0,255,0),3)
cv2.circle(im,(x2,y2),13,(0,255,0),3)
cv2.circle(im,(x3,y3),13,(0,255,0),3)

lx,ly=x1,y1
for tt in range(0,10,1):
    t=tt/10
    x = int((1-t)** 2 * x1 + 2 * (1-t) * t * x2 + t**2 * x3)
    y = int((1-t)** 2 * y1 + 2 * (1-t) * t * y2 + t**2 * y3)

    cv2.circle(im,(x,y),3,(0,0,255),3)
    cv2.line(im,(x,y),(lx,ly),(255,0,255),3)

    lx,ly = x,y


cv2.imshow('im',im)
cv2.waitKey(0)

3 分的 Bazier 曲线

You could apply the Bazier Curve formula using 3 points at a time from countours points

import numpy as np
import cv2 as cv
im = cv.imread('test.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

more info: https://docs.opencv.org/master/d4/d73/tutorial_py_contours_begin.html

Draw contours

image = cv2.drawContours(image,contours,-1,(0,0,255),3)

Or you can draw polylines from contours points

for contour in contours:
    image = cv2.polylines(image, [contour],  
                      isClosed, (0,255,0), 3) 

more info here: https://docs.opencv.org/master/dc/da5/tutorial_py_drawing_functions.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM