{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "99452ae4-dcb5-4256-9d77-8e3aebbdd5a6",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Label 0 is 103\n",
      "Label 1 is 103\n"
     ]
    }
   ],
   "source": [
    "cluster_label_list = [0, 1, 0, 0, 1, 0, 1]\n",
    "gt_global_id_list = [105, 103, 103, 103, 105, 103]\n",
    "\n",
    "# Remove the last element from the cluster_label_list\n",
    "cluster_label_list = cluster_label_list[:-1]\n",
    "\n",
    "# Create a dictionary to store the count of connections between cluster_label_list and gt_global_id_list\n",
    "connection_count = {}\n",
    "\n",
    "# Count the connections\n",
    "for label, gt_id in zip(cluster_label_list, gt_global_id_list):\n",
    "    if (label, gt_id) not in connection_count:\n",
    "        connection_count[(label, gt_id)] = 1\n",
    "    else:\n",
    "        connection_count[(label, gt_id)] += 1\n",
    "\n",
    "# Find the most frequent gt_global_id for each label\n",
    "label_mapping = {}\n",
    "for key, value in connection_count.items():\n",
    "    label, gt_id = key\n",
    "    if label not in label_mapping or value > label_mapping[label][1]:\n",
    "        label_mapping[label] = (gt_id, value)\n",
    "\n",
    "# Print the results\n",
    "for label, (gt_id, count) in label_mapping.items():\n",
    "    print(f\"Label {label} is {gt_id}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "cd5f93d6-91f3-4494-8388-7fa76001e596",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: (103, 3), 1: (103, 1)}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a336cac3-7205-4c06-9861-127f1682ca51",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "103"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_mapping[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8be948b2-f0dc-4fee-a4d2-14ca9698445d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "dic = {1:5,3:5,6:7}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "19ca7ad2-abf3-4214-9981-a775fa3092ca",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(1, 5), (3, 5), (6, 7)])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "88ff7cb0-da81-4408-86f5-d48f13069d08",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "temp = [1,1,2,2,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fcf47be6-8a59-44ce-9d8b-160c1bbe4c39",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(set(temp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "314bb2e1-92f3-46b2-8d2c-68af204cd8c1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "217b3901-ff72-44f2-a7a2-8f491d6b83aa",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "pts1 = np.array([(1753, 515), (1348, 411),(869, 289),(476, 231),(624, 227),(1093, 166)])\n",
    "pts2 = np.array([(1065, 197), (1065, 342),(1065, 562),(1108, 753),(1065, 709),(858, 562)])\n",
    "H_c124, _ = cv2.findHomography(pts1, pts2, cv2.RANSAC)\n",
    "\n",
    "pts1 = np.array([(176,447),(692,251),(1074,156),(400,147),(635,80),(929,161)])\n",
    "pts2 = np.array([(1065,562),(1065,342),(1108,153),(858,342),(861,197),(1065,197)])\n",
    "H_c125, _ = cv2.findHomography(pts1, pts2, cv2.RANSAC)\n",
    "\n",
    "pts1 = np.array([(566, 488), (968, 548), (718,487), (1495,674), (1210, 449),(1413,477),(1305,320),(175,898)])\n",
    "pts2 = np.array([(817, 153), (861, 342), (861,197), (861,562), (1065, 342),(1040,452),(1400,200),(520,350)])\n",
    "H_c126, _ = cv2.findHomography(pts1, pts2, cv2.RANSAC,5.0)\n",
    "\n",
    "pts1 = np.array([(508,388),(220,605),(760,471),(1007,410),(1151,410),(323,233),(408,233)])\n",
    "pts2 = np.array([(1065,562),(858,342),(858,562),(858,709),(817,753),(1598,709),(1554,753)])\n",
    "H_c127, _ = cv2.findHomography(pts1, pts2, cv2.RANSAC)\n",
    "\n",
    "pts1 = np.array([(117, 858), (66, 588), (198, 488),(1219, 251),(1361, 265)])\n",
    "pts2 = np.array([(858, 562), (858, 709),(817, 753),(369, 753),(325, 709)])\n",
    "H_c128, _ = cv2.findHomography(pts1, pts2, cv2.RANSAC)\n",
    "\n",
    "pts1 = np.array([(30,756),(601,697),(1284,623),(1638,584),(1033,505),(926,350),(1006,355), (1854,604)])\n",
    "pts2 = np.array([(1065,709),(1065,562),(1065,342),(1065,197),(858,342),(325,197),(367,153), (1108,153)])\n",
    "H_c129, _ = cv2.findHomography(pts1, pts2, cv2.RANSAC, 5.0)\n",
    "\n",
    "\n",
    "H_list = [H_c124,H_c125,H_c126,H_c127,H_c128,H_c129]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a18c2e1f-d6bc-416f-ae14-09d5a0ef5460",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "with open('homography_list.pkl', 'wb') as file:\n",
    "    pickle.dump(H_list, file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6a4c8613-734f-4d20-9673-1368efdf2d76",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[-4.52505060e-01,  3.82315632e+00,  9.66338329e+02],\n",
      "       [-6.04459275e-01,  2.00799407e-01,  1.35207631e+03],\n",
      "       [-6.89094155e-05,  2.19783671e-03,  1.00000000e+00]]), array([[ 7.86471877e-01,  4.67320753e+00,  3.14394485e+02],\n",
      "       [-4.37559122e-01,  2.36817836e+00,  3.59907453e+02],\n",
      "       [ 2.17446666e-04,  3.01644503e-03,  1.00000000e+00]]), array([[-7.56730557e-01, -1.23343965e+00,  3.75910101e+02],\n",
      "       [-7.07884558e-01, -2.14129577e+00,  1.46552760e+03],\n",
      "       [-6.19893085e-04, -2.84338999e-03,  1.00000000e+00]]), array([[-2.21313864e+00,  2.68022161e+00,  6.57702807e+03],\n",
      "       [ 2.04772657e+00,  1.09358246e+00,  1.96174657e+03],\n",
      "       [-7.18194062e-05,  1.32293195e-02,  1.00000000e+00]]), array([[-5.75210932e-01,  1.76868213e+00,  8.29890216e+02],\n",
      "       [-3.04538335e-01, -1.75768583e-02,  1.54421697e+03],\n",
      "       [ 5.65581305e-05,  1.92408506e-03,  1.00000000e+00]]), array([[-5.05246083e-01, -1.29951152e+01,  4.41194639e+03],\n",
      "       [ 1.20498645e+00, -5.33882350e+00,  3.86470146e+02],\n",
      "       [-4.04171058e-05, -8.06272677e-03,  1.00000000e+00]])]\n"
     ]
    }
   ],
   "source": [
    "with open('homography_list.pkl', 'rb') as file:\n",
    "    loaded_list = pickle.load(file)\n",
    "\n",
    "print(loaded_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1eed5eb3-111b-4fb9-bb1e-0f3d6e894f31",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "scit",
   "language": "python",
   "name": "scit"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
