Skip to content
This repository was archived by the owner on Jun 21, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions groep6/DSmeetup_waterdetectie.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Detectie van water\n",
"\n",
"\n",
"Schaal van plaatjes: 1 pixel is 10x10cm \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"# Load image to np-data frame (function from tiff.py)\n",
"\n",
"def img2df(path):\n",
" ''' read an image into a dataframe '''\n",
" img = Image.open(path)\n",
" img.size = (1024, 1024)\n",
" #img.show() # to see image\n",
" arr = np.array(img.getdata())\n",
" df = pd.DataFrame(arr)\n",
" return df\n",
"\n",
"# Specify coordinates and load image:\n",
"coords = '163600.0_563400.0'\n",
"\n",
"# read image\n",
"img = img2df('tegels100x100/data/%s.tif' % coords)\n",
"\n",
"img.columns = ['R', 'G', 'B']\n",
"\n",
"# img.shape = (1048576, 3), which is length (1024x1024) by width 3 (RGB)\n",
"\n",
"# View \n",
"img.head()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using openCV\n",
"\n",
"\n",
"https://media.readthedocs.org/pdf/opencv-python-tutroals/latest/opencv-python-tutroals.pdf\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"\n",
"coords = '163600.0_563400.0'\n",
"\n",
"img = cv2.imread('tegels100x100/data/%s.tif' % coords)\n",
"imglabel = cv2.imread('tegels100x100/labels/WaterVlakkenRasterUitBGT_%s.tif' % coords)\n",
"\n",
"def show(image):\n",
" cv2.imshow('image %s.tif' %coords,image) \n",
" # Show image, press ESC to exit\n",
" k = cv2.waitKey(0)\n",
" if k == 27: # wait for ESC key to exit\n",
" cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"# To see the image:\n",
"show(img)\n",
"show(imglabel)\n",
"\n",
"# Get to the first pixels BGR values, get the green value: (NOTE: order is here BGR instead of RGB)\n",
"img[0,0], img[0,0,1]\n",
"\n",
"# Take red color out:\n",
"#img[:,:,2] =0\n"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"# Compare image with its label:\n",
"\n",
"img1 = img\n",
"img2 = imglabel\n",
"dst = cv2.addWeighted(img1,0.5,img2,0.5,0)\n",
"cv2.imshow('dst',dst)\n",
"\n",
"cv2.imwrite('overlapwithlabel.png',dst)\n",
"\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}