Skip to content

Commit

Permalink
Good resulst!
Browse files Browse the repository at this point in the history
  • Loading branch information
rfgzuid committed Jun 13, 2024
1 parent 6b324dd commit e124464
Show file tree
Hide file tree
Showing 100 changed files with 498 additions and 143 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
246 changes: 246 additions & 0 deletions Test Data/testo.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
15:02:05;0;;
15:02:06;0;;
15:02:07;0;;
15:02:08;0;;
15:02:09;0;;
15:02:10;0;;
15:02:11;0;;
15:02:12;0;;
15:02:13;0;;
15:02:14;0;;
15:02:15;0;;
15:02:16;0;;
15:02:17;0;;
15:02:18;0;;
15:02:19;0;;
15:02:20;0;;
15:02:21;0;;
15:02:22;0;;
15:02:23;0;;
15:02:24;0;;
15:02:25;0;;
15:02:26;0;;
15:02:27;0;;
15:02:28;0;;
15:02:29;0;;
15:02:30;0;;
15:02:31;0;;
15:02:32;0;;
15:02:33;0;;
15:02:34;0;;
15:02:35;0;;
15:02:36;0;;
15:02:37;0;;
15:02:38;0;;
15:02:39;0;;
15:02:40;0,55;;
15:02:41;1,01;;
15:02:42;1,37;;
15:02:43;1,52;;
15:02:44;1,52;;
15:02:45;1,5;;
15:02:46;1,49;;
15:02:47;1,52;;
15:02:48;1,46;;
15:02:49;1,48;;
15:02:50;1,44;;
15:02:51;1,45;;
15:02:52;1,45;;
15:02:53;1,47;;
15:02:54;1,49;;
15:02:55;1,45;;
15:02:56;1,48;;
15:02:57;1,5;;
15:02:58;1,5;;
15:02:59;1,48;;
15:03:00;1,5;;
15:03:01;1,57;;
15:03:02;1,53;;
15:03:03;1,58;;
15:03:04;1,59;;
15:03:05;1,55;;
15:03:06;1,55;;
15:03:07;1,52;;
15:03:08;1,6;;
15:03:09;1,57;;
15:03:10;1,5;;
15:03:11;1,57;;
15:03:12;1,57;;
15:03:13;1,55;;
15:03:14;1,55;;
15:03:15;1,59;;
15:03:16;1,58;;
15:03:17;1,5;;
15:03:18;1,58;;
15:03:19;1,55;;
15:03:20;1,57;;
15:03:21;1,6;;
15:03:22;1,52;;
15:03:23;1,57;;
15:03:24;1,5;;
15:03:25;1,51;;
15:03:26;1,51;;
15:03:27;1,6;;
15:03:28;1,51;;
15:03:29;1,52;;
15:03:30;1,58;;
15:03:31;1,52;;
15:03:32;1,62;;
15:03:33;1,72;;
15:03:34;1,69;;
15:03:35;1,72;;
15:03:36;1,72;;
15:03:37;1,62;;
15:03:38;1,61;;
15:03:39;1,55;;
15:03:40;1,6;;
15:03:41;1,6;;
15:03:42;1,63;;
15:03:43;1,65;;
15:03:44;1,65;;
15:03:45;1,61;;
15:03:46;1,65;;
15:03:47;1,62;;
15:03:48;1,61;;
15:03:49;1,59;;
15:03:50;1,68;;
15:03:51;1,65;;
15:03:52;1,59;;
15:03:53;1,7;;
15:03:54;1,71;;
15:03:55;1,78;;
15:03:56;1,82;;
15:03:57;1,86;;
15:03:58;1,87;;
15:03:59;1,87;;
15:04:00;1,8;;
15:04:01;1,81;;
15:04:02;1,81;;
15:04:03;1,8;;
15:04:04;1,81;;
15:04:05;1,82;;
15:04:06;1,74;;
15:04:07;1,78;;
15:04:08;1,73;;
15:04:09;1,78;;
15:04:10;1,74;;
15:04:11;1,74;;
15:04:12;1,71;;
15:04:13;1,72;;
15:04:14;1,9;;
15:04:15;1,89;;
15:04:16;1,88;;
15:04:17;2;;
15:04:18;1,93;;
15:04:19;1,96;;
15:04:20;1,93;;
15:04:21;1,89;;
15:04:22;1,84;;
15:04:23;1,86;;
15:04:24;1,88;;
15:04:25;1,86;;
15:04:26;1,91;;
15:04:27;1,93;;
15:04:28;1,93;;
15:04:29;1,93;;
15:04:30;1,95;;
15:04:31;1,92;;
15:04:32;1,92;;
15:04:33;1,99;;
15:04:34;2,1;;
15:04:35;2,1;;
15:04:36;2,08;;
15:04:37;2,09;;
15:04:38;2,09;;
15:04:39;2,05;;
15:04:40;2,05;;
15:04:41;2,02;;
15:04:42;2,01;;
15:04:43;2,04;;
15:04:44;2,04;;
15:04:45;1,97;;
15:04:46;2,01;;
15:04:47;2,06;;
15:04:48;2,06;;
15:04:49;2,03;;
15:04:50;2,11;;
15:04:51;2,09;;
15:04:52;2,06;;
15:04:53;2,06;;
15:04:54;2,06;;
15:04:55;1,9;;
15:04:56;1,42;;
15:04:57;0,99;;
15:04:58;0,77;;
15:04:59;0,77;;
15:05:00;0,54;;
15:05:01;0,47;;
15:05:02;0,41;;
15:05:03;0,41;;
15:05:04;0;;
15:05:05;0;;
15:05:06;0;;
15:05:07;0;;
15:05:08;0;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
;;;
307 changes: 200 additions & 107 deletions experiments/PIV Test.ipynb

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/SIV_library/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from src.SIV_library.lib import OpticalFlow, SIV

