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.

139 lines
2.7 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "Exercise 1: Create numpy array and draw rgb color objects"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": null,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "import numpy as np\n",
  17. "import matplotlib.pyplot as plt"
  18. ]
  19. },
  20. {
  21. "cell_type": "markdown",
  22. "metadata": {},
  23. "source": [
  24. "create data array 2x2 as pixel position and 1x3 as rgb color data"
  25. ]
  26. },
  27. {
  28. "cell_type": "code",
  29. "execution_count": null,
  30. "metadata": {},
  31. "outputs": [],
  32. "source": [
  33. "width, height = 200, 200\n",
  34. "data = np.zeros((height, width, 3), dtype=np.uint8)\n"
  35. ]
  36. },
  37. {
  38. "cell_type": "markdown",
  39. "metadata": {},
  40. "source": [
  41. "draw blue cross"
  42. ]
  43. },
  44. {
  45. "cell_type": "code",
  46. "execution_count": null,
  47. "metadata": {},
  48. "outputs": [],
  49. "source": [
  50. "x = np.arange(width)\n",
  51. "x_1 = np.arange(width)\n",
  52. "x_2 = np.arange(width-1,-1,-1)\n",
  53. "y = np.arange(height)\n",
  54. "data[x_1,y] = [0,0,255]\n",
  55. "data[x_2,y] = [0,0,255]"
  56. ]
  57. },
  58. {
  59. "cell_type": "markdown",
  60. "metadata": {},
  61. "source": [
  62. " draw a square "
  63. ]
  64. },
  65. {
  66. "cell_type": "code",
  67. "execution_count": null,
  68. "metadata": {},
  69. "outputs": [],
  70. "source": [
  71. "lower = 55\n",
  72. "upper = 75\n",
  73. "data[lower:upper,lower:upper] = [0,255,0]"
  74. ]
  75. },
  76. {
  77. "cell_type": "markdown",
  78. "metadata": {},
  79. "source": [
  80. "create a mask of a circle using indexing\n",
  81. "np.newaxis adds another dimension\n",
  82. "we create a row and column vector and fill it using the condition"
  83. ]
  84. },
  85. {
  86. "cell_type": "code",
  87. "execution_count": null,
  88. "metadata": {},
  89. "outputs": [],
  90. "source": [
  91. "x_center = 100\n",
  92. "y_center = 100\n",
  93. "radius = 10\n",
  94. "mask = (x[np.newaxis,:]-x_center)**2 + (y[:,np.newaxis]-y_center)**2 < radius**2\n",
  95. "data[mask] = [255,0,0]\n"
  96. ]
  97. },
  98. {
  99. "cell_type": "code",
  100. "execution_count": null,
  101. "metadata": {},
  102. "outputs": [],
  103. "source": [
  104. "# plot image\n",
  105. "plt.figure(figsize=(4.,4.),dpi=100,facecolor='lightgrey')\n",
  106. "plt.imshow(data)\n",
  107. "plt.show()"
  108. ]
  109. },
  110. {
  111. "cell_type": "code",
  112. "execution_count": null,
  113. "metadata": {},
  114. "outputs": [],
  115. "source": []
  116. }
  117. ],
  118. "metadata": {
  119. "kernelspec": {
  120. "display_name": "Python 3 (ipykernel)",
  121. "language": "python",
  122. "name": "python3"
  123. },
  124. "language_info": {
  125. "codemirror_mode": {
  126. "name": "ipython",
  127. "version": 3
  128. },
  129. "file_extension": ".py",
  130. "mimetype": "text/x-python",
  131. "name": "python",
  132. "nbconvert_exporter": "python",
  133. "pygments_lexer": "ipython3",
  134. "version": "3.8.16"
  135. }
  136. },
  137. "nbformat": 4,
  138. "nbformat_minor": 4
  139. }