我有一个问题,如下所示,上面的是原始图像,下面的被向左旋转了90度。有没有一种方法来计算角度的变化?我已经尝试过Hough变换,可以检测到位置,但无法检测到角度的变化。是否有其他方法来检测角度的变化?
您还可以通过恒定角度循环旋转第二张图像,并在每次迭代中计算两个图像之间的RMSE。匹配角度将是RMSE最小值的位置。
这里是一个powershell脚本,它使用ImageMagick实现了这个想法:
# convert images to equal sizes for pixel by pixel comparision
convert.exe p1.png -resize 73x73! p1.png
convert.exe p2.png -resize 73x73! p2.png
# initialize variables
$min_rmse = 1.0
$degrees_rotated = -1.0
# rotate second image by 10 degrees in each iteration
# and after that measure RMSE between first image and second rotated image
for ($i=0; $i -le 350; $i+=10) {
convert.exe p2.png -rotate $i tmp.png
convert.exe tmp.png -resize 73x73! tmp.png
$rmse = compare.exe -metric rmse p1.png tmp.png diff.png 2>&1
$rmse = ([string] $rmse).split(" ")[1]
$rmse = $rmse.replace("(","")
$rmse = [double] $rmse.replace(")","")
# find rotation angle where RMSE is minimal
if ($rmse -lt $min_rmse) {
$min_rmse = $rmse
$degrees_rotated = $i
}
}
Write-Host "two images are most similar when second image is rotated by $degrees_rotated deg (rmse is $min_rmse)"
Write-Host "Press any key to exit ..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
希望这能有所帮助。
附言:
根据维基百科,两个向量之间的均方根误差(RMSE)可以用两种不同的方式计算。我不知道ImageMagick中实现了哪一种方法作为< pre>compare.exe -metric rmse命令,但可以通过询问IM开发人员或通过试错法确定。