Try kernel density estimation
This commit is contained in:
parent
3274683983
commit
d3f9b66b9a
186
Notebooks/Kernel Density Estimation.ipynb
Normal file
186
Notebooks/Kernel Density Estimation.ipynb
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user