Machine Learning Kurs im Rahmen der Studierendentage im SS 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.1 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "Exercise 1b: Read a binary file which contains pixel data and apply\n",
  8. "transformations"
  9. ]
  10. },
  11. {
  12. "cell_type": "code",
  13. "execution_count": null,
  14. "metadata": {},
  15. "outputs": [],
  16. "source": [
  17. "import numpy as np\n",
  18. "import matplotlib.pyplot as plt"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": null,
  24. "metadata": {},
  25. "outputs": [],
  26. "source": [
  27. "# load figure as 2D array \n",
  28. "data = np.load('horse.npy')\n",
  29. "print(data.shape)"
  30. ]
  31. },
  32. {
  33. "cell_type": "code",
  34. "execution_count": null,
  35. "metadata": {},
  36. "outputs": [],
  37. "source": [
  38. "# just scale the data by a factor and shift by trans\n",
  39. "trans = np.ones(data.shape)\n",
  40. "trans[0,:] *=0.6\n",
  41. "trans[1,:] *=0.4\n",
  42. "factor = 0.5 \n",
  43. "data_scale = data * factor + trans"
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": null,
  49. "metadata": {},
  50. "outputs": [],
  51. "source": [
  52. "#compression in x and y \n",
  53. "sx = 0.4\n",
  54. "sy = 0.9\n",
  55. "t = np.array([[sx,0],[0,sy]])\n",
  56. "data_comp = t@data"
  57. ]
  58. },
  59. {
  60. "cell_type": "code",
  61. "execution_count": null,
  62. "metadata": {},
  63. "outputs": [],
  64. "source": [
  65. "#rotation by an angle theta\n",
  66. "theta = 0.5\n",
  67. "data_rot = np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta), np.cos(theta)]])@data"
  68. ]
  69. },
  70. {
  71. "cell_type": "code",
  72. "execution_count": null,
  73. "metadata": {},
  74. "outputs": [],
  75. "source": [
  76. "#spiegelung an der x Achse\n",
  77. "tx = np.array([[1,0],[0,-1]]) # mirror x axis\n",
  78. "ty = np.array([[-1,0],[0,1]]) # mirror y axis\n",
  79. "tp = np.array([[-1,0],[0,-1]]) # mirror (0,0)\n",
  80. "data_mirror = tp@data"
  81. ]
  82. },
  83. {
  84. "cell_type": "code",
  85. "execution_count": null,
  86. "metadata": {},
  87. "outputs": [],
  88. "source": [
  89. "# create figure for the transformations\n",
  90. "plt.figure(figsize=(10.0,10.0),dpi=100,facecolor='lightgrey')\n",
  91. "plt.suptitle('Plot Transformations')\n",
  92. "plt.subplot(2,2,1)\n",
  93. "plt.title('original picture')\n",
  94. "plt.plot(data[0,:],data[1,:],'.')\n",
  95. "plt.axis([-1.2,1.2,-1.2,1.2])\n",
  96. "plt.subplot(2,2,2)\n",
  97. "plt.title('scaling and translation')\n",
  98. "plt.plot(data_scale[0,:],data_scale[1,:],'.')\n",
  99. "plt.axis([-1.2,1.2,-1.2,1.2])\n",
  100. "plt.subplot(2,2,3)\n",
  101. "plt.title('compression')\n",
  102. "plt.plot(data_comp[0,:],data_comp[1,:],'.')\n",
  103. "plt.axis([-1.2,1.2,-1.2,1.2])\n",
  104. "plt.subplot(2,2,4)\n",
  105. "plt.title('rotation and mirror at p(0,0)')\n",
  106. "plt.plot(data_rot[0,:],data_rot[1,:],'.')\n",
  107. "plt.plot(data_mirror[0,:],data_mirror[1,:],'.')\n",
  108. "plt.axis([-1.2,1.2,-1.2,1.2])"
  109. ]
  110. }
  111. ],
  112. "metadata": {
  113. "kernelspec": {
  114. "display_name": "Python 3 (ipykernel)",
  115. "language": "python",
  116. "name": "python3"
  117. },
  118. "language_info": {
  119. "codemirror_mode": {
  120. "name": "ipython",
  121. "version": 3
  122. },
  123. "file_extension": ".py",
  124. "mimetype": "text/x-python",
  125. "name": "python",
  126. "nbconvert_exporter": "python",
  127. "pygments_lexer": "ipython3",
  128. "version": "3.8.16"
  129. }
  130. },
  131. "nbformat": 4,
  132. "nbformat_minor": 4
  133. }