{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"pip install imutils","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from imutils import perspective\nfrom imutils import contours\nimport imutils\nfrom scipy.spatial import distance as dist\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport cv2\nimport math\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/jpeg-melanoma-256x256/train.csv\")\nimage_name_arr = train[\"image_name\"].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=6, figsize=(15,15), gridspec_kw={'wspace':0.1, 'hspace':0})\nj=0\nran = 4352\nfor i in range(ran+6):\n  if i>ran-1:\n    im = cv2.imread(\"../input/jpeg-melanoma-256x256/train/\"+image_name_arr[i]+\".jpg\")\n    im = HAIR_SORRY_REMOVE(im)\n    im = detecting_nevus(im)\n    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)\n    ax[j].imshow(im)\n    ax[j].axis(\"off\")\n    j+=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def HAIR_SORRY_REMOVE(image, clip_hist_percent=1):\n    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n    kernel = cv2.getStructuringElement(1,(17,17))\n    blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel)\n    _,threshold = cv2.threshold(blackhat,10,255,cv2.THRESH_BINARY)\n    final_image = cv2.inpaint(image,threshold,1,cv2.INPAINT_TELEA)\n    return (final_image)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def midpoint(ptA, ptB):\n\treturn ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)\ndef detecting_nevus(img_start):\n  # load img and Blur\n  img_start2 = cv2.GaussianBlur(img_start, ( 17, 17 ), 0)\n  # img_start2 = cv2.blur(img_start,(10,10))\n  Z = img_start2.reshape((-1,3))\n  Z = np.float32(Z)\n  criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)\n  K = 2\n  ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)\n  center = np.uint8(center)\n  thresh = center[label.flatten()]\n  thresh = thresh.reshape((img_start.shape))\n  # plt.imshow(img)\n\n  thresh = cv2.cvtColor( thresh, cv2.COLOR_BGR2GRAY)\n\n  thresh = cv2.Canny( thresh, 50, 60)\n  kernel = np.ones((3,3),np.uint8)\n  thresh = cv2.dilate( thresh, kernel, iterations=1)\n\n\n  contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n  im2 = cv2.drawContours(img_start.copy(), contours,-1, (0, 255, 0), 3)\n  orig = img_start.copy()\n  S = list()\n  for c in contours:\n    box = cv2.minAreaRect(c)\n    box = cv2.cv.BoxPoints(box)  if imutils.is_cv2() else cv2.boxPoints( box)\n    box = np.array(box, dtype=\"int\")\n    box = perspective.order_points( box)\n    center = 256/2\n    delta = 70\n    ( tl, tr, br, bl) = box\n    (centerX, centerY) = midpoint( tl, br)\n    if (centerX>center-delta and centerX<center+delta) and (centerY>center-delta and centerY<center+delta):\n          if cv2.contourArea(c) >  70 and cv2.contourArea(c) <  20000:\n            S.append(cv2.contourArea(c))\n  if len(S)==0:\n    for c in contours:\n      box = cv2.minAreaRect(c)\n      box = cv2.cv.BoxPoints(box)  if imutils.is_cv2() else cv2.boxPoints( box)\n      box = np.array(box, dtype=\"int\")\n      box = perspective.order_points( box)\n      center = 256/2\n      delta = 60\n      ( tl, tr, br, bl) = box\n      (centerX, centerY) = midpoint( tl, br)\n      if (centerX>center-delta and centerX<center+delta) and (centerY>center-delta and centerY<center+delta):\n        if cv2.contourArea(c) >  40 and cv2.contourArea(c) <  20000:\n          S.append(cv2.contourArea(c))\n  if len(S) == 0:\n     return img_start\n    \n  if len(S)>0:\n    for c in contours:\n\n          \n      if cv2.contourArea(c) ==  max(S):\n        box = cv2.minAreaRect(c)\n        box = cv2.cv.BoxPoints(box)  if imutils.is_cv2() else cv2.boxPoints( box)\n        box = np.array(box, dtype=\"int\")\n        box = perspective.order_points( box)\n        orig = cv2.drawContours(orig, [box.astype(\"int\")] , -1 , ( 0 , 255 , 0 ) , 2)\n        for ( x, y)  in box:\n          cv2.circle(orig, (int(x), int(y)) , 5 , ( 0 , 0 , 255 ) , -1)\n        ( tl, tr, br, bl) = box\n        \n        (tltrX, tltrY) = midpoint( tl, tr)\n        ( blbrX, blbrY) = midpoint( bl, br)\n        ( tlblX, tlblY) = midpoint( tl, bl)\n        ( trbrX, trbrY) = midpoint( tr, br)\n\n        ( centerXX, centerYY) = midpoint( tl, br)\n        # draw the midpoints on the image\n        dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))\n        cv2.circle(orig, (int(blbrX), int(blbrY)) , 5 , ( 255 , 0 , 0 ) , -1)\n        cv2.circle(orig, (int(tlblX), int( tlblY)) , 5 , ( 255 , 0 , 0 ) , -1)\n        cv2.circle(orig, (int(trbrX), int(trbrY)) , 5 , ( 255 , 0 , 0 ) , -1)\n      # # draw lines between the midpoints\n        cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),(255, 0, 255), 2)\n        cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),(255, 0, 255), 2)\n        dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))\n        dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))\n        cv2.putText( orig, \" {:.1f}in\".format(dA),(int(tltrX - 15), int( tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,0.65, (255, 255, 255), 2)\n        cv2.putText( orig, \" {:.1f}in\".format(dB),(int(trbrX + 10), int( trbrY)), cv2.FONT_HERSHEY_SIMPLEX,0.65, (255, 255, 255), 2)\n        return orig","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}