用opencv检查是否为白纸

def check_blank_image(file):
  img = cv2.imread(file)
  if img is None:
    return False
  # grary
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  # threshold to two value image
  ret,thresh = cv2.threshold(img, 150, 255,cv2.THRESH_BINARY)
  # white %
  nonzero = np.count_nonzero(thresh)
  all = thresh.size
  return (nonzero / float(all)) > 0.99

解释下:先转灰度,再做二值化,然后判断白色占比。

上面的150是卡的阈值,超过150都认为是黑色设为255,小于的则认为是白色,这里要比50%的125偏黑一点,不然 字少的容易检测成白纸,卡的阀值99类似的道理。

Leave a Reply

Your email address will not be published. Required fields are marked *