Last active
October 3, 2017 01:44
-
-
Save hiroto-takatoshi/d47408eb6f61eb360d9ae51f1c630e52 to your computer and use it in GitHub Desktop.
current pos and dir detection
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from PIL import Image" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"img = Image.open('map1.bmp', 'r')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"IOPub data rate exceeded.\n", | |
"The notebook server will temporarily stop sending output\n", | |
"to the client in order to avoid crashing it.\n", | |
"To change this limit, set the config variable\n", | |
"`--NotebookApp.iopub_data_rate_limit`.\n" | |
] | |
} | |
], | |
"source": [ | |
"img" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"a=list(img.getdata())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"104650" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"len(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import cv2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 103, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"img_rgb = cv2.imread('source/7.jpg')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 104, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"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)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 112, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'numpy.ndarray'>\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 112, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"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", | |
"#result = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)\n", | |
"\n", | |
"#'''\n", | |
"#result = cv2.imread('res33.png')\n", | |
"#circles = cv2.HoughCircles(result, cv2.HOUGH_GRADIENT, 1, 40)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 113, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1100.5 833.5 275.348\n", | |
"1117.5 853.5 273.065\n", | |
"1084.5 864.5 308.436\n", | |
"1099.5 859.5 292.151\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 113, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"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)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 114, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1178.5 853.5 212.59\n", | |
"1100.5 833.5 275.348\n", | |
"1117.5 853.5 273.065\n", | |
"1084.5 864.5 308.436\n", | |
"1099.5 859.5 292.151\n", | |
"1026.5 808.5 234.479\n", | |
"1105.5 881.5 335.634\n", | |
"1101.78571429 850.642857143\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 114, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"output = img_rgb.copy()\n", | |
"for (x,y,r) in circles[0]:\n", | |
" '''\n", | |
" if r > 330 :\n", | |
" pass\n", | |
" elif r < 250 :\n", | |
" pass\n", | |
" '''\n", | |
" if 0 :\n", | |
" pass\n", | |
" else :\n", | |
" cv2.circle(output, (x,y), r, (0, 255, 0), 2)\n", | |
" print (x,y,r)\n", | |
"xsum = 0.0\n", | |
"ysum = 0.0\n", | |
"for (x,y,r) in circles[0]:\n", | |
" xsum+=x\n", | |
" ysum+=y\n", | |
"xsum/=len(circles[0])\n", | |
"ysum/=len(circles[0])\n", | |
"print(xsum,ysum)\n", | |
"cv2.circle(output, (int(xsum),int(ysum)), 306, (0, 0, 255), 4)\n", | |
"cv2.imwrite('res44.png',output)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 175, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"img_3333 = cv2.imread('res33.png')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 191, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"res3333 = cv2.GaussianBlur(img_3333,(15,15),0)\n", | |
"\n", | |
"res3333 = gray = cv2.cvtColor(res3333, cv2.COLOR_BGR2GRAY)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 194, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"circles = cv2.HoughCircles(res3333, cv2.HOUGH_GRADIENT, 1, 40)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 195, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"circles" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 185, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 185, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"cv2.imwrite('res3334.png',res3333)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 125, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 125, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\n", | |
"cv2.imwrite('res33.png',result)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"temp = cv2.imread('temp.bmp')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"res = cv2.matchTemplate(img_rgb,temp,cv2.TM_CCOEFF_NORMED)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[-0.01178034, -0.01962119, -0.02975524, ..., -0.038022 ,\n", | |
" -0.03149711, -0.02691574],\n", | |
" [-0.01594876, -0.02231932, -0.03238218, ..., -0.0259725 ,\n", | |
" -0.01762902, -0.01573199],\n", | |
" [-0.04249095, -0.04583302, -0.05361817, ..., -0.01204339,\n", | |
" 0.00043032, 0.00436186],\n", | |
" ..., \n", | |
" [ 0.11109395, 0.12578124, 0.14391096, ..., -0.02656269,\n", | |
" 0.00156921, 0.07241241],\n", | |
" [ 0.15072724, 0.16819492, 0.18066916, ..., 0.02713473,\n", | |
" 0.07004382, 0.12981163],\n", | |
" [ 0.1916292 , 0.20746163, 0.21573578, ..., 0.11084837,\n", | |
" 0.15856628, 0.17211723]], dtype=float32)" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"res" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"threshold = 0.7" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"loc = np.where( res >= threshold)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(array([17, 17, 18, 18]), array([70, 71, 70, 71]))" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"loc" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from matplotlib import pyplot as plt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(3, 24, 39)" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\n", | |
"temp.shape[::-1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"for pt in zip(*loc[::-1]):\n", | |
" cv2.rectangle(img_rgb, pt, (pt[0] + 24, pt[1] + 39), (0,0,255), 2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"cv2.imwrite('res1.png',img_rgb)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import cv2\n", | |
"timg = cv2.imread('res3333.bmp')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"c1 = 0\n", | |
"c2 = 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for (y,x) in [(x,y) for x in range(32) for y in range(31)]:\n", | |
" if all(timg[x,y] == (255,255,255)):\n", | |
" c1 +=1\n", | |
" c2 += 1\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"404" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"c1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1984" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"c2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.20362903225806453" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"c1/c2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"hist=cv2.equalizeHist(cv2.cvtColor(timg, cv2.COLOR_BGR2GRAY))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import matplotlib.pyplot as plt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 155, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAADfCAYAAAD/aAQpAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFUxJREFUeJzt3X+wXGddx/H3x4QW+VGSFNqpSTBBItpxRhsyJciPcSiW\npiKpChoGp5kaJ+MMKIiOBJkR1H+sP6h2YMpEWkmZSosFphkHhEzoiH/Y2qQt/UEouRRoLgkJkFLQ\nOkDk6x/7bHpys3fv7p6zZ59zzuc1s3N3n3v27rNn934/z/Ps2V1FBGZm1j0/NusOmJnZbDgAzMw6\nygFgZtZRDgAzs45yAJiZdZQDwMyso2oPAElXSHpE0pykXXXfvpmZ9ajO9wFIWgZ8CfhlYB64B3hj\nRHyhtk6YmRlQ/wzgUmAuIh6NiB8AtwJba+6DmZkBy2u+vdXAkcLleeAli20syW9TNjMb37ci4nlL\nbVR3AGhA2xlFXtJOYGc93TEza6WvjbJR3QEwD6wtXF4DHC1uEBG7gd3gGYCZ2TTV/RrAPcAGSesl\nnQNsA/bW3Aczy5g/oLI+tc4AIuKUpLcAnwaWATdFxMN19sHM8iaJiEAatGJsVar1MNBxeQnIzGwi\nByNi01Ib+Z3AZmYd5Q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k9\nsIHeiz519feZkp7dP0/vBcCHUr/6R59sB+4o9PfqdATLZuCJ/vJGzc4YQeW6fwt9GGdffhq4XNLK\ntKRxeWqrhaQrgHcAr4uIJwvtz5O0LJ1/Ab19+Wjq8/ckbU7P/6sL93HafR33cc+hZrwa+GJEnF7a\nyXHfnqHuV51HPdE7kuJL9BLzXRn05+X0pmgPAPen05XAh4EHU/te4KLCdd6V+v8INb/CT+9oiM+n\n08P9fQicD+wHDqefq1K7gPen/j4IbJrBPn4G8G3gOYW2LPYvvVA6BvyQ3uhtxyT7kt7a+1w6XVNz\nf+forZP3n78fSNv+RnqOfB64F/jVwt/ZRK/4fhl4H+nNozX0dezHva6aMai/qf1DwO8t2Ham+3ap\nk98JbGbWUbkuAZmZ2ZQ5AMzMOsoBYGbWUQ4AM7OOcgCYmXWUA8DMrKMcAGZmHeUAMDPrqP8Hq2BI\nGT5jt8sAAAAASUVORK5CYII=\n", | |
"text/plain": [ | |
"<matplotlib.figure.Figure at 0x20133ca84e0>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"plt.imshow(hist)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 153, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"hist = cv2.imread('res33.png')\n", | |
"timg = cv2.cvtColor(hist, cv2.COLOR_BGR2GRAY)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 156, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 156, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"cv2.imwrite('res1234.png',hist)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"hist=cv2.GaussianBlur(timg,(15,15),0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 116, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ret, contours, hierarchy = cv2.findContours(timg.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE )\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[0, 0, 0, ..., 0, 0, 0],\n", | |
" [0, 0, 0, ..., 0, 0, 0],\n", | |
" [0, 0, 0, ..., 0, 0, 0],\n", | |
" ..., \n", | |
" [0, 0, 0, ..., 0, 0, 0],\n", | |
" [0, 0, 0, ..., 0, 0, 0],\n", | |
" [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ret" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 126, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"contours= 1462 hierarchy= 1\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"contours=\",len(contours), \"hierarchy=\",len(hierarchy) )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 154, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"6.670932292938232\n", | |
"6.670932292938232\n" | |
] | |
} | |
], | |
"source": [ | |
"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 radius1 > 4 and radius1 < 8 and (x1-x)**2 + (y1-y)**2 < radius**2:\n", | |
" flag = True\n", | |
" print(radius1)\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", | |
" hist = cv2.line(hist,(cols-1,righty),(0,lefty),(0,255,0),2)\n", | |
" if flag:\n", | |
" cv2.drawContours(hist, _, -1,( 0,255, 0),1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"14.922653198242188" | |
] | |
}, | |
"execution_count": 75, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"radius" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(850.1032104492188, 180.1234893798828)" | |
] | |
}, | |
"execution_count": 77, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(x,y)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 98, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"center = (int(x),int(y))\n", | |
"radius = int(radius)\n", | |
"hist = cv2.circle(hist,center,radius,(0,255,0),3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.0\n", | |
"0.0\n", | |
"0.5\n", | |
"0.0\n", | |
"1.5\n", | |
"1.5\n", | |
"0.0\n", | |
"0.5\n", | |
"5.5\n", | |
"11.5\n", | |
"14.0\n" | |
] | |
} | |
], | |
"source": [ | |
"for _ in contours:\n", | |
" area = cv2.contourArea(_)\n", | |
" print(area)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" ..., \n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0]],\n", | |
"\n", | |
" [[0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" ..., \n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0]],\n", | |
"\n", | |
" [[0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" ..., \n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0]],\n", | |
"\n", | |
" ..., \n", | |
" [[0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" ..., \n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0]],\n", | |
"\n", | |
" [[0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" ..., \n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0]],\n", | |
"\n", | |
" [[0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" ..., \n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0],\n", | |
" [0, 0, 0]]], dtype=uint8)" | |
] | |
}, | |
"execution_count": 90, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"cv2.drawContours(hist, contours[8], -1,( 0,255, 0),1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 95, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"rows,cols = hist.shape[:2]\n", | |
"[vx,vy,x,y] = cv2.fitLine(contours[8], cv2.DIST_L2,0,0.01,0.01)\n", | |
"lefty = int((-x*vy/vx) + y)\n", | |
"righty = int(((cols-x)*vy/vx)+y)\n", | |
"hist = cv2.line(hist,(cols-1,righty),(0,lefty),(0,255,0),2)" | |
] | |
}, | |
{ | |
"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