ytm
ytm

Reputation: 503

Aligning an Image for OCR

I am trying to align an image as a preprocessing step for OCR. The problem is that I wish to align regions in images that contain text without using a template of an aligned image.

My idea was to first extract the borders of the ROI:

enter image description here

Then, draw some rectangle with fitting proportions(haven't thought on a way to do it automaticlly yet), and try to estimate the geometric transformation between them by using imregcorr:

enter image description here

After that, apply the obtained transformation on the image itself:

enter image description here

As you can see, the final result is far from perfect.

So I wish to know if there is a better way to obtain the coordinates change between the border and the rectangle and apply it on the image.

There's also the possiblity that my approach is too naive, and the solution to this is completely different, so feel free to suggest other methods as you see fit.

Thanks.

Upvotes: 2

Views: 1181

Answers (1)

ytm
ytm

Reputation: 503

Thanks dervish!

I don't really know much about openCV, as I still have to learn C++/python (soon I will, probably). But I did manage to implement something similar in matlab, using a projective transformation:

m = [mx my]; % Red border corners
s=[sx sy]; % Fixed rectangle borders
BW = poly2mask(mx,my,168,290); 
mask = uint8(repmat(BW,[1 1 3]));
TFORM = cp2tform(m,s, 'projective');
Iw = imtransform(I.*mask,TFORM);
[y,x] = find(im2bw(Iw,0));
imshow(Iw(min(y):max(y),min(x):max(x),:))

And here's the result:

enter image description here

Upvotes: 2

Related Questions