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

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 1: Create numpy array and draw rgb color objects"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"create data array 2x2 as pixel position and 1x3 as rgb color data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"width, height = 200, 200\n",
"data = np.zeros((height, width, 3), dtype=np.uint8)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"draw blue cross"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.arange(width)\n",
"x_1 = np.arange(width)\n",
"x_2 = np.arange(width-1,-1,-1)\n",
"y = np.arange(height)\n",
"data[x_1,y] = [0,0,255]\n",
"data[x_2,y] = [0,0,255]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" draw a square "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lower = 55\n",
"upper = 75\n",
"data[lower:upper,lower:upper] = [0,255,0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"create a mask of a circle using indexing\n",
"np.newaxis adds another dimension\n",
"we create a row and column vector and fill it using the condition"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x_center = 100\n",
"y_center = 100\n",
"radius = 10\n",
"mask = (x[np.newaxis,:]-x_center)**2 + (y[:,np.newaxis]-y_center)**2 < radius**2\n",
"data[mask] = [255,0,0]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# plot image\n",
"plt.figure(figsize=(4.,4.),dpi=100,facecolor='lightgrey')\n",
"plt.imshow(data)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}