投影法多用于图像的阈值分割。闲话不多说,现用Python实现。
上代码。
import cv2
import numpy
img = cv2.imread('D:/0.jpg', cv2.COLOR_BGR2GRAY)
height, width = img.shape[:2]
#resized = cv2.resize(img, (3*width,3*height), interpolation=cv2.INTER_CUBIC)
#二值化
(_, thresh) = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
#cv2.imshow('thresh', thresh)
#扩大黑色面积,使效果更明显
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 10))#形态学处理,定义矩形结构
closed = cv2.erode(thresh, None, iterations = 5)
cv2.imshow('erode',closed)
height, width = closed.shape[:2]
v = [0]*width
z = [0]*height
a = 0
#垂直投影
#统计并存储每一列的黑点数
for x in range(0, width):
for y in range(0, height):
if closed[y,x][0] == 0:
a = a &#