1

I have 2 images, one is the original image which has grids in it, the other is the same image except it has digits written in those grids, and is rotated, I am supposed to find the rotation angle for that image so that I can make it straight and recognize those digits written.

For the digits I'd probably put them in 2D pixel arrays and see the patterns, but I can't really understand how to write the code to detect the rotation angle. This has been answered here Calculating translation value and rotation angle of a rotated 2D image, but I have no idea how to translate that code into C#.

Also, I can't use any libraries that do all that for me, I need to implement the algorithm from scratch. I can only use byte arrays and the Bitmap class.

Here are the picture examples:

Community
  • 1
  • 1
Pavel
  • 1
  • 3
  • 17
  • 51

1 Answers1

3

This answer is simply an idea on how you should go about doing this, and not the exact code used.

What you can do is "scan" down the page by getting the pixel values at the X and Y position. Start near the top left (actual position can be determined from your example image), and scan down starting where the first red line is in my example, until you hit a color that is close to black. Record this position and do the same for the other side. With these two points, you can calculate the angle between them, and translate your image accordingly.

http://i.imgur.com/fKqDwCC.png

To reduce false positives, after finding these positions, you can skip X number of pixels down until you find the next row, and make sure that it is the same angle or the expected distance from the first results. (Green arrows)

If you can change this paper, I would suggest adding larger lines signifying where the rows are (on each side, so an angle can be found), similar to what is seen on the left of this bubble sheet

Community
  • 1
  • 1
Cyral
  • 13,999
  • 6
  • 50
  • 90
  • That's a very good answer, but what if my image is rotated by 90 degrees? Is there any algorithm to go about that? – Pavel Mar 15 '15 at 21:46
  • Well, you could consider checking for the blank space between the rows, or the edges, or something to see if it is rotated. – Cyral Mar 15 '15 at 21:52