You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

niblack_thresholding.py 531 B

123456789101112131415161718
  1. import cv2
  2. import numpy as np
  3. def niBlackThreshold( src, blockSize, k, binarizationMethod= 0 ):
  4. mean = cv2.boxFilter(src,cv2.CV_32F,(blockSize, blockSize),borderType=cv2.BORDER_REPLICATE)
  5. sqmean = cv2.sqrBoxFilter(src, cv2.CV_32F, (blockSize, blockSize), borderType = cv2.BORDER_REPLICATE)
  6. variance = sqmean - (mean*mean)
  7. stddev = np.sqrt(variance)
  8. thresh = mean + stddev * float(-k)
  9. thresh = thresh.astype(src.dtype)
  10. k = (src>thresh)*255
  11. k = k.astype(np.uint8)
  12. return k
  13. # cv2.imshow()