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.95)
nominator_thresh, red_thresh = cv2.threshold(red_c, filter_condition, 255, cv2.THRESH_BINARY)
return red_thresh
def shijuanqingli(self, image):
thresh, dst1 = cv2.threshold(image, 50, 255, cv2.THRESH_BINARY)
dst1_without_pen = dst1
return dst1_without_pen
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__':
src = r'mask/dehw_train_00471.jpg'
image0 = cv2.imread(src)
seal_rm = SealRemove()
image_0 = seal_rm.remove_red_seal(image0)
# image_0_1 = cv2.cvtColor(image_0, cv2.COLOR_BGR2GRAY)
image1 = cv2.imread(src, 0)
#image_1 = seal_rm.shijuanqingli(image1)
image_result = seal_rm.join_image(image_0, image1)
cv2.imshow('new image', image_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
src_temp = src.split(r'.')
src_result = src_temp[0] + '_new.' + src_temp[1]
cv2.imwrite(src_result,image_result)
![]() |
![]() |
2023 |
![]() |
![]() |
1970 |
![]() |
![]() |
1763 |
4 |
![]() |
1718 |
5 |
![]() |
1710 |
6 |
![]() |
1701 |
7 |
![]() |
1684 |
8 |
|
1678 |
9 |
![]() |
1664 |
10 |
![]() |
1653 |
效果不错