{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":21755,"databundleVersionId":1475600,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# i have implemented a Monet-style effect using the Contrast Limited Adaptive Histogram Equalization (CLAHE) algorithm.","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport os\nimport shutil\nimport matplotlib.pyplot as plt\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Remove the existing directory if it exists","metadata":{}},{"cell_type":"code","source":"\nif os.path.exists(\"../images\"):\n    shutil.rmtree(\"../images\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-09T13:05:07.029532Z","iopub.execute_input":"2024-05-09T13:05:07.029911Z","iopub.status.idle":"2024-05-09T13:05:07.342646Z","shell.execute_reply.started":"2024-05-09T13:05:07.029880Z","shell.execute_reply":"2024-05-09T13:05:07.341539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a directory to save generated images","metadata":{}},{"cell_type":"code","source":"\nos.makedirs(\"../images\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-09T13:05:09.316798Z","iopub.execute_input":"2024-05-09T13:05:09.317396Z","iopub.status.idle":"2024-05-09T13:05:09.320975Z","shell.execute_reply.started":"2024-05-09T13:05:09.317363Z","shell.execute_reply":"2024-05-09T13:05:09.320187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Path to the directory containing the test images","metadata":{}},{"cell_type":"code","source":"\nimage_path = \"/kaggle/input/gan-getting-started/photo_jpg/\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-09T13:05:10.989216Z","iopub.execute_input":"2024-05-09T13:05:10.990091Z","iopub.status.idle":"2024-05-09T13:05:10.995358Z","shell.execute_reply.started":"2024-05-09T13:05:10.990056Z","shell.execute_reply":"2024-05-09T13:05:10.994313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# List all images in the directory","metadata":{}},{"cell_type":"code","source":"\nimage_list = os.listdir(image_path)\nprint(f\"Number of images: {len(image_list)}\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-09T13:05:12.981193Z","iopub.execute_input":"2024-05-09T13:05:12.981557Z","iopub.status.idle":"2024-05-09T13:05:12.989350Z","shell.execute_reply.started":"2024-05-09T13:05:12.981529Z","shell.execute_reply":"2024-05-09T13:05:12.988452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function to apply Monet-style effect to an image","metadata":{}},{"cell_type":"code","source":"# Function to apply Monet-style effect to an image\ndef apply_monet_style(image):\n    # Convert image to LAB color space\n    lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)\n    \n    # Split LAB image into channels\n    l, a, b = cv2.split(lab)\n    \n    # Apply CLAHE (Contrast Limited Adaptive Histogram Equalization) to enhance contrast\n    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))\n    cl = clahe.apply(l)\n    \n    # Merge the enhanced L channel with the original A and B channels\n    limg = cv2.merge((cl,a,b))\n    \n    # Convert back to BGR color space\n    final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)\n    \n    return final","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-09T13:05:15.109539Z","iopub.execute_input":"2024-05-09T13:05:15.109899Z","iopub.status.idle":"2024-05-09T13:05:15.116317Z","shell.execute_reply.started":"2024-05-09T13:05:15.109871Z","shell.execute_reply":"2024-05-09T13:05:15.115181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate Monet-style images","metadata":{}},{"cell_type":"code","source":"generated_images = []\nfor idx, image_name in enumerate(image_list):\n    full_path = os.path.join(image_path, image_name)\n    \n    # Read the image\n    img = cv2.imread(full_path)\n    \n    # Apply Monet-style effect\n    monet_style_img = apply_monet_style(img)\n    \n    # Save the generated image\n    cv2.imwrite(f\"../images/{idx}.jpg\", monet_style_img)\n    \n    # Append the generated image to the list\n    generated_images.append(monet_style_img)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-09T13:05:17.709025Z","iopub.execute_input":"2024-05-09T13:05:17.709413Z","iopub.status.idle":"2024-05-09T13:06:04.776820Z","shell.execute_reply.started":"2024-05-09T13:05:17.709381Z","shell.execute_reply":"2024-05-09T13:06:04.775503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Display some generated images","metadata":{}},{"cell_type":"code","source":"    \nnum_images_to_display = 5\nfor i in range(num_images_to_display):\n    plt.subplot(1, num_images_to_display, i+1)\n    plt.imshow(cv2.cvtColor(generated_images[i], cv2.COLOR_BGR2RGB))\n    plt.axis('off')\nplt.show()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-09T13:06:26.040501Z","iopub.execute_input":"2024-05-09T13:06:26.040978Z","iopub.status.idle":"2024-05-09T13:06:26.300250Z","shell.execute_reply.started":"2024-05-09T13:06:26.040945Z","shell.execute_reply":"2024-05-09T13:06:26.298726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a zip archive of the generated images","metadata":{}},{"cell_type":"code","source":"\nshutil.make_archive(\"/kaggle/working/images\", 'zip', \"/kaggle/images\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-09T13:06:30.016444Z","iopub.execute_input":"2024-05-09T13:06:30.017039Z","iopub.status.idle":"2024-05-09T13:06:42.509710Z","shell.execute_reply.started":"2024-05-09T13:06:30.017006Z","shell.execute_reply":"2024-05-09T13:06:42.508108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create histograms to analyze pixel intensities","metadata":{}},{"cell_type":"code","source":"\nplt.figure(figsize=(12, 6))\nplt.subplot(1, 2, 1)\nplt.hist(img.ravel(), bins=256, color='blue', alpha=0.7, label='Original Image')\nplt.hist(monet_style_img.ravel(), bins=256, color='red', alpha=0.7, label='Monet-style Image')\nplt.title('Pixel Intensity Histogram')\nplt.xlabel('Pixel Intensity')\nplt.ylabel('Frequency')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T13:08:42.616073Z","iopub.execute_input":"2024-05-09T13:08:42.616835Z","iopub.status.idle":"2024-05-09T13:08:46.129133Z","shell.execute_reply.started":"2024-05-09T13:08:42.616796Z","shell.execute_reply":"2024-05-09T13:08:46.128085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create scatter plots to analyze color distributions","metadata":{}},{"cell_type":"code","source":"\nplt.subplot(1, 2, 2)\nplt.scatter(img[:,:,0].ravel(), img[:,:,1].ravel(), color='blue', alpha=0.5, label='Original Image')\nplt.scatter(monet_style_img[:,:,0].ravel(), monet_style_img[:,:,1].ravel(), color='red', alpha=0.5, label='Monet-style Image')\nplt.title('Color Distribution Scatter Plot')\nplt.xlabel('Blue Channel')\nplt.ylabel('Green Channel')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T13:08:42.616073Z","iopub.execute_input":"2024-05-09T13:08:42.616835Z","iopub.status.idle":"2024-05-09T13:08:46.129133Z","shell.execute_reply.started":"2024-05-09T13:08:42.616796Z","shell.execute_reply":"2024-05-09T13:08:46.128085Z"},"trusted":true},"execution_count":null,"outputs":[]}]}