将锯齿状的边缘近似为线 [英] Approximating jagged edges as lines

查看:188
本文介绍了将锯齿状的边缘近似为线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到墨迹斑点角落的准确位置,如下所示:

I am trying to find accurate locations for the corners on ink blotches as seen below:

我的想法是使线条适合边缘,然后找到它们相交的位置.到目前为止,我已经尝试将cv2.approxPolyDP()与epsilon的各种值一起使用来近似边缘,但是这看起来并不可行.我的cv.approxPolyDP代码给出以下结果:

My idea is to fit lines to the edges and then find where they intersect. As of now, I've tried using cv2.approxPolyDP() with various values of epsilon to approximate the edges, however this doesn't look like the way to go. My cv.approxPolyDP code gives the following result:

理想情况下,这就是我要生产的(用油漆绘制):

Ideally, this is what I want to produce (drawn on paint):

是否存在解决此类问题的CV功能?我已经考虑过在阈值步骤之前使用高斯模糊,尽管该方法似乎对于拐角查找似乎并不十分准确.此外,我希望它对旋转图像具有较强的鲁棒性,因此,在没有其他考虑的情况下,对垂直线和水平线的过滤不一定会起作用.

Are there CV functions in place for this sort of problem? I've considered using Gaussian blurring before the threshold step although that method does not seem like it would be very accurate for corner finding. Additionally, I would like this to be robust to rotated images, so filtering for vertical and horizontal lines won't necessarily work without other considerations.

代码*:

import numpy as np
from PIL import ImageGrab
import cv2


def process_image4(original_image):  # Douglas-peucker approximation
    # Convert to black and white threshold map
    gray = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    (thresh, bw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    # Convert bw image back to colored so that red, green and blue contour lines are visible, draw contours
    modified_image = cv2.cvtColor(bw, cv2.COLOR_GRAY2BGR)
    contours, hierarchy = cv2.findContours(bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(modified_image, contours, -1, (255, 0, 0), 3)

    # Contour approximation
    try:  # Just to be sure it doesn't crash while testing!
        for cnt in contours:
            epsilon = 0.005 * cv2.arcLength(cnt, True)
            approx = cv2.approxPolyDP(cnt, epsilon, True)
            # cv2.drawContours(modified_image, [approx], -1, (0, 0, 255), 3)
    except:
        pass
    return modified_image


def screen_record():
    while(True):
        screen = np.array(ImageGrab.grab(bbox=(100, 240, 750, 600)))
        image = process_image4(screen)
        cv2.imshow('window', image)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

screen_record()

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