{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c2776d5b-cf50-d5bc-e0d1-045c70d8375c"},"outputs":[],"source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input/Train\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"0b3bc9c3-e23b-e25f-bd27-f323836e1202"},"outputs":[],"source":"#Parameters\nmargin=100\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"19697a73-fc7d-126f-929b-eb1fbf544dcf"},"outputs":[],"source":"img = cv2.cvtColor(cv2.imread(\"../input/TrainDotted/3.jpg\"), cv2.COLOR_BGR2RGB)\ncoordCrop=np.zeros((2,3))#Array coord crop\n\n# Mask everything but the red dots.\ncmsk = cv2.inRange(img, np.array([160, 0, 0]), np.array([255, 50, 50])) # Get the red -ish stuff.\n# Find the circles in the masked image.\ncircles = cv2.HoughCircles(cmsk,cv2.HOUGH_GRADIENT,1,50, param1=40,param2=1,minRadius=0,maxRadius=25)\n\nif circles is not None:\n    circles = np.int16(np.around(circles))\n    circles=circles[0,:]\n    print('%d circles found.' % len(circles))\n    for i in circles:\n        cv2.rectangle(img, (i[0] - 50, i[1] - 50), (i[0] + 50, i[1] + 50), (255, 0, 0), 3)\n    #Coordonate for croping with margin\n    coordCrop[0]=np.amin(circles,axis=0)-100\n    coordCrop[1]=np.amax(circles,axis=0)+100\n    coordCrop[coordCrop<0]=0 #Set to 0 all negative value\n    if coordCrop[1,0]>img.shape[1]:\n        coordCrop[1,0]=img.shape[1]\n    if coordCrop[1,1]>img.shape[0]:\n        coordCrop[1,1]=img.shape[0]\ncoordCrop=np.int16(coordCrop)#convert to int\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"494cc194-a3f3-4cd6-8752-8e83f58e012b"},"outputs":[],"source":"crop_img = img[coordCrop[0,1]:coordCrop[1,1],coordCrop[0,0]:coordCrop[1,0]]\n# NOTE: its img[y: y + h, x: x + w] and *not* img[x: x + w, y: y + h]\n#cv2.imshow(\"cropped\", crop_img)\nplt.imshow(crop_img, cmap = 'gray', interpolation = 'bicubic')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d6db5188-2a5c-bf65-c6dd-98da38bbdb3e"},"outputs":[],"source":"img.shape[1] "}],"metadata":{"_change_revision":0,"_is_fork":false,"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.0"}},"nbformat":4,"nbformat_minor":0}