1

I have a lot of images like the ones in this gif: enter image description here

I want to crop them to remove the uneven border and leave only the writings inside of the border, for example: enter image description here

Some of the source images are a bit rotated and there is a slight difference in the border location so I need something to dynamically find the rectangle surrounding the writings.

What is the best way of doing that?

Raj
  • 1,113
  • 1
  • 17
  • 34
expGuy
  • 97
  • 1
  • 10
  • Is the greenish border always the same colour and style? – Mark Setchell Mar 25 '16 at 14:18
  • Yes all the images are like the one in the gif, and they all have the greenish border – expGuy Mar 25 '16 at 14:21
  • _[OpenCv](http://opencv.org/)_ if you are looking for a programmatic way of doing it, and if you are comfortable with C/C++. There is much written in examples. Here is a _[brief conversation](http://stackoverflow.com/a/12706208/645128)_ on ROI for example. Here is another on _[background elimination](http://docs.opencv.org/3.1.0/d1/dc5/tutorial_background_subtraction.html#gsc.tab=0)_. – ryyker Mar 25 '16 at 14:26
  • you dont know the first thing about image processing?? and you want to go to opencv?? good luck with that. Your best approach is to find some guy who can do it for you – gpasch Mar 25 '16 at 16:21

3 Answers3

1

Not a complete, or rigorous solution, but an answer that may help you work it out. I choose to use ImageMagick which is installed on most Linux distros and is available for OSX and Windows - for free. There are Java bindings (that I don't use or know) called im4java at Sourceforge. You can also do it in PHP, Perl, C++, C...

Anyway, at the command line, you can convert the first frame of your manuscript ([0]) into Hue/Saturation/Lightness colorspace and discard the Saturation and Lightness so you are just left with the Hue, like this:

convert manuscript.gif[0] -colorspace hsl -separate -delete 1,2 -blur 0x3 -contrast-stretch 10% -threshold 50% hue.png

I then contrast stretch that and save it as hue.png. It looks like this:

enter image description here

I can then take that image (hue.png) and apply a "Connected Components Analysis" (also known as "Blob Analysis") on it, and discard all the small blobs - a blob is just a contiguous area.

convert hue.png                                      \
   -define connected-components:verbose=true         \
   -define connected-components:area-threshold=10000 \     
   -connected-components 8 -auto-level output.png

Objects (id: bounding-box centroid area mean-color):
  6: 645x1033+50+78 372.4,594.0 663108 gray(0)
  0: 851x1231+0+0 595.5,678.2 244279 gray(0)
  2: 728x1123+9+36 376.7,604.3 140194 gray(255)

If I draw in the first, black box, in red like this:

convert manuscript.gif[0] -fill none -stroke red -strokewidth 5 -draw "rectangle 50,78 695,1100" result.png

I'll get this:

enter image description here

Or, I could crop it like this:

convert manuscript.gif[0] -crop 645x1033+50+78 result.png

and get this:

enter image description here

You may have to fiddle around a bit with the numbers and values but the general idea should be fairly applicable to your problem.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thank you for the detailed answer. I have played around with the parameters but still can't get it to work 100%. I have resigned and hired a freelancer but he is also having some difficulty =/ – expGuy Apr 07 '16 at 15:56
0

I am certain there is no such thing as the best way to do anything.

You can reduce the image to only the color range of the frame (Hue).

Then find the box using Hough transform, or a corner detector or whatever. You could also apply template matching to find the corners of the box if they always look the same.

This problem is way to broad to give you a complete solution here. Why don't you split it up into small problems.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • the problem is that i have 0 experience in image processing so i didnt know where to start. i might go ahead with opencv (even though i hate c++ and love java :( ) – expGuy Mar 25 '16 at 15:01
  • @expGuy OpenCV supports Java. Just google for it. You will find tutorials on how to get startet. But make sure you get the basics. Otherwise image processing can be a pain in the arse. You could give the program ImageJ a try. And I can recommend books by Burger and Burge about Java image processing – Piglet Mar 25 '16 at 15:10
-1

Opencv's contour detection and approximation will solve this problem. I did the following, convert to grey scale threshold using otsu Detect contours sort contours Approximate for a rectangle. Walk from the middle of the image to extract the first largest rectangle

enter image description here Image is squeezed as i took a screenshot.

Raj
  • 1,113
  • 1
  • 17
  • 34