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)
Used tesseract library,but it takes finite(minutes) time to extract word(text) from the image.