Ask Your Question

Revision history [back]

detect text in image and put an image on it

hello i'm beginner in opencv development i wanna get the emplacement of a text like "Helloworld" on a lot of images and get the position to put an image on every text on image without fault.

this is what i made for now with threshold 0.5 but sometimes it's mistake and give me just one emplacement of the code.

def get_box(input_img, input_template, threshold):
img_slicer = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
ccnorm = cv2.matchTemplate(img_slicer, input_template, cv2.TM_CCOEFF_NORMED)
loc = np.where(ccnorm == ccnorm.max())
tw, th = template.shape[::-1]
if ccnorm[zip(*loc[::-1])[0][::-1]] < threshold:
     return None, [0,0]
else:
    (_, maxVal, _, maxLoc) = cv2.minMaxLoc(ccnorm)
    (startX, startY) = (2 * int(maxLoc[0]), 2 * int(maxLoc[1]))
    (endX, endY) = (2 * int((maxLoc[0] +  tw) ), 2 * int((maxLoc[1] +  th)))
    sub_face = input_img[startY:endY, startX:endX]
    return cv2.GaussianBlur(sub_face,(23, 23), 30), [startY, startX]
click to hide/show revision 2
retagged

updated 2016-11-25 17:11:22 -0600

berak gravatar image

detect text in image and put an image on it

hello i'm beginner in opencv development i wanna get the emplacement of a text like "Helloworld" on a lot of images and get the position to put an image on every text on image without fault.

this is what i made for now with threshold 0.5 but sometimes it's mistake and give me just one emplacement of the code.

def get_box(input_img, input_template, threshold):
img_slicer = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
ccnorm = cv2.matchTemplate(img_slicer, input_template, cv2.TM_CCOEFF_NORMED)
loc = np.where(ccnorm == ccnorm.max())
tw, th = template.shape[::-1]
if ccnorm[zip(*loc[::-1])[0][::-1]] < threshold:
     return None, [0,0]
else:
    (_, maxVal, _, maxLoc) = cv2.minMaxLoc(ccnorm)
    (startX, startY) = (2 * int(maxLoc[0]), 2 * int(maxLoc[1]))
    (endX, endY) = (2 * int((maxLoc[0] +  tw) ), 2 * int((maxLoc[1] +  th)))
    sub_face = input_img[startY:endY, startX:endX]
    return cv2.GaussianBlur(sub_face,(23, 23), 30), [startY, startX]