版权声明:本文为博主原创文章,可以自由转载。 https://blog.csdn.net/u010953692/article/details/84075672
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()
-
原图
-
二值图
-
垂直投影
-
验证码
参考: