1

On text extraction, how to set kernel size dynamically for morphologyEx operation in cv2?Basically, I want to extract the word from image depending on various types of fonts, size. My code work for particular images only.how to find out what the right size of the kernel should be given the image contents? My code snippet below.

def text_ROI_word(thresh,output):
kernel = np.ones((2,1), np.uint8)
kernel2 = np.ones((1,4), np.uint8)
temp_img = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=2)
word_img = cv2.dilate(temp_img,kernel2,iterations=1)
(image,contours,hierarchy ) = cv2.findContours(word_img.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    cv2.rectangle(output,(x-1,y-5),(x+w,y+h),(255,0,0),1)
return output
image = cv2.imread("local path")
output_image_word=image.copy()
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret,th = 
cv2.threshold(gray_image,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
output_image_word = text_ROI_word_2(th,output_image_word)
cv2.imwrite("local path", output_image_word)

sample image

Used tesseract library,but it takes finite(minutes) time to extract word(text) from the image.

devesh
  • 618
  • 6
  • 26
  • 1
    "how to set kernel size dynamically..." Do you mean how to use a variable to define the kernel, or are you asking how to find out what the right size should be given the image contents? The first one is trivial, the second one is not. If you mean the second one, please expand on your question: what is the goal of the filter? How did you determine the current values that are OK for the given image? – Cris Luengo Feb 25 '19 at 15:47
  • Yes, depending on text font or size image how to set kernel size, will edit question – devesh Feb 25 '19 at 16:19
  • 1
    So, do you know anything in advance about the font? My guess is that you need to know the interline spacing (which you can determine by projecting the image onto a vertical line) and the interword spacing (the size of the space character). I'm not sure if standard methods exists for this, but then again I don't work with images of text a whole lot. :) – Cris Luengo Feb 25 '19 at 16:40
  • Thanks,for the reply actually its subpart of optical character recognition(OCR) I will be having only image, – devesh Feb 25 '19 at 16:50

0 Answers0