Skip to content

Instantly share code, notes, and snippets.

@hiroto-takatoshi
Last active October 3, 2017 18:24
Show Gist options
  • Save hiroto-takatoshi/2db8f4a7c858a51652050a6bc8493542 to your computer and use it in GitHub Desktop.
Save hiroto-takatoshi/2db8f4a7c858a51652050a6bc8493542 to your computer and use it in GitHub Desktop.
stable version, tells you coordinates, direction
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"724.5 357.5 296.992\n",
"742.5 375.5 292.623\n",
"722.5 375.5 309.714\n",
"1273.5 624.5\n",
"(1919, 6406) (0, -10754)\n",
"[-0.11119268] [ 0.99379885]\n",
"[[ 353.61593628]]\n",
"1273.5 624.5\n",
"(1919, 6406) (0, -10754)\n",
"[-0.11119268] [ 0.99379885]\n",
"[[ 353.61593628]]\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from numpy.linalg import norm\n",
"\n",
"img_rgb = cv2.imread('mapa.jpg')\n",
"\n",
"for x in range(0,420):\n",
" for y in range(0,1080):\n",
" img_rgb[y,x] = (0,0,0)\n",
" img_rgb[y,1919-x] = (0,0,0)\n",
" \n",
"res = cv2.bilateralFilter(img_rgb,9,80,80)\n",
"\n",
"res = cv2.GaussianBlur(res,(5,5),0)\n",
"\n",
"res = cv2.bilateralFilter(res,9,80,80)\n",
"\n",
"res = cv2.GaussianBlur(res,(5,5),0)\n",
"\n",
"cv2.imwrite('res34.png',res)\n",
"\n",
"#%run ./mul.py\n",
"\n",
"#'''\n",
"gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)\n",
"\n",
"\n",
"result = cv2.Canny(gray, 200, 20)\n",
"#result = cv2.Laplacian(res,cv2.CV_64F)\n",
"\n",
"img_none = cv2.imread('empty.png')\n",
"\n",
"#print(type(result))\n",
"\n",
"for x in range(0,1920):\n",
" for y in range(0,1080):\n",
" #y,x\n",
" if all(result[y,x] == (255,255,255)) and all(img_none[y,x] == (255,255,255)):\n",
" result[y,x] -= result[y,x]\n",
" \n",
"\n",
"cv2.imwrite('res33.png',result)\n",
"\n",
"result1 = cv2.imread('res33.png')\n",
"result = cv2.cvtColor(result1, cv2.COLOR_BGR2GRAY)\n",
"param2_t = 120\n",
"flag = False\n",
"circles = None\n",
"while not flag :\n",
" param2_t -= 5\n",
" circles = cv2.HoughCircles(result, cv2.HOUGH_GRADIENT, dp=1, minDist=10, param1=80, param2=param2_t)\n",
" \n",
" try:\n",
" for (x,y,r) in circles[0]:\n",
" if r > 330 :\n",
" pass\n",
" elif r < 250 :\n",
" pass\n",
" else :\n",
" flag = True\n",
" except TypeError :\n",
" pass\n",
"#circles\n",
"output = result1.copy()\n",
"for (x,y,r) in circles[0]:\n",
" \n",
" if r > 330 :\n",
" pass\n",
" elif r < 250 :\n",
" pass\n",
" \n",
" #f 0 :\n",
" # pass\n",
" else :\n",
" cv2.circle(output, (x,y), r, (0, 255, 0), 2)\n",
" print (x,y,r)\n",
"cv2.imwrite('res666.png',output)\n",
"\n",
"hist = cv2.imread('res33.png')\n",
"timg = cv2.cvtColor(hist, cv2.COLOR_BGR2GRAY)\n",
"\n",
"ret, contours, hierarchy = cv2.findContours(timg.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE )\n",
"\n",
"def dist(p1,p2,p3):\n",
" return norm(np.cross(p2-p1, p1-p3))/norm(p2-p1)\n",
"\n",
"for _ in contours:\n",
" (x,y),radius = cv2.minEnclosingCircle(_)\n",
" if radius > 12 and radius < 16:\n",
" #cv2.drawContours(hist, _, -1,( 0,255, 0),1)\n",
" #(x,y),radius = cv2.minEnclosingCircle(_)\n",
" #print(x,y)\n",
" flag = False\n",
" for k in contours:\n",
" if k is not _ :\n",
" (x1,y1),radius1 = cv2.minEnclosingCircle(k)\n",
" if (x1-x)**2 + (y1-y)**2 < radius**2 and radius1 > 6 and radius1 < 7 :\n",
" flag = True\n",
" \n",
" print(x1,y1)\n",
" \n",
" hull = cv2.convexHull(k)\n",
" xx = (hull[0][0][0] + hull[-1][0][0]) / 2\n",
" yy = (hull[0][0][1] + hull[-1][0][1]) / 2\n",
" pt1 = (int(xx),int(yy))\n",
" pt0 = (int(x1), int(y1))\n",
" \n",
" #cv2.drawContours(hist, [hull], -1, (0, 0, 255), 1)\n",
" cv2.arrowedLine(hist, pt0, pt1, (255,0,0), 1)\n",
" \n",
" \n",
" rows,cols = hist.shape[:2]\n",
" [vx,vy,x,y] = cv2.fitLine(k, cv2.DIST_L2,0,0.01,0.01)\n",
" lefty = int((-x*vy/vx) + y)\n",
" righty = int(((cols-x)*vy/vx)+y)\n",
" print((cols-1,righty),(0,lefty))\n",
" #hist = cv2.line(hist,(cols-1,righty),(0,lefty),(0,255,0),2)\n",
" \n",
" #y = yy - y1\n",
" #x = xx - x1\n",
" \n",
" angle = np.arctan2([y],[x])/np.pi*180\n",
" dlist = [dist(np.array([cols-1,righty]),np.array([0,lefty]),np.array(i)) for i in hull]\n",
" dindex = (dlist.index(max(dlist)))\n",
" dirvec = np.array(hull[dindex]) - np.array([x1,y1])\n",
" linevec = np.array([cols-1,righty]) - np.array([0,lefty])\n",
" if np.dot(linevec.copy(), dirvec[0]) < 0:\n",
" linevec = -linevec\n",
" vx = -vx\n",
" print(vx,vy) \n",
" lefty = int((-x*vy/vx) + y)\n",
" righty = int(((cols-x)*vy/vx)+y)\n",
" hist = cv2.arrowedLine(hist,(cols-1,righty),(0,lefty),(0,255,0),3)\n",
" angle = np.arctan2([vy],[vx])/np.pi*180\n",
" angle = 90 - angle\n",
" angle += 360\n",
" if angle >= 360 :\n",
" angle -= 360\n",
" print(angle)\n",
" \n",
" \n",
" if flag:\n",
" cv2.drawContours(hist, _, -1,( 0,255, 0),1)\n",
" \n",
"cv2.imwrite('res1234.png',hist)"
]
},
{
"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.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment