|
267 | 267 | "source": [
|
268 | 268 | "### Quadratic form\n",
|
269 | 269 | "\n",
|
270 |
| - "The matrix definition of an ellipsoid : $x^{\\intercal}Mx$ can obviously be developped, and give a polynomial of degree 2 of the form\n", |
| 270 | + "The matrix definition of an ellipsoid : $x^{\\intercal}Mx$ can obviously be developped, and give a multivariate polynomial of degree 2 of the form\n", |
271 | 271 | "$$\n",
|
272 | 272 | "\\sum_{i=0, j=0}^{n-1, n-1} M_{ij} x_i x_j\n",
|
273 | 273 | "$$\n",
|
274 | 274 | "\n",
|
275 |
| - "This sum has $n^2$ terms, but, as all $M_{ij}$ and $M_{ji}$ terms can be factorized, the underlying quadratic form actually has $\\frac{n \\times (n-1)}{2}$ terms. Unfortunately, we won't talk much about quadratic forms here." |
| 275 | + "This sum has $n^2$ terms, but, as all $M_{ij}$ and $M_{ji}$ terms can be factorized, the underlying quadratic form actually has $\\frac{n \\times (n-1)}{2}$ terms. Unfortunately, we won't talk much about quadratic forms here.\n", |
| 276 | + "\n", |
| 277 | + "More on quadratic forms in the notebook QuadraticFormsForFun" |
276 | 278 | ]
|
277 | 279 | },
|
278 | 280 | {
|
|
457 | 459 | "plt.axis('equal')"
|
458 | 460 | ]
|
459 | 461 | },
|
| 462 | + { |
| 463 | + "cell_type": "markdown", |
| 464 | + "metadata": {}, |
| 465 | + "source": [ |
| 466 | + "## Ellipsoid estimation from boundary points\n", |
| 467 | + "\n", |
| 468 | + "In the field of image processing, it might sometimes be needed to estimate an ellipse, not from a a set of points pertaining to that geometric objects, but directly to a set of points that are supposed to lie on the edge of the ellipsoid.\n", |
| 469 | + "\n", |
| 470 | + "### Origin centered ellipsoid\n", |
| 471 | + "\n", |
| 472 | + "Lets recall that a origin centered ellipsoid E equation can be written as:\n", |
| 473 | + "\n", |
| 474 | + "\\begin{align*}\n", |
| 475 | + " E &= x \\quad \\text{s.t.} \\sqrt{x^t M x} = r \\\\\n", |
| 476 | + " &= x \\quad \\text{s.t.} \\sqrt{x^t M x} - r = 0 \\\\\n", |
| 477 | + "\\end{align*}\n", |
| 478 | + "\n", |
| 479 | + "In 2d, given we can also write this more explicitly as:\n", |
| 480 | + "\n", |
| 481 | + "\\begin{align*}\n", |
| 482 | + " (x_0, x_1) \\quad \\text{s.t.} \\sqrt{\n", |
| 483 | + " \\begin{pmatrix}\n", |
| 484 | + " x_0\\\\\n", |
| 485 | + " x_1\n", |
| 486 | + " \\end{pmatrix} ^t\n", |
| 487 | + " \\begin{pmatrix}\n", |
| 488 | + " M_a & M_b\\\\\n", |
| 489 | + " M_c & M_d\\\\\n", |
| 490 | + " \\end{pmatrix}\n", |
| 491 | + " \\begin{pmatrix}\n", |
| 492 | + " x_0\\\\\n", |
| 493 | + " x_1\n", |
| 494 | + " \\end{pmatrix}} - r = 0 \\\\\n", |
| 495 | + " \\sqrt{\n", |
| 496 | + " \\begin{pmatrix}\n", |
| 497 | + " x_0\\\\\n", |
| 498 | + " x_1\n", |
| 499 | + " \\end{pmatrix} ^t\n", |
| 500 | + " \\begin{pmatrix}\n", |
| 501 | + " M_a x_0 + M_b x_1\\\\\n", |
| 502 | + " M_c x_0 + M_d x_1\\\\\n", |
| 503 | + " \\end{pmatrix}} - r = 0 \\\\\n", |
| 504 | + " \\sqrt{M_a x_0^2 + M_d x_1^2 + M_b M_c x_0 x_1} - r = 0\n", |
| 505 | + "\\end{align*}\n", |
| 506 | + "\n", |
| 507 | + "Lets take a closer look at the resulting multivariate polynomial:\n", |
| 508 | + "\\begin{align*}\n", |
| 509 | + " \\sqrt{M_a x_0^2 + M_d x_1^2 + M_b M_c x_0 x_1} - r = 0 \\\\\n", |
| 510 | + " \\Leftrightarrow M_a x_0^2 + M_d x_1^2 + M_b M_c x_0 x_1 - r^2 = 0\n", |
| 511 | + "\\end{align*}\n", |
| 512 | + "\n", |
| 513 | + "Given multiple instances of $(x_{0,i}, x_{1,i})$ pairs, we can easily setup a least square problem, in order to find an optimally fitting ellipsoid, in the least square sense:\n", |
| 514 | + "\n", |
| 515 | + "\\begin{align*}\n", |
| 516 | + " \\underset{{M_a, M_b, M_c, M_d, r} \\in \\mathbb{R}^5}{argmin} &\\|\\begin{pmatrix}\n", |
| 517 | + " x_{0,0}^2 & x_{1,0}^2 & x_{0,0} x_{1,0} & -1\\\\\n", |
| 518 | + " x_{0,0}^2 & x_{1,0}^2 & x_{0,0} x_{1,0} & -1\\\\\n", |
| 519 | + " \\vdots & \\vdots & \\vdots & -1\\\\\n", |
| 520 | + " x_{0,n-1}^2 & x_{1,n-1}^2 & x_{0,n-1} x_{1,n-1} & -1\\\\\n", |
| 521 | + " \\end{pmatrix}\n", |
| 522 | + " \\begin{pmatrix}\n", |
| 523 | + " M_a \\\\ M_d \\\\ M_b M_c \\\\ r^2\n", |
| 524 | + " \\end{pmatrix} - \\vec{0}\\|_2^2 \\\\\n", |
| 525 | + " \\Leftrightarrow\n", |
| 526 | + " \\underset{{M_a, M_b, M_c, M_d, r} \\in \\mathbb{R}^5}{argmin} &\\|\\begin{pmatrix}\n", |
| 527 | + " x_{0,0}^2 & x_{1,0}^2 & x_{0,0} x_{1,0} & -1\\\\\n", |
| 528 | + " x_{0,0}^2 & x_{1,0}^2 & x_{0,0} x_{1,0} & -1\\\\\n", |
| 529 | + " \\vdots & \\vdots & \\vdots & -1\\\\\n", |
| 530 | + " x_{0,n-1}^2 & x_{1,n-1}^2 & x_{0,n-1} x_{1,n-1} & -1\\\\\n", |
| 531 | + " \\end{pmatrix}\n", |
| 532 | + " \\begin{pmatrix}\n", |
| 533 | + " a \\\\ b \\\\ c \\\\ d\n", |
| 534 | + " \\end{pmatrix}\\|_2^2 \\\\\n", |
| 535 | + " \\Leftrightarrow\n", |
| 536 | + " \\underset{{M_a, M_b, M_c, M_d, r} \\in \\mathbb{R}^5}{argmin} &M_2\n", |
| 537 | + " \\begin{pmatrix}\n", |
| 538 | + " a \\\\ b \\\\ c \\\\ d\n", |
| 539 | + " \\end{pmatrix}\\|_2^2 \n", |
| 540 | + "\\end{align*}\n", |
| 541 | + "\n", |
| 542 | + "On can easily see that this problem is equivalent to finding the kernel of matrix $M_2$ or $M_2^t M_2$.\n", |
| 543 | + "\n", |
| 544 | + "Of course, the zero vector is a trivial solution. This is why it is usually better to formulate the problem with the builtin constraint that the zero solution is excluded (that project the problem from $\\mathbb{R}^5$ onto the lower dimensional $\\mathbb{R}^{4*}$ space):\n", |
| 545 | + "\n", |
| 546 | + "\\begin{align*}\n", |
| 547 | + " \\sqrt{M_a x_0^2 + M_d x_1^2 + M_b M_c x_0 x_1} &= r \\\\\n", |
| 548 | + " \\Leftrightarrow M_a x_0^2 + M_d x_1^2 + M_b M_c x_0 x_1 - r^2 &= 0\\\\\n", |
| 549 | + " \\Leftrightarrow \\frac{M_a}{r^2} x_0^2 + \\frac{M_d}{r^2} x_1^2 + \\frac{M_b M_c}{r^2} x_0 x_1 - 1 &= 0 \\quad \\text{with } r\\neq 0\\\\\n", |
| 550 | + "\\end{align*}" |
| 551 | + ] |
| 552 | + }, |
| 553 | + { |
| 554 | + "cell_type": "markdown", |
| 555 | + "metadata": {}, |
| 556 | + "source": [ |
| 557 | + "### Arbitrary ellipsoid\n", |
| 558 | + "\n", |
| 559 | + "Lets now assume that the unknown ellipsoid can be arbitrary centered at $$c=\\begin{pmatrix}c_0\\\\c_1end{pmatrix}$$\n", |
| 560 | + "\n", |
| 561 | + "\\begin{align*}\n", |
| 562 | + " (x_0, x_1-c_1) \\quad \\text{s.t.} \\sqrt{\n", |
| 563 | + " \\begin{pmatrix}\n", |
| 564 | + " x_0 - c_0\\\\\n", |
| 565 | + " x_1 - c_1\n", |
| 566 | + " \\end{pmatrix} ^t\n", |
| 567 | + " \\begin{pmatrix}\n", |
| 568 | + " M_a & M_b\\\\\n", |
| 569 | + " M_c & M_d\\\\\n", |
| 570 | + " \\end{pmatrix}\n", |
| 571 | + " \\begin{pmatrix}\n", |
| 572 | + " x_0 - c_0\\\\\n", |
| 573 | + " x_1 - c_1\n", |
| 574 | + " \\end{pmatrix}} - r = 0 \\\\\n", |
| 575 | + " \\sqrt{\n", |
| 576 | + " \\begin{pmatrix}\n", |
| 577 | + " x_0\\\\\n", |
| 578 | + " x_1\n", |
| 579 | + " \\end{pmatrix} ^t\n", |
| 580 | + " \\begin{pmatrix}\n", |
| 581 | + " M_a x_0 + M_b x_1\\\\\n", |
| 582 | + " M_c x_0 + M_d x_1\\\\\n", |
| 583 | + " \\end{pmatrix}} - r = 0 \\\\\n", |
| 584 | + " \\left(M_a \\right) x_0^2 + \\left(M_a M_b \\right) x_1^2 + \\left(M_a M_b \\right) x_0 + \\left(M_a M_b \\right) x_1 + \\left(M_a M_b \\right) x_0 x_1 + \\left(M_a M_b \\right)\n", |
| 585 | + "\\end{align*}\n" |
| 586 | + ] |
| 587 | + }, |
460 | 588 | {
|
461 | 589 | "cell_type": "markdown",
|
462 | 590 | "metadata": {},
|
|
963 | 1091 | "name": "python",
|
964 | 1092 | "nbconvert_exporter": "python",
|
965 | 1093 | "pygments_lexer": "ipython3",
|
966 |
| - "version": "3.6.6" |
| 1094 | + "version": "3.6.7" |
967 | 1095 | }
|
968 | 1096 | },
|
969 | 1097 | "nbformat": 4,
|
|
0 commit comments