{"cells":[{"metadata":{},"cell_type":"markdown","source":"長軸位置合わせについて\\\nhttps://www.jstage.jst.go.jp/article/fss/31/0/31_379/_pdf/-char/ja\\\n\n\"*ただ回転処理によってサン\nプル数の拡張を行うよりも，メラノーマの形状特徴をよ\nり顕著に反映する腫瘍領域の長軸の位置合わせ処理を行\nうほうが，DCNN を用いたメラノーマの識別において\nは有効であると考えられる．*\n\"","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nfrom matplotlib import pyplot as plt\n%matplotlib inline\nfrom PIL import Image","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#各種パス設定\nBASEPATH = \"../input/siim-isic-melanoma-classification\"\nJPEGPATH = \"jpeg/train/\"\ndf_train = pd.read_csv(os.path.join(BASEPATH, 'train.csv'))\ndf_test  = pd.read_csv(os.path.join(BASEPATH, 'test.csv'))\ndf_sub   = pd.read_csv(os.path.join(BASEPATH, 'sample_submission.csv'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"参考(体毛除去はできなかったので輪郭抽出のみ)\\\nhttps://ja.coder.work/so/python/905564","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def cut_hokuro(img):\n    kernel = np.ones((15,15),np.uint8)\n\n    # クロージング(穴埋め)\n    closing = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel, iterations = 2)\n    #15×15箱フィルタによる平均\n    blur = cv2.blur(closing,(15,15))\n\n    # グレスケ化\n    gray = cv2.cvtColor(blur,cv2.COLOR_BGR2GRAY)\n    # 二値化\n    _, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)\n\n\n    # ほくろの輪郭情報取得\n    contours, hierarchy =  cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)\n    cnt = max(contours, key=cv2.contourArea)\n\n    # 幅と高さ取得\n    h, w = img.shape[:2]\n    mask = np.zeros((h, w), np.uint8)\n\n    # 輪郭の描画\n    cv2.drawContours(mask, [cnt],-1, 255, -1)\n    res = cv2.bitwise_and(img, img, mask=mask)\n    \n    # 対象を楕円で囲んだ時の中心点,長軸短軸,傾きを取得\n    _,_,angle = cv2.fitEllipse(cnt)\n    scale=1\n    angle=angle+90\n    \n    return res,angle","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base = []\nconvert = []\nrotate = []\nfor i in range(0,50):\n    img = cv2.imread(os.path.join(os.path.join(BASEPATH,JPEGPATH),df_train[\"image_name\"][i]+\".jpg\"))\n    img = cv2.resize(img,(256,256)) \n    base.append(img)\n    #中心部分を抜き出して輪郭&角度取得\n    cut_image,angle = cut_hokuro(img[64:192,64:192])\n    #cut_image,angle = cut_hokuro(img[74:182,74:182])\n    #cut_image,angle = cut_hokuro(img)\n    convert.append(cut_image)\n    rotate.append(angle)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i=1\nfig = plt.figure(figsize=(10,10))\nfor base_image,hokuro_image in zip(base,convert):\n    plt.subplot(10,10,i)\n    base_image = cv2.cvtColor(base_image,cv2.COLOR_BGR2RGB)\n    plt.imshow(base_image)\n    plt.axis(\"off\")\n    plt.subplot(10,10,i+1)\n    hokuro_image = cv2.cvtColor(hokuro_image,cv2.COLOR_BGR2RGB)\n    plt.imshow(hokuro_image)\n    i+=2\n    plt.axis(\"off\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i=1\nfig = plt.figure(figsize=(10,10))\n\n#画像の幅と高さ\nwidth = 256\nheight = 256\n\n#画像の中心\ncenter = (128,128)\n#スケール\nscale = 1\n\nfor base_image,angle in zip(base,rotate):\n    base_image = cv2.cvtColor(base_image,cv2.COLOR_BGR2RGB)\n    trans = cv2.getRotationMatrix2D(center,angle,scale)\n    rotate_image = cv2.warpAffine(base_image,trans,(width,height))\n    \n    plt.subplot(10,10,i)\n    plt.imshow(base_image)\n    plt.axis(\"off\")\n    plt.subplot(10,10,i+1)\n    plt.imshow(rotate_image)\n    i+=2\n    plt.axis(\"off\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}