【问题标题】:Correcting rough edges修正粗糙的边缘
【发布时间】:2017-03-18 08:58:48
【问题描述】:

有一张图片是从某个高度(约 130 英尺)点击的另一张图片中提取的。现在,当提取这个较小的图像时,它包含一个对象,该对象实际上具有非常规则和平滑的形状,并且边缘非常粗糙。现在我想检测没有。角,对象有(不使用轮廓)。但由于这些粗糙的边缘,没有。检测到的角落的数量大大增加。

以下是示例图片:

我怎样才能使边缘笔直?

【问题讨论】:

    标签: python python-2.7 opencv image-processing


    【解决方案1】:

    我认为您正在寻找的是一种简单的边缘平滑算法。我为你实现了一个。但是,它不会将彩色标志保存在外部形状内-如果这也很重要-因为您在问题中没有提到这一点-您必须自己弄清楚那部分。结果:

    我已经实现了轨迹栏,因此您可以根据自己的喜好使用平滑值。按“c”确认您选择的值。

    import cv2
    import numpy as np
    
    
    def empty_function(*arg):
        pass
    
    
    def SmootherEdgesTrackbar(img, win_name):
        trackbar_name = win_name + "Trackbar"
    
        cv2.namedWindow(win_name, cv2.WINDOW_NORMAL)
        cv2.resizeWindow(win_name, 1000, 500)
        cv2.createTrackbar("first_blur", win_name, 3, 255, empty_function)
        cv2.createTrackbar("second_blur", win_name, 3, 255, empty_function)
        cv2.createTrackbar("threshold", win_name, 0, 255, empty_function)
    
        while True:
            first_blur_pos = cv2.getTrackbarPos("first_blur", win_name)
            second_blur_pos = cv2.getTrackbarPos("second_blur", win_name)
            thresh_pos = cv2.getTrackbarPos("threshold", win_name)
            if first_blur_pos < 3:
                first_blur_pos = 3
            if second_blur_pos < 3:
                second_blur_pos = 3
            img_res = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            img_res = smoother_edges(img_res, (first_blur_pos * 2 + 1, first_blur_pos * 2 + 1),
                                     (second_blur_pos * 2 + 1, second_blur_pos * 2 + 1))
            _, img_res = cv2.threshold(img_res, thresh_pos, 255, 0)
            cv2.imshow(win_name, img_res)
    
            key = cv2.waitKey(1) & 0xFF
            if key == ord("c"):
                break
    
        cv2.destroyAllWindows()
        return img_res
    
    
    def unsharp_mask(img, blur_size, imgWeight, gaussianWeight):
        gaussian = cv2.GaussianBlur(img, blur_size, 0)
        return cv2.addWeighted(img, imgWeight, gaussian, gaussianWeight, 0)
    
    
    def smoother_edges(img, first_blur_size, second_blur_size=(5, 5),
                       imgWeight=1.5, gaussianWeight=-0.5):
        # blur the image before unsharp masking
        img = cv2.GaussianBlur(img, first_blur_size, 0)
        # perform unsharp masking
        return unsharp_mask(img, second_blur_size, imgWeight, gaussianWeight)
    
    
    # read the image
    img = cv2.imread("sample.jpg")
    # smoothen edges
    img = SmootherEdgesTrackbar(img, "Smoother Edges Trackbar")
    # show and save image
    cv2.imshow("img", img)
    cv2.imwrite("result.png", img)
    cv2.waitKey(0)
    

    编辑: 确定适合自己的值后,只需删除轨迹栏功能并使用固定值执行步骤即可。算法是这样的:

    convert to gray
    blur
    unsharp mask
    threshold 
    

    2 个中间步骤组合在 smoother_edges() 函数中。

    【讨论】:

    • 你发布的输出真的很好。所以让我告诉你我能理解你的程序在做什么。首先,它要求用户手动设置阈值和模糊值,然后将这些值传递给应用的其他函数,这些函数使用这些值隐式地对图像应用阈值和模糊。此外,在设置阈值和模糊后,我只会得到一个小的白色窗口。顺便谢谢。
    • 。而且我想要一个不涉及任何手工工作的人
    • 确定适合自己的数值后,只需删除轨迹栏功能,执行固定数值的步骤即可。算法是这样的:转换为灰度、模糊、不锐化蒙版、阈值。 Smoother_edges() 函数中结合了 2 个中间步骤。
    猜你喜欢
    • 1970-01-01
    • 2018-05-09
    • 2021-11-13
    • 1970-01-01
    • 2021-06-02
    • 2021-06-13
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多