How can picture of page be straightened out to look as if it was scanned?

此生再无相见时 提交于 2019-12-02 21:55:53

问题


I have seen apps, and wondered how can I programmatically take a picture of image. Define how it needs to be transformed so that it looks parallel to camera and not skewed perspective wise.

Then combine multiple photos to create a pdf file. For example this app does it: https://play.google.com/store/apps/details?id=com.appxy.tinyscan&hl=en


回答1:


I do not use books for such trivial things so sorry I can not recommend any (especially in English). What you need to do is this:

  1. input image
  2. find main contours

    ideally whole grid but even outer contour will suffice (in case no grid is present). You need to divide the contour into horizontal (Red) and vertical (Green) curves (or set of points).

  3. sample contour curves by 4 "equidistant" points

    as the image is distorted (not just rotated) then we need to use at least bi-cubic interpolation. For that we need 16 points (Aqua) per patch.

  4. add mirror points to cover whole grid

    on the image are mirrored (Yellow) points only for horizontal contours you should do this also for vertical contours (did not fit me in the image and did not want to enlarge resolution just for that) and also for the corner points so you got 6x6 control points. The mirror can be done linearly (like I did).

Now the transformation is done like this:

  1. Process all pixels dst(x0,y0) of target image
  2. Handle x,y as parameter for cubic interpolation

    if xs,ys is target image resolution then:

    u=(3.0*x)/xs
    v=(3.0*y)/ys
    

    Now cubic interpolation is usually done on parameter t=<0.0,1.0) so
    if u=<0.0,1.0> use t=u and control points 0,1,2,3.
    if u=<1.0,2.0) use t=u-1.0 and control points 1,2,3,4
    if u=<2.0,3.0> use t=u-2.0 and control points 2,3,4,5

    The same goes for vertical contours and v. Compute xi,yi as bi cubic interpolation of (u,v). And copy pixel:

    dst(x,y)=src(xi,yi);
    

    This is just nearest neighbor but you can also use bilinear for this ... As cubic curve I would use this polynomial.

    The idea behind bi-cubic interpolation is easy. compute point corresponding to parameter u on 4 horizontal contours. That will give you 4 control points for the final cubic interpolation in vertical direction and v as parameter. Resulting coordinate is your source pixel position.

For more info see:

  • How can i produce multi point linear interpolation?
  • Bicubic interpolation
  • OpenCV Birdseye view without loss of data

In case you do not have a grid use any info that can be used as one. For example lines of text can be considered a contour for this ...



来源:https://stackoverflow.com/questions/42144001/how-can-picture-of-page-be-straightened-out-to-look-as-if-it-was-scanned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!