1

With a mobile device I take a picture of a flat light object on a dark surface. (for instance a coupon clipped out of a newspaper).

The image is then run through a brightness/contrast filter. If it is too dark, vital components are left out. If it is too bright, the writing on the coupon is lost.

This image is then converted into a bitonal image. Any pixel that is 50% or more dark is converted to black, everything else is white. (done)

I am left with a skewed bitonal image (think of a white trapezoid inside a larger rectangle with a black background).

I need to figure out how to crop the image - which when it's on a black background is easier than when it's on a white background. Then, I have to de-skew the image so it is rectangular instead of trapezoidal, while attempting to preserve aspect.

The end result should be a nicely cropped, bitonal, readable image of the coupon.

Josh
  • 16,286
  • 25
  • 113
  • 158
  • This post may help you get started. It contains a method for resizing an image in csharp without skewing the aspect ratio. http://stackoverflow.com/questions/4681084/expand-canvas-transparent-background-in-bitmapimage-in-a-wpf-app – mrtsherman Apr 13 '11 at 21:36
  • 1
    The skewing is most likely a bug in the previous steps, to get to the bitonal image. Skewing tends to happen when someone forgets to account for padding in the scanlines of a bitmap. – Chris O Apr 13 '11 at 21:54
  • Skewing (in this case) happens when the user takes a picture that is not directly overhead the piece of paper. – Josh Apr 14 '11 at 13:16

1 Answers1

1

To crop your image, you can use the LockBits method and scan through all your pixels to find the first pixel with content from the top, left, right and bottom, respectively. How to use LockBits is described nicely here: https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx

Assuming your image is not rotated, and that the skewing comes from the camera held at an angle against the table where the coupon is being photographed, you should now have a skewed image of the coupon, fitting perfectly within the bounds of the cropped bitmap. You should also know the four corners of the trapezoid.

"Undistorting" an image is not as easy as you might think though. However, good people have solved this problem and you can probably port their code to your own use. Here is a link I used to explore this problem in a similar case some time ago:

http://ryoushin.com/cmerighi/en-US/2007-10-29_61/Image_Distortion_Enhancements

I also have some code stored somewhere if you can't make any sense of what you find.

Pedery
  • 3,632
  • 1
  • 27
  • 39