ale*_*oid 5 ocr imagemagick image-processing image-preprocessing
为了提高 OCR 质量,我需要对扫描的图像进行预处理。有时我需要用几张图片(页面上的组件并且它们处于不同的角度 - 例如,一次扫描一些纸质文档)对图像进行 OCR,例如:
是否可以自动以编程方式将此类图像划分为包含每个逻辑文档的单独图像?例如使用 ImageMagick 之类的工具或其他工具?是否有针对此类问题的解决方案/技术?
在 ImageMagick 6 中,您可以对图像进行足够模糊,以使文本重叠并设置阈值,以便每个文本框都是白色背景上的一个大黑色区域。然后,您可以使用连接组件来查找每个单独的黑灰色(0)区域及其边界框。然后使用边界框值裁剪每个此类区域的原始图像。
输入:
Unix 语法(将模糊调整到足够大以保持文本区域纯黑色):
infile="image.png"
inname=`convert -ping $infile -format "%t" info:`
OLDIFS=$IFS
IFS=$'\n'
arr=(`convert $infile -blur 0x5 -auto-level -threshold 99% -type bilevel +write tmp.png \
-define connected-components:verbose=true \
-connected-components 8 \
null: | tail -n +2 | sed 's/^[ ]*//'`)
num=${#arr[*]}
IFS=$OLDIFS
for ((i=0; i<num; i++)); do
#echo "${arr[$i]}"
color=`echo ${arr[$i]} | cut -d\ -f5`
bbox=`echo ${arr[$i]} | cut -d\ -f2`
echo "color=$color; bbox=$bbox"
if [ "$color" = "gray(0)" ]; then
convert $infile -crop $bbox +repage -fuzz 10% -trim +repage ${inname}_$i.png
fi
done
Run Code Online (Sandbox Code Playgroud)
文字列表:
color=gray(255); bbox=892x1008+0+0
color=gray(0); bbox=337x430+36+13
color=gray(0); bbox=430x337+266+630
color=gray(0); bbox=202x147+506+252
Run Code Online (Sandbox Code Playgroud)
tmp.png 显示模糊和阈值区域:
裁剪图像: