python - 我如何使用Python openCV在表格的这张图片中找到左上角框的位置(x,y,宽度,高度)?

标签 python opencv image-processing python-tesseract

我有这张图片,我需要找到的位置(仅)在左上方的框及其宽度和高度。如何在openCV中使用Python做到这一点? enter image description here

最佳答案

这是在Python / OpenCV / Numpy中执行此操作的一种方法。

  • 读取输入的
  • 转换为灰色
  • 二进制
  • 的阈值
  • 计算每一行和每一列中黑色像素的总和
  • 阈值总和必须大于图像
  • 的高度和宽度的80%
  • 在这些总和中找到所有具有非零值的坐标
  • 过滤坐标以删除彼此相差10像素以内的所有值,以避免与1像素以上的线重复出现
  • 获取过滤后的坐标的第一个和第二个坐标作为左上方矩形的边界
  • 在这些边界处裁剪输入图像
  • 保存结果

  • 输入:

    enter image description here
    import cv2
    import numpy as np
    
    # read input
    img = cv2.imread("table_cells.png")
    hh, ww = img.shape[:2]
    
    # convert to gray
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    # threshold to binary
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
    
    # get sum of black values in rows and columns
    row_sums = np.sum(thresh==0, axis=1)
    column_sums = np.sum(thresh==0, axis=0)
    
    # threshold sums to counts above 80% of hh and ww
    row_sums[np.where(row_sums<0.8*ww)] = 0
    column_sums[np.where(column_sums<0.8*hh)] = 0
    
    # find coordinates that have non-zero values
    row_coords = np.argwhere(row_sums>0)
    column_coords = np.argwhere(column_sums>0)
    num_rows = len(row_coords)
    num_cols = len(column_coords)
    
    # filter row_coords to avoid duplicates within 10 pixels
    row_coords_filt = [row_coords[0]]
    for i in range(num_rows-1):
        if (row_coords[i] > row_coords[i-1]+10):
            row_coords_filt.append(row_coords[i])
    
    column_coords_filt = [column_coords[0]]
    for i in range(num_cols-1):
        if (column_coords[i] > column_coords[i-1]+10):
            column_coords_filt.append(column_coords[i])
    
    # print row_coords_filt
    print('grid row coordinates:')
    for c in row_coords_filt:
        print (c)
    
    print('')
    
    # print column_coords_filt
    print('grid column coordinates:')
    for c in column_coords_filt:
        print (c)
    
    # get left, right, top, bottom of upper left rectangle
    left = int(column_coords_filt[0])
    right = int(column_coords_filt[1])
    top = int(row_coords_filt[0])
    bottom = int(row_coords_filt[1])
    
    # crop rectangle
    rectangle = img[top:bottom, left:right]
    
    # save output
    cv2.imwrite('table_cells_crop.png', rectangle)
    
    cv2.imshow('thresh', thresh)
    cv2.imshow('rectangle', rectangle)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    裁剪矩形:

    enter image description here

    找到的座标:
    grid row coordinates:
    [30]
    [315]
    [599]
    [884]
    
    grid column coordinates:
    [41]
    [790]
    [1540]
    [2289]
    

    关于python - 我如何使用Python openCV在表格的这张图片中找到左上角框的位置(x,y,宽度,高度)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61842616/

    相关文章:

    python - 如何使用python将图像中方形标题的背景从黑色反转为白色?

    c++ - cvSetImageROI 似乎不够快

    matlab - 将 2 个图像合并为一个平面图像

    python - 笛卡尔积获取一组索引以指向 NumPy 数组中的唯一元素

    python - 基于元组的子集 Pandas 数据框

    python - 将图像转换为 Python 中的二维坐标数组以实现两点相关

    iphone - 如何在 IOS 5 上使用 Open CV 查找模式的出现?

    java - 使用 BufferedImage 从 Java 中的 RGB 色彩空间获取灰度像素值

    python:不支持 OpenCV 图像深度 (CV_64F)

    python - 过滤数据框列值大于零?