How to detect a blank column in a page using ImageMagick to distinguish two column papers

两盒软妹~` 提交于 2020-07-09 23:58:27

问题


I try to distinguish two-column pdf scientific papers using ImageMagick and locate the point where the two columns are separated using a bash script.

My idea is to use a sliding window at the center of the page to find a window which is completely white. Something like:

  for i in {0..$page_width..$step} do 
     # get the color of a box 100x100+"$i"+50%
     # if the box is completely blank or 90% blank, then return ($i + 50)
  done

However, I don't know the code to check a box in the picture is completely blank.

Sample image:


回答1:


One way to do this is to calculate a "projection" (i.e. mean) of all the pixels down the vertical columns by resizing the image to just 1pixel tall. I have inverted/negated the image so you can see the colours better and stretched the 1pixel tall image to 20pixels tall so you can see it at the bottom:

magick paper.png -alpha off -negate -scale x1\! -auto-level -scale x20\! result.png

So, if you take that a little further and make the output a single row of 100 pixels and look at it as text, you might hope for some particularly low values around the 50% position at the centre of the page:

magick paper.png -alpha off -colorspace gray -negate -scale x1\! -scale 100x\! -auto-level txt:

Sample Output

# ImageMagick pixel enumeration: 100,1,65535,gray
0,0: (0)  #000000  gray(0)
1,0: (0)  #000000  gray(0)
2,0: (0)  #000000  gray(0)
3,0: (56.1923)  #383838  gray(22.0362%)
4,0: (209.168)  #D1D1D1  gray(82.0267%)
5,0: (187.374)  #BBBBBB  gray(73.4801%)
6,0: (180.066)  #B4B4B4  gray(70.6143%)
7,0: (188.581)  #BDBDBD  gray(73.9534%)
8,0: (197.288)  #C5C5C5  gray(77.368%)
9,0: (230.555)  #E7E7E7  gray(90.4138%)
...
...
45,0: (221.296)  #DDDDDD  gray(86.7826%)
46,0: (234.086)  #EAEAEA  gray(91.7985%)
47,0: (239.522)  #F0F0F0  gray(93.93%)
48,0: (191.323)  #BFBFBF  gray(75.0288%)
49,0: (44.6977)  #2D2D2D  gray(17.5285%)    <--- HERE IS THE CENTRAL GUTTER
50,0: (27.1557)  #1B1B1B  gray(10.6493%)    <--- AND HERE - AROUND 50% ACROSS
51,0: (145.663)  #929292  gray(57.1227%)
52,0: (154.567)  #9B9B9B  gray(60.6144%)
53,0: (184.97)  #B9B9B9  gray(72.5372%)
...
...
91,0: (156.178)  #9C9C9C  gray(61.2462%)
92,0: (151.242)  #979797  gray(59.3107%)
93,0: (146.532)  #939393  gray(57.4635%)
94,0: (149.511)  #969696  gray(58.6319%)
95,0: (185.506)  #BABABA  gray(72.7475%)
96,0: (21.7851)  #161616  gray(8.54316%)
97,0: (0)  #000000  gray(0)
98,0: (0)  #000000  gray(0)
99,0: (0)  #000000  gray(0)

Other ideas:

  • You could let ImageMagick do the thresholding for you with something like this and look around line 572:

    magick paper.png -alpha off -colorspace gray -negate -scale x1\! -threshold 2% txt:

  • You could shave 10% off the left and right edges first, to get rid of edge-effects:

    magick paper.png -shave 10%x ... result.png



来源:https://stackoverflow.com/questions/61059833/how-to-detect-a-blank-column-in-a-page-using-imagemagick-to-distinguish-two-colu

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