ML_course/assignment 2/assignment2_bonus_unsolved.ipynb

143 lines
4.0 KiB
Plaintext
Raw Normal View History

2023-03-13 17:57:32 +00:00
{
"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": 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": []
}
]
}