{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":20270,"databundleVersionId":1222630,"sourceType":"competition"}],"dockerImageVersionId":29985,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Remove unnecessary parts from the images Focus on what we have to diagnose\n\n","metadata":{}},{"cell_type":"markdown","source":"Removing unnecessary skin and focus on the melanoma cells ","metadata":{}},{"cell_type":"markdown","source":"You can pass the and process images directly with  help of ImageDataGenerator of tensorflow utilizing Argument  preprocessing_function\n\n\ntf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=crop_and_zoom)","metadata":{}},{"cell_type":"code","source":"from scipy import ndimage\nimport operator\nimport cv2\nimport numpy as np \nimport os \nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt ","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:32:32.589705Z","iopub.execute_input":"2025-01-26T16:32:32.589953Z","iopub.status.idle":"2025-01-26T16:32:32.884792Z","shell.execute_reply.started":"2025-01-26T16:32:32.589927Z","shell.execute_reply":"2025-01-26T16:32:32.883967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_paths=os.listdir('../input/siim-isic-melanoma-classification/jpeg/train')\nimage_paths= [\"../input/siim-isic-melanoma-classification/jpeg/train/\" + str(x) for x in image_paths]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:32:32.886860Z","iopub.execute_input":"2025-01-26T16:32:32.887080Z","iopub.status.idle":"2025-01-26T16:32:34.300000Z","shell.execute_reply.started":"2025-01-26T16:32:32.887059Z","shell.execute_reply":"2025-01-26T16:32:34.299304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def resize_and_crop_to_square(img, target_size):\n    \"\"\"\n    Resize ảnh theo cạnh nhỏ hơn và cắt thành hình vuông từ phần trung tâm.\n    \n    Args:\n        img : Ảnh đầu vào (numpy array).\n        target_size : Kích thước mong muốn của hình vuông đầu ra.\n    \n    Returns:\n        result : Ảnh sau khi resize và cắt thành hình vuông.\n    \"\"\"\n    # Lấy kích thước ảnh gốc\n    height, width = img.shape[:2]\n\n    # Tính tỷ lệ resize dựa trên cạnh nhỏ hơn\n    if width < height:\n        resize_ratio = target_size / width\n        new_width = target_size\n        new_height = int(height * resize_ratio)\n    else:\n        resize_ratio = target_size / height\n        new_height = target_size\n        new_width = int(width * resize_ratio)\n    \n    # Resize ảnh theo tỷ lệ\n    resized_img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_AREA)\n\n    # Tính tọa độ để cắt hình vuông từ trung tâm\n    x1 = (new_width - target_size) // 2\n    y1 = (new_height - target_size) // 2\n    x2 = x1 + target_size\n    y2 = y1 + target_size\n\n    # Cắt hình vuông từ phần trung tâm của ảnh đã resize\n    cropped_img = resized_img[y1:y2, x1:x2]\n\n    return cropped_img","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:32:34.301138Z","iopub.execute_input":"2025-01-26T16:32:34.301447Z","iopub.status.idle":"2025-01-26T16:32:34.309390Z","shell.execute_reply.started":"2025-01-26T16:32:34.301418Z","shell.execute_reply":"2025-01-26T16:32:34.308687Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def crop_and_zoom(img):\n    return resize_and_crop_to_square(img,256)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:34:43.858070Z","iopub.execute_input":"2025-01-26T16:34:43.858436Z","iopub.status.idle":"2025-01-26T16:34:43.862823Z","shell.execute_reply.started":"2025-01-26T16:34:43.858390Z","shell.execute_reply":"2025-01-26T16:34:43.861761Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualization ","metadata":{}},{"cell_type":"code","source":"example_image='../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0368894.jpg'\nz=plt.imread(example_image)\nplt.imshow(z)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:34:46.236414Z","iopub.execute_input":"2025-01-26T16:34:46.236752Z","iopub.status.idle":"2025-01-26T16:34:46.603705Z","shell.execute_reply.started":"2025-01-26T16:34:46.236722Z","shell.execute_reply":"2025-01-26T16:34:46.602904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(crop_and_zoom(z))\nplt.imsave(\"example1.png\",z)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:34:51.095599Z","iopub.execute_input":"2025-01-26T16:34:51.095917Z","iopub.status.idle":"2025-01-26T16:34:52.018142Z","shell.execute_reply.started":"2025-01-26T16:34:51.095890Z","shell.execute_reply":"2025-01-26T16:34:52.017474Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"example_2=\"../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0094775.jpg\"\nz2=plt.imread(example_2)\nplt.imshow(z2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:35:00.492681Z","iopub.execute_input":"2025-01-26T16:35:00.492984Z","iopub.status.idle":"2025-01-26T16:35:02.976929Z","shell.execute_reply.started":"2025-01-26T16:35:00.492958Z","shell.execute_reply":"2025-01-26T16:35:02.975982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(crop_and_zoom(z2))\nplt.imsave(\"example2.png\",z2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:35:05.161657Z","iopub.execute_input":"2025-01-26T16:35:05.161951Z","iopub.status.idle":"2025-01-26T16:35:12.063194Z","shell.execute_reply.started":"2025-01-26T16:35:05.161927Z","shell.execute_reply":"2025-01-26T16:35:12.062437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"example_3=\"../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0166988.jpg\"\nz3=plt.imread(example_3)\nplt.imshow(z3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:35:19.995777Z","iopub.execute_input":"2025-01-26T16:35:19.996080Z","iopub.status.idle":"2025-01-26T16:35:20.369695Z","shell.execute_reply.started":"2025-01-26T16:35:19.996054Z","shell.execute_reply":"2025-01-26T16:35:20.368566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(crop_and_zoom(z3))\nplt.imsave(\"example3.png\",z3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:35:23.797420Z","iopub.execute_input":"2025-01-26T16:35:23.797741Z","iopub.status.idle":"2025-01-26T16:35:24.936532Z","shell.execute_reply.started":"2025-01-26T16:35:23.797714Z","shell.execute_reply":"2025-01-26T16:35:24.935756Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Generate Images\n\nYou can run the follwing function if you want to generate new images with the given croping and zoom ","metadata":{}},{"cell_type":"code","source":"def generate_images(imagelist):\n    for x in imagelist:\n        img_name=x.split(sep='/')[-1]\n        img= plt.imread(x)\n        img=crop_and_zoom(img)\n        plt.imsave(img_name,img)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:28:27.731614Z","iopub.execute_input":"2025-01-26T16:28:27.731836Z","iopub.status.idle":"2025-01-26T16:28:27.736599Z","shell.execute_reply.started":"2025-01-26T16:28:27.731813Z","shell.execute_reply":"2025-01-26T16:28:27.735713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"generate_images(image_paths)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-26T16:28:27.738302Z","iopub.execute_input":"2025-01-26T16:28:27.738662Z","execution_failed":"2025-01-26T16:32:10.747Z"}},"outputs":[],"execution_count":null}]}