update solutions

This commit is contained in:
ligerfotis 2023-04-27 15:24:21 +02:00
parent e38ed01912
commit 572d6b1ca8
5 changed files with 951 additions and 743 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,199 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "HoNMeHx5EDAm"
},
"outputs": [],
"source": [
"# By Univ.-Prof. Dr. Elmar Rueckert, 02.03.2022\n",
"# *******************************************************\n",
"\n",
"\n",
"import pickle, os\n",
"import numpy as np\n",
"from PIL import Image\n",
"from itertools import product"
]
},
{
"cell_type": "markdown",
"source": [
"Download the picture in this link https://cps.unileoben.ac.at/wp/DALL·E-2023-02-09-17.32.48-robot-hand-communicating-with-sign-language.png and add it to the repository.\n",
"Name it 'hand.png'"
],
"metadata": {
"id": "6SMr_2ZyFQJ0"
}
},
{
"cell_type": "markdown",
"source": [
"The \"filtered\" callback function takes a kernel (k) and an image as inputs, and applies the convolution operation on the image using the kernel. It first calculates the offset value for the kernel based on its shape, and then creates a zero-filled array of the same shape as the input image. It then iterates through all the pixel coordinates of the image, and for each pixel, it extracts the corresponding kernel-sized sub-image from the input image. It then performs the element-wise multiplication between the sub-image and the kernel, and sums up the resulting values to get a single output value. This output value is then clipped between 0 and 255, and stored in the corresponding pixel location of the output image. Finally, the filtered image is returned as output."
],
"metadata": {
"id": "DZgexX8UE0HV"
}
},
{
"cell_type": "code",
"source": [
"\n",
"def filtered(k, image):\n",
" offset = int(np.floor(k.shape[0] / 2))\n",
" filtered_image = np.zeros(image.shape)\n",
" size = range(offset, filtered_image.shape[0] - offset)\n",
" for i, j in product(size, size):\n",
" image_part = image[i - offset:i + offset + 1, j - offset:j + offset + 1]\n",
" value = (image_part * k).sum()\n",
" filtered_image[i, j] = min(255, max(0, value))\n",
" # Return filtered image\n",
" return filtered_image"
],
"metadata": {
"id": "pwuRm4gUE-eA"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Develop the image processing function that takes the filter and the callback as arguments"
],
"metadata": {
"id": "zqrb5mT6EjRz"
}
},
{
"cell_type": "code",
"source": [
"def image_processing(k, callback):\n",
" # Acquire image\n",
" file_name = 'hand.png'\n",
" image = Image.open(file_name).convert('L')\n",
" image = np.array(image)\n",
"\n",
" filtered_image = callback(k, image)\n",
"\n",
" # Display image\n",
" image = Image.fromarray(image)\n",
" image.show()\n",
" image_tmp = Image.fromarray(filtered_image)\n",
" image_tmp.show()\n",
"\n"
],
"metadata": {
"id": "wjdHq6ONEjau"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Main function"
],
"metadata": {
"id": "h_yhIiCCEe_y"
}
},
{
"cell_type": "code",
"source": [
"if __name__ == '__main__':\n",
" # Laplace kernel\n",
" k_laplacian = np.asarray([[-1, -1, -1],\n",
" [-1, 8, -1],\n",
" [-1, -1, -1]])\n",
"\n",
" # Gaussian Smoothing Kernel\n",
" k_gaussian = np.asarray([[1, 1, 1],\n",
" [1, 1, 1],\n",
" [1, 1, 1]],dtype=float)\n",
" k_gaussian /= 9"
],
"metadata": {
"id": "Y2-B4ii1EewG"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Image Processing"
],
"metadata": {
"id": "XQdU2c8nEb5H"
}
},
{
"cell_type": "code",
"source": [
" image_processing(k_laplacian,filtered)"
],
"metadata": {
"id": "1XN9C4p-EZTF"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"(eog:14526): EOG-CRITICAL **: 16:14:55.972: eog_image_get_file: assertion 'EOG_IS_IMAGE (img)' failed\n",
"\n",
"(eog:14526): GLib-GIO-CRITICAL **: 16:14:55.972: g_file_equal: assertion 'G_IS_FILE (file1)' failed\n"
]
}
],
"source": [
" image_processing(k_gaussian, filtered)\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
]
}

