diff --git a/Notebooks/Kernel Density Estimation.ipynb b/Notebooks/Kernel Density Estimation.ipynb new file mode 100644 index 0000000..5d993b5 --- /dev/null +++ b/Notebooks/Kernel Density Estimation.ipynb @@ -0,0 +1,186 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "from pathlib import Path\n", + "import numpy as np\n", + "from scipy import stats\n", + "import matplotlib.pyplot as plt\n", + "\n", + "triskele_path = Path('../triskele/python/')\n", + "sys.path.append(str(triskele_path.resolve()))\n", + "import triskele\n", + "\n", + "# Specific Utils\n", + "\n", + "def DFC_filter(raster):\n", + " raster[raster > 1e4] = raster[raster < 1e4].max()\n", + "\n", + "def show(im, im_size=1, save=None):\n", + " plt.figure(figsize=(16*im_size,3*im_size))\n", + " plt.imshow(im)\n", + " plt.colorbar()\n", + " \n", + " if save is not None:\n", + " plt.savefig(save, bbox_inches='tight', pad_inches=1)\n", + " \n", + " plt.show()\n", + "\n", + "def mshow(Xs, titles=None, im_size=1, save=None):\n", + " s = len(Xs)\n", + "\n", + " plt.figure(figsize=(16*im_size,3*im_size*s))\n", + "\n", + " for i in range(s):\n", + " plt.subplot(s,1,i+1)\n", + " plt.imshow(Xs[i])\n", + " \n", + " if titles is not None:\n", + " plt.title(titles[i])\n", + " \n", + " plt.colorbar()\n", + " \n", + " if save is not None:\n", + " plt.savefig(save, bbox_inches='tight', pad_inches=1)\n", + " \n", + " plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Kernel Density Estimation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "raster = triskele.read('../Data/test.tiff')\n", + "show(raster)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "kernel = stats.gaussian_kde(raster.reshape(-1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import cv2\n", + "\n", + "test = cv2.imread('/home/florent/Pictures/Jura-Panorama.jpg')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "bins = [x for x in range(100)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "kernel.pdf(bins)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(bins, kernel.pdf(bins))\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "show(test)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "kB = stats.gaussian_kde(test[:,:,0].reshape(-1))\n", + "kG = stats.gaussian_kde(test[:,:,1].reshape(-1))\n", + "kR = stats.gaussian_kde(test[:,:,2].reshape(-1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "bins = [x for x in range(255)]\n", + "plt.plot(bins, kB.pdf(bins))\n", + "plt.plot(bins, kG.pdf(bins))\n", + "plt.plot(bins, kR.pdf(bins))\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "bins = [x for x in range(10)]\n", + "plt.plot(bins, kB.pdf(bins))\n", + "plt.plot(bins, kG.pdf(bins))\n", + "plt.plot(bins, kR.pdf(bins))\n", + "plt.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/ld2dap/AttributeProfiles.py b/ld2dap/AttributeProfiles.py index c8551cf..402907c 100644 --- a/ld2dap/AttributeProfiles.py +++ b/ld2dap/AttributeProfiles.py @@ -12,6 +12,7 @@ from core import Filter ## TODO: dep import sys +import numpy as np sys.path.append('../triskele/python') import triskele @@ -31,4 +32,6 @@ class AttributeProfiles(Filter): standard_deviation=self.sd, moment_of_inertia=self.moi) - return att, None + att = np.dstack((att_min, att_max)) + + return att, metadata diff --git a/ld2dap/LoadTIFF.py b/ld2dap/LoadTIFF.py index e63830b..7f05870 100644 --- a/ld2dap/LoadTIFF.py +++ b/ld2dap/LoadTIFF.py @@ -8,7 +8,7 @@ # # TODO details -from core import Input +from core import Input, Stack import numpy as np ## TODO: dep @@ -23,10 +23,15 @@ class LoadTIFF(Input): def _process(self, data, metadata): layers = list() + metadata = list() - for file in self.files: + for i, file in enumerate(self.files): print('Loading {}'.format(file)) layers.append(triskele.read(file)) + metadata.append(Stack(i, desc=file, symb='I_{{{}}}'.format(i))) - return np.stack(layers, axis=2), self.files + return np.stack(layers, axis=2), metadata + + def I(self, i): + return self.files[i] diff --git a/ld2dap/SaveFig.py b/ld2dap/SaveFig.py new file mode 100644 index 0000000..c51c94a --- /dev/null +++ b/ld2dap/SaveFig.py @@ -0,0 +1,31 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# \file SaveFig.py +# \brief TODO +# \author Florent Guiotte +# \version 0.1 +# \date 09 avril 2018 +# +# TODO details + +from core import Output + +import matplotlib.pyplot as plt + +class SaveFig(Output): + def __init__(self, fname, bbox_inches='tight', pad_inches=1): + super().__init__(self.__class__.__name__) + self.fname = fname + self.bbox_inches = bbox_inches + self.pad_inches = pad_inches + + def _process(self, data, metadata): + im_size = 2 + fig = plt.figure(figsize=(16*im_size,3*im_size)) + f1 = fig.add_subplot(111) + img = f1.imshow(data[:,:,-1]) + plt.colorbar(img) + if metadata is not None: + f1.set_title(metadata[-1]) + fig.savefig(self.fname, bbox_inches=self.bbox_inches, pad_inches=self.pad_inches) + plt.close(fig) diff --git a/ld2dap/ShowFig.py b/ld2dap/ShowFig.py new file mode 100644 index 0000000..08494b0 --- /dev/null +++ b/ld2dap/ShowFig.py @@ -0,0 +1,31 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# \file SaveFig.py +# \brief TODO +# \author Florent Guiotte +# \version 0.1 +# \date 09 avril 2018 +# +# TODO details + +from core import Output + +import matplotlib.pyplot as plt + +class ShowFig(Output): + def __init__(self, fname, bbox_inches='tight', pad_inches=1): + super().__init__(self.__class__.__name__) + self.fname = fname + self.bbox_inches = bbox_inches + self.pad_inches = pad_inches + + def _process(self, data, metadata): + im_size = 2 + fig = plt.figure(figsize=(16*im_size,3*im_size)) + f1 = fig.add_subplot(111) + img = f1.imshow(data[:,:,-1]) + plt.colorbar(img) + if metadata is not None: + f1.set_title(metadata[-1]) + plt.show() + plt.close(fig) diff --git a/ld2dap/Treshold.py b/ld2dap/Treshold.py new file mode 100644 index 0000000..37f1540 --- /dev/null +++ b/ld2dap/Treshold.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# \file Treshold.py +# \brief TODO +# \author Florent Guiotte +# \version 0.1 +# \date 09 avril 2018 +# +# TODO details + +from core import Filter + +class Treshold(Filter): + def __init__(self, treshold, max_value=None): + super().__init__(self.__class__.__name__) + self.treshold = treshold + self.max_value = max_value #if max_value is not None else treshold + + def _process(self, data, metadata): + if self.max_value is None: + self.max_value = data[data < self.treshold].max() + + return data * (data < self.treshold) + self.max_value * (data >= self.treshold), metadata diff --git a/ld2dap/core/Node.py b/ld2dap/core/Node.py index 1a6ca27..3af0305 100644 --- a/ld2dap/core/Node.py +++ b/ld2dap/core/Node.py @@ -17,7 +17,7 @@ class Node(object): return ("Node:{}".format(self.name)) def process(self, data, metadata=None): - self._process(data) + self._process(data, metadata) def _process(self, data, metadata=None): raise NotImplementedError( diff --git a/ld2dap/core/Stack.py b/ld2dap/core/Stack.py new file mode 100644 index 0000000..8ea3fa5 --- /dev/null +++ b/ld2dap/core/Stack.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# \file %filename%.py +# \brief TODO +# \author Florent Guiotte +# \version 0.1 +# \date 11 avril 2018 +# +# TODO details + + +class Stack(object): + def __init__(self, begin=0, size=1, desc=None, symb=None) : + self.begin = begin + self.end = begin + size + self.desc = list() + self.symb = list() + + if desc is not None: + self.desc.append(desc) + + if symb is not None: + self.symb.append(symb) diff --git a/ld2dap/core/__init__.py b/ld2dap/core/__init__.py index ac7bdea..cbc170d 100644 --- a/ld2dap/core/__init__.py +++ b/ld2dap/core/__init__.py @@ -11,3 +11,4 @@ from .Output import Output from .Input import Input from .Filter import Filter +from .Stack import Stack diff --git a/ld2dap/test.py b/ld2dap/test.py index 1e3fbed..00c0725 100644 --- a/ld2dap/test.py +++ b/ld2dap/test.py @@ -8,19 +8,28 @@ # # TODO details -from core import Input, Output, Filter +#from core import Input, Output, Filter from LoadTIFF import LoadTIFF from AttributeProfiles import AttributeProfiles as APs +from SaveFig import SaveFig +from Treshold import Treshold +from ShowFig import ShowFig + +import numpy as np def main(): - i = LoadTIFF(['../Data/test.tiff']) - ap = APs() - o = Output('o') + i = LoadTIFF(['../Data/test.tiff', '../Data/test.tiff']) + t = Treshold(1e4) + ap = APs(np.array([100,1e3,1e4])) + o = SaveFig('test.png') + s = ShowFig(None) - ap.input = i - o.input = i + t.input = i + ap.input = t + o.input = ap + s.input = ap - o.run() + i.run() if __name__ == '__main__': main()