Posted in Python onJanuary 17, 2020
投影法多用于图像的阈值分割。闲话不多说,现用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 + 1 else : continue v[x] = a a = 0 l = 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] = b cv2.imshow('chuizhi', emptyImage) #水平投影 #统计每一行的黑点数 a = 0 emptyImage1 = 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 = 0 l = 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] = b cv2.imshow('shuipin', emptyImage1) cv2.waitKey(0)
原图
垂直投影图
水平投影图
由这两图可以确定我们所需的分割点,从而可以进行下一步的文本分割。这将在下一篇博客中实现。
以上这篇Python实现投影法分割图像示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。
Python实现投影法分割图像示例(一)
- Author -
TIME_LEAF声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@