from collections.abc import Generator


class Warp(torch.nn.Module):
"""Custom module that creates a warped images according to the velocity field acquired in a previous pass
Expand Down Expand Up @@ -41,6 +43,36 @@ def interpolate_field(self, img_shape) -> None:
self.x, self.y = x.to(self.x.device), y.to(self.y.device)


class CTF:
"""
runs the optical flow algorithm in a coarse-to-fine pyramidal structure, allowing for larger displacements
https://www.ipol.im/pub/art/2013/20/article.pdf
"""
def __init__(self, optical: OpticalFlow, num_passes: int = 3, scale_factor: float = 1/2):
self.optical = optical
self.num_passes, self.scale_factor = num_passes, scale_factor

def __len__(self) -> int:
return len(self.optical.dataset)

def __call__(self) -> Generator:
img_shape = self.optical.dataset.img_shape
scales = [self.scale_factor ** (self.num_passes - p - 1) for p in range(self.num_passes)]
sizes = [(round(img_shape[0] * scale), round(img_shape[1] * scale)) for scale in scales]

for size in sizes:
# y, x = torch.meshgrid(torch.arange(0, size[0], 1), torch.arange(0, size[1], 1))
# resize = Resize(size, InterpolationMode.BICUBIC)
# resize.apply_to = ['a', 'b'] # apply the resize transform to both images in the pair
# warp = Warp(x, y, u, v)
#
# optical.dataset.img_shape = size
# optical.dataset.transforms = [resize, warp]
#
# for
pass


def ctf_optical(optical: OpticalFlow, num_passes: int = 3, scale_factor: float = 1/2):
"""
runs the optical flow algorithm in a coarse-to-fine pyramidal structure, allowing for larger displacements
Expand Down
52 changes: 18 additions & 34 deletions src/SIV_library/lib.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from .matching import window_array, get_field_shape, block_match, get_x_y, correlation_to_displacement, WindowShift
from .optical_flow import optical_flow
from collections.abc import Generator

import torch
from torch.utils.data import Dataset
from torch.utils.data import Dataset, DataLoader

