首页 > 解决方案 > 检测图像中网络链接周围的网络创建矩形

问题描述

我目前正在尝试编写一小段代码,可以准确检测图像中的网页链接并在它们周围创建一个矩形。这是我到目前为止的位置:

frame = self.current_frame
    lower_color_bounds = np.array([121, 50, 50], dtype=np.uint8)
    upper_color_bounds = np.array([130, 255, 255], dtype=np.uint8)

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower_color_bounds, upper_color_bounds)
    res = cv2.bitwise_and(frame, frame, mask=mask)

    kernel = np.ones((22, 30), np.uint8)
    res = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

    cv2.imshow("test", res)
    cv2.moveWindow("test", 2000, 0)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 1))
    closed = cv2.morphologyEx(res, cv2.MORPH_CLOSE, kernel)
    closed = cv2.erode(closed, kernel, iterations=1)
    closed = cv2.dilate(closed, kernel, iterations=1)
    (_, tmp2) = cv2.threshold(closed, 0, 255, cv2.THRESH_BINARY)
    edges = cv2.Canny(tmp2, 100, 100)
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    output = self.current_frame.copy()
    for contour in contours:
        x, y, w, h = cv2.boundingRect(contour)
        cv2.rectangle(self.current_frame, (x, y), (x + w, y + h), AColor.dark_red, 2)

我遇到的问题是我得到了许多嵌套部分,其中没有一个突出显示整个链接。关于如何清理它的任何建议?

标签: pythonopencv-contour

解决方案


推荐阅读