Python opencv:在点之间画线并找到完整的轮廓 [英] Python opencv: Draw lines between points and find full contours
问题描述
我有一组三个点,在一个三角形中.例如:[[390 37]、[371 179]、[555 179]]
画线后,
三角形填充轮廓图像:
I have a set of three points, in a triangle. eg: [[390 37], [371 179], [555 179]]
After drawing the lines, How to draw lines between points in OpenCV?
How do I find the full Contours between the lines I Just drew?
I keep receiving error below:
img = np.zeros([1000, 1000, 3], np.uint8)
cv2.drawContours(img, triangle_vertices, 0, (0, 0, 0), -1)
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
Error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'
I believe your main issue is that you can only find contours on a binary image (not color). Your points also need to be in a Numpy array (as x,y pairs -- note the comma). So here is how I did it in Python/OpenCV.
import cv2
import numpy as np
# create black background image
img = np.zeros((500, 1000), dtype=np.uint8)
# define triangle points
points = np.array( [[390,37], [371,179], [555,179]], dtype=np.int32 )
# draw triangle polygon on copy of input
triangle = img.copy()
cv2.polylines(triangle, [points], True, 255, 1)
# find the external contours
cntrs = cv2.findContours(triangle, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
# get the single contour of the triangle
cntr = cntrs[0]
# draw filled contour on copy of input
triangle_filled = img.copy()
cv2.drawContours(triangle_filled, [cntr], 0, 255, -1)
# save results
cv2.imwrite('triangle.jpg', triangle)
cv2.imwrite('triangle_filled.jpg', triangle_filled)
cv2.imshow('triangle', triangle)
cv2.imshow('triangle_filled', triangle_filled)
cv2.waitKey()
Triangle Polygon Image:
Triangle Filled Contour Image:
这篇关于Python opencv:在点之间画线并找到完整的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!