0

I'm using opencv to extract text from images like this. Right now my goal is in trying to delete the most of the walls/windows/doors I can while leaving the text intact. This is what I came up with:

def process_image(path, out_path):
    image = Image.open(path)

    gray_image = cv2.cvtColor(np.asarray(image), cv2.COLOR_BGR2GRAY)
    ret,th = cv2.threshold(gray_image,127,255,cv2.THRESH_OTSU)

    kernel = np.ones((4,4),np.uint8)
    edges = cv2.dilate(th,kernel,iterations = 1)
    edges = cv2.erode(edges, kernel, iterations = 1)


    edges = cv2.bitwise_and(cv2.bitwise_not(th), edges)
    ret, labels = cv2.connectedComponents(edges, connectivity = 8)

    for label in range(ret):
        if  : #if connComp is adjacent to a wall
            edges[labels == label] = 0


cv2.imwrite(out_path,edges)

I basically dilate the image so that all is left are the tickest walls, then I use this image as a filter to actually delete those walls. In the end I look for connected components and now I'd like to cycle on those and check wheter they "touch" the filter I just used (meaning a white pixel is next to a white pixel) and if they do I'd like to recolor the component black. This way I should be able to filter the image even more leaving the text untouched since it never touches a wall, problem is this is my first time using opencv and I'm not really sure how to implement the last bit of my idea. Any tip on how to do it?

  • 1
    Just to be sure, if you just want to extract the text, why don't you apply OCR detection directly? The image look pretty "clean" and OCR should work. Even if you don't want to use OCR, canny detection and filtering should give you the area of the text (but not recognized). – Rick M. Aug 01 '18 at 14:43
  • i tried tesseract but it wasn't working well with the whole image, so I'd like to first crop it and then use tesseract – Alessandro Aug 01 '18 at 15:26
  • Hmm, ok. Since your text is always black in color, why don't you just get only black pixels in your image? Sorry, I am not trying to solve this question but basically trying to figure why you'd design such filtering when the task can be trivial. – Rick M. Aug 01 '18 at 15:34
  • What do you mean? Not only the text is formed by black pixels – Alessandro Aug 01 '18 at 15:41
  • Yes indeed but then you could get a "pseudo-cropped" version of the image. I suggested this to make your OCR easier – Rick M. Aug 16 '18 at 12:48

0 Answers0