{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Cropping microscope images \nI didn't find any public notebook on cropping microscope images, so I decided to write it myself. If you have any ideas on how to improve it or have questions feel free to write about it in comments. Please upvote if you find it useful. Peace .)\n","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"scrolled":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"def crop_microscope(img_to_crop):\n    pad_y = img_to_crop.shape[0]//200 \n    pad_x = img_to_crop.shape[1]//200\n    img = img_to_crop[pad_y:-pad_y, pad_y:-pad_y,:]\n    '''\ncropping 0.5% from every side, because some microscope images\nhave frames along the edges so cv2.boundingRect crops by frame, \nbut not by needed part of the image.\n    '''\n    \n    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n    _,thresh = cv2.threshold(gray,50,255,cv2.THRESH_BINARY) \n    x,y,w,h = cv2.boundingRect(thresh) #getting crop points\n    \n#since we cropped borders we need to uncrop it back \n    if y!=0: \n        y = y+pad_y\n    if h == thresh.shape[0]:\n        h = h+pad_y\n    if x !=0:\n        x = x +pad_x\n    if w == thresh.shape[1]:\n        w = w + pad_x\n    h = h+pad_y\n    w = w + pad_x\n    crop = img_to_crop[y:y+h,x:x+w]\n    return crop","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true},"cell_type":"code","source":"#some examples\npathes = [\n    '../input/siim-isic-melanoma-classification/jpeg/test/ISIC_0073502.jpg',\n    '../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0170285.jpg',\n    '../input/melanoma-merged-external-data-512x512-jpeg/512x512-dataset-melanoma/512x512-dataset-melanoma/ISIC_0072042.jpg',\n    '../input/melanoma-merged-external-data-512x512-jpeg/512x512-dataset-melanoma/512x512-dataset-melanoma/ISIC_0063521.jpg',\n    '../input/siim-isic-melanoma-classification/jpeg/test/ISIC_0197440.jpg',\n    '../input/siim-isic-melanoma-classification/jpeg/test/ISIC_0112420.jpg',\n    '../input/siim-isic-melanoma-classification/jpeg/test/ISIC_0336093.jpg',\n    '../input/siim-isic-melanoma-classification/jpeg/test/ISIC_0371907.jpg',\n    '../input/siim-isic-melanoma-classification/jpeg/test/ISIC_0591142.jpg',\n    '../input/siim-isic-melanoma-classification/jpeg/test/ISIC_0874437.jpg',\n    '../input/siim-isic-melanoma-classification/jpeg/test/ISIC_1089351.jpg',\n]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false},"cell_type":"code","source":"plt.figure(figsize = (20,70))\nfor i in range(len(pathes)):\n    img = cv2.imread(pathes[i])\n    plt.subplot(len(pathes), 2, i*2+1)\n    plt.imshow(crop_microscope(img))\n    plt.title(f'Cropped shape: {crop_microscope(img).shape}', fontsize=20)\n    plt.subplot(len(pathes), 2, i*2+2)\n    plt.imshow(img)\n    plt.title(f'Original shape: {img.shape}', fontsize=20);","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}