使用Python去掉试卷上的蓝色和红色笔记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- 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

posted on   隨風.NET  阅读(595)  评论(1编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示