【发布时间】:2019-08-27 02:03:42
【问题描述】:
我需要将contours 分组并绘制一个包含所有轮廓的bounding rectangle,就像这样
from matplotlib import pyplot as plt
import cv2 as cv
img = cv.imread('shapes1.png', 0)
imgRGB = cv.cvtColor(img.copy(), cv.COLOR_GRAY2RGB)
_, ctrs, _ = cv.findContours(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
boxes = []
for ctr in ctrs:
x, y, w, h = cv.boundingRect(ctr)
boxes.append([x, y, w, h])
for box in boxes:
top_left = (box[0], box[1])
bottom_right = (box[0] + box[2], box[1] + box[3])
cv.rectangle(imgRGB, top_left, bottom_right, (0,255,0), 2)
fig = plt.figure(figsize = (10, 10))
ax = fig.add_subplot(111)
ax.imshow(imgRGB, cmap='gray')
是否有任何直接的方法可以做到这一点,而不是以编程方式合并所有边界矩形
【问题讨论】:
标签: python python-3.x opencv opencv-contour