Convert Black Lines to Red:
I’m able to successfully convert all black lines in the images to red lines using OpenCV.
Text Detection and Interference:
I’m using a library to detect and process Chinese text, but this process interferes with the previously converted red lines.
Challenges:
The interference occurs because the text detection library changes the color of the red lines back to black.
Desired Outcome:
I’m looking for a solution that allows me to maintain the reenter image description hered color of the lines while also accurately detecting and processing the Chinese text.
Current Code
python
复制代码
{import cv2
import numpy as np
import os
from glob import glob
import easyocr
def process_image(image_path, output_path):
image = cv2.imread(image_path)
if image is None:
print(f"Failed to load image: {image_path}")
return
# Convert black lines to red
red_lower = np.array([0, 0, 0], dtype=np.uint8)
red_upper = np.array([60, 60, 255], dtype=np.uint8)
mask = cv2.inRange(image, red_lower, red_upper)
image[mask > 0] = [0, 0, 255] # Red lines
# Text detection and convert to black
reader = easyocr.Reader(['ch_sim'], gpu=False)
result = reader.readtext(image_path)
for bbox, text, prob in result:
if prob > 0.4: # Confidence threshold
xmin, ymin = bbox[0][0], bbox[0][1]
xmax, ymax = bbox[2][0], bbox[2][1]
image[ymin:ymax, xmin:xmax] = [0, 0, 0] # Black text
cv2.imwrite(output_path, image)
print(f"Processed and saved image: {output_path}")
def batch_process_images(input_folder, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
image_paths = glob(os.path.join(input_folder, "*.png"))
for image_path in image_paths:
output_path = os.path.join(output_folder, os.path.basename(image_path))
process_image(image_path, output_path)
if __name__ == "__main__":
input_folder = r"C:\Users\chese\Desktop\aa" # Input image folder
output_folder = r"C:\Users\chese\Desktop\cc" # Output image folder
`your text`
batch_process_images(input_folder, output_folder)}