Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/cpge-itc/itc2
Browse files Browse the repository at this point in the history
  • Loading branch information
fortierq committed Nov 13, 2023
2 parents 6e3a3e0 + 5473564 commit ffccc3c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 290 deletions.
2 changes: 1 addition & 1 deletion _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ parts:
file: algo/prog_dyn/td/matrice_prog_dyn.pdf
- tp: algo/prog_dyn/tp/tp1/tp_prog_dyn.ipynb
- tp: algo/prog_dyn/tp/tp2/tp_sac_dos.ipynb
- tp: algo/prog_dyn/seam_carving/seam_carving.ipynb
- cor: algo/prog_dyn/seam_carving/seam_carving.ipynb
root: intro
130 changes: 130 additions & 0 deletions files/dl/algo/prog_dyn/seam_carving/seam_carving.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@
"**Question** : Écrire une fonction `matrice_gradient(m)` qui renvoie `g` à partir de `m`. On pourra utiliser, au choix, une liste de listes ou un tableau `numpy` (en utilisant, par exemple, `np.zeros((..., ...))` pour créer `g`). On pourra utiliser `abs` pour la valeur absolue."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"tags": [
"cor"
]
},
"outputs": [],
"source": [
"def matrice_gradient(m):\n",
" n, k = np.shape(m)\n",
" g = np.zeros((n, k))\n",
" g[0][0] = abs(m[1][0] - m[0][0]) + abs(m[0][1] - m[0][0])\n",
" g[n-1][k-1] = abs(m[n-1][k-1] - m[n-2][k-1]) + abs(m[n-1][k-1] - m[n-1][k-2])\n",
" g[0][k-1] = abs(m[1][k-1] - m[0][k-1]) + abs(m[0][k-1] - m[0][k-2])\n",
" g[n-1][0] = abs(m[n-1][0] - m[n-2][0]) + abs(m[n-1][1] - m[n-1][0])\n",
" for i in range(1, n-1):\n",
" g[i][0] = abs(m[i+1][0] - m[i-1][0]) + abs(m[i][1] - m[i][0])\n",
" g[i][k-1] = abs(m[i+1][k-1] - m[i-1][k-1]) + abs(m[i][k-1] - m[i][k-2])\n",
" for j in range(1, k-1):\n",
" g[0][j] = abs(m[1][j] - m[0][j]) + abs(m[0][j+1] - m[0][j-1])\n",
" g[n-1][j] = abs(m[n-1][j] - m[n-2][j]) + abs(m[n-1][j+1] - m[n-1][j-1])\n",
" for i in range(1, n-1):\n",
" for j in range(1, k-1):\n",
" g[i][j] = abs(m[i+1][j] - m[i-1][j]) + abs(m[i][j+1] - m[i][j-1])\n",
" return g"
]
},
{
"cell_type": "code",
"execution_count": 4,
Expand Down Expand Up @@ -206,6 +235,28 @@
" return np.array(m2) # conversion en matrice numpy"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"tags": [
"cor"
]
},
"outputs": [],
"source": [
"def enlever_chemin(m, c):\n",
" n, p = np.shape(m)\n",
" m2 = []\n",
" for i in range(n):\n",
" l = []\n",
" for j in range(p):\n",
" if j != c[i]:\n",
" l.append(m[i][j])\n",
" m2.append(l)\n",
" return np.array(m2)"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down Expand Up @@ -233,6 +284,27 @@
"**Question** : Écrire une fonction `dp_chemin(g)` qui renvoie une matrice `d` de même dimension que `g` telle que `d[i][j]` est l'énergie minimum d'un chemin depuis le haut de l'image jusqu'à un pixel `i, j`."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"tags": [
"cor"
]
},
"outputs": [],
"source": [
"def dp_chemin(g):\n",
" d = np.zeros(g.shape)\n",
" d[0] = g[0]\n",
" for i in range(1, g.shape[0]):\n",
" d[i, 0] = g[i, 0] + min(d[i-1, 0], d[i-1, 1])\n",
" d[i, -1] = g[i, -1] + min(d[i-1, -1], d[i-1, -2])\n",
" for j in range(1, g.shape[1]-1):\n",
" d[i, j] = g[i, j] + min(d[i-1, j-1], d[i-1, j], d[i-1, j+1])\n",
" return d"
]
},
{
"cell_type": "code",
"execution_count": 9,
Expand Down Expand Up @@ -272,6 +344,24 @@
"**Question** : Écrire une fonction `min_energie_bas(d)` qui renvoie le numéro de colonne `j` du pixel de la dernière ligne d'énergie minimale, c'est-à-dire tel que `d[n - 1][j]` est minimal, où `d` est la matrice renvoyée par la fonction `dp_chemin(m)` et `n` est le nombre de lignes de `d`."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"tags": [
"cor"
]
},
"outputs": [],
"source": [
"def min_energie_bas(d):\n",
" j_mini = 0 # indice du minimum de d sur la dernière ligne\n",
" for j in range(1, d.shape[1]):\n",
" if d[-1, j] < d[-1, j_mini]:\n",
" j_mini = j\n",
" return j_mini"
]
},
{
"cell_type": "code",
"execution_count": 11,
Expand Down Expand Up @@ -317,6 +407,29 @@
" return c[::-1] # pour inverser c"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"tags": [
"cor"
]
},
"outputs": [],
"source": [
"def min_chemin(m):\n",
" d = dp_chemin(matrice_gradient(m))\n",
" c = [min_energie_bas(d)]\n",
" for i in range(len(m) - 2, -1, -1):\n",
" j = c[-1]\n",
" mini = j\n",
" for k in range(j - 1, j + 2):\n",
" if k >= 0 and k < len(d[i]) and d[i, k] < d[i, j]:\n",
" mini = k\n",
" c.append(mini)\n",
" return c[::-1] # pour inverser c"
]
},
{
"cell_type": "code",
"execution_count": 14,
Expand Down Expand Up @@ -362,6 +475,23 @@
"- Enlever le chemin `c` de `m`"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"tags": [
"cor"
]
},
"outputs": [],
"source": [
"def seam_carving(m, n):\n",
" for i in range(n):\n",
" c = min_chemin(m)\n",
" m = enlever_chemin(m, c)\n",
" return m"
]
},
{
"cell_type": "code",
"execution_count": 16,
Expand Down
Loading

0 comments on commit ffccc3c

Please sign in to comment.