Input Image from which I tried extract character
我想裁剪所有的字符从图像到文件夹,并给他们一行一行的名字,但字符裁剪不好,也不能正确命名。 在输出图像中,我的代码识别字符,但也有一些额外的行。在
我尝试了这个代码,但它不适用于所有大小的图像。在
import cv2
import numpy as np
cropped_Image_Location = "./"
image = cv2.imread(cropped_Image_Location+"Sample/17.jpg")
gray= cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
(thresh, im_bw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)
edges = cv2.Canny(im_bw, 100, 150, apertureSize=5)
cv2.imwrite(cropped_Image_Location + 'CroppedImages/edges-50-150.jpg', edges)
minLineLength = 100
lines = cv2.HoughLinesP(image=edges, rho=1, theta=np.pi / 180,
threshold=200, lines=np.array([]),minLineLength=minLineLength, maxLineGap=20000)
try:
a, b, c = lines.shape
print(a,b,c)
for i in range(a):
cv2.line(im_bw, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 3, cv2.LINE_AA)
except:
pass
_,thresh = cv2.threshold(im_bw,70,255,cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 0)
contours, hierarchy =
cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
i=1
for contour in contours:
[x,y,w,h] = cv2.boundingRect(contour)
print(x,y,w,h)
if w>1 and h>1:
print(x, y, w, h,"........")
cv2.rectangle(im_bw, (x-3, y-3), (x + w+3, y + h+3), (0, 0, 0), 1)
cv2.imwrite(cropped_Image_Location+"CroppedImages/"+str(i)+".jpg",image[y-2:y+h+2,x-2:x+w+2])
i=i+1
cv2.imwrite(cropped_Image_Location + 'CroppedImages/lectureAll.jpg', im_bw)
# cv2.imshow("gray",im_bw)
cv2.waitKey(0)
cv2.destroyAllWindows()
目前没有回答
相关问题 更多 >
编程相关推荐