forked from pontikos/retinal_imaging
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"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", | ||
"version": "3.7.3" | ||
}, | ||
"colab": { | ||
"name": "create_filters.ipynb", | ||
"provenance": [], | ||
"include_colab_link": true | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "view-in-github", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"<a href=\"https://colab.research.google.com/github/pontikos/retinal_imaging/blob/master/create_filters.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "f3gYl7iDPU4i", | ||
"colab_type": "code", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"import cv2\n", | ||
"import pandas as pd\n", | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import pickle" | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "GZKHkzwfPU4n", | ||
"colab_type": "code", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"imgpath = './combine/female_correct.png'" | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "0fxbRxw9PU4q", | ||
"colab_type": "code", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"scale = (450,450)\n", | ||
"cropcenter = (scale[0]/2, scale[1]/2)\n", | ||
"bopcenter = cropcenter\n", | ||
"clicking = False\n", | ||
"zoomed = False\n", | ||
"library = []" | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "2PGZHfvlPU4t", | ||
"colab_type": "code", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"def click_and_zoom(event, x, y, flags, param):\n", | ||
" global cropcenter, clicking, zoomed\n", | ||
" \n", | ||
" if event == cv2.EVENT_LBUTTONDOWN:\n", | ||
" if not zoomed:\n", | ||
" cropcenter = (x,y)\n", | ||
" clicking = True\n", | ||
"\n", | ||
" \n", | ||
" elif event == cv2.EVENT_LBUTTONUP:\n", | ||
" clicking = False" | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "0BXQVXEWPU4w", | ||
"colab_type": "code", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"img = cv2.imread(imgpath,1)" | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "zqyAtiSdPU4z", | ||
"colab_type": "code", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"cv2.namedWindow('Microaneurysm_Labelling')\n", | ||
"cv2.setMouseCallback('Microaneurysm_Labelling', click_and_zoom)\n", | ||
"\n", | ||
"height, width, channels = img.shape\n", | ||
"missalignment = width-height\n", | ||
"offset = int(missalignment/2)\n", | ||
"\n", | ||
"radiusXFilter = 5\n", | ||
"radiusYFilter = 5\n", | ||
"\n", | ||
"radiusXCrop = 20\n", | ||
"radiusYCrop = 20\n", | ||
"\n", | ||
"radiusXZoom = ((radiusXFilter/radiusXCrop) * scale[0]) /2\n", | ||
"radiusYZoom = ((radiusYFilter/radiusYCrop) * scale[0]) /2\n", | ||
"\n", | ||
"if(missalignment < 0):\n", | ||
" print(\"ERROR: negative missalignment\")\n", | ||
"if(missalignment % 2 == 1):\n", | ||
" print(\"ERROR: Uneven missalignment\")\n", | ||
" \n", | ||
"\n", | ||
"\n", | ||
"img = img[0:height, offset:(width-offset)]\n", | ||
"img = cv2.resize(img, scale)\n", | ||
"bimg = img.copy()\n", | ||
"zimg = img.copy()\n", | ||
"\n", | ||
"while True:\n", | ||
" \n", | ||
" if not zoomed:\n", | ||
" img = bimg.copy()\n", | ||
" else:\n", | ||
" img = zimg.copy()\n", | ||
" \n", | ||
" centerX,centerY = cropcenter\n", | ||
" \n", | ||
" #TODO: Check Borders\n", | ||
" \n", | ||
" if not zoomed:\n", | ||
" minXFilter,maxXFilter = int(centerX-radiusXFilter), int(centerX+radiusXFilter)\n", | ||
" minYFilter,maxYFilter = int(centerY-radiusYFilter), int(centerY+radiusYFilter)\n", | ||
" else:\n", | ||
" minXFilter,maxXFilter = int(centerX-radiusXZoom), int(centerX+radiusXZoom)\n", | ||
" minYFilter,maxYFilter = int(centerY-radiusYZoom), int(centerY+radiusYZoom)\n", | ||
" \n", | ||
" \n", | ||
" \n", | ||
" imgRec = cv2.rectangle(img, (minXFilter,minYFilter), (maxXFilter,maxYFilter), (0,255,0), 2)\n", | ||
" \n", | ||
" cv2.imshow('Microaneurysm_Labelling', imgRec)\n", | ||
" \n", | ||
" key = cv2.waitKeyEx(1) #& 0xFF\n", | ||
" \n", | ||
" #TODO: Zoom functionality\n", | ||
" \n", | ||
" if key == ord('i'):\n", | ||
" print('i')\n", | ||
" zoomed = True\n", | ||
"\n", | ||
" #TODO: Check Borders\n", | ||
" minXCrop, maxXCrop = centerX - radiusXCrop, centerX + radiusXCrop\n", | ||
" minYCrop, maxYCrop = centerY - radiusYCrop, centerY + radiusYCrop\n", | ||
" img = bimg.copy()\n", | ||
" img = img[minYCrop:maxYCrop, minXCrop:maxXCrop]\n", | ||
" img = cv2.resize(img, scale)\n", | ||
" \n", | ||
" zimg = img.copy()\n", | ||
" \n", | ||
" bopcenter = cropcenter\n", | ||
" cropcenter = (scale[0]/2, scale[1]/2)\n", | ||
" \n", | ||
" \n", | ||
" if key == ord('o'):\n", | ||
" print('o')\n", | ||
" zoomed = False\n", | ||
" \n", | ||
" img = bimg.copy()\n", | ||
" cropcenter = bopcenter\n", | ||
" \n", | ||
" if key == ord('r'):\n", | ||
" print('r')\n", | ||
" minXFilter,maxXFilter = int(centerX-radiusXFilter), int(centerX+radiusXFilter)\n", | ||
" minYFilter,maxYFilter = int(centerY-radiusYFilter), int(centerY+radiusYFilter)\n", | ||
" \n", | ||
" reference = bimg[minYFilter:maxYFilter, minXFilter:maxXFilter]\n", | ||
" \n", | ||
" library.append(reference)\n", | ||
" \n", | ||
" if key == 2424832: #Left on Windows\n", | ||
" print('Left')\n", | ||
" cropcenter = cropcenter[0]-1, cropcenter[1]\n", | ||
" \n", | ||
" if key == 2555904: #Right on Windows\n", | ||
" print('Right')\n", | ||
" cropcenter = cropcenter[0]+1, cropcenter[1]\n", | ||
" \n", | ||
" if key == 2490368: #Up on Windows\n", | ||
" print('Up')\n", | ||
" cropcenter = cropcenter[0], cropcenter[1]-1\n", | ||
" \n", | ||
" if key == 2621440: #Down on Windows\n", | ||
" print('Down')\n", | ||
" cropcenter = cropcenter[0], cropcenter[1]+1\n", | ||
" \n", | ||
" if key == 27: #Esc\n", | ||
" break\n", | ||
" \n", | ||
" #Use this code to check for correct keycodes on your system \n", | ||
" #if(key == -1):\n", | ||
" # continue\n", | ||
" #else:\n", | ||
" # print(key)\n", | ||
" \n", | ||
"with open('referenceLib.pkl', 'wb') as f:\n", | ||
" pickle.dump(library, f)\n", | ||
" \n", | ||
"cv2.destroyAllWindows()" | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "tJTY_VVOPU44", | ||
"colab_type": "code", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"" | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
} | ||
] | ||
} |