简体   繁体   中英

How to remove Edge Text in image using Opencv

I want to remove the text on the edged in the image I have used the following code but it does not work it also remove the text in the center

Input Image

Output image

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')

How can i modify the above code to remove the text around the edge

You provided image is in JPEG, it should be binary right?

How do you remove the text which is at the edge? Here is a rough way to attack the problem: remove every connected component which touches the border.

To solve this, you can add a 1 pixel border, extract the connected components and then use the one corresponding to the border as a binary mask.

The mask becomes面具 The result is结果

Notice how a 7 and few commas get removed as well.

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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