使用Python去掉试卷上的蓝色和红色笔记
# -*- encoding: utf-8 -*- import cv2 import numpy as np class SealRemove(object): """ 印章处理类 """ def remove_red_seal(self, image): """ 去除红色印章 """ # 获得红色通道 blue_c, green_c, red_c = cv2.split(image) # 多传入一个参数cv2.THRESH_OTSU,并且把阈值thresh设为0,算法会找到最优阈值 thresh, ret = cv2.threshold(red_c, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # 实测调整为95%效果好一些 filter_condition = int(thresh * 0.5) _, red_thresh = cv2.threshold(red_c, filter_condition, 255, cv2.THRESH_BINARY) # 把图片转回 3 通道 result_img = np.expand_dims(red_thresh, axis=2) result_img = np.concatenate((result_img, result_img, result_img), axis=-1) return result_img def remove_blue_seal(self, image): """ 去除红色印章 """ # 获得红色通道 blue_c, green_c, red_c = cv2.split(image) # 多传入一个参数cv2.THRESH_OTSU,并且把阈值thresh设为0,算法会找到最优阈值 thresh, ret = cv2.threshold(blue_c, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # 实测调整为95%效果好一些 filter_condition = int(thresh * 0.2) _, red_thresh = cv2.threshold(blue_c, filter_condition, 255, cv2.THRESH_BINARY) # 把图片转回 3 通道 result_img = np.expand_dims(red_thresh, axis=2) result_img = np.concatenate((result_img, result_img, result_img), axis=-1) return result_img def join_image(self, img_without_red, dst1_without_pen): ret = cv2.bitwise_or(img_without_red, dst1_without_pen) return ret if __name__ == '__main__': image = r'C:\Users\keying\Desktop\math\2.jpg' img = cv2.imread(image) seal_rm = SealRemove() rm_img1 = seal_rm.remove_red_seal(img) rm_img2 = seal_rm.remove_blue_seal(img) rm_img = seal_rm.join_image(rm_img1,rm_img2) cv2.imwrite(r"C:\Users\keying\Desktop\math\2.new.jpg", rm_img)
参考:
https://blog.csdn.net/sogobaidu/article/details/115829791