【问题标题】:Detecting borders of a page on a table and then "refocus"检测表格上页面的边框,然后“重新聚焦”
【发布时间】:2020-04-25 03:41:16
【问题描述】:

我有以下图片,我想检测页面的边框,然后重新聚焦以查看“仅”页面。如何开始使用 opencv3 和 Python 3 编写代码?

【问题讨论】:

    标签: python-3.x opencv3.0


    【解决方案1】:

    您可以简单地使用阈值方法将纸张与背景分开。演示:

    读取图像并转换为灰色。

    image = cv2.imread("page.jpg")
    gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    

    检查直方图以选择阈值。更多here

    color = ('b','g','r')
    fig = plt.figure(figsize=(12,12))
    ax = fig.add_subplot(1,2,1)
    ax.imshow(image)
    ax1 = fig.add_subplot(1,2,2)
    for i,col in enumerate(color):
        histogram = cv2.calcHist([image],[i],None,[256],[0,256])
        ax1.plot(histogram,color = col)
        ax1.set_xlim([0,256])
    

    使用模糊去除笔记本的细节。

    blurred_gray_image = cv2.blur(gray_image,(21,21))
    

    thresholding。使用我们从直方图中得到的值。

    _,thresholded_blurry_image = cv2.threshold(blurred_gray_image,165,255,cv2.THRESH_BINARY)
    

    检测contours(未分割的闭合形状)。

    contours, hierarchy = cv2.findContours(thresholded_blurry_image,
    cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    

    如果有轮廓,将最大轮廓的轮廓绘制到原始图像的副本中。Source帖子寻找最大轮廓。

    output = image.copy()
    if len(contours) != 0:
        c = max(contours, key = cv2.contourArea)
        # coordinates of the contour
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(output,(x,y),(x+w,y+h),(0,255,0),2)
    

    显示结果

    output = cv2.cvtColor(output,cv2.COLOR_BGR2RGB)
    plt.imshow(output)
    

    您可以使用 cv2.imwrite() 函数来保存图像。 希望这个答案能满足你的问题。但请注意,这种方法并不总是有效,因为我们自己评估直方图并手动选择阈值。如果您想采用更通用的方法,请尝试adaptive thresholding 或借助算法评估直方图值。祝你好运。

    【讨论】:

    • 非常感谢这个详细而清晰的回答!
    • 没问题,我很乐意提供帮助 :) 。请务必了解每个步骤及其背后的逻辑。继续编码:D
    猜你喜欢
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    相关资源
    最近更新 更多