{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Whale/Dolphin Dorsal Fin Image Binarization","metadata":{"papermill":{"duration":0.004195,"end_time":"2023-03-15T17:10:43.550448","exception":false,"start_time":"2023-03-15T17:10:43.546253","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg","metadata":{"papermill":{"duration":0.232211,"end_time":"2023-03-15T17:10:43.786193","exception":false,"start_time":"2023-03-15T17:10:43.553982","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T07:09:32.815315Z","iopub.execute_input":"2023-03-21T07:09:32.815694Z","iopub.status.idle":"2023-03-21T07:09:32.821161Z","shell.execute_reply.started":"2023-03-21T07:09:32.815660Z","shell.execute_reply":"2023-03-21T07:09:32.820170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read image\nimg = cv2.imread('/kaggle/input/happy-whale-and-dolphin/train_images/00103cbe9d25ce.jpg')\nprint(img.shape)\n\n# grayscale conversion\ngray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\nprint(gray_img.shape)\n\n# binarization\nthreshold_value =50\nmax_value = 255\nthreshold_type = cv2.THRESH_BINARY\n_, binary_img = cv2.threshold(gray_img, threshold_value, max_value, threshold_type)\nprint(binary_img.shape)","metadata":{"papermill":{"duration":0.06535,"end_time":"2023-03-15T17:10:43.855040","exception":false,"start_time":"2023-03-15T17:10:43.789690","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T07:09:32.823214Z","iopub.execute_input":"2023-03-21T07:09:32.823640Z","iopub.status.idle":"2023-03-21T07:09:32.972552Z","shell.execute_reply.started":"2023-03-21T07:09:32.823601Z","shell.execute_reply":"2023-03-21T07:09:32.971293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\nplt.title('Pixel Value Distribution')\n\nplt.subplot(1, 2, 1)\nplt.hist(gray_img.ravel(), 256, [0, 256])\nplt.axvline(x=threshold_value, color='r')\nplt.title('Grayscale Image')\n\nplt.subplot(1, 2, 2)\nplt.hist(binary_img.ravel(), 256, [0, 256])\nplt.title('Binarized Image')\n\nplt.suptitle('Pixel Value Distribution', fontsize=20)\nplt.show()","metadata":{"papermill":{"duration":1.155041,"end_time":"2023-03-15T17:10:45.013598","exception":false,"start_time":"2023-03-15T17:10:43.858557","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T07:09:32.973837Z","iopub.execute_input":"2023-03-21T07:09:32.974161Z","iopub.status.idle":"2023-03-21T07:09:34.509790Z","shell.execute_reply.started":"2023-03-21T07:09:32.974128Z","shell.execute_reply":"2023-03-21T07:09:34.508659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1 = img\nimg2 = gray_img\nimg3 = binary_img\n\nfig, axs = plt.subplots(1,3,figsize=(12,4))\nfig.suptitle('Binarization of Image')\n\naxs[0].imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))\naxs[1].imshow(img2, cmap='gray')\naxs[2].imshow(img3)\n\nfor ax in axs:\n    ax.set_xticks([])\n    ax.set_yticks([])\n\nplt.show()","metadata":{"papermill":{"duration":0.285222,"end_time":"2023-03-15T17:10:45.302899","exception":false,"start_time":"2023-03-15T17:10:45.017677","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T07:09:34.511199Z","iopub.execute_input":"2023-03-21T07:09:34.511914Z","iopub.status.idle":"2023-03-21T07:09:37.000788Z","shell.execute_reply.started":"2023-03-21T07:09:34.511873Z","shell.execute_reply":"2023-03-21T07:09:36.999722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\ndef create_animation(ims):\n    fig=plt.figure(figsize=(4,4))\n    im=plt.imshow(cv2.cvtColor(ims[5],cv2.COLOR_BGR2RGB))\n    text = plt.text(0.05, 0.05, f'Slide {0}', transform=fig.transFigure, fontsize=14)\n    plt.close()    \n    def animate_func(i):\n        im.set_array(cv2.cvtColor(ims[i],cv2.COLOR_BGR2RGB))\n        text.set_text(f'Slide {i}')\n        return [im]\n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000//3)","metadata":{"papermill":{"duration":0.023501,"end_time":"2023-03-15T17:10:46.698942","exception":false,"start_time":"2023-03-15T17:10:46.675441","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T07:09:37.003455Z","iopub.execute_input":"2023-03-21T07:09:37.003778Z","iopub.status.idle":"2023-03-21T07:09:37.011531Z","shell.execute_reply.started":"2023-03-21T07:09:37.003747Z","shell.execute_reply":"2023-03-21T07:09:37.010281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,255,5):\n    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    threshold_value =i\n    max_value = 255\n    threshold_type = cv2.THRESH_BINARY\n    _, binary_img = cv2.threshold(gray_img, threshold_value, max_value, threshold_type) \n    mpimg.imsave(str(i).zfill(3) + '.png', binary_img)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:09:37.013004Z","iopub.execute_input":"2023-03-21T07:09:37.014067Z","iopub.status.idle":"2023-03-21T07:10:16.958026Z","shell.execute_reply.started":"2023-03-21T07:09:37.014016Z","shell.execute_reply":"2023-03-21T07:10:16.956859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths=[]\nfor dirname, _, filenames in os.walk('/kaggle/working/'):\n    for filename in filenames:\n        if filename[-4:]=='.png':\n            paths+=[os.path.join(dirname, filename)]     \npaths=sorted(paths)\nimages=[]\nfor i in range(len(paths)):\n    images+=[cv2.imread(paths[i])]","metadata":{"papermill":{"duration":0.015741,"end_time":"2023-03-15T17:10:46.720237","exception":false,"start_time":"2023-03-15T17:10:46.704496","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T07:10:16.959566Z","iopub.execute_input":"2023-03-21T07:10:16.960329Z","iopub.status.idle":"2023-03-21T07:10:23.828988Z","shell.execute_reply.started":"2023-03-21T07:10:16.960278Z","shell.execute_reply":"2023-03-21T07:10:23.827974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images)","metadata":{"papermill":{"duration":4.460698,"end_time":"2023-03-15T17:10:51.474086","exception":false,"start_time":"2023-03-15T17:10:47.013388","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-03-21T07:10:23.830197Z","iopub.execute_input":"2023-03-21T07:10:23.830539Z","iopub.status.idle":"2023-03-21T07:11:25.558588Z","shell.execute_reply.started":"2023-03-21T07:10:23.830506Z","shell.execute_reply":"2023-03-21T07:11:25.557406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,255,5):\n    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    threshold_value = i\n    max_value = 255\n    threshold_type = cv2.THRESH_BINARY\n    _, binary_img = cv2.threshold(gray_img, threshold_value, max_value, threshold_type) \n    mpimg.imsave(str(i).zfill(3) + '.png', binary_img)","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:11:25.560007Z","iopub.execute_input":"2023-03-21T07:11:25.560392Z","iopub.status.idle":"2023-03-21T07:12:06.353106Z","shell.execute_reply.started":"2023-03-21T07:11:25.560349Z","shell.execute_reply":"2023-03-21T07:12:06.351872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=gray_img\nimg=(img/np.max(img)*255).astype('uint8')\nfor i in range(10):\n    i2= i*10\n    j2= i2+40\n    mask = np.logical_and(img >= i2, img <= j2)\n    new_img = np.zeros_like(img)\n    new_img[mask] = img[mask]\n    print((i2,j2))\n    plt.figure(figsize=(4,4))\n    plt.imshow(new_img)\n    plt.axis(\"off\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-21T07:12:06.354705Z","iopub.execute_input":"2023-03-21T07:12:06.355482Z","iopub.status.idle":"2023-03-21T07:12:13.575437Z","shell.execute_reply.started":"2023-03-21T07:12:06.355431Z","shell.execute_reply":"2023-03-21T07:12:13.574264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.025925,"end_time":"2023-03-15T17:10:51.527036","exception":false,"start_time":"2023-03-15T17:10:51.501111","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.025467,"end_time":"2023-03-15T17:10:51.577993","exception":false,"start_time":"2023-03-15T17:10:51.552526","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}