{"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":"# Clip Dorsal Fins from Image","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-09-26T13:20:08.885079Z","iopub.execute_input":"2023-09-26T13:20:08.885662Z","iopub.status.idle":"2023-09-26T13:20:08.895507Z","shell.execute_reply.started":"2023-09-26T13:20:08.885613Z","shell.execute_reply":"2023-09-26T13:20:08.894292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* imageR: RGB image\n* imageB: BGR image","metadata":{}},{"cell_type":"code","source":"path0='/kaggle/input/happy-whale-and-dolphin/train_images/0017b3749cd769.jpg'","metadata":{"execution":{"iopub.status.busy":"2023-09-26T13:20:08.899033Z","iopub.execute_input":"2023-09-26T13:20:08.899777Z","iopub.status.idle":"2023-09-26T13:20:08.912325Z","shell.execute_reply.started":"2023-09-26T13:20:08.899729Z","shell.execute_reply":"2023-09-26T13:20:08.910983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageR = plt.imread(path0)\nimageR= cv2.resize(imageR,dsize=None,fx=0.2,fy=0.2)\nimageR = imageR[:,:,0:3]\nprint(imageR.shape)\nimageB=cv2.cvtColor(imageR, cv2.COLOR_RGB2BGR)\nplt.figure(figsize=(10,4))\nplt.imshow(imageR)\n#plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-26T13:20:08.913609Z","iopub.execute_input":"2023-09-26T13:20:08.914316Z","iopub.status.idle":"2023-09-26T13:20:09.385131Z","shell.execute_reply.started":"2023-09-26T13:20:08.914282Z","shell.execute_reply":"2023-09-26T13:20:09.384018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image1 = cv2.cvtColor(imageB[250:260,380:390], cv2.COLOR_BGR2RGB)\nimage2 = cv2.cvtColor(imageB[220:230,340:350], cv2.COLOR_BGR2RGB)\nimage3 = cv2.cvtColor(imageB[260:270,340:350], cv2.COLOR_BGR2RGB)\nimageRR=imageR.copy()\ncv2.rectangle(imageRR,(380,250),(390,260), (255, 0, 0) ,2)\ncv2.rectangle(imageRR,(340,220),(350,230), (255, 0, 0) ,2)\ncv2.rectangle(imageRR,(340,260),(350,270), (255, 0, 0) ,2)\nplt.figure(figsize=(10,4))\nplt.imshow(imageRR)\n#plt.axis('off')\nplt.show()\n\nimage_list = [image1,image2,image3]\n\nconcatenated_image = np.concatenate(image_list, axis=1)\nplt.figure(figsize=(10, 2))\nplt.imshow(concatenated_image)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-26T13:20:09.386386Z","iopub.execute_input":"2023-09-26T13:20:09.386700Z","iopub.status.idle":"2023-09-26T13:20:09.794133Z","shell.execute_reply.started":"2023-09-26T13:20:09.386671Z","shell.execute_reply":"2023-09-26T13:20:09.792767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def judge_mean_color(image):\n    mean_red = np.mean(image[:,:,0])  \n    mean_green = np.mean(image[:,:,1])  \n    mean_blue = np.mean(image[:,:,2]) \n    \n    min_red = np.min(image[:,:,0])  \n    min_green = np.min(image[:,:,1])  \n    min_blue = np.min(image[:,:,2])   \n    \n    max_red = np.max(image[:,:,0])  \n    max_green = np.max(image[:,:,1])  \n    max_blue = np.max(image[:,:,2])     \n    \n    nc=[ np.array((int(round(mean_red,0)),int(round(mean_green,0)),int(round(mean_blue,0)))) ]\n    min_nc=[ np.array((int(round(min_red,0)),int(round(min_green,0)),int(round(min_blue,0)))) ]\n    max_nc=[ np.array((int(round(max_red,0)),int(round(max_green,0)),int(round(max_blue,0)))) ]\n    \n    print('mean',tuple(nc[0]),', min',tuple(min_nc[0]),', max',tuple(max_nc[0]))\n   \n    image_list = [[nc],[min_nc],[max_nc]]\n\n    concatenated_image = np.concatenate(image_list, axis=1)\n    plt.figure(figsize=(10, 2))\n    plt.imshow(concatenated_image)\n    plt.axis('off')\n    plt.show()\n    \njudge_mean_color(concatenated_image)   ","metadata":{"execution":{"iopub.status.busy":"2023-09-26T13:20:09.797177Z","iopub.execute_input":"2023-09-26T13:20:09.797706Z","iopub.status.idle":"2023-09-26T13:20:09.881294Z","shell.execute_reply.started":"2023-09-26T13:20:09.797664Z","shell.execute_reply":"2023-09-26T13:20:09.879882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lower_color = np.array([18, 18, 14])  # lower RGB value\nupper_color = np.array([88, 80, 77])  # upper RGB value\n\n# RGB variation\ncolor_variations = np.linspace(lower_color, upper_color, 100)\n\nfig, ax = plt.subplots(figsize=(10,5))\nax.imshow([color_variations.astype(int)])\nax.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-26T13:20:09.883270Z","iopub.execute_input":"2023-09-26T13:20:09.884055Z","iopub.status.idle":"2023-09-26T13:20:09.946773Z","shell.execute_reply.started":"2023-09-26T13:20:09.884009Z","shell.execute_reply":"2023-09-26T13:20:09.945488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageR=cv2.cvtColor(imageB, cv2.COLOR_BGR2RGB)\nmask = (imageR >= lower_color) & (imageR <= upper_color)\nmask = mask.all(axis=-1) ","metadata":{"execution":{"iopub.status.busy":"2023-09-26T13:20:09.949063Z","iopub.execute_input":"2023-09-26T13:20:09.949856Z","iopub.status.idle":"2023-09-26T13:20:09.987139Z","shell.execute_reply.started":"2023-09-26T13:20:09.949796Z","shell.execute_reply":"2023-09-26T13:20:09.986016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(imageR)\nplt.show()\n\ncropped_image = imageR.copy()\n#cropped_image[mask] = [255,255,255]  # non-mask region to white\ncropped_image[~mask] = [255,255,255]  # mask region to white\n\nplt.imshow(cropped_image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-26T13:20:09.992718Z","iopub.execute_input":"2023-09-26T13:20:09.993430Z","iopub.status.idle":"2023-09-26T13:20:10.689105Z","shell.execute_reply.started":"2023-09-26T13:20:09.993386Z","shell.execute_reply":"2023-09-26T13:20:10.688101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}