投影法多用于图像的阈值分割。闲话不多说,现用Python实现。
上代码。
import cv2import numpyimg = 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]*widthz = [0]*heighta = 0#垂直投影#统计并存储每一列的黑点数for x in range(0, width): for y in range(0, height): if closed[y,x][0] == 0: a = a + 1 else : continue v[x] = a a = 0l = len(v)#print l#print width#创建空白图片,绘制垂直投影图emptyImage = numpy.zeros((height, width, 3), numpy.uint8) for x in range(0,width): for y in range(0, v[x]): b = (255,255,255) emptyImage[y,x] = bcv2.imshow('chuizhi', emptyImage)#水平投影#统计每一行的黑点数a = 0emptyImage1 = numpy.zeros((height, width, 3), numpy.uint8) for y in range(0, height): for x in range(0, width): if closed[y,x][0] == 0: a = a + 1 else : continue z[y] = a a = 0l = len(z)#print l#print height#绘制水平投影图for y in range(0,height): for x in range(0, z[y]): b = (255,255,255) emptyImage1[y,x] = bcv2.imshow('shuipin', emptyImage1)cv2.waitKey(0)
原图
垂直投影图
水平投影图
由这两图可以确定我们所需的分割点,从而可以进行下一步的文本分割。这将在下一篇博客中实现。
以上这篇Python实现投影法分割图像示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网之家。
新闻热点
疑难解答