如何修复不完整的网格单元并修复图像中的缺失部分 [英] How to repair incomplete grid cells and fix missing sections in image

查看:70
本文介绍了如何修复不完整的网格单元并修复图像中的缺失部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用霍夫线在这张图中得到了水平线和垂直线的交点:

I used Hough lines to get intersection of horizontal and vertical lines in this image:

,但是复杂度随着网格单元数的显着增加而增加.

but the complexity grows with the grid cells count significantly.

是否有任何简单的方法可以在不使用线检测的情况下完成网格?

Is there any simple way to complete the grid without using line detection ?

谢谢.

推荐答案

以下是使用

  • 获取二进制图像.加载灰度图像, 获取水平/垂直线蒙版.创建水平/垂直内核,并使用 cv2.morphologyEx

    Obtain horizontal/vertical lines masks. Create horizontal/vertical kernel and isolate horizontal/vertical grid lines with cv2.getStructuringElement and cv2.morphologyEx

    组合面具.

    Combine masks. Bitwise-and masks together to complete grid

    填充单个网格孔.

    Fill individual grid holes. Find contours and fix holes by filling in each grid cell


    二进制图片


    Binary image

    水平蒙版(左)和垂直蒙版(右)

    Horizontal mask (left) and vertical mask (right)

    联合口罩

    修复单个网格孔

    求结果

    import cv2
    
    # Load image, grayscale, blur, Otsu's threshold
    image = cv2.imread('1.jpg')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (3,3), 0)
    thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    
    # Obtain horizontal lines mask
    horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
    horizontal_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=1)
    horizontal_mask = cv2.dilate(horizontal_mask, horizontal_kernel, iterations=9)
    
    # Obtain vertical lines mask
    vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
    vertical_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=1)
    vertical_mask= cv2.dilate(vertical_mask, vertical_kernel, iterations=9)
    
    # Bitwise-and masks together
    result = 255 - cv2.bitwise_or(vertical_mask, horizontal_mask)
    
    # Fill individual grid holes
    cnts = cv2.findContours(result, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(result, (x, y), (x + w, y + h), 255, -1)
    
    cv2.imshow('thresh', thresh)
    cv2.imshow('vertical_mask', vertical_mask)
    cv2.imshow('horizontal_mask', horizontal_mask)
    cv2.imshow('result', result)
    cv2.waitKey()
    

    这篇关于如何修复不完整的网格单元并修复图像中的缺失部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