Python + openCV去除图片中红色字体
HappyOJ  post at 1 years ago 80.1k 1 0
  1. import cv2
  2. import numpy as np
  3. class SealRemove(object):
  4. def remove_red_seal(self, image):
  5. # 获得红色通道
  6. blue_c, green_c, red_c = cv2.split(image)
  7. # 多传入一个参数cv2.THRESH_OTSU,并且把阈值thresh设为0,算法会找到最优阈值
  8. thresh, ret = cv2.threshold(red_c, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  9. # 实测调整为95%效果好一些
  10. filter_condition = int(thresh * 0.95)
  11. nominator_thresh, red_thresh = cv2.threshold(red_c, filter_condition, 255, cv2.THRESH_BINARY)
  12. return red_thresh
  13. def shijuanqingli(self, image):
  14. thresh, dst1 = cv2.threshold(image, 50, 255, cv2.THRESH_BINARY)
  15. dst1_without_pen = dst1
  16. return dst1_without_pen
  17. def join_image(self, img_without_red, dst1_without_pen):
  18. ret = cv2.bitwise_or(img_without_red, dst1_without_pen)
  19. return ret
  20. if __name__ == '__main__':
  21. src = r'mask/dehw_train_00471.jpg'
  22. image0 = cv2.imread(src)
  23. seal_rm = SealRemove()
  24. image_0 = seal_rm.remove_red_seal(image0)
  25. # image_0_1 = cv2.cvtColor(image_0, cv2.COLOR_BGR2GRAY)
  26. image1 = cv2.imread(src, 0)
  27. #image_1 = seal_rm.shijuanqingli(image1)
  28. image_result = seal_rm.join_image(image_0, image1)
  29. cv2.imshow('new image', image_result)
  30. cv2.waitKey(0)
  31. cv2.destroyAllWindows()
  32. src_temp = src.split(r'.')
  33. src_result = src_temp[0] + '_new.' + src_temp[1]
  34. cv2.imwrite(src_result,image_result)

Comments ( 1 )