-1

I need to warp an image of an image in Python using my Raspberry Pi 4. Well, unwarp I should say. I'm writing a script to solve CAPTCHA codes to automate a task. All of the CAPTCHA codes are made of 4 letters and numbers in the same font, and are all warped the same way, in the sape of a giant sideways "S". What I'm needing to do is unwarp the image to make it more flat for the captcha to read it. Any suggestions on how to go about this? I apologize, I'm completely stumped and have no idea how to even begin with this task. I've tried installing OpenCV but no matter what I try I cant get it to work on my Raspberry Pi 4 (running Raspbian).

*side note, to test and make sure the warp theory would work, I took a few of the captcha codes to photoshop and warped them in the same pattern, and it flattened them out enough my Pytesseract script was able to read it just fine. Also, will upload pictures of several of the captcha codes.

Thanks in advance for any help!

Michael
  • 7
  • 3

1 Answers1

1

You can use cv2.remap to do this kind of thing, eg

import numpy as np
import cv2

def remap_test(img_arr):
    H,W = img_arr.shape[0:2]
    map = np.mgrid[0:H,0:W]
    # start off with a 'do-nothing' or identity xform, add to that as desired
    map_x = map[1].astype(np.float32)
    map_y = map[0].astype(np.float32)
    # add a sine-wave offset to the x-values
    for i in range(H):
        map_x[i,:]=map_x[i,:]+30*np.sin(2*3.14*float(i)/H)
    img_warped = cv2.remap(img_arr,map_x,map_y,cv2.INTER_LINEAR)

Some sample results here where I used a sine as above to make an approximate S. You can replace that sine with the exact curve you are looking for, eg if you trace the offsets from a straight line to the corresponding points on your S.

jeremy_rutman
  • 3,552
  • 4
  • 28
  • 47
  • Thank you so much! I'll keep trying to get cv2 on my Raspberry Pi4. As soon as I do I'll try this!! It looks very promising!! – Michael Dec 24 '19 at 00:53