View File

@ -1,143 +1,143 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "HoNMeHx5EDAm"
},
"outputs": [],
"source": [
"# By Univ.-Prof. Dr. Elmar Rueckert, 02.03.2022\n",
"# *******************************************************\n",
"\n",
"\n",
"import pickle, os\n",
"import numpy as np\n",
"from PIL import Image\n",
"from itertools import product"
]
},
{
"cell_type": "markdown",
"source": [
"Download the picture in this link https://cps.unileoben.ac.at/wp/DALL·E-2023-02-09-17.32.48-robot-hand-communicating-with-sign-language.png and add it to the repository.\n",
"Name it 'hand.png'"
],
"metadata": {
"id": "6SMr_2ZyFQJ0"
}
},
{
"cell_type": "markdown",
"source": [
"The \"filtered\" callback function takes a kernel (k) and an image as inputs, and applies the convolution operation on the image using the kernel. It first calculates the offset value for the kernel based on its shape, and then creates a zero-filled array of the same shape as the input image. It then iterates through all the pixel coordinates of the image, and for each pixel, it extracts the corresponding kernel-sized sub-image from the input image. It then performs the element-wise multiplication between the sub-image and the kernel, and sums up the resulting values to get a single output value. This output value is then clipped between 0 and 255, and stored in the corresponding pixel location of the output image. Finally, the filtered image is returned as output."
],
"metadata": {
"id": "DZgexX8UE0HV"
}
},
{
"cell_type": "code",
"source": [
"\n",
"def filtered(k, image):\n",
" \n",
" return filtered_image"
],
"metadata": {
"id": "pwuRm4gUE-eA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Develop the image processing function that takes the filter and the callback as arguments"
],
"metadata": {
"id": "zqrb5mT6EjRz"
}
},
{
"cell_type": "code",
"source": [
"def image_processing(k, callback):\n",
" # Acquire image\n",
"\n",
" # Display image"
],
"metadata": {
"id": "wjdHq6ONEjau"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Main function"
],
"metadata": {
"id": "h_yhIiCCEe_y"
}
},
{
"cell_type": "code",
"source": [
"if __name__ == '__main__':\n",
" # Laplace kernel\n",
" k_laplacian = np.asarray([[-1, -1, -1],\n",
" [-1, 8, -1],\n",
" [-1, -1, -1]])\n",
"\n",
" # Gaussian Smoothing Kernel\n",
" k_gaussian = np.asarray([[1, 1, 1],\n",
" [1, 1, 1],\n",
" [1, 1, 1]],dtype=float)\n",
" k_gaussian /= 9"
],
"metadata": {
"id": "Y2-B4ii1EewG"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Image Processing"
],
"metadata": {
"id": "XQdU2c8nEb5H"
}
},
{
"cell_type": "code",
"source": [
" image_processing(k_laplacian,filtered)\n",
" image_processing(k_gaussian, filtered)"
],
"metadata": {
"id": "1XN9C4p-EZTF"
},
"execution_count": null,
"outputs": []
}
]
}
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "HoNMeHx5EDAm"
},
"outputs": [],
"source": [
"# By Univ.-Prof. Dr. Elmar Rueckert, 02.03.2022\n",
"# *******************************************************\n",
"\n",
"\n",
"import pickle, os\n",
"import numpy as np\n",
"from PIL import Image\n",
"from itertools import product"
]
},
{
"cell_type": "markdown",
"source": [
"Download the picture in this link https://cps.unileoben.ac.at/wp/DALL·E-2023-02-09-17.32.48-robot-hand-communicating-with-sign-language.png and add it to the repository.\n",
"Name it 'hand.png'"
],
"metadata": {
"id": "6SMr_2ZyFQJ0"
}
},
{
"cell_type": "markdown",
"source": [
"The \"filtered\" callback function takes a kernel (k) and an image as inputs, and applies the convolution operation on the image using the kernel. It first calculates the offset value for the kernel based on its shape, and then creates a zero-filled array of the same shape as the input image. It then iterates through all the pixel coordinates of the image, and for each pixel, it extracts the corresponding kernel-sized sub-image from the input image. It then performs the element-wise multiplication between the sub-image and the kernel, and sums up the resulting values to get a single output value. This output value is then clipped between 0 and 255, and stored in the corresponding pixel location of the output image. Finally, the filtered image is returned as output."
],
"metadata": {
"id": "DZgexX8UE0HV"
}
},
{
"cell_type": "code",
"source": [
"\n",
"def filtered(k, image):\n",
"\n",
" return filtered_image"
],
"metadata": {
"id": "pwuRm4gUE-eA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Develop the image processing function that takes the filter and the callback as arguments"
],
"metadata": {
"id": "zqrb5mT6EjRz"
}
},
{
"cell_type": "code",
"source": [
"def image_processing(k, callback):\n",
" # Acquire image\n",
"\n",
" # Display image"
],
"metadata": {
"id": "wjdHq6ONEjau"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Main function"
],
"metadata": {
"id": "h_yhIiCCEe_y"
}
},
{
"cell_type": "code",
"source": [
"if __name__ == '__main__':\n",
" # Laplace kernel\n",
" k_laplacian = np.asarray([[-1, -1, -1],\n",
" [-1, 8, -1],\n",
" [-1, -1, -1]])\n",
"\n",
" # Gaussian Smoothing Kernel\n",
" k_gaussian = np.asarray([[1, 1, 1],\n",
" [1, 1, 1],\n",
" [1, 1, 1]],dtype=float)\n",
" k_gaussian /= 9"
],
"metadata": {
"id": "Y2-B4ii1EewG"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Image Processing"
],
"metadata": {
"id": "XQdU2c8nEb5H"
}
},
{
"cell_type": "code",
"source": [
" image_processing(k_laplacian,filtered)\n",
" image_processing(k_gaussian, filtered)"
],
"metadata": {
"id": "1XN9C4p-EZTF"
},
"execution_count": null,
"outputs": []
}
]
}

