Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor function imports and use more explicit imports #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions scripts/optical_flow_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import cv2
import numpy as np
from PIL import Image
import scripts.berry_utility as utilityb
from scripts.berry_utility import base64_to_texture


def read_image(file_path):
image = cv2.imread(file_path, cv2.IMREAD_COLOR)
Expand Down Expand Up @@ -48,6 +49,7 @@ def compute_optical_flow(image1, image2):
flow = cv2.calcOpticalFlowFarneback(image1, image2, None, pyr_scale=0.5, levels=3, winsize=15, iterations=3, poly_n=5, poly_sigma=1.2, flags=0)
return flow


def warp_image(image, flow):
h, w = image.shape[:2]
flow_map = np.array([[x, y] for y in range(h) for x in range(w)], dtype=np.float32) - flow.reshape(-1, 2)
Expand All @@ -57,23 +59,16 @@ def warp_image(image, flow):
flow_map[:, :, 0] = np.clip(flow_map[:, :, 0], 0, w - 1)
flow_map[:, :, 1] = np.clip(flow_map[:, :, 1], 0, h - 1)

warped_image = cv2.remap(image, flow_map, None, cv2.INTER_LANCZOS4 )
warped_image = cv2.remap(image, flow_map, None, cv2.INTER_LANCZOS4)
return warped_image

def process_image_basic (image1_path, image2_path, provided_image_path,max_dimension, index,output_folder):

#image1 = read_image(image1_path)
# image2 = read_image(image2_path)

# image1 = resize_image(image1, max_dimension)
# image2 = resize_image(image2, max_dimension)
image1 = resize_image(utilityb.base64_to_texture(image1_path),max_dimension)
image2 = resize_image(utilityb.base64_to_texture(image2_path),max_dimension)

# provided_image = read_image(provided_image_path)
provided_image = utilityb.base64_to_texture(provided_image_path)
def process_image_basic (image1_path, image2_path, provided_image_path,max_dimension, index,output_folder):
image1 = resize_image(base64_to_texture(image1_path),max_dimension)
image2 = resize_image(base64_to_texture(image2_path),max_dimension)
provided_image = base64_to_texture(provided_image_path)
provided_image = resize_image(provided_image, max_dimension)

flow = compute_optical_flow(cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY), cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY))
warped_image = warp_image(provided_image, flow)

Expand All @@ -82,13 +77,14 @@ def process_image_basic (image1_path, image2_path, provided_image_path,max_dimen
print(f"Warped image saved as '{warped_image_path}'")
combine_images(image1,image2,provided_image,warped_image,f"{index}.png")
return warped_image_path,flow
# return provided_image_path,flow
#AAAAAAAAAAAAAAAAAAA


def process_image(image1, image2, provided_image, output_folder, flow_output_folder, max_dimension, index):
image1 = resize_image(image1, max_dimension)
image2 = resize_image(image2, max_dimension)

provided_image = provided_image or np.zeros_like(image1) # Handle None provided_image

flow = compute_optical_flow(cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY), cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY))
warped_image = warp_image(provided_image, flow)

Expand All @@ -102,6 +98,7 @@ def process_image(image1, image2, provided_image, output_folder, flow_output_fol

return warped_image


def process_images(input_folder, output_folder, flow_output_folder, provided_image_path, max_dimension):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
Expand All @@ -123,6 +120,7 @@ def process_images(input_folder, output_folder, flow_output_folder, provided_ima

provided_image = process_image(image1, image2, provided_image, output_folder, flow_output_folder, max_dimension, i)


def main():
input_folder = "Input_Images"
output_folder = "output_images"
Expand Down Expand Up @@ -162,4 +160,3 @@ def combine_images(img1, img2, img3, img4, output_file, output_folder="debug"):
# Save combined image to output folder
output_path = os.path.join(output_folder, output_file)
combined_image.save(output_path)