如何对轮廓进行分组并绘制单个边界矩形

问题描述 投票:0回答:3

我需要组合contours并绘制一个包含所有轮廓的bounding rectangle,像这样的东西

enter image description here

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')

enter image description here

有没有直接的方法来做它而不是以编程方式合并所有边界矩形

python python-3.x opencv opencv-contour
3个回答
2
投票

如果您不介意使用numpy,可以从那里简单地使用concatenate函数,请参阅以下代码。注意:我使用OpenCV 4.0.0,其中findContours的返回值的顺序是不同的。

import cv2
import numpy as np

# Input image
input = cv2.imread('images/kchZb.png', cv2.IMREAD_GRAYSCALE)

# Modify input image to extract "original" image
_, input = cv2.threshold(input[10:400, 40:580], 224, 255, cv2.THRESH_BINARY)

# Find contours
cnts, _ = cv2.findContours(input, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Concatenate all contours
cnts = np.concatenate(cnts)

# Determine and draw bounding rectangle
x, y, w, h = cv2.boundingRect(cnts)
cv2.rectangle(input, (x, y), (x + w - 1, y + h - 1), 255, 2)

# Output image
cv2.imwrite('images/output.png', input)
cv2.imshow('Input', input)
cv2.waitKey(0)

Output

免责声明:我是Python的新手,特别是OpenCV的Python API(获胜的C ++)。评论,改进,突出Python no-gos非常受欢迎!


0
投票

您可以通过迭代所有轮廓并从每个轮廓创建一个边界框,然后计算minX,minY,maxX,maxY来实现。

int minX=MAX_INTEGER, minY=MAX_INTEGER, maxX=0, maxY=0;

for each contour in contours:
    Rect rect = boundRecFromContour(contour)
    if rect.x < minX:
        minx = rect.x
    if rect.y < minY:
        minY = rect.y
    if rect.x+rect.width > maxX:
        maxX = rect.x+rect.width
    if rect.y+rect.height > maxY:
        maxY = rect.y+rect.height
Rect groupRect = (minX, minY, minX+maxX, minY+maxY)

现在你可以画出groupRect

此外,您还可以使用该技术来解决问题:

cv::Mat idx;
cv::findNonZero(binaryImg, idx);
// find min max points
double maxX = 0, minX = MAX_INTEGER;
double maxY = 0, minY = MAX_INTEGER;
for (int i=0; i<idx.rows; ++i) {
    cv::Point pnt = idx.at<cv::Point>(i);
    if (pnt.x > maxX) {
        maxX = pnt.x;
    }
    if (pnt.x < minX) {
        minX = pnt.x;
    }
    if (pnt.y > maxY) {
        maxY = pnt.y;
    }
    if (pnt.y < minY) {
        minY = pnt.y;
    }
}
Rect groupRect = cv::Rect(cv::Point(int(minX), int(minY)), cv::Point(int(maxX), int(maxY)));

0
投票

@HansHirse解决方案非常优雅。这是另一种解决方案,使用包围边界矩形。这可以直接使用C ++中OpenCV中的Rect类和OpenCV Wrapper library

or(|)运算符用于给出最小的包围矩形。

import cv2 as cv
import opencv_wrapper as cvw

img = cv.imread("shapes1.png", 0)
imgRGB = cvw.gray2bgr(img)

contours = cvw.find_external_contours(img)

enclosing_rect = contours[0].bounding_rect | contours[1].bounding_rect
enclosing_rect = enclosing_rect | contours[2].bounding_rect

cvw.rectangle(imgRGB, enclosing_rect, cvw.Color.GREEN)

cv.imshow("Image", imgRGB)
cvw.wait_key(0)

披露:我是OpenCV Wrapper的作者

© www.soinside.com 2019 - 2024. All rights reserved.