问题描述
我正在尝试构建一个手写文本系统来比较每个文本,它们之间如何变化。我需要提取线段以为每个文本创建每个图像,然后进一步处理图像差异。我试图参考此处发布的代码。 https://stackoverflow.com/questions/46282691/opencv-cropping-handwritten-lines-line-segmentation
这是我的代码,它们是按轮廓排列的,但是它没有创建正确的边界框。
import cv2
import numpy as np
import imutils
#sort contours
def sort_contours(cnts):
# initialize the reverse flag and sort index
reverse = False
i = 1
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts,boundingBoxes) = zip(*sorted(zip(cnts,boundingBoxes),key=lambda b:b[1][i],reverse=reverse))
# return the list of sorted contours and bounding Boxes
return (cnts)
#import image
image = cv2.imread("002.jpg")
cv2.imshow('orig',image)
#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
cv2.waitKey(0)
#binary
#ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
ret,cv2.THRESH_BINARY_INV)
cv2.imshow('binary',thresh)
cv2.waitKey(0)
#dilation
kernel = np.ones((5,100),np.uint8)
#kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,150))
img_dilation = cv2.dilate(thresh,kernel,iterations=1)
cv2.imshow('dilated',img_dilation )
cv2.waitKey(0)
#find contours
ctrs= cv2.findContours(img_dilation.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# loop over the (Now sorted) contours
sorted_ctrs = sort_contours(imutils.grab_contours(ctrs))
for i,ctr in enumerate(sorted_ctrs):
# Get bounding Box
x,y,w,h = cv2.boundingRect(ctr)
# Getting ROI
roi = image[y:y+h,x:x+w]
# show ROI
cv2.imshow('segment no:'+str(i),roi)
cv2.rectangle(image,(x,y),( x + w,y + h ),(90,255),2)
cv2.waitKey(0)
cv2.imshow('marked areas',image)
cv2.imwrite('bounded_Box_image.png',image)
cv2.waitKey(0)
我感谢这些建议。 谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)