c++ 如何获得多个独立形状的组合凸形船体

zqdjd7g9  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(73)

我有2个形状(图片1),需要找到一个convexHull的两个组合(图片2)。更确切地说,我有兴趣获得外角(紫色圆圈图片2)。形状是分离的。我跟踪的形状是一个透明塑料的正方形,侧面有两个彩色条纹。条纹很容易跟踪(inRange)。
我想的一个快速而肮脏的方法是用一条白色线连接条纹的中心,然后获得convexHull。我也在考虑连接两个形状的顶点列表,并获得组合的convexHull,但我不确定这种方法是否会崩溃convexHull函数。
有没有更好的方法来解决这个问题?
请帮
图1


的数据
Pic 2


ztyzrc3y

ztyzrc3y1#

问题已解决

工作起来很有魅力。连接不同形状的点不会撞上凸壳。
我在GitHub https://github.com/wojciechkrukar/OpenCV/blob/master/RectangleDetector/RectangleDetector.ipynb上发布了代码
这就是结果:


的数据
下面是最重要的一段代码:

_ , contours,hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#size of contour points
length = len(contours)
#concatinate poits form all shapes into one array
cont = np.vstack(contours[i] for i in range(length))
hull = cv2.convexHull(cont)
uni_hull = []
uni_hull.append(hull) # <- array as first element of list
cv2.drawContours(image,uni_hull,-1,255,2);

字符串

o3imoua4

o3imoua42#

这个答案指的是以前的一个答案中的一个小错误。我没有足够的声望在那里添加评论。在所选的答案中,在行中
第一个月
有一种类型。它应该是:
cont = np.vstack([contours[i] for i in range(length)])
否则,您将得到以下错误:TypeError: arrays to stack must be passed as a "sequence" type such as list or tuple.

zsohkypk

zsohkypk3#


的数据

import numpy as np
import cv2

img = cv2.imread('in1.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(_, thresh) = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)

thresh = ~thresh

points = np.column_stack(np.where(thresh.transpose() > 0))
hull1 = cv2.convexHull(points)
result1 = cv2.polylines(img.copy(), [hull1], True, (0,0,255), 2)

cv2.imshow('result1', result1)

points2 = np.fliplr(np.transpose(np.nonzero(thresh)))
approx = cv2.convexHull(points2)
result2 = cv2.polylines(img.copy(), [approx], True, (255,255,0), 2)

cv2.imshow('result2', result2)

(contours, _) = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
points3 = [pt[0] for ctr in contours for pt in ctr]
points3 = np.array(points3).reshape((-1,1,2)).astype(np.int32)

hull3 = cv2.convexHull(points3)
result3 = cv2.drawContours(img.copy(), [hull3], -1, (0,255,0), 1, cv2.LINE_AA)

cv2.imshow('result3', result3)

points4 = list(set(zip(*np.where(img >= 128)[1::-1])))
points4 = np.array(points4).reshape((-1,1,2)).astype(np.int32)

hull4 = cv2.convexHull(points4)
result4 = cv2.drawContours(img.copy(), [hull4], -1, (0,255,255), 1, cv2.LINE_AA)

cv2.imshow('result4', result4)

result = np.hstack([result1, result2, result3, result4])

cv2.imwrite('result.jpg', result)

字符串

相关问题