Answer the question
In order to leave comments, you need to log in
Select center character in image?
I cut the image into symbols for recognition like this:
ret, im_th = cv2.threshold(im_gray, th, 255, cv2.THRESH_BINARY_INV)
im2, ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
i = 0
for rect in rects:
leng = int(rect[3] * 1.6)
pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
if (roi.shape[0]>20 and roi.shape[0]<100) and (roi.shape[1]>20 and roi.shape[1]<100):
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi = cv2.dilate(roi, (3, 3))
cv2.rectangle(img, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
print(roi)
i += 1
cv2.imwrite(f'./indata/sample_c{i}.jpg', roi)
Answer the question
In order to leave comments, you need to log in
You can remember the region of the symbol and fill with zeros along its boundaries:
x1 = rect[0] - pt2
x2 = rect[0] + rect[2] - pt2
....
if (roi.shape[0]>20 and roi.shape [0]<100) and (roi.shape[1]>20 and roi.shape[1]<100):
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
x1=int (x1*28/leng)
x2=int(x2*28/leng)
roi[:,:x1]=0
roi[:,x2:]=0
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question