import os
import cv2
Expand Down Expand Up @@ -58,30 +59,18 @@ def __init__(self,
self.window_size, self.overlap, self.search_area = window_size, overlap, search_area
self.mode, self.num_passes, self.scale_factor = mode, num_passes, scale_factor

def run(self):
def __len__(self) -> int:
return len(self.dataset)

def __call__(self) -> Generator:
img_shape = self.dataset.img_shape
scales = [self.scale_factor ** p for p in range(self.num_passes)]

final_window, final_overlap = int(self.window_size * scales[-1]), int(self.overlap * scales[-1])

n_rows, n_cols = get_field_shape(img_shape, final_window, final_overlap)
xp, yp = get_x_y(img_shape, final_window, final_overlap)

u = torch.zeros((len(self.dataset), n_rows, n_cols)).to(self.device)
v = torch.zeros((len(self.dataset), n_rows, n_cols)).to(self.device)

x, y = xp.reshape(n_rows, n_cols).to(self.device), yp.reshape(n_rows, n_cols).to(self.device)
x, y = x.expand(len(self.dataset), -1, -1), y.expand(len(self.dataset), -1, -1)

for idx, data in tqdm(enumerate(self.dataset), total=len(self.dataset),
desc="SAD" if self.mode == 1 else "Correlation"):
img_a, img_b = data
a, b = img_a.to(self.device), img_b.to(self.device)

# multipass loop
loader = DataLoader(self.dataset)
for a, b in tqdm(loader, total=len(loader), desc="SAD" if self.mode == 1 else "Correlation"):
for i, scale in enumerate(scales):
window_size, overlap = int(self.window_size * scale), int(self.overlap * scale)
search_area = self.search_area # tuple(int(pad * scale) for pad in self.search_area)
search_area = tuple(int(pad * scale) for pad in self.search_area)

n_rows, n_cols = get_field_shape(img_shape, window_size, overlap)
xp, yp = get_x_y(img_shape, window_size, overlap)
Expand All @@ -98,8 +87,7 @@ def run(self):
du, dv = correlation_to_displacement(match, search_area, n_rows, n_cols, self.mode)

up, vp = (du, dv) if i == 0 else (up + du, vp + dv)
u[idx], v[idx] = up, vp
return x, y, u, -v
yield xp, yp, up, -vp


class OpticalFlow:
Expand All @@ -116,19 +104,15 @@ def __init__(self,
self.device = device
self.alpha, self.num_iter, self.eps = alpha, num_iter, eps

def run(self):
rows, cols = self.dataset.img_shape

u = torch.zeros((len(self.dataset), rows, cols)).to(self.device)
v = torch.zeros((len(self.dataset), rows, cols)).to(self.device)
def __len__(self) -> int:
return len(self.dataset)

def __call__(self) -> Generator:
rows, cols = self.dataset.img_shape
y, x = torch.meshgrid(torch.arange(0, rows, 1), torch.arange(0, cols, 1))
x, y = x.expand(len(self.dataset), -1, -1).to(self.device), y.expand(len(self.dataset), -1, -1).to(self.device)

for idx, data in tqdm(enumerate(self.dataset), total=len(self.dataset), desc='Optical flow'):
img_a, img_b = data
a, b = img_a.to(self.device), img_b.to(self.device)
x, y = x.to(self.device), y.to(self.device)

loader = DataLoader(self.dataset)
for a, b in tqdm(loader, total=len(loader), desc='Optical flow'):
du, dv = optical_flow(a, b, self.alpha, self.num_iter, self.eps)
u[idx], v[idx] = du, dv
return x, y, u, -v
yield x, y, du, -dv
2 changes: 1 addition & 1 deletion src/SIV_library/optical_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def optical_flow(img1, img2, alpha, num_iter, eps):
device = img1.device

a, b = img1[None, None, :, :].float(), img2[None, None, :, :].float()
a, b = img1[None, :, :, :].float(), img2[None, :, :, :].float()
u, v = torch.zeros_like(a), torch.zeros_like(a)

Ix, Iy, It = compute_derivatives(a, b)
Expand Down
2 changes: 1 addition & 1 deletion src/SIV_library/t.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def correlation_to_displacement(
nom2 = torch.log(cb) - torch.log(ct)
den2 = 2 * (torch.log(cb) + torch.log(ct)) - 4 * torch.log(cm)

m2d = torch.cat((m // d, m % k), -1)
m2d = torch.cat((m // k, m % k), -1)

v = m2d[:, 0][:, None] + nom2 / den2
u = m2d[:, 1][:, None] + nom1 / den1
Expand Down

0 comments on commit e124464

Please sign in to comment.