From a2a126e7551792f2543b220920ba9ea331ef3a0a Mon Sep 17 00:00:00 2001 From: mhby1g21 <mhby1g21@soton.ac.uk> Date: Wed, 16 Oct 2024 01:14:07 +0100 Subject: [PATCH] dynamic dir changes and no need to use path.txt no more, added new folder to gitignore --- .gitignore | 2 +- combine_img.py | 86 +++++++++++++++++++------------------------------- split_img.py | 62 +++++++++++++++--------------------- ui.py | 12 +++++-- 4 files changed, 68 insertions(+), 94 deletions(-) diff --git a/.gitignore b/.gitignore index 36b3b53..98ff992 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ data/matbase/masks/* # intermediate files output/ -split_output/ +cubemap_faces/ # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/combine_img.py b/combine_img.py index 4e02ebd..691fdeb 100644 --- a/combine_img.py +++ b/combine_img.py @@ -1,6 +1,7 @@ import numpy as np import imageio import os +import sys from ui import get_res #makes sure value of x stays within the range of min and max value to prevent out of bound values accses for the image @@ -9,29 +10,11 @@ def clamp(x, min_val, max_val): #clamps the floating point coordinates to nearest integer value, copies pixel value from the image to the nearest int coord def nearest_neighbour_interpolation(img, x, y): - h, w, _ = img.shape + # Modified to handle both 2D and 3D images + h, w = img.shape[:2] x, y = clamp(int(x), 0, w-1), clamp(int(y), 0, h-1) return img[y, x] - -# def orientation_to_face(x, y, z): -# abs_x, abs_y, abs_z = abs(x), abs(y), abs(z) -# if abs_x >= abs_y and abs_x >= abs_z: -# if x > 0: -# return 'front', -y / abs_x, -z / abs_x -# else: -# return 'back', y / abs_x, -z / abs_x -# elif abs_y >= abs_x and abs_y >= abs_z: -# if y > 0: -# return 'right', -x / abs_y, -z / abs_y -# else: -# return 'left', x / abs_y, -z / abs_y -# else: -# if z > 0: -# return 'top', x / abs_z, y / abs_z -# else: -# return 'bottom', -x / abs_z, y / abs_z - #maps the 3d coords for cube faces to 2d coords on that cube face #finds out which cube face corresponds to the current pixel for which the 3d coords are calculated and calcs the normalised 2d coords on that face def orientation_to_face(x, y, z): @@ -54,7 +37,14 @@ def orientation_to_face(x, y, z): #converts cube maps into omnidirectional image def cubemap_to_omnidirectional(cube_faces, out_width, out_height): - omnidirectional = np.zeros((out_height, out_width, 3), dtype=np.uint8) + # Determine if the input is grayscale or RGB + is_grayscale = len(cube_faces[list(cube_faces.keys())[0]].shape) == 2 + + # Create the appropriate output array based on input type + if is_grayscale: + omnidirectional = np.zeros((out_height, out_width), dtype=np.uint8) + else: + omnidirectional = np.zeros((out_height, out_width, 3), dtype=np.uint8) #iterates through the pixels in o/p image, for each pixel calulates spherical coord used to map 2d pixel loc to 3d points on a sphere #which are then converted to 3d cartesian coord to find which face of cube map current pixel corresponds to @@ -79,23 +69,27 @@ def cubemap_to_omnidirectional(cube_faces, out_width, out_height): return omnidirectional -# if __name__ == "__main__": -# # Load the cubemap images -# cube_faces_dir = input("Enter the directory containing the cubemap images: ").strip() -# faces = ["right", "left", "top", "bottom", "front", "back"] -# cube_faces = {} - -# for face in faces: -# cube_faces[face] = imageio.imread(os.path.join(cube_faces_dir, f"{face}.jpg")) +if __name__ == "__main__": + # Get the directory of the current script + script_dir = os.path.dirname(os.path.abspath(__file__)) + + # Default paths relative to the script directory + default_cube_faces_dir = os.path.join(script_dir, "output", "cubemap_faces") + default_output_dir = os.path.join(script_dir, "..", "edgenet-360", "Data", "Input") + rgb_png_path = os.path.join(script_dir, "..", "scripts", "360monodepthexecution", "rgb.jpg") + # Use command-line arguments for custom paths if provided + if len(sys.argv) > 2: + cube_faces_dir = sys.argv[1] + output_dir = sys.argv[2] + else: + cube_faces_dir = default_cube_faces_dir + output_dir = default_output_dir + # Ensure output directory exists + os.makedirs(output_dir, exist_ok=True) -if __name__ == "__main__": # Load the cubemap images - #cube_faces_dir = input("Enter the directory containing the cubemap images: ").strip() - cube_faces_dir = "C:\Project\AVVR-Pipeline-Internship\material_recognition\Dynamic-Backward-Attention-Transformer\output\split_output" - - #faces = ["right", "left", "top", "bottom", "front", "back"] faces = ["rightrgb", "leftrgb", "toprgb", "bottomrgb", "frontrgb", "backrgb"] cube_faces = {} @@ -103,39 +97,23 @@ if __name__ == "__main__": image_path = os.path.join(cube_faces_dir, f"{face}.png") image_data = imageio.imread(image_path) - #rotate top and bottom face by 90 deg - # if face in ["top", "bottom"]: - # image_data = np.rot90(image_data, 1) - - # #flip the top, bottom, front and back faces in horizontal direction - # if face not in ["left", "right"]: - # image_data = image_data[:, ::-1] - if face in ["toprgb", "bottomrgb"]: image_data = np.rot90(image_data, 1) if face not in ["leftrgb", "rightrgb"]: image_data = image_data[:, ::-1] - cube_faces[face] = image_data - - # output_width = int(input("Enter output omnidirectional width: ")) - # output_height = int(input("Enter output omnidirectional height: ")) - with open('path.txt', 'r') as file: - input_path = file.readline() - print(f'path = {input_path}') - os.remove('path.txt') - height, width = get_res(input_path) - print(height, width) + # Use the rgb.png file from edgenet-360 folder for dimensions + height, width = get_res(rgb_png_path) + print(f"Using dimensions from {rgb_png_path}: {width}x{height}") output_width = width output_height = height - #print(f"height: {height}, width: {width}") omnidirectional_img = cubemap_to_omnidirectional(cube_faces, output_width, output_height) - output_path = "C:\Project\AVVR-Pipeline-Internship\edgenet360\Data\Input\material.png" + output_path = os.path.join(output_dir, "material.png") imageio.v2.imsave(output_path, omnidirectional_img) print(f"Omnidirectional image saved to {output_path}") diff --git a/split_img.py b/split_img.py index 83459dd..969a526 100644 --- a/split_img.py +++ b/split_img.py @@ -1,6 +1,5 @@ ###### This code has been Referenced from: https://github.com/jaxry/panorama-to-cubemap/blob/gh-pages/convert.js - import numpy as np import imageio import os @@ -16,7 +15,7 @@ def mod(x, n): return ((x % n) + n) % n #clamps the floating point coordinates to nearest integer value, copies pixel value from the image to the nearest int coord -def nearest_neigbour_interpolation(img, x, y): +def nearest_neighbour_interpolation(img, x, y): h, w, _ = img.shape x, y = clamp(int(x), 0, w-1), clamp(int(y), 0, h-1) return img[y, x] @@ -49,56 +48,45 @@ def face_rendering(img, face, face_size): longitude = mod(np.arctan2(out[1], out[0]), 2 * np.pi) latitude = np.arccos(out[2] / r) s_x, s_y = img.shape[1] * longitude / (2 * np.pi) - 0.5, img.shape[0] * latitude / np.pi - 0.5 - out_face[y, x] = nearest_neigbour_interpolation(img, s_x, s_y) + out_face[y, x] = nearest_neighbour_interpolation(img, s_x, s_y) return out_face #generates 6 cube faces -def generate_cube_faces(input_path, output_path="cube_faces_output"): - +def generate_cube_faces(input_path, output_path): img = imageio.imread(input_path) face_size = 512 #each face o/p image will be 512x512 faces = ["right", "left", "top", "bottom", "front", "back"] - results = {} for face in faces: - results[face] = face_rendering(img, face, face_size) + face_img = face_rendering(img, face, face_size) face_output_path = os.path.join(output_path, f"{face}.png") - imageio.imsave(face_output_path, results[face]) + imageio.imsave(face_output_path, face_img) print(f"Saved {face} face to {face_output_path}") - - - - -# if __name__ == "__main__": -# input_path = select_image() -# height, width = get_res(input_path) -# print("width: , Height:", width, height) -# #output_path = input("Enter output directory: ").strip() -# #if not output_path: -# output_path = "C:\Project\AVVR-Pipeline-Internship\material_recognition\Dynamic-Backward-Attention-Transformer\split_output" -# if not os.path.exists(output_path): -# os.makedirs(output_path) -# generate_cube_faces(input_path, output_path) - - -#input_path = select_image() -input_path = sys.argv[1] -with open('path.txt', 'w') as file: - file.write(input_path) - file.close() - -#output_path = input("Enter output directory: ").strip() -#if not output_path: -output_path = "C:\Project\AVVR-Pipeline-Internship\material_recognition\Dynamic-Backward-Attention-Transformer\split_output" -if not os.path.exists(output_path): - os.makedirs(output_path) -generate_cube_faces(input_path, output_path) - +if __name__ == "__main__": + # Get the directory of the current script + script_dir = os.path.dirname(os.path.abspath(__file__)) + # Default output path relative to the script directory + default_output_path = os.path.join(script_dir, "cubemap_faces") + # Use command-line arguments if provided + if len(sys.argv) > 1: + input_path = sys.argv[1] + if len(sys.argv) > 2: + output_path = sys.argv[2] + else: + output_path = default_output_path + else: + input_path = select_image() + output_path = default_output_path + # Ensure output directory exists + os.makedirs(output_path, exist_ok=True) + # Generate cube faces + generate_cube_faces(input_path, output_path) + print(f"Cubemap faces saved to: {output_path}") \ No newline at end of file diff --git a/ui.py b/ui.py index 6cd25e6..e40408f 100644 --- a/ui.py +++ b/ui.py @@ -1,9 +1,9 @@ import tkinter as tk from tkinter import filedialog import cv2 +import os def select_image(): - root = tk.Tk() root.withdraw() @@ -16,7 +16,15 @@ def select_image(): def get_res(path): img = cv2.imread(path) height, width, _ = img.shape - cv2.imwrite('C:\Project\AVVR-Pipeline-Internship\edgenet360\Data\Input\\rgb.png', img) + + # Construct the output path dynamically + script_dir = os.path.dirname(os.path.abspath(__file__)) + output_dir = os.path.join(script_dir, "..", "edgenet-360", "Data", "Input") + os.makedirs(output_dir, exist_ok=True) + output_path = os.path.join(output_dir, "rgb.png") + + cv2.imwrite(output_path, img) + print(f"RGB image saved to: {output_path}") return height, width -- GitLab