我想合并两张图片,但我不知道如何通过任何 Linux/Unix 工具找到 y 轴的剪切位置,但是这里图像魔术师作为第一个想法。欢迎任何方法:编程解决方案或通过手动帮助解决问题的方法。两张图片在 y 轴上具有联合相似性,我想将其最小化,然后合并以输出。伪代码
- 找到两张图片中数据第一次相等的 y 轴位置。
- 只要图片之间不相等,就最小化两张图片的 y 轴相等性。
- 将每张图片剪切到适当的位置。按百分比排名靠前这里,
convert -gravity SouthWest -crop 100x70%x+0+0 infile.jpg outfile.jpg
.对于底部类似,convert -gravity NorthWest -crop 100x70%x+0+0 infile.jpg outfile.jpg
. - 合并两个图像的正确部分等(这里) 经过
convert -append A-edit.jpg B-edit.jpg output.png
图 1 图像 A、图 2 图像 B、图 3 预期输出
答案1
Hugin 将为您缝合这些图像。
里面有一个脚本可用拼接扫描图像的教程被称为运行扫描pto_var.sh这将完全满足您的需要。
在我的 Debian 系统上,我需要安装两个软件包(当然还有它们的依赖项):
apt-get install hugin hugin-tools
为了问题的完整性,我在这里包含了一个稍微修改的版本(此版本接受命令行上的图像文件名,而不是硬编码):
#! /bin/sh
# hugin command tools script to stitch scanned images, fov unknown
# use of fov >= 10 should be OK, could simply set FOV=10
# Terry Duell 2013, 2014
# usage...run-scan-pto_var.sh outputprefix fov
#get the output file prefix
Prefix=$1
# get the fov
FOV=$2
shift 2
pto_gen --projection=0 --fov=$FOV -o project.pto "$@"
pto_lensstack -o project1.pto --new-lens i1 project.pto
cpfind -o project1.pto --multirow project1.pto
cpclean -o project2.pto project1.pto
linefind -o project3.pto project2.pto
pto_var -o setoptim.pto --opt r,d,e,!r0,!d0,!e0 project3.pto
autooptimiser -n -o autoptim.pto setoptim.pto
pano_modify --projection=0 --fov=AUTO --center --canvas=AUTO --crop=AUTO -o autoptim2.pto autoptim.pto
pto2mk -o project.mk -p $Prefix autoptim2.pto
make -j 2 -f project.mk all
# Clean up afterwards
rm -f project.pto project1.pto project2.pto project2.pto project.mk
rm -f "$Prefix"[0-9][0-9][0-9][0-9].tif
rm -f autoptim.pto autoptim2.pto autoptim2.pto_rsp.arg
rm -f setoptim.pto
如果您的图像被调用wIowW.jpg
-orMDp.jpg
正如您的图像的命名一样 - 并且您想要结果,rsp.tif
您可以像这样运行脚本:
./run-scan-pto_var.sh rsp 10 *.jpg
输出始终写入 TIFF 文件。然而,这种格式几乎可以转换为任何其他图像格式。
结果?
答案2
编辑和更新的脚本:
该解决方案基于原始图像。它将合并任意高度的两个图像,但两个输入图像的宽度必须匹配。
您可以像这样调用该脚本:
sh ./name-of-script topImg.jpg bottomImg.jpg
检查两个输入图像是否有匹配的交叉部分。如果未找到匹配项,脚本将退出。
只需要 ImageMagick 即可实现此功能。
#!/bin/sh
#
# Assume (based on the test images) that the input images are "part of" a
# single image. If not, this will not work :]
#
# Compare threshold - a value from 0 (100% match) to 100 (nothing alike).
# A low value is a good idea
THRESHOLD=5
# We assume at least 10 pixels overlap
PX=10
# Keep track of the best overlap height
PX_BEST=0
# The overlap diff must be as close to 0 as possible
PX_DIFF=100
# And keep track of the best result so far.
PX_DIFF_BEST=100
# The list of temp files created.
fileList="check1.png check2.png crop2.png"
usage () {
echo "
Usages:
${0##*/} imgTop imgBot
imgTop: The 'top' image. Height of imgTop does not matter but the
width must match the width of the second image.
imgBot: The 'bottom' image. Same rules as 'imgTop'.
Output will be single image with a file name like:
imgTop-imgBot-widthxheight.jpg
"
exit
}
# Check a PX overlap value for a match
checkOverlap () {
# Cut the overlap amount from the bottom of the top image.
convert -gravity SouthWest -crop ${W1}x${PX}+0+0 "${1}" +repage check1.png
# And the top of the bottom image
convert -gravity NorthWest -crop ${W2}x${PX}+0+0 "${2}" +repage check2.png
# Compare the two overlap sections
PX_DIFF=`convert check1.png check2.png -compose Difference -composite -colorspace gray -format '%[fx:mean*100]' info:`
}
# We have a PX_BEST which is the size of the overlap, so crop
# this from top and bottom, then join
cropAndJoin () {
# Crop the overlap from the bottom image
H2Crop=$(($H2 - $PX_BEST))
convert -gravity SouthWest -crop ${W2}x${H2Crop}+0+0 "${2}" +repage crop2.png
# Join the original top with the cropped bottom
convert -append "${1}" crop2.png merged.png
echo ""
}
# clean up the temp images and rename the result.
cleanUp () {
# Get the name of the orginal images
tmp1=${1##*/}
tmp2=${2##*/}
# remove the file extensions
tmp1=${tmp1%\.*}
tmp2=${tmp2%\.*}
# get the file path for the first image (assume second image is here too)
tmpPath=${1%/*}
if [ "$tmpPath" = "$1" ]
then
tmpPath="."
fi
# the name of the new, merged image
H=`identify -format '%h' "merged.png"`
merged="${tmpPath}/${tmp1}-${tmp2}-${W1}x${H}.png"
# Remove the temp files
for x in $fileList
do
if [ -f "${x}" ]
then
rm -f "${x}"
fi
done
if [ -f "merged.png" ]
then
mv -f "merged.png" "${merged}"
echo "
Merged file can be found here:
${merged}
"
else
echo "
Something went wrong. Maybe the images where not a match?
"
fi
}
# Basic checks
if [ "$2" = "" ]
then
usage
fi
if [ ! -f "$1" -o ! -f "$2" ]
then
usage
fi
# The width and height of the images
W1=`identify -format '%w' "${1}"`
W2=`identify -format '%w' "${2}"`
# The width of the two images must match
if [ $W1 -ne $W2 ]
then
echo "
Images are not the same width.
"
exit
fi
# Need to know the height of the images too.
H1=`identify -format '%h' "${1}"`
H2=`identify -format '%h' "${2}"`
# Max height is need to make sure the loop does not go on forever
MAX=$H1
if [ $H1 -gt $H2 ]
then
MAX=$H2
fi
echo -n "
Looking for a match point "
while [ $PX -lt $MAX ]
do
echo -n "."
checkOverlap "${1}" "${2}"
if [ "$PX_DIFF" != "0" ]
then
PX_DIFF=${PX_DIFF%%.*}
fi
if [ $PX_DIFF -lt $PX_DIFF_BEST ]
then
PX_DIFF_BEST=$PX_DIFF
PX_BEST=$PX
fi
# Check for a perfect match
if [ $PX_DIFF -eq 0 ]
then
PX=$MAX
else
PX=$(($PX + 1))
fi
done
# Check we have a good match, if not tell them.
if [ $PX_DIFF_BEST -gt $THRESHOLD ]
then
echo "
Unable to find a good match point between the two images.
No merge performed.
"
exit
fi
# We can assume a good match, try to merge the images.
cropAndJoin "${1}" "${2}"
cleanUp "${1}" "${2}"
echo "Done."
exit