Python 如何在OpenCV中将轮廓拆分为开放圆弧

Python 如何在OpenCV中将轮廓拆分为开放圆弧,python,opencv,image-processing,Python,Opencv,Image Processing,我有下面的图像,我需要分割轮廓,以创建不同的30度圆弧,然后我需要拟合一个圆通过。我唯一不知道如何分割轮廓。我正在用python做这件事。任何帮助都将显示。此答案解释了如何将轮廓分割为12个切片。这个答案有三个步骤: 1. Find contours 2. Find the region that constitutes a slice 3. Test whether the contour point lies in that slice. 这是我用来寻找轮廓的代码: import cv2

我有下面的图像,我需要分割轮廓,以创建不同的30度圆弧,然后我需要拟合一个圆通过。我唯一不知道如何分割轮廓。我正在用python做这件事。任何帮助都将显示。

此答案解释了如何将轮廓分割为12个切片。这个答案有三个步骤:

1. Find contours
2. Find the region that constitutes a slice
3. Test whether the contour point lies in that slice.
这是我用来寻找轮廓的代码:

import cv2
import numpy as np
import math
import random
img = cv2.imread('/home/stephen/Desktop/cluv0.jpg')
h, w, _ = img.shape
low = np.array([99,130,144])
high = np.array([132,255,255])
mask = cv2.inRange(cv2.cvtColor(img, cv2.COLOR_BGR2HSV), low, high)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour = contours[0]
center, radius = cv2.minEnclosingCircle(contour)
center = tuple(np.array(center, int))
这是我用来查找分区的代码:

distance = 2*max(img.shape)
for i in range(divisions):
    # Get some start and end points that slice the image into pieces (like a pizza)
    x = math.sin(2*i*math.pi/divisions) * distance + center[0]
    y = math.cos(2*i*math.pi/divisions) * distance + center[1]    
    x2 = math.sin(2*(i+1)*math.pi/divisions) * distance + center[0]
    y2 = math.cos(2*(i+1)*math.pi/divisions) * distance + center[1]    
    xMid = math.sin(2*(i+.5)*math.pi/divisions) * 123 + center[0]
    yMid = math.cos(2*(i+.5)*math.pi/divisions) * 123 + center[1]

    top = tuple(np.array((x,y), int))
    bottom = tuple(np.array((x2,y2), int))
    midpoint = tuple(np.array((xMid,yMid), int))

为了测试轮廓点是否位于切片中,我制作了一个临时遮罩并将切片绘制为白色。然后,我检查该点是否位于遮罩图像中的白色区域:

# Create a mask and draw the slice in white
mask = np.zeros((h,w), np.uint8)    
cv2.line(mask, center, top, 255, 1)
cv2.line(temp_image, center, top, (255,0,0), 3)
cv2.line(mask, center, bottom, 255, 1)
cv2.floodFill(mask, None, midpoint, 255)
cv2.imshow('mask', mask)
cv2.waitKey()


# Iterate through the points in the contour
for contour_point in contour:
    x,y = tuple(contour_point[0])
    # Check if the point is in the white section
    if mask[y,x] == 255:
        cv2.circle(img, (x,y), 3, color, 3)
for i in range(25):
    vid_writer.write(cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR))
此gif显示了切片:

这是输出图像:


为什么需要这些弧?可以在整个轮廓中拟合一个圆。分割轮廓有什么问题?我的意思是你有要点。您还希望得到什么?看起来您在找到蓝色显示的轮廓方面取得了一些进展。你能发布源图像和你用来寻找轮廓的代码吗?这对回答这个问题非常有帮助。在上面的方法中,您使用MineConclosing circle()取圆心,然后分割轮廓。我实际上需要从我的代码中找出孔中心(通过最小封闭圆)和孔的各种圆弧(通过将一个圆分割并拟合到圆弧)的差异。我的问题是,圆心不会影响弧的中心。如果这是真的,那么我的弧和孔中心将对齐。我可能错了。你能编辑你原来的问题,让它包含这些新信息吗?@KshitijPatil我添加了一些代码,解释如何在黑色背景上画一个白色三角形。