Using the observation that the desired text is in white and that words are structured in a horizontal alignment, we can use color segmentation to extract and OCR the letters. Since no one has posted a complete solution, here’s an approach. ''' # write original image with added contours to disk
S = file_name + '/crop_' + str(index) + '.jpg' #you can crop image and send to OCR, false detected will return no text :)Ĭropped = img_final # Don't plot small false positives that aren't text if w < 35 and h < 35:Ĭontinue # draw rectangle around contour on original imageĬv2.rectangle(img, (x, y), (x + w, y + h), ( 255, 0, 255), 2) _, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # findContours returns 3 variables for getting contours # for cv3.x.x comment above line and uncomment line below #image, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) for contour in contours: Kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, ( 3,ģ)) # to manipulate the orientation of dilution, large x means horizonatally dilating more, large y means vertically dilating moreĭilated = cv2.dilate(new_img, kernel, iterations= 9) # dilate, more the iteration more the dilation # for cv2.x.x
Ret, new_img = cv2.threshold(image_final, 180, 255, cv2.THRESH_BINARY) # for black text, cv.THRESH_BINARY_INV ''' Image_final = cv2.bitwise_and(img2gray, img2gray, mask=mask) Ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY) For more details for writing text on images using opencv, view official opencv documentation.Img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) We can increase font scale and thickness to increase font size and its area according to scale. So, this way we can use multiple font face for drawing on images using opencv. # iterate and draw text using all font face. Now we can also iterate over each of font face and view how it looks like for same text. Image = cv2.putText(image, "This text string is in center", (400, 300), font, font_scale, color, thickness) Image = cv2.putText(image, "This is some text", (80, 80), font, font_scale, color, thickness) Now we can write some text on image using opencv. Font Face provided by opencv are as follows.