簡體   English   中英

如何使用 Opencv 刪除圖像中的邊緣文本

[英]How to remove Edge Text in image using Opencv

我想刪除圖像邊緣的文本我使用了以下代碼但它不起作用它也刪除了中心的文本

輸入圖像

Output 圖像

import cv2
import matplotlib.pyplot as plt
import glob
import os
import numpy as np
def crop_buttom_text(img):
    """ Remove the text from the bottom edge of img """
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #blur = cv2.GaussianBlur(gray, (9,9), 0)  # No need for blurring
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Create rectangular structuring element and dilate
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 1))  # Use horizontal line as kernel - dilate horizontally.
    dilate = cv2.dilate(thresh, kernel, iterations=1)
    #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))
    #dilate = cv2.morphologyEx(dilate, cv2.MORPH_OPEN, kernel)    # No need for opening

    # Find contours and draw rectangle
    cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]  # [-2] indexing takes return value before last (due to OpenCV compatibility issues).
    #cnts = cnts[0] if len(cnts) == 2 else cnts[1] # [-2] is shorter....

    res_img = img.copy()  # Copy img to res_img - in case there is no edges text.

    for c in cnts:
        x, y, w, h = cv2.boundingRect(c)

        y2 = y + h  # Bottom y coordinate of the bounding rectangle

        if (y2 >= img.shape[0]):
            # If the rectangle touches the bottom of the img
            res_img = res_img[0:y-1, :].copy()  # Crop rows from first row to row y-1

    return res_img
def remove_lines(image_path,outdir):
    image = cv2.imread(image_path)
    img1 = crop_buttom_text(image)
    img2 = crop_buttom_text(np.rot90(img1)) # Rotate by 90 degrees and crop.
    img3 = crop_buttom_text(np.rot90(img2)) # Rotate by 90 degrees and crop.
    img4 = crop_buttom_text(np.rot90(img3)) # Rotate by 90 degrees and crop.

    output_img = np.rot90(img4)
    cv2.imwrite(os.path.join(outdir,os.path.basename(image_path)), output_img)
    
for jpgfile in glob.glob(r'/content/Dataset/*'):
    print(jpgfile)
    remove_lines(jpgfile,r'/content/output')

如何修改上面的代碼以刪除邊緣的文本

您提供的圖像是 JPEG 格式,應該是二進制的吧?

你如何刪除邊緣的文本? 這是解決問題的一種粗略方法:刪除每個接觸邊界的連接組件。

為了解決這個問題,您可以添加一個 1 像素的邊框,提取連接的組件,然后使用與邊框對應的一個作為二進制掩碼。

面具變成面具 結果是結果

請注意如何刪除 7 和幾個逗號。

from cv2 import cv2
import numpy as np


# Load original image
img = cv2.imread('kShDc.jpg', cv2.IMREAD_GRAYSCALE)
# Save original dimensions
h, w = img.shape[:2]
# Ensure only bilevel image with white as foreground
_, bimg = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
# Add one pixel border
bimg = cv2.copyMakeBorder(bimg, 1, 1, 1, 1, cv2.BORDER_CONSTANT, None, 255)
# Extract connected components (Spaghetti is the fastest algorithm)
nlabels, label_image = cv2.connectedComponentsWithAlgorithm(bimg, 8, cv2.CV_32S, cv2.CCL_SPAGHETTI)
# Make a mask with the edge label (0 is background, 1 is the first encountered label, i.e. the border)
ccedge = np.uint8((label_image != 1)*255)
cv2.imwrite("mask.png", ccedge, [cv2.IMWRITE_PNG_BILEVEL, 1])
# Zero every pixel touching the border
bimg = cv2.bitwise_and(bimg, ccedge)
# Remove border and invert again
bimg = 255 - bimg[1:h+1, 1:w+1]
# Save result
cv2.imwrite("result.png", bimg, [cv2.IMWRITE_PNG_BILEVEL, 1])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM