繁体   English   中英

识别图像中的表格网格

[英]Identify table grid in image

我必须识别此图像中的表格网格并将其更改为 Grimson 红色。 我是图像处理的初学者。

img_arr = mpimg.imread("1.jpg")

plt.imshow(img_arr)

grid = img_arr[470:800,42:670,(0,1,2)]

plt.imshow(grid.data)

根据图像尺寸,我能够看到图像的网格部分,但我不知道如何识别网格并更改其颜色。 如果有人对此有任何想法,请回复。

这是一种方法:

  • 将图像转换为灰度和阈值
  • 查找轮廓并使用轮廓区域进行过滤以隔离网格
  • 查找水平线和垂直线
  • 在图像上画线

这是结果

import cv2
import numpy as np

image = cv2.imread('1.png')
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Detect only grid
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 10000:
        cv2.drawContours(mask, [c], -1, (255,255,255), -1)

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
mask = cv2.bitwise_and(mask, thresh)

# Find horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (55,1))
detect_horizontal = cv2.morphologyEx(mask, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (0,0,255), 2)

# Find vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,25))
detect_vertical = cv2.morphologyEx(mask, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (0,0,255), 2)

cv2.imshow('thresh', thresh)
cv2.imshow('mask', mask)
cv2.imshow('image', image)
cv2.waitKey()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM