Last active
December 12, 2019 15:27
-
-
Save Feyn-Man/c3079cc4f0680e0197310c92ddd52aca to your computer and use it in GitHub Desktop.
Python code for a sliding window on numpy arrays (images)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "sliding-window.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/Feyn-Man/c3079cc4f0680e0197310c92ddd52aca/sliding-window.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "V4wnVI9goiU7", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"import numpy as np" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "xCATCcGLoQxJ", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"vec = np.array([[1,1,1,1,1],\n", | |
" [1,1,1,1,1],\n", | |
" [1,1,1,1,1],\n", | |
" [1,1,1,1,1]])" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "h4Y1gy_aoth3", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "cf9df74b-bfb7-4a59-9f79-8ae6f0b53b99" | |
}, | |
"source": [ | |
"vec.shape" | |
], | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(4, 5)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 9 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "8CGZlZP9R9ha", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def sliding_window(img):\n", | |
"\n", | |
" (rows, columns) = img.shape\n", | |
" \n", | |
" #dimensioni della finestra con cui scorrere sull'immaigine\n", | |
" windowsize_r = 2\n", | |
" windowsize_c = 2\n", | |
"\n", | |
" if windowsize_r > rows or windowsize_c > columns:\n", | |
" print('window is too big. Please specify window dimensions smaller than the shape of the image.')\n", | |
" return\n", | |
" \n", | |
" #punti di inizio e fine della finestra per righe e colonne\n", | |
" start_r = 0\n", | |
" start_c = 0\n", | |
" end_r = start_r + windowsize_r\n", | |
" end_c = start_c + windowsize_c\n", | |
"\n", | |
"\n", | |
" #si scorre da sx vs dx e dall'alto vs il basso\n", | |
" while (True):#questo per scorrere le righe\n", | |
" while (True): #questo per scorrere le colonne\n", | |
"\n", | |
" window = img[start_r:end_r, start_c:end_c]\n", | |
" \n", | |
" print(start_r, \":\", end_r, \",\", start_c, \":\", end_c)\n", | |
" \n", | |
" start_c = start_c + (windowsize_c-1) #sovrapponimento di 10 con la finestra precedente\n", | |
" if start_c < 0: #se ci spostiamo indietro di troppo per la sovrapposizione\n", | |
" print('Error while sliding the window on columns, please check parameters.')\n", | |
" return\n", | |
" end_c = start_c + windowsize_c\n", | |
"\n", | |
" if (end_c > columns): #se la fine sborda (fine immagine) si mette come massimo l'ultimo valore di col e si calcola lbp per l'ultima finestra su quella riga\n", | |
" \n", | |
" end_c = columns\n", | |
" window = img[start_r:end_r, start_c:end_c]\n", | |
" \n", | |
" print(start_r, \":\", end_r, \",\", start_c, \":\", columns)\n", | |
" \n", | |
" start_c = 0\n", | |
" end_c = start_c + windowsize_c\n", | |
" break\n", | |
"\n", | |
" if (end_r == rows):\n", | |
" # se la fine della finestra è uguale all'ultimo indice di riga allora si è finita l'immagine\n", | |
" break\n", | |
" start_r = start_r + (windowsize_r-1)\n", | |
" if start_r < 0: #se ci spostiamo indietro di troppo per la sovrapposizione\n", | |
" print('Error while sliding the window on rows, please check parameters.')\n", | |
" return\n", | |
" end_r = start_r + windowsize_r\n", | |
" if (end_r > rows): #se la finestra sborda in basso allora si mette come fine della riga della finestra il valore massimo di riga\n", | |
" end_r = rows\n", | |
" \n", | |
" #return features_vec" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Ac0WL2g8obTZ", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 290 | |
}, | |
"outputId": "aaec383e-019d-4598-adec-795963029c18" | |
}, | |
"source": [ | |
"sliding_window(vec)" | |
], | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"0 : 2 , 0 : 2\n", | |
"0 : 2 , 1 : 3\n", | |
"0 : 2 , 2 : 4\n", | |
"0 : 2 , 3 : 5\n", | |
"0 : 2 , 4 : 5\n", | |
"1 : 3 , 0 : 2\n", | |
"1 : 3 , 1 : 3\n", | |
"1 : 3 , 2 : 4\n", | |
"1 : 3 , 3 : 5\n", | |
"1 : 3 , 4 : 5\n", | |
"2 : 4 , 0 : 2\n", | |
"2 : 4 , 1 : 3\n", | |
"2 : 4 , 2 : 4\n", | |
"2 : 4 , 3 : 5\n", | |
"2 : 4 , 4 : 5\n" | |
], | |
"name": "stdout" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment