{"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":"# Image Matching 2023 rotation matrix","metadata":{}},{"cell_type":"markdown","source":"The rotation_matrix and translation_vector are matrices and vectors used to represent 3D geometric transformations. rotation_matrix and translation_vector are commonly used to compose 3D geometric transformations. For example, it is used when translating after rotating a 3D object. By combining these transformations, you can change the position and pose of 3D objects.\n\n## rotation_matrix: \nA rotation matrix is a matrix for rotating a point or object in 3D space. It is usually represented as a 3x3 square matrix. A rotation matrix is defined by an axis of rotation and an angle. Rotation matrices allow you to rotate 3D points and objects.\n\n## translation_vector: \nA translation vector is a vector for translating a point or object in 3D space. It is usually represented as a 3-dimensional column vector. Translation vectors describe the respective amounts of translation along the x-, y-, and z-axes. A translation vector allows you to translate a 3D point or object in a specified direction.\n\n","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:09.077933Z","iopub.execute_input":"2023-04-12T16:13:09.078332Z","iopub.status.idle":"2023-04-12T16:13:09.085049Z","shell.execute_reply.started":"2023-04-12T16:13:09.078298Z","shell.execute_reply":"2023-04-12T16:13:09.083592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels=pd.read_csv('/kaggle/input/image-matching-challenge-2023/train/train_labels.csv')\ndisplay(train_labels[0:3])","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:09.087059Z","iopub.execute_input":"2023-04-12T16:13:09.087438Z","iopub.status.idle":"2023-04-12T16:13:09.115986Z","shell.execute_reply.started":"2023-04-12T16:13:09.087394Z","shell.execute_reply":"2023-04-12T16:13:09.114603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=train_labels[train_labels['dataset']=='urban'][train_labels['scene']=='kyiv-puppet-theater']\ndisplay(data[0:3])\nprint(data.columns.tolist())","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:09.118680Z","iopub.execute_input":"2023-04-12T16:13:09.119497Z","iopub.status.idle":"2023-04-12T16:13:09.138701Z","shell.execute_reply.started":"2023-04-12T16:13:09.119442Z","shell.execute_reply":"2023-04-12T16:13:09.137247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# images","metadata":{}},{"cell_type":"code","source":"paths=[]\nfiles=[]\nfor dirname, _, filenames in os.walk('/kaggle/input/image-matching-challenge-2023/train/urban/kyiv-puppet-theater/images'):\n    for filename in filenames:\n        paths+=[(os.path.join(dirname, filename))]\n        files+=[filename]","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:09.140368Z","iopub.execute_input":"2023-04-12T16:13:09.141597Z","iopub.status.idle":"2023-04-12T16:13:09.151486Z","shell.execute_reply.started":"2023-04-12T16:13:09.141545Z","shell.execute_reply":"2023-04-12T16:13:09.150317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(5,6, figsize=(12,12))\nfor i, ax in enumerate(axs.flat):\n    if i < len(paths):\n        img = mpimg.imread(paths[i])\n        ax.imshow(img)\n    ax.axis('off') \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:09.154497Z","iopub.execute_input":"2023-04-12T16:13:09.155483Z","iopub.status.idle":"2023-04-12T16:13:16.568228Z","shell.execute_reply.started":"2023-04-12T16:13:09.155429Z","shell.execute_reply":"2023-04-12T16:13:16.567022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_paths=[]\nfull_files=[]\nfor dirname, _, filenames in os.walk('/kaggle/input/image-matching-challenge-2023/train/urban/kyiv-puppet-theater/images_full_set'):\n    for filename in filenames:\n        full_paths+=[(os.path.join(dirname, filename))]\n        full_files+=[filename]\nprint(len(files),len(full_files))\nprint(set(files) & set(full_files) == set(files))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:16.569467Z","iopub.execute_input":"2023-04-12T16:13:16.570524Z","iopub.status.idle":"2023-04-12T16:13:16.583694Z","shell.execute_reply.started":"2023-04-12T16:13:16.570473Z","shell.execute_reply":"2023-04-12T16:13:16.581669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"fig, axs = plt.subplots(5, 6, figsize=(10, 10))\nfor i, ax in enumerate(axs.flat):\n    if i < len(full_paths):\n        img = mpimg.imread(full_paths[i])\n        ax.imshow(img)\n    ax.axis('off') \nplt.show()","metadata":{}},{"cell_type":"markdown","source":"# train_labels info","metadata":{}},{"cell_type":"code","source":"dir0='/kaggle/input/image-matching-challenge-2023/train/'\nprint(data.iloc[0,2]) #'image_path'\nprint(data.iloc[0,3]) #'rotation_matrix'\nprint(data.iloc[0,4]) #'translation_vector'\npath0=dir0+data.iloc[0,2]\nimg = mpimg.imread(path0)\nplt.imshow(img)\nplt.axis('off') \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:16.585748Z","iopub.execute_input":"2023-04-12T16:13:16.586721Z","iopub.status.idle":"2023-04-12T16:13:17.085419Z","shell.execute_reply.started":"2023-04-12T16:13:16.586677Z","shell.execute_reply":"2023-04-12T16:13:17.084566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrixs=data.iloc[0,3].split(';')\nM=[]\nfor m in matrixs:\n    M+=[float(m)]\nrotation_matrix=np.array(M).reshape(3,3)\nprint(rotation_matrix)\n# [cos(theta_x), -sin(theta_x), 0]\n# [cos(theta_y), -sin(theta_y), 0] \n# [cos(theta_z), -sin(theta_z), 0]\n#theta_x represents the rotation angle around the x-axis.\n#theta_y represents the rotation angle around the y-axis.\n#theta_z represents the rotation angle around the z-axis.","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:17.086689Z","iopub.execute_input":"2023-04-12T16:13:17.087188Z","iopub.status.idle":"2023-04-12T16:13:17.094490Z","shell.execute_reply.started":"2023-04-12T16:13:17.087156Z","shell.execute_reply":"2023-04-12T16:13:17.093183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\nR=rotation_matrix\ntheta_x = math.atan2(R[2,1], R[2,2])\ntheta_y = math.asin(-R[2,0])\ntheta_z = math.atan2(R[1,0], R[0,0])\nprint(theta_x,theta_y,theta_z)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:17.097786Z","iopub.execute_input":"2023-04-12T16:13:17.098158Z","iopub.status.idle":"2023-04-12T16:13:17.106805Z","shell.execute_reply.started":"2023-04-12T16:13:17.098122Z","shell.execute_reply":"2023-04-12T16:13:17.105148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=rotation_matrix\n\nis_square = matrix.shape[0] == matrix.shape[1]\nis_orthogonal = np.allclose(np.dot(matrix, matrix.T), np.eye(3)) and np.allclose(np.linalg.norm(matrix, axis=0), 1)\nis_determinant_one = np.isclose(np.linalg.det(matrix), 1)\n\nis_rotation_matrix = is_square and is_orthogonal and is_determinant_one\n\nprint(\"is_rotation_matrix:\", is_rotation_matrix)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:17.108257Z","iopub.execute_input":"2023-04-12T16:13:17.108679Z","iopub.status.idle":"2023-04-12T16:13:17.119070Z","shell.execute_reply.started":"2023-04-12T16:13:17.108615Z","shell.execute_reply":"2023-04-12T16:13:17.117314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectors=data.iloc[0,4].split(';')\nV=[]\nfor v in vectors:\n    V+=[float(v)]\ntranslation_vector=np.array(V)\nprint(translation_vector)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:17.120735Z","iopub.execute_input":"2023-04-12T16:13:17.121445Z","iopub.status.idle":"2023-04-12T16:13:17.129321Z","shell.execute_reply.started":"2023-04-12T16:13:17.121402Z","shell.execute_reply":"2023-04-12T16:13:17.127934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# rotate image to create rotated image","metadata":{}},{"cell_type":"code","source":"#before\nplt.imshow(img)\nplt.axis('off') \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:17.130883Z","iopub.execute_input":"2023-04-12T16:13:17.131200Z","iopub.status.idle":"2023-04-12T16:13:17.578070Z","shell.execute_reply.started":"2023-04-12T16:13:17.131170Z","shell.execute_reply":"2023-04-12T16:13:17.577119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#theta_z represents the rotation angle around the z-axis.\nheight,width = img.shape[:2]\ntheta_z = np.pi/6\n#sample rotation matrix as R_z\nR_z = np.array([[np.cos(theta_z), -np.sin(theta_z), 0],\n                [np.sin(theta_z), np.cos(theta_z), 0],\n                [0, 0, 1]])\nrotated_img2 = cv2.warpAffine(img, R_z[:2,:], (width,height))\nplt.imshow(rotated_img2)\nplt.axis('off') \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:17.579394Z","iopub.execute_input":"2023-04-12T16:13:17.580111Z","iopub.status.idle":"2023-04-12T16:13:17.992152Z","shell.execute_reply.started":"2023-04-12T16:13:17.580074Z","shell.execute_reply":"2023-04-12T16:13:17.990792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"# camera text\ncamera=pd.read_csv('/kaggle/input/image-matching-challenge-2023/train/urban/kyiv-puppet-theater/sfm/cameras.txt',sep=' ')\ndisplay(camera)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:18.029425Z","iopub.status.idle":"2023-04-12T16:13:18.029977Z","shell.execute_reply.started":"2023-04-12T16:13:18.029745Z","shell.execute_reply":"2023-04-12T16:13:18.029772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images txt\nwith open('/kaggle/input/image-matching-challenge-2023/train/urban/kyiv-puppet-theater/sfm/images.txt', 'r') as file:\n    file_contents = file.read()\n    print(file_contents[:1000])","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:18.031551Z","iopub.status.idle":"2023-04-12T16:13:18.031955Z","shell.execute_reply.started":"2023-04-12T16:13:18.031751Z","shell.execute_reply":"2023-04-12T16:13:18.031772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# points3D txt\nwith open('/kaggle/input/image-matching-challenge-2023/train/urban/kyiv-puppet-theater/sfm/points3D.txt', 'r') as file:\n    file_contents = file.read()\n    print(file_contents[:1000])","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:18.034142Z","iopub.status.idle":"2023-04-12T16:13:18.034772Z","shell.execute_reply.started":"2023-04-12T16:13:18.034558Z","shell.execute_reply":"2023-04-12T16:13:18.034581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scale_factor txt\nwith open('/kaggle/input/image-matching-challenge-2023/train/urban/kyiv-puppet-theater/sfm/scale_factor.txt', 'r') as file:\n    file_contents = file.read()\n    print(file_contents[:10])","metadata":{"execution":{"iopub.status.busy":"2023-04-12T16:13:18.036212Z","iopub.status.idle":"2023-04-12T16:13:18.036673Z","shell.execute_reply.started":"2023-04-12T16:13:18.036408Z","shell.execute_reply":"2023-04-12T16:13:18.036436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}