关于垂直投影法的python,opncv的实现方式
关于垂直投影的具体介绍,就不多说,直接上代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34import cv2
import numpy as np
# 灰度化读取图片
image = cv2.imreade('number.jpg',0)
# 将图片二值化
retval, img = cv2.threshold(image,170,255,cv2.THRESH_BINAPY_INV)
# 创建一个空白图片(img.shape[0]为height,img.shape[1]为width)
paintx = np.zeros(img.shape,np.uint8)
# 将新图像数组中的所有通道元素的值都设置为0
cv2.cv.Zero(cv2.cv.fromarray(paintx))
# 创建width长度都为0的数组
w = [0]*image.shape[1]
# 对每一行计算投影值
for x in range(image.shape[1]):
for y in range(image.shape[0]):
t = cv2.cv.Get2D(cv2.cv.fromarray(img),y,x)
if t[0] == 0:
w[x] += 1
# 绘制垂直投影图
for x in range(image.shape[1]):
for y in range(w[x]):
# 把为0的像素变成白
cv2.cv.Set2D(cv2.cv.fromarray(paintx),y,x(255,255,255,0))
# 显示图片
cv2.imshow('paintx',paintx)
cv2.waitKey(0)
另外把水平投影也一起写了
1 | painty = np.zeros(img.shape,np.uint8) |