ML-Kurs-SS2023/notebooks/01_intro_ex_1b_sol.ipynb

163 lines
112 KiB
Plaintext
Raw Normal View History

2023-04-03 13:08:49 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 1b: Read a binary file which contains pixel data and apply\n",
"transformations"
]
},
{
"cell_type": "code",
2023-04-11 13:16:53 +02:00
"execution_count": 1,
2023-04-03 13:08:49 +02:00
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
2023-04-11 13:16:53 +02:00
"execution_count": 2,
2023-04-03 13:08:49 +02:00
"metadata": {},
2023-04-11 13:16:53 +02:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 59520)\n"
]
}
],
2023-04-03 13:08:49 +02:00
"source": [
"# load figure as 2D array \n",
"data = np.load('horse.npy')\n",
"print(data.shape)"
]
},
{
"cell_type": "code",
2023-04-11 13:16:53 +02:00
"execution_count": 3,
2023-04-03 13:08:49 +02:00
"metadata": {},
"outputs": [],
"source": [
"# just scale the data by a factor and shift by trans\n",
"trans = np.ones(data.shape)\n",
"trans[0,:] *=0.6\n",
"trans[1,:] *=0.4\n",
"factor = 0.5 \n",
"data_scale = data * factor + trans"
]
},
{
"cell_type": "code",
2023-04-11 13:16:53 +02:00
"execution_count": 4,
2023-04-03 13:08:49 +02:00
"metadata": {},
"outputs": [],
"source": [
"#compression in x and y \n",
"sx = 0.4\n",
"sy = 0.9\n",
"t = np.array([[sx,0],[0,sy]])\n",
"data_comp = t@data"
]
},
{
"cell_type": "code",
2023-04-11 13:16:53 +02:00
"execution_count": 5,
2023-04-03 13:08:49 +02:00
"metadata": {},
"outputs": [],
"source": [
"#rotation by an angle theta\n",
"theta = 0.5\n",
"data_rot = np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta), np.cos(theta)]])@data"
]
},
{
"cell_type": "code",
2023-04-11 13:16:53 +02:00
"execution_count": 6,
2023-04-03 13:08:49 +02:00
"metadata": {},
"outputs": [],
"source": [
"#spiegelung an der x Achse\n",
"tx = np.array([[1,0],[0,-1]]) # mirror x axis\n",
"ty = np.array([[-1,0],[0,1]]) # mirror y axis\n",
"tp = np.array([[-1,0],[0,-1]]) # mirror (0,0)\n",
"data_mirror = tp@data"
]
},
{
"cell_type": "code",
2023-04-11 13:16:53 +02:00
"execution_count": 7,
2023-04-03 13:08:49 +02:00
"metadata": {},
2023-04-11 13:16:53 +02:00
"outputs": [
{
"data": {
"text/plain": [
"(-1.2, 1.2, -1.2, 1.2)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0YAAAORCAYAAADWH7KPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/P9b71AAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdeVyU1f4H8A/7KjuKIIq4L4iymEBiLglJ6s2tX+nVVLRMu23XPUsrc1/KRFNLu2m5kVpiWqFpKaloEiqYoggiKMgmMOz8/iAmBmbfh/m8Xy9fL3nmPOc58zhy5vucc77HJDk5uQ5ERERERERGzFTXDSAiIiIiItI1BkZERERERGT0GBgREREREZHRY2BERERERERGj4EREREREREZPQZGRERERERk9BgYERERERGR0WNgRERERERERo+BERERERERGT1zXTeAiKilOnz4MJYsWSL82czMDK6urggJCcFrr72GNm3aAAAuXryIadOm4YsvvkBwcLBC17hy5QrOnTuHSZMmwcHBQWK5rKwsREZGylXn8ePH4eXlpVA7NOn48ePYunUr7t27h4qKChw4cADdu3fXdbPklpaWhhMnTmD06NHN7uvixYuRmJiIEydO6Kh1RETUgIEREZGGffDBB+jYsSMqKipw6dIl7NixA4mJifj2229ha2urUt1XrlzBli1bMHr0aKmBkbu7O3bv3i1ybPny5Xj8+DFWrlzZrKy+yM/Px8KFC/Hkk09i8eLFsLS0RIcOHXTdL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369u2Lbdu2Nct+165dO8yfPx+
"text/plain": [
"<Figure size 1000x1000 with 4 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
2023-04-03 13:08:49 +02:00
"source": [
"# create figure for the transformations\n",
"plt.figure(figsize=(10.0,10.0),dpi=100,facecolor='lightgrey')\n",
"plt.suptitle('Plot Transformations')\n",
"plt.subplot(2,2,1)\n",
"plt.title('original picture')\n",
"plt.plot(data[0,:],data[1,:],'.')\n",
"plt.axis([-1.2,1.2,-1.2,1.2])\n",
"plt.subplot(2,2,2)\n",
"plt.title('scaling and translation')\n",
"plt.plot(data_scale[0,:],data_scale[1,:],'.')\n",
"plt.axis([-1.2,1.2,-1.2,1.2])\n",
"plt.subplot(2,2,3)\n",
"plt.title('compression')\n",
"plt.plot(data_comp[0,:],data_comp[1,:],'.')\n",
"plt.axis([-1.2,1.2,-1.2,1.2])\n",
"plt.subplot(2,2,4)\n",
"plt.title('rotation and mirror at p(0,0)')\n",
"plt.plot(data_rot[0,:],data_rot[1,:],'.')\n",
"plt.plot(data_mirror[0,:],data_mirror[1,:],'.')\n",
"plt.axis([-1.2,1.2,-1.2,1.2])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 4
}