使用 Python OpenCV 删除图像的黑色标题部分 [英] Remove black header section of image using Python OpenCV

查看:87
本文介绍了使用 Python OpenCV 删除图像的黑色标题部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Python CV 删除图像多个部分中的变黑部分.我尝试去噪,但效果不佳.

例如.我需要删除表格标题中的黑色部分(下图)并将标题背景转换为白色,内容为黑色.

谁能帮助我选择正确的库或解决方案来克服这个问题?

解决方案

这是@eldesgraciado 方法的修改版本,该方法使用 Python 中目标像素的形态命中或未命中操作来过滤点状图案.不同之处在于,我们不是用二值图像减去掩码,这会降低文本质量,而是先对二值图像进行膨胀,然后按位扩展,以保持文本质量.

  1. 获取二值图像.加载图像,灰度,

    点掩码

    去除圆点

    扩张以修复阈值处理过程中损坏的文本像素

    结果

    代码

    导入 cv2将 numpy 导入为 np# 加载图像,灰度,大津阈值图像 = cv2.imread('1.jpg')灰色 = cv2.cvtColor(图像,cv2.COLOR_BGR2GRAY)thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]# 执行形态学命中或未命中操作内核 = np.array([[-1,-1,-1], [-1,1,-1], [-1,-1,-1]])dot_mask = cv2.filter2D(thresh, -1, kernel)# 二进制图像的位异或掩码以去除点结果 = cv2.bitwise_xor(thresh, dot_mask)# 膨胀以修复损坏的文本像素# 因为文本质量因阈值化而降低# 然后与输入图像按位与内核 = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))dilate = cv2.dilate(结果,内核,迭代=1)结果 = cv2.bitwise_and(image, image, mask=dilate)结果[扩张==0] = [255,255,255]cv2.imshow('dot_mask', dot_mask)cv2.imshow('thresh', thresh)cv2.imshow('结果', 结果)cv2.imshow('扩张', 扩张)cv2.waitKey()

    I need to remove the blackened section in multiple parts of image using Python CV. I tried with denoising which doesn't give satisfactory results.

    Eg. I need to remove the blackened part in Table Header (below image) and convert the header background to white with contents as black.

    Can anyone help me with choosing the correct library or solution to overcome this?

    解决方案

    Here's a modified version of @eldesgraciado's approach to filter the dotted pattern using a morphological hit or miss operation on the target pixels in Python. The difference is that instead of subtracting the mask with the binary image which decreases text quality, we dilate the binary image then bitwise-and to retain the text quality.

    1. Obtain binary image. Load image, grayscale, Otsu's threshold

    2. Perform morphological hit or miss operation. We create a dot pattern kernel with cv2.getStructuringElement then use cv2.filter2D to convolve the image

    3. Remove dots. We cv2.bitwise-xor the mask with the binary image

    4. Fix damaged text pixels. We cv2.dilate then cv2.bitwise_and the finalized mask with the input image and color background pixels white


    Binary image

    Dot mask

    Remove dots

    Dilate to fix damaged text pixels from the thresholding process

    Result

    Code

    import cv2
    import numpy as np
    
    # Load image, grayscale, Otsu's threshold
    image = cv2.imread('1.jpg')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    
    # Perform morphological hit or miss operation
    kernel = np.array([[-1,-1,-1], [-1,1,-1], [-1,-1,-1]])
    dot_mask = cv2.filter2D(thresh, -1, kernel)
    
    # Bitwise-xor mask with binary image to remove dots
    result = cv2.bitwise_xor(thresh, dot_mask)
    
    # Dilate to fix damaged text pixels
    # since the text quality has decreased from thresholding
    # then bitwise-and with input image
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
    dilate = cv2.dilate(result, kernel, iterations=1)
    result = cv2.bitwise_and(image, image, mask=dilate)
    result[dilate==0] = [255,255,255]
    
    cv2.imshow('dot_mask', dot_mask)
    cv2.imshow('thresh', thresh)
    cv2.imshow('result', result)
    cv2.imshow('dilate', dilate)
    cv2.waitKey()
    

    这篇关于使用 Python OpenCV 删除图像的黑色标题部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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