有谁懂得畸形图片校正的算法吗?
如图:把任意四边形校正成一个矩形,有没有透视变换公式?本帖最后由 vigiles 于 2024-12-6 14:58 编辑
https://pic1.58cdn.com.cn/nowater/webim/big/n_v2e733f98e6b90419497938b74b4b2a7e0.jpg
https://pic1.58cdn.com.cn/nowater/webim/big/n_v2e733f98e6b90419497938b74b4b2a7e0.jpg
====
请原谅,理解错了:face (9):
原理在这里:
https://blog.csdn.net/weixin_44322778/article/details/129996366
https://zhuanlan.zhihu.com/p/445907317
要求出转换矩阵,要求8阶逆阵、8阶行列式、伴随矩阵,太复杂了! ai实现
#include <GDIPlus.au3>
#include <Array.au3>
#include <Math.au3>
; 初始化 GDI+
_GDIPlus_Startup()
Func PerspectiveTransform($srcImage, $srcPoints, $dstPoints)
; 计算透视变换矩阵
Local $matrix = _GetPerspectiveMatrix($srcPoints, $dstPoints)
If Not IsArray($matrix) Then Return False
; 获取原图尺寸
Local $width = _GDIPlus_ImageGetWidth($srcImage)
Local $height = _GDIPlus_ImageGetHeight($srcImage)
; 创建目标图像
Local $dstImage = _GDIPlus_BitmapCreateFromScan0($width, $height)
Local $graphics = _GDIPlus_ImageGetGraphicsContext($dstImage)
; 应用变换
Local $points
For $y = 0 To $height - 1
For $x = 0 To $width - 1
; 计算源坐标
Local $srcX = ($matrix * $x + $matrix * $y + $matrix) / _
($matrix * $x + $matrix * $y + $matrix)
Local $srcY = ($matrix * $x + $matrix * $y + $matrix) / _
($matrix * $x + $matrix * $y + $matrix)
; 检查坐标是否在范围内
If $srcX >= 0 And $srcX < $width And $srcY >= 0 And $srcY < $height Then
; 获取源像素颜色并设置目标像素
Local $color = _GDIPlus_BitmapGetPixel($srcImage, $srcX, $srcY)
_GDIPlus_BitmapSetPixel($dstImage, $x, $y, $color)
EndIf
Next
Next
Return $dstImage
EndFunc
Func _GetPerspectiveMatrix($src, $dst)
; 构建方程组系数矩阵
Local $A, $B
For $i = 0 To 3
$A[$i * 2] = $src[$i]
$A[$i * 2] = $src[$i]
$A[$i * 2] = 1
$A[$i * 2] = 0
$A[$i * 2] = 0
$A[$i * 2] = 0
$A[$i * 2] = -$src[$i] * $dst[$i]
$A[$i * 2] = -$src[$i] * $dst[$i]
$B[$i * 2] = $dst[$i]
$A[$i * 2 + 1] = 0
$A[$i * 2 + 1] = 0
$A[$i * 2 + 1] = 0
$A[$i * 2 + 1] = $src[$i]
$A[$i * 2 + 1] = $src[$i]
$A[$i * 2 + 1] = 1
$A[$i * 2 + 1] = -$src[$i] * $dst[$i]
$A[$i * 2 + 1] = -$src[$i] * $dst[$i]
$B[$i * 2 + 1] = $dst[$i]
Next
; 解线性方程组
Local $h = _SolveLinearSystem($A, $B)
If Not IsArray($h) Then Return False
; 构建透视变换矩阵
Local $matrix
$matrix = $h
$matrix = $h
$matrix = $h
$matrix = $h
$matrix = $h
$matrix = $h
$matrix = $h
$matrix = $h
$matrix = 1
Return $matrix
EndFunc
; 使用示例
Func Main()
; 加载源图像
Local $srcImage = _GDIPlus_ImageLoadFromFile("source.jpg")
; 定义源图像中的四个点(畸变图像的四个角点)
Local $srcPoints = [, , , ]
; 定义目标图像中的四个点(矩形的四个角点)
Local $width = 400
Local $height = 300
Local $dstPoints = [, [$width, 0], , [$width, $height]]
; 执行透视变换
Local $dstImage = PerspectiveTransform($srcImage, $srcPoints, $dstPoints)
; 保存结果
_GDIPlus_ImageSaveToFile($dstImage, "result.jpg")
; 清理资源
_GDIPlus_ImageDispose($srcImage)
_GDIPlus_ImageDispose($dstImage)
_GDIPlus_Shutdown()
EndFunc
Main() 虫子樱桃 发表于 2024-12-11 18:14
ai实现
有些函数好像没有的:
ERROR: _GDIPlus_BitmapCreateFromScan0(): undefined function.
Local $dstImage = _GDIPlus_BitmapCreateFromScan0($width, $height)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
ERROR: _GDIPlus_BitmapGetPixel(): undefined function.
Local $color = _GDIPlus_BitmapGetPixel($srcImage, $srcX, $srcY)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
ERROR: _GDIPlus_BitmapSetPixel(): undefined function.
_GDIPlus_BitmapSetPixel($dstImage, $x, $y, $color)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
ERROR: _SolveLinearSystem(): undefined function.
Local $h = _SolveLinearSystem($A, $B) 转身离开 分手说不出来 海鸟跟鱼相爱 只是一场意外 我们的爱差异一直存在 风中尘埃竟累积成伤 auto896006008 发表于 2024-12-13 11:12
转身离开 分手说不出来 海鸟跟鱼相爱 只是一场意外 我们的爱差异一直存在 风中尘埃竟累积成伤 ...
别灌水。。。还拿着我的个性签名灌水。。。
虫子樱桃 发表于 2024-12-11 18:14
ai实现
太好了,感谢大神,学习一下
页:
[1]