【问题标题】:Crop page from facing-page scan从对开页扫描中裁剪页面
【发布时间】:2019-12-20 17:02:48
【问题描述】:

假设我想从螺旋笔记本的对页扫描中仅裁剪左页,如下例所示(来自Paolini.net)。

有没有比简单地将图像宽度除以一半更可靠的方法?例如,更智能的算法会检测螺旋绑定并将其设为右边界,甚至排除页面左侧的黑色区域。

如果有使用 OpenCV 或 ImageMagick 的相对简单的方法,我很乐意学习。

【问题讨论】:

  • "roboust" 取决于很多事情。照明、更改书籍和装订类型、页面对齐...
  • 您希望它能够与不同颜色的螺旋装订一起使用吗?没有螺旋?用其他方式装订的书?
  • @MarkSetchell 不,我只是在处理 one 笔记本的不同页面,因此可以假设图像相似,略有不同。如果解决方案适用于一个,它应该很容易适用于所有。

标签: opencv image-processing imagemagick


【解决方案1】:

在带有 Unix 脚本的 ImageMagick 6 中,一种可能的方法是执行以下操作:

Trim the image to remove most of the black on the sides

Scale the image down to 1 row, then scale up to 50 rows just for visualization

Threshold the scaled image so that you get the black region down the spine as the largest black region

Do connected components process to find the x coordinate of the largest black region

Crop the image according to the results from the connected components


输入:

convert img.jpg -fuzz 25% -trim +repage img_trim.png


convert img_trim.png -scale x1! -scale x50! -threshold 80% img_trim_x1.png


centx=$(convert img_trim_x1.png -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:verbose=true \
-connected-components 4 null: | \
grep "gray(0)" | head -n 1 | awk '{print $3}' | cut -d, -f1)

convert img_trim.png -crop ${centx}x+0+0 img_result.jpg


来自连接组件的数据具有以下标题和结构:

Objects (id: bounding-box centroid area mean-color):

所以 head -n 1 得到第一个黑色,即最大的灰色(0)区域(从大到小排序)。 awk 打印第三个条目,centroid,并且 cut 得到 x 分量。


如果使用 ImageMagick 7,则将 convert 更改为 magick

如果要排除中间的活页夹,则使用连接组件列表中边界框的 x 偏移量:

convert img_trim.png -scale x1! -scale x50! -threshold 80% img_trim_x1.png
leftcenterx=$(convert img_trim_x1.png -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:verbose=true \
-connected-components 4 null: | \
grep "gray(0)" | head -n 1 | awk '{print $2}' | cut -d+ -f2 | cut -d+ -f1)
convert img_trim.png -crop ${leftcenterx}x+0+0 img_result2.jpg


如果你只想要两个页面,那么我们可以找到白色区域,即 gray(255) 并根据边界框的宽度和 x 偏移量裁剪它们。

convert img.jpg -fuzz 25% -trim +repage img_trim.png
convert img_trim.png -scale x1! -scale x50! -threshold 80% img_trim_x1.png
OLDIFS=$IFS
IFS=$'\n'
bboxArr=(`convert img_trim_x1.png -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=100 \
-define connected-components:verbose=true \
-connected-components 4 null: | \
grep "gray(255)" | awk '{print $2}'`)
IFS=$OLDIFS
num=${#bboxArr[*]}
for ((i=0; i<num; i++)); do
WW=`echo ${bboxArr[$i]} | cut -dx -f1`
Xoff=`echo ${bboxArr[$i]} | cut -d+ -f2`
convert img_trim.png -crop ${WW}x+${Xoff}+0 img_result3_$i.jpg
done


【讨论】:

  • 干得好!不是在电脑上,但我认为你已经在 xcentcentx 之间玩弄了。
猜你喜欢
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
  • 2013-09-13
  • 2010-10-02
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多