diff --git a/content/SVDimgNovoTeste.ipynb b/content/SVDimgNovoTeste.ipynb new file mode 100644 index 0000000..068489b --- /dev/null +++ b/content/SVDimgNovoTeste.ipynb @@ -0,0 +1,140 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "30cc35f5", + "metadata": {}, + "outputs": [], + "source": [ + "import skimage\n", + "import skimage.color\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import ipywidgets as widgets" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "b6a94ec8", + "metadata": {}, + "outputs": [], + "source": [ + "from skimage import data\n", + "from skimage.color import rgb2gray\n", + "from skimage import img_as_float" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6fcfddbd", + "metadata": {}, + "outputs": [], + "source": [ + "dictPhotos = {\"cat\":rgb2gray(img_as_float(data.chelsea())), \n", + " \"astronaut\": rgb2gray(img_as_float(data.astronaut())),\n", + " \"photographer\": data.camera(), \n", + " \"coffee\": rgb2gray(img_as_float(data.coffee())) }" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "346b99c1", + "metadata": {}, + "outputs": [], + "source": [ + "from numpy.linalg import svd" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "7b587f36", + "metadata": {}, + "outputs": [], + "source": [ + "def SVD(img, k):\n", + " U, S, VT = svd(img, full_matrices = False)\n", + " singVal = np.diag(S)\n", + " imgProduct = np.dot(U[:,:k], np.dot(singVal[:k,:k], VT[:k,:]))\n", + " return imgProduct" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "70c2676d", + "metadata": {}, + "outputs": [], + "source": [ + "def showImg(name, k):\n", + " img = dictPhotos[name]\n", + " imgCompressed = SVD(img, k)\n", + " plt.imshow(imgCompressed, cmap = 'gray')\n", + " plt.axis('off')\n", + " plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "e6703100", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "d69234a4863247b0ac9d0fddac1f54fa", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(Dropdown(description='Photo:', options=('cat', 'astronaut', 'photographer', 'coffee'), v…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from ipywidgets import interact\n", + "interact(showImg, name=widgets.Dropdown(options=['cat', 'astronaut', 'photographer', 'coffee'], value=\"cat\", description='Photo:'), \n", + " k=widgets.IntSlider(min=0, max=300, step=1, value=150))" + ] + } + ], + "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.9.16" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}