Skip to content

Instantly share code, notes, and snippets.

@welmends
Created August 24, 2022 20:24
Show Gist options
  • Save welmends/ac36952a88fde4bc33257e222fdcb56b to your computer and use it in GitHub Desktop.
Save welmends/ac36952a88fde4bc33257e222fdcb56b to your computer and use it in GitHub Desktop.
Signature Background Removal with OpenCV
import cv2 as cv
import numpy as np
class SignatureBackgroundRemoval:
def process_signature(self, img):
img = cv.GaussianBlur(img,(5,5),0)
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, img = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
alpha = np.ones(img.shape, dtype=img.dtype)*255
out = cv.merge((img, img, img, alpha))
out[np.all(out == [255, 255, 255, 255], axis=2)] = [0, 0, 0, 0]
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment