Practical Guide | OpenCV + OCR Circular Text Recognition

Click the "Xiaobai Learns Vision" above, select "Star" or "Top"
Heavy content delivered first

Introduction
This article will introduce the detailed steps and code demonstration for implementing circular text recognition using OpenCV + OCR. (Source: WeChat Official Account: OpenCV and AI Deep Learning)

Background Introduction

In optical character recognition (OCR) scenarios, there are many special cases, such as noise, dirt, tilt, deformation, etc., that can affect recognition. Circular text is one of them; we usually cannot recognize them directly but first convert the text to a horizontal direction before recognition. As shown in the figure below:

Practical Guide | OpenCV + OCR Circular Text Recognition

If we recognize directly, it is easy to fail. So what should we do? Below, I will introduce the recognition steps for the text in the above image in detail, which can also be seen as the general steps for circular text recognition.

Detailed Implementation Steps

[1] Find the circular contours in the image. Circular positioning can be achieved using general Blob analysis or Hough Circle Transform. Here, since the circles are relatively regular and clear, we can directly use Hough Circle Transform. The code and detection results are as follows:
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)gray = cv2.medianBlur(gray,3)circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,100,
         param1=200,param2=30,minRadius=200,maxRadius=300)
isNG = Falseif circles is None:  print("Failed to find the circle!")  else:  circles = np.uint16(np.around(circles))  a, b, c = circles.shape  print (circles.shape)  for i in range(b):    cv2.circle(img_copy, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA)    cv2.circle(img_copy, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle  cv2.imshow("findCircle", img_copy)

Practical Guide | OpenCV + OCR Circular Text Recognition

[2] Perform polar coordinate transformation based on the found circle to convert the text to a horizontal direction.
x = circles[0][i][0] - circles[0][i][2]y = circles[0][i][1] - circles[0][i][2]w = h = 2 * circles[0][i][2]center = (circles[0][i][0], circles[0][i][1])radius = circles[0][i][2]C = 2 * math.pi * radiusprint(C,radius)
ROI = img[y:y+h,x:x+w].copy()cv2.imshow('ROI',ROI)trans_center = (center[0]-x, center[1]-y)polarImg = cv2.warpPolar(ROI,(int(radius),int(C)),trans_center,radius,cv2.INTER_LINEAR + cv2.WARP_POLAR_LINEAR)polarImg = cv2.flip(polarImg,1) # MirrorpolarImg = cv2.transpose(polarImg)# Transposecv2.imshow('polarImg',polarImg)cv2.imwrite('polarImg.png',polarImg)

Practical Guide | OpenCV + OCR Circular Text Recognition

[3] Text recognition. You can use either EasyOCR or PaddleOCR; here we use EasyOCR.

result = reader.readtext(polarImg)print(result)if len(result) > 0:  for i in range(0,len(result)):    print(result[i][1])    if(result[i][2] < 0.4):      continue    for j in range(4):      if j > 0:        cv2.line(polarImg,(tuple(result[i][0][j-1])),(tuple(result[i][0][j])),(0,255,0),2,cv2.LINE_AA)    cv2.line(polarImg,(tuple(result[i][0][0])),(tuple(result[i][0][3])),(0,255,0),2,cv2.LINE_AA)      strText = result[i][1].replace(' ','')    cv2.putText(polarImg,strText,(result[i][0][3][0],result[i][0][3][1]+20),0,0.8,(0,0,255),2)
  cv2.imshow('polarImg-OCR',polarImg)

Practical Guide | OpenCV + OCR Circular Text Recognition

[4] Inverse polar coordinate transformation to restore the image containing the recognition results to a circular shape.

polarImg = cv2.flip(polarImg,0) # MirrorpolarImg = cv2.transpose(polarImg)# TransposepolarImg_Inv = cv2.warpPolar(polarImg,(w,h),trans_center,radius,cv2.INTER_LINEAR + 
                       cv2.WARP_POLAR_LINEAR + cv2.WARP_INVERSE_MAP)cv2.imshow('polarImg_Inv',polarImg_Inv)

Practical Guide | OpenCV + OCR Circular Text Recognition

[5] Create a circular mask image to perform copyTo operation, ensuring the outside of the circle matches the original image.

mask = np.zeros((h,w,1),np.uint8)  cv2.circle(mask,trans_center,radius-3,(255,255,255),-1, cv2.LINE_AA)  cv2.imshow('mask', mask)  ROI = img[y:y+h,x:x+w]  for i in range(0,ROI.shape[0]):    for j in range(0, ROI.shape[1]):      if mask[i,j] > 0:        ROI[i,j] = polarImg_Inv[i,j]  cv2.imshow('result', img)  cv2.waitKey(0)

Practical Guide | OpenCV + OCR Circular Text Recognition

Practical Guide | OpenCV + OCR Circular Text Recognition

Download 1: OpenCV-Contrib Extended Module Chinese Version Tutorial
Reply to “Xiaobai Learns Vision” in the WeChat Official Account backend:Extended Module Chinese Tutorial, to download the first Chinese version of the OpenCV extended module tutorial available online, covering installation of extended modules, SFM algorithms, stereo vision, object tracking, biological vision, super-resolution processing and more than twenty chapters.
Download 2: Python Vision Practical Project 52 Lectures
Reply to Xiaobai Learns Vision in the WeChat Official Account backend: Python Vision Practical Project, to download 31 vision practical projects including image segmentation, mask detection, lane line detection, vehicle counting, adding eyeliner, license plate recognition, character recognition, emotion detection, text content extraction, facial recognition, etc., to help quickly learn computer vision.
Download 3: OpenCV Practical Project 20 Lectures
Reply to Xiaobai Learns Vision in the WeChat Official Account backend: OpenCV Practical Project 20 Lectures, to download 20 practical projects based on OpenCV for advancing OpenCV learning.

Group Chat

Welcome to join the WeChat reader group to communicate with peers. Currently, there are WeChat groups for SLAM, 3D vision, sensors, autonomous driving, computational photography, detection, segmentation, recognition, medical imaging, GAN, algorithm competitions (which will gradually be subdivided in the future), please scan the WeChat ID below to join the group, with the note: “Nickname + School/Company + Research Direction”, for example: “Zhang San + Shanghai Jiao Tong University + Vision SLAM”. Please follow the format for the note, otherwise, it will not be approved. Successful additions will be invited into relevant WeChat groups based on research direction. Please do not send advertisements in the group, otherwise, you will be removed from the group. Thank you for your understanding~

Practical Guide | OpenCV + OCR Circular Text Recognition

Practical Guide | OpenCV + OCR Circular Text Recognition

Leave a Comment