如何在python OpenCv中检测重叠或嵌入的矩形 [英] How to detect overlapping or embedded rectangle in python OpenCv

查看:211
本文介绍了如何在python OpenCv中检测重叠或嵌入的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我无法通过python OpenCv检测作为独立矩形嵌入和重叠的矩形

如果提供此图片:

So Im having trouble detecting rectangles that are embedded and overlapping as separate rectangles with python OpenCv

If given this image: These are rectangles embedded

or this image:

enter image description here

how do I detect these rectangles as 2 separate rectangles and not just one big polygon? and could you print the output onto the image?

解决方案

Here is the code to identify the rectangles separately. The explanations are inline with the code:

import numpy as np
import cv2

# The standard stuff: image reading, grayscale conversion, blurring & edge detection
image = cv2.imread('rect_image.png')
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
edges = cv2.Canny(gray, 50, 200)

# Finding and sorting contours based on contour area
cnts = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:6]

vertices = []
for i, c in enumerate(cnts):
    if i == 0:
        # This is the largest contour
        # For overlapping case the largest one will be the only one contour
        peri = cv2.arcLength(cnts[i], True)
        approx = cv2.approxPolyDP(cnts[i], 0.02 * peri, True)
        vertices.append(approx)
    elif i < len(cnts) - 1:
        # Searches for any other inner contour
        # Also filters out close contours generated due to thick line
        if not np.isclose(cv2.contourArea(cnts[i]), cv2.contourArea(cnts[i+1]), atol=20000):
            peri = cv2.arcLength(cnts[i+1], True)
            approx = cv2.approxPolyDP(cnts[i+1], 0.02 * peri, True)
            vertices.append(approx)

if len(vertices) == 1:
    # This case is where there is only one contour (the overlapping case)
    # There are eight extreme points for two overlapping rectangles
    # The distinct rectangles are colored in 'green' and 'red'
    extLeft1 = tuple(vertices[0][vertices[0][:, :, 0].argmin()][0])
    extRight1 = tuple(vertices[0][vertices[0][:, :, 0].argmax()][0])
    extTop1 = tuple(vertices[0][vertices[0][:, :, 1].argmin()][0])
    extBot1 = tuple(vertices[0][vertices[0][:, :, 1].argmax()][0])
    mask = np.isin(vertices[0][:, :, 1], (extRight1, extLeft1, extTop1, extBot1))
    indices = np.where(mask)
    vertices = np.delete(vertices[0], indices, 0)
    extLeft2 = tuple(vertices[vertices[:, :, 0].argmin()][0])
    extRight2 = tuple(vertices[vertices[:, :, 0].argmax()][0])
    extTop2 = tuple(vertices[vertices[:, :, 1].argmin()][0])
    extBot2 = tuple(vertices[vertices[:, :, 1].argmax()][0])

    x, y, w, h = cv2.boundingRect(np.array([extLeft1, extLeft2, extRight1, extRight2]))
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    x, y, w, h = cv2.boundingRect(np.array([extTop1, extTop2, extBot1, extBot2]))
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
else:
    # This case is where there are inner rectangle (the embedded case)
    # The distinct rectangles are colored in 'green' and 'red'
    x, y, w, h = cv2.boundingRect(vertices[0])
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    x, y, w, h = cv2.boundingRect(vertices[1])
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)

# Displaying the images with identified colored rectangles
cv2.imshow("Input", orig)
cv2.imshow("Contour", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Below are the output of the detected rectangles drawn on the image in green and red colors:

这篇关于如何在python OpenCv中检测重叠或嵌入的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