macOS python opencv 驗證碼 垂直投影
阿新 • • 發佈:2018-12-16
python opencv 圖片垂直投影
1,驗證碼垂直投影
#! /usr/local/bin/python3 # coding:utf-8 """ 圖片垂直投影 """ import cv2 import numpy as np from PIL import Image img = cv2.imread("/Users/y50/1310.jpg") #灰度圖 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #二值化,點值大於127的都為255 白色 ret, thresh1 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) #print (thresh1) # ret 為閥值 print (ret) #cv2.imshow("org", thresh1) #cv2.waitKey() h, w = thresh1.shape #建立一個值為0,長度為圖片寬度的列表,後面用於記錄每列黑點的數量 a = [0 for x in range(0, w)] for x in range (0, w): for y in range(0, h): # thresh1[x,y]==0 報錯 ??? if thresh1[y,x]==0: #如果該點為黑點 a[x] += 1 # 記錄該列黑點數量 thresh1[y,x] = 255 #黑點變百點 #塗黑 ??? for x in range(0 , w): for y in range((h-a[x]), h): thresh1[y,x ] = 0 cv2.imshow("ret", thresh1) cv2.waitKey()
-
原圖
-
二值圖
-
垂直投影
-
驗證碼
參考: