| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class PerspectiveTransform |
| 4 |
* |
| 5 |
* @created 17.01.2021 |
| 6 |
* @author ZXing Authors |
| 7 |
* @author Smiley <[email protected]> |
| 8 |
* @copyright 2021 Smiley |
| 9 |
* @license Apache-2.0 |
| 10 |
*/ |
| 11 |
declare(strict_types=1); |
| 12 |
|
| 13 |
namespace chillerlan\QRCode\Detector; |
| 14 |
|
| 15 |
use function count; |
| 16 |
|
| 17 |
/** |
| 18 |
* This class implements a perspective transform in two dimensions. Given four source and four |
| 19 |
* destination points, it will compute the transformation implied between them. The code is based |
| 20 |
* directly upon section 3.4.2 of George Wolberg's "Digital Image Warping"; see pages 54-56. |
| 21 |
* |
| 22 |
* @author Sean Owen |
| 23 |
*/ |
| 24 |
final class PerspectiveTransform{ |
| 25 |
|
| 26 |
private float $a11; |
| 27 |
private float $a12; |
| 28 |
private float $a13; |
| 29 |
private float $a21; |
| 30 |
private float $a22; |
| 31 |
private float $a23; |
| 32 |
private float $a31; |
| 33 |
private float $a32; |
| 34 |
private float $a33; |
| 35 |
|
| 36 |
private function set( |
| 37 |
float $a11, float $a21, float $a31, |
| 38 |
float $a12, float $a22, float $a32, |
| 39 |
float $a13, float $a23, float $a33, |
| 40 |
):self{ |
| 41 |
$this->a11 = $a11; |
| 42 |
$this->a12 = $a12; |
| 43 |
$this->a13 = $a13; |
| 44 |
$this->a21 = $a21; |
| 45 |
$this->a22 = $a22; |
| 46 |
$this->a23 = $a23; |
| 47 |
$this->a31 = $a31; |
| 48 |
$this->a32 = $a32; |
| 49 |
$this->a33 = $a33; |
| 50 |
|
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
public function quadrilateralToQuadrilateral( |
| 55 |
float $x0, float $y0, float $x1, float $y1, float $x2, float $y2, float $x3, float $y3, |
| 56 |
float $x0p, float $y0p, float $x1p, float $y1p, float $x2p, float $y2p, float $x3p, float $y3p, |
| 57 |
):self{ |
| 58 |
return (new self) |
| 59 |
->squareToQuadrilateral($x0p, $y0p, $x1p, $y1p, $x2p, $y2p, $x3p, $y3p) |
| 60 |
->times($this->quadrilateralToSquare($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3)); |
| 61 |
} |
| 62 |
|
| 63 |
private function quadrilateralToSquare( |
| 64 |
float $x0, float $y0, float $x1, float $y1, |
| 65 |
float $x2, float $y2, float $x3, float $y3, |
| 66 |
):self{ |
| 67 |
// Here, the adjoint serves as the inverse: |
| 68 |
return $this |
| 69 |
->squareToQuadrilateral($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3) |
| 70 |
->buildAdjoint(); |
| 71 |
} |
| 72 |
|
| 73 |
private function buildAdjoint():self{ |
| 74 |
// Adjoint is the transpose of the cofactor matrix: |
| 75 |
return $this->set( |
| 76 |
($this->a22 * $this->a33 - $this->a23 * $this->a32), |
| 77 |
($this->a23 * $this->a31 - $this->a21 * $this->a33), |
| 78 |
($this->a21 * $this->a32 - $this->a22 * $this->a31), |
| 79 |
($this->a13 * $this->a32 - $this->a12 * $this->a33), |
| 80 |
($this->a11 * $this->a33 - $this->a13 * $this->a31), |
| 81 |
($this->a12 * $this->a31 - $this->a11 * $this->a32), |
| 82 |
($this->a12 * $this->a23 - $this->a13 * $this->a22), |
| 83 |
($this->a13 * $this->a21 - $this->a11 * $this->a23), |
| 84 |
($this->a11 * $this->a22 - $this->a12 * $this->a21), |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
private function squareToQuadrilateral( |
| 89 |
float $x0, float $y0, float $x1, float $y1, |
| 90 |
float $x2, float $y2, float $x3, float $y3, |
| 91 |
):self{ |
| 92 |
$dx3 = ($x0 - $x1 + $x2 - $x3); |
| 93 |
$dy3 = ($y0 - $y1 + $y2 - $y3); |
| 94 |
|
| 95 |
if($dx3 === 0.0 && $dy3 === 0.0){ |
| 96 |
// Affine |
| 97 |
return $this->set(($x1 - $x0), ($x2 - $x1), $x0, ($y1 - $y0), ($y2 - $y1), $y0, 0.0, 0.0, 1.0); |
| 98 |
} |
| 99 |
|
| 100 |
$dx1 = ($x1 - $x2); |
| 101 |
$dx2 = ($x3 - $x2); |
| 102 |
$dy1 = ($y1 - $y2); |
| 103 |
$dy2 = ($y3 - $y2); |
| 104 |
$denominator = ($dx1 * $dy2 - $dx2 * $dy1); |
| 105 |
$a13 = (($dx3 * $dy2 - $dx2 * $dy3) / $denominator); |
| 106 |
$a23 = (($dx1 * $dy3 - $dx3 * $dy1) / $denominator); |
| 107 |
|
| 108 |
return $this->set( |
| 109 |
($x1 - $x0 + $a13 * $x1), |
| 110 |
($x3 - $x0 + $a23 * $x3), |
| 111 |
$x0, |
| 112 |
($y1 - $y0 + $a13 * $y1), |
| 113 |
($y3 - $y0 + $a23 * $y3), |
| 114 |
$y0, |
| 115 |
$a13, |
| 116 |
$a23, |
| 117 |
1.0, |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
private function times(PerspectiveTransform $other):self{ |
| 122 |
return $this->set( |
| 123 |
($this->a11 * $other->a11 + $this->a21 * $other->a12 + $this->a31 * $other->a13), |
| 124 |
($this->a11 * $other->a21 + $this->a21 * $other->a22 + $this->a31 * $other->a23), |
| 125 |
($this->a11 * $other->a31 + $this->a21 * $other->a32 + $this->a31 * $other->a33), |
| 126 |
($this->a12 * $other->a11 + $this->a22 * $other->a12 + $this->a32 * $other->a13), |
| 127 |
($this->a12 * $other->a21 + $this->a22 * $other->a22 + $this->a32 * $other->a23), |
| 128 |
($this->a12 * $other->a31 + $this->a22 * $other->a32 + $this->a32 * $other->a33), |
| 129 |
($this->a13 * $other->a11 + $this->a23 * $other->a12 + $this->a33 * $other->a13), |
| 130 |
($this->a13 * $other->a21 + $this->a23 * $other->a22 + $this->a33 * $other->a23), |
| 131 |
($this->a13 * $other->a31 + $this->a23 * $other->a32 + $this->a33 * $other->a33), |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @param float[] $xValues |
| 137 |
* @param float[]|null $yValues |
| 138 |
* |
| 139 |
* @return float[][] [$xValues, $yValues] |
| 140 |
*/ |
| 141 |
public function transformPoints(array $xValues, array|null $yValues = null):array{ |
| 142 |
$max = count($xValues); |
| 143 |
|
| 144 |
if($yValues !== null){ // unused |
| 145 |
|
| 146 |
for($i = 0; $i < $max; $i++){ |
| 147 |
$x = $xValues[$i]; |
| 148 |
$y = $yValues[$i]; |
| 149 |
$denominator = ($this->a13 * $x + $this->a23 * $y + $this->a33); |
| 150 |
$xValues[$i] = (($this->a11 * $x + $this->a21 * $y + $this->a31) / $denominator); |
| 151 |
$yValues[$i] = (($this->a12 * $x + $this->a22 * $y + $this->a32) / $denominator); |
| 152 |
} |
| 153 |
|
| 154 |
return [$xValues, $yValues]; |
| 155 |
} |
| 156 |
|
| 157 |
for($i = 0; $i < $max; $i += 2){ |
| 158 |
$x = $xValues[$i]; |
| 159 |
$y = $xValues[($i + 1)]; |
| 160 |
$denominator = ($this->a13 * $x + $this->a23 * $y + $this->a33); |
| 161 |
$xValues[$i] = (($this->a11 * $x + $this->a21 * $y + $this->a31) / $denominator); |
| 162 |
$xValues[($i + 1)] = (($this->a12 * $x + $this->a22 * $y + $this->a32) / $denominator); |
| 163 |
} |
| 164 |
|
| 165 |
return [$xValues, []]; |
| 166 |
} |
| 167 |
|
| 168 |
} |