1

In my project I deal with images which I don't know if they are inclined or not.

I work with C++ and OpenCV. I try with Hough transformation to determine the angle of inclination: if it is 90 or 180. But it doesn't give a result.

A link to example image (full resolution TIFF) here.

The following illustration is the full-res image scaled down and converted to PNG:

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Do you mean 'rotated'? My guess is the angle on the image you provide is not 90 degrees. – Lubo Antonov Feb 16 '15 at 10:00
  • Does this provide any insight? http://stackoverflow.com/questions/1141928/algorithm-to-detect-photo-orientation – rayryeng Feb 16 '15 at 10:02
  • Stick to Hough Transform and try to fix it imo. Maybe this gives you some ideas: http://stackoverflow.com/questions/28525218/skew-angle-detection-on-a-image-with-scattered-characters/28525570 – runDOSrun Feb 16 '15 at 12:01
  • Surely you can tell if your image is rotated +/-90 by comparing the width and height - can't you? Then you only need to detect if it is rotated 180 or not... please say if that is correct, then we can work on that problem. – Mark Setchell Feb 16 '15 at 12:40
  • comparing the width and the height it is a solution but there is some images that with width bigger than height but they are not inclined – jihed azaza Feb 16 '15 at 13:34
  • I still do not understand your question. Are you trying to detect a rotation of +/-90 or +/-180 or a rotation of around 5-10degrees? Will the answer to your question be a) +/-90 or +/-180 or b) +/-5 to +/-10? – Mark Setchell Feb 16 '15 at 14:12
  • a +/- 90 and +/-180 degrees – jihed azaza Feb 16 '15 at 14:24

2 Answers2

3

If I want to attack your image with the Hough lines method, I would do a Canny edge detection first, then find the Hough lines and then look at the generated lines. So it would look like this in ImageMagick - you can transform to OpenCV:

convert input.jpg \
   \( +clone -canny x10+10%+30%    \
      -background none -fill red   \
      -stroke red -strokewidth 2   \
      -hough-lines 9x9+150         \
      -write lines.mvg             \
   \)                              \
   -composite hough.png

enter image description here

And in the lines.mvg file, I can see the individual detected lines:

# Hough line transform: 9x9+150
viewbox 0 0 349 500
line 0,-3.74454 349,8.44281  # 160
line 0,55.2914 349,67.4788  # 206
line 1,0 1,500  # 193
line 0,71.3012 349,83.4885  # 169
line 0,125.334 349,137.521  # 202
line 0,142.344 349,154.532  # 156
line 0,152.351 349,164.538  # 155
line 0,205.383 349,217.57  # 162
line 0,239.453 349,245.545  # 172
line 0,252.455 349,258.547  # 152
line 0,293.461 349,299.553  # 163
line 0,314.464 349,320.556  # 169
line 0,335.468 349,341.559  # 189
line 0,351.47 349,357.562  # 196
line 0,404.478 349,410.57  # 209
line 349.39,0 340.662,500  # 187
line 0,441.484 349,447.576  # 198
line 0,446.484 349,452.576  # 165
line 0,455.486 349,461.578  # 174
line 0,475.489 349,481.581  # 193
line 0,498.5 349,498.5  # 161

I resized your image to 349 pixels wide (to make it fit on Stack Overflow and process faster), so you can see there are lots of lines that start at 0 on the left side of the image and end at 349 on the right side which tells you they go across the image, not up and down it. Also, you can see that the right end of the lines is generally 16 pixels lower than the left, so the image is rotated tan inverse (16/349) degrees.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Please, add support here: http://area51.stackexchange.com/proposals/66531/computer-vision/ Give votes to questions... – Royi Feb 19 '15 at 08:17
2

Here is a fairly simple approach that may help you get started, or give you ideas that you can adapt. I use ImageMagick, but the concepts and techniques should be readily applicable in OpenCV.

First, I note that the image is rotated a few degrees and that gives the black triangle at top right, so the first thing I would consider is cropping the middle out of the image - i.e. removing around 10-15% off each side.

The next thing I note is that, the image is poorly scanned with lots of noisy, muddy grey areas. I would tend to want to blur these together so that they become a bit more uniform and can be thresholded.

So, if I want to do those two things in ImageMagick, I would do this:

convert input.tif                     \
    -gravity center -crop 75x75%+0+0  \
    -blur x10 -threshold 50%          \
    -negate                           \
    stage1.jpg

enter image description here

Now, I can count the number of horizontal black lines that run the full width of the image (without crossing anything white). I do this by squidging the image till it is just a single pixel wide (but still the full original height) and counting the number of black rows:

convert stage1.jpg -resize 1x! -threshold 1 txt: | grep -c black
1368

And I do the same for vertical black lines that run the full height of the image from top to bottom, uninterrupted by white. I do that by squidging the image till it is a single pixel tall and the full original width:

convert stage1.jpg -resize x1! -threshold 1 txt: | grep -c black
0

Therefore there are 1,368 lines across the image and none up and down it, so I can say the dark lines in the original image tend to run left-right across the image rather than top-bottom up and down the image.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432