View File

@ -14,6 +14,17 @@
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"### Solution for Assignment 2 of the course \"Introduction to Machine Learning\" at the University of Leoben.\n",
"##### Author: Fotios Lygerakis\n",
"##### Semester: SS 2022/2023"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"outputs": [],
"source": [
"import numpy as np"
@ -89,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"outputs": [
{
"name": "stdout",
@ -98,19 +98,18 @@
"[[5 1 3]\n",
" [1 2 1]\n",
" [1 2 1]]\n",
"[[1 2 3]]\n",
"[[16]\n",
" [ 8]\n",
" [ 8]]\n"
"[1 2 3]\n",
"[16 8 8]\n"
]
}
],
"source": [
"tmp_array = np.array([[5,1,3],[1,2,1],[1,2,1]])\n",
"tmp_array2 = np.array([[1,2,3]])\n",
"tmp_array2 = np.array([1,2,3])\n",
"print(tmp_array)\n",
"print(tmp_array2)\n",
"print(np.dot(tmp_array,tmp_array2.T))"
"# print dot product of tmp_array and tmp_array2\n",
"print(np.dot(tmp_array,tmp_array2))"
],
"metadata": {
"collapsed": false
@ -134,7 +133,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"outputs": [
{
"name": "stdout",
@ -142,8 +141,7 @@
"text": [
"[[4 1]\n",
" [2 2]]\n",
"[[1]\n",
" [2]]\n"
"[1 2]\n"
]
}
],
@ -153,7 +151,7 @@
"B = np.array([[4,1],[2,2]])\n",
"print(np.dot(A,B))\n",
"# A*c\n",
"c = np.array([[1,2]])\n",
"c = np.array([1,2])\n",
"print(np.dot(A,c.T))"
],
"metadata": {