【问题标题】:How to make boundaries of an object well defined? [closed]如何明确定义对象的边界? [关闭]
【发布时间】:2017-08-25 05:50:31
【问题描述】:

是否有任何工具可以在图像中很好地定义边界。 我正在使用 Python-OpenCV。它有实现此功能的任何方法吗?

例如。

让我的输入图像是这样的。

您可以在边界上看到一些干扰。一些像素只是从边界投影。边界不是完美的直线。

期望的输出是这样的。

【问题讨论】:

  • 算法应该如何猜测你想要实现的目标?您能否做出任何假设,例如从任何黑色像素到最近的白色像素的距离可能不大于 x(您的字母线的宽度)?还是你有一套目标模板?

标签: opencv image-processing ocr image-preprocessing


【解决方案1】:

您可以使用cv2.findContours() 检测图像中的轮廓,然后用线条近似轮廓。请参阅OpenCV contours tutorial 上的轮廓近似部分。请注意,在教程中,在盒子内拍摄的部分可能有点锯齿状,但轮廓近似只是给出了很好的直线来绘制。你可以把 K 想成同样的方式,整体轮廓就像一个盒子,但里面有一些碎片。

这似乎运作良好:

import cv2
import numpy as np

img = cv2.imread('k.png', 0)  # read as grayscale
img = 255 - img  # flip black and white

# find contours in the image
bin_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1]
mode = cv2.RETR_EXTERNAL
method = cv2.CHAIN_APPROX_SIMPLE
contours = cv2.findContours(bin_img, mode, method)[1]

# take largest contour, approximate it
contour = contours[0]  
epsilon = 0.005 * cv2.arcLength(contour, True)
approx_contour = cv2.approxPolyDP(contour, epsilon, True)

contour_img = np.zeros_like(img)
contour_img = cv2.fillPoly(contour_img, [approx_contour], 255)
contour_img = 255 - contour_img  # flip back black and white

cv2.imshow('Fixed k', contour_img)
cv2.waitKey()

输入:

输出:

【讨论】:

    【解决方案2】:

    您可以进行 OCR,然后使用所需的字体打印结果。没有其他方法可以将您的输入图像转换为完美的所需形状。

    【讨论】:

    • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
    • 问题是:“是否有任何工具可以在图像中很好地定义边界。”答案是 OCR 工具,因为要重建“完美”的对象轮廓,您需要有关可以遇到的对象的信息。如果我们使用字母,有关“完美”形状的信息存储为字体字母形状。如果你做OCR,你可以打印出完美的形状。
    猜你喜欢
    • 1970-01-01
    • 2017-10-07
    • 2018-08-24
    • 2020-05-28
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    相关资源
    最近更新 更多