{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":91498,"databundleVersionId":11655853,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# # import os\n# # for dirname, _, filenames in os.walk('/kaggle/input'):\n# #     for filename in filenames:\n# #         print(os.path.join(dirname, filename))\n\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:18.132175Z","iopub.execute_input":"2025-05-06T18:58:18.132594Z","iopub.status.idle":"2025-05-06T18:58:18.137957Z","shell.execute_reply.started":"2025-05-06T18:58:18.132551Z","shell.execute_reply":"2025-05-06T18:58:18.136665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport glob\nimport os\nfrom sklearn.cluster import DBSCAN\nfrom sklearn.preprocessing import StandardScaler\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:18.139583Z","iopub.execute_input":"2025-05-06T18:58:18.139997Z","iopub.status.idle":"2025-05-06T18:58:18.166782Z","shell.execute_reply.started":"2025-05-06T18:58:18.139960Z","shell.execute_reply":"2025-05-06T18:58:18.165476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_directory_ETS='/kaggle/input/image-matching-challenge-2025/test/ETs'\ntrain_directory_ETS='/kaggle/input/image-matching-challenge-2025/train/ETs'\n\ntest_directory_stairs='/kaggle/input/image-matching-challenge-2025/test/stairs'\ntrain_directory_stairs='/kaggle/input/image-matching-challenge-2025/train/stairs'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:18.169144Z","iopub.execute_input":"2025-05-06T18:58:18.169626Z","iopub.status.idle":"2025-05-06T18:58:18.189323Z","shell.execute_reply.started":"2025-05-06T18:58:18.169584Z","shell.execute_reply":"2025-05-06T18:58:18.188020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_file_ETS=os.listdir(test_directory_ETS)\nprint(f'Number of image present in Test ETS {len(test_file_ETS)}')\nprint('*'*50)\ntrain_file_ETS=os.listdir(train_directory_ETS)\nprint(f'Number of image present in Train ETS {len(train_file_ETS)}')\nprint('*'*50)\ntest_file_stairs=os.listdir(test_directory_stairs)\nprint(f'Number of image present in test stairs {len(test_file_stairs)}')\nprint('*'*50)\ntrain_file_stairs=os.listdir(train_directory_stairs)\nprint(f'Number of image present in Train stairs {len(train_file_stairs)}')\nprint('+'*50)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:18.191373Z","iopub.execute_input":"2025-05-06T18:58:18.191838Z","iopub.status.idle":"2025-05-06T18:58:18.219599Z","shell.execute_reply.started":"2025-05-06T18:58:18.191797Z","shell.execute_reply":"2025-05-06T18:58:18.218390Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Image visualising**","metadata":{}},{"cell_type":"code","source":"img_train=mpimg.imread(\"/kaggle/input/image-matching-challenge-2025/train/ETs/another_et_another_et001.png\")\nplt.title('Train data ETS Image')\nimg_plot=plt.imshow(img_train)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:18.221051Z","iopub.execute_input":"2025-05-06T18:58:18.221420Z","iopub.status.idle":"2025-05-06T18:58:18.487699Z","shell.execute_reply.started":"2025-05-06T18:58:18.221387Z","shell.execute_reply":"2025-05-06T18:58:18.486225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_train=mpimg.imread(\"/kaggle/input/image-matching-challenge-2025/train/stairs/stairs_split_1_1710453576271.png\")\nplt.title('Train data Stairs Image')\nimg_plot=plt.imshow(img_train)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:18.489310Z","iopub.execute_input":"2025-05-06T18:58:18.489749Z","iopub.status.idle":"2025-05-06T18:58:19.118897Z","shell.execute_reply.started":"2025-05-06T18:58:18.489710Z","shell.execute_reply":"2025-05-06T18:58:19.117444Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Load and Extract image**","metadata":{}},{"cell_type":"code","source":"def load_image(path):\n    return cv2.imread(path, cv2.IMREAD_COLOR)\n\ndef extract_orb_features(image):\n    orb = cv2.ORB_create(nfeatures=1000)\n    keypoints, descriptors = orb.detectAndCompute(image, None)\n    return keypoints, descriptors\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:19.121720Z","iopub.execute_input":"2025-05-06T18:58:19.122112Z","iopub.status.idle":"2025-05-06T18:58:19.128758Z","shell.execute_reply.started":"2025-05-06T18:58:19.122081Z","shell.execute_reply":"2025-05-06T18:58:19.127292Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Match image in folder**","metadata":{}},{"cell_type":"code","source":"def match_orb_features(des1, des2):\n    bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)\n    matches = bf.match(des1, des2)\n    matches = sorted(matches, key=lambda x: x.distance)\n    return matches\n\ndef visualize_matches(img1, kp1, img2, kp2, matches, top_n=50):\n    matched_img = cv2.drawMatches(img1, kp1, img2, kp2, matches[:top_n], None, flags=2)\n    plt.figure(figsize=(12, 6))\n    plt.imshow(matched_img)\n    plt.axis('off')\n    plt.title(\"Top Image Matches\")\n    plt.show()\n\ndef match_images_in_folder(folder_path):\n    image_paths = sorted(glob.glob(os.path.join(folder_path, \"*.png\")))\n    \n    if len(image_paths) < 2:\n        print(\"Need at least two images to match.\")\n        return\n\n    img1 = load_image(image_paths[0])\n    img2 = load_image(image_paths[1])\n\n    kp1, des1 = extract_orb_features(img1)\n    kp2, des2 = extract_orb_features(img2)\n\n    if des1 is None or des2 is None:\n        print(\"Descriptors missing in one or both images.\")\n        return\n\n    matches = match_orb_features(des1, des2)\n    print(f\"Found {len(matches)} matches between the first two images.\")\n\n    visualize_matches(img1, kp1, img2, kp2, matches)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:19.130822Z","iopub.execute_input":"2025-05-06T18:58:19.131370Z","iopub.status.idle":"2025-05-06T18:58:19.152866Z","shell.execute_reply.started":"2025-05-06T18:58:19.131314Z","shell.execute_reply":"2025-05-06T18:58:19.151241Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Similar image test for stairs**","metadata":{}},{"cell_type":"code","source":"match_images_in_folder('/kaggle/input/image-matching-challenge-2025/test/stairs/')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:19.153887Z","iopub.execute_input":"2025-05-06T18:58:19.154260Z","iopub.status.idle":"2025-05-06T18:58:19.998579Z","shell.execute_reply.started":"2025-05-06T18:58:19.154226Z","shell.execute_reply":"2025-05-06T18:58:19.997006Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Similar image test for ETs**","metadata":{}},{"cell_type":"code","source":" match_images_in_folder('/kaggle/input/image-matching-challenge-2025/test/ETs/')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:20.000306Z","iopub.execute_input":"2025-05-06T18:58:20.000769Z","iopub.status.idle":"2025-05-06T18:58:20.364482Z","shell.execute_reply.started":"2025-05-06T18:58:20.000730Z","shell.execute_reply":"2025-05-06T18:58:20.362654Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Similar gardens test**","metadata":{}},{"cell_type":"code","source":"match_images_in_folder('/kaggle/input/image-matching-challenge-2025/train/amy_gardens/')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-06T18:58:20.366297Z","iopub.execute_input":"2025-05-06T18:58:20.366816Z","iopub.status.idle":"2025-05-06T18:58:20.898075Z","shell.execute_reply.started":"2025-05-06T18:58:20.366767Z","shell.execute_reply":"2025-05-06T18:58:20.896584Z"}},"outputs":[],"execution_count":null}]}