【发布时间】:2018-02-03 09:15:42
【问题描述】:
我想将these 2 个矩形合并为一个,例如this。我能想到的最简单的方法是获取顶部矩形的顶部 y 坐标和底部矩形的底部 y 坐标,并在cv2.rectangle() 中使用它们,但由于 for 循环,我无法同时获得这两点。
代码如下:
#Finding contours (always finds those 2 retangles + some noise):
_, conts, hierarchy = cv2.findContours(img_green, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in conts:
area = cv2.contourArea(cnt)
#filter all the noise
if area > 20:
x1, y1, w, h = cv2.boundingRect(cnt)
x2 = x1 + w # (x1, y1) = top-left vertex
y2 = y1 + h # (x2, y2) = bottom-right vertex
cv2.rectangle(green_bar_win, (x1, y1), (x2, y2), (255,0,0), 2)
print("x1:", x1, " y1:", y1, " x2:", x2, " y2:", y2)
这是打印结果(它在循环的不同迭代中打印两个矩形的左上角和右下角矩形点的 x,y 坐标):
x1: 60 y1: 217 x2: 83 y2: 288
x1: 60 y1: 169 x2: 83 y2: 216
x1: 60 y1: 217 x2: 83 y2: 288
x1: 60 y1: 169 x2: 83 y2: 216...
谢谢。
编辑:我的solution
【问题讨论】:
标签: python opencv computer-vision