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

Implement simpler API for optimizing intrinsic parameters #236

Closed
eigenvivek opened this issue May 16, 2024 · 0 comments · Fixed by #240
Closed

Implement simpler API for optimizing intrinsic parameters #236

eigenvivek opened this issue May 16, 2024 · 0 comments · Fixed by #240

Comments

@eigenvivek
Copy link
Owner

The current focus of DiffDRR is on optimizing extrinsic parameters. This is perfectly fine for calibrated C-arms (e.g., research scanners), but it seems that clinical scanners are often uncalibrated! Image-based 2D/3D registration therefore requires optimization of the intrinsic parameters as well as the extrinsics. With the current DiffDRR API, this is cumbersome.

Proposed change:

  • Instead of initializing the diffdrr.detector.Detector with a specific pixel spacing, principal point, and focal length, simply initialize with unit values (on the GPU). In the forward method, pass delx, dely, x0, y0, and sdd, and use these values to resize the unit detector. Perhaps this could be implemented as an affine matrix?
    def _initialize_carm(self: Detector):
    """Initialize the default position for the source and detector plane."""
    try:
    device = self.sdd.device
    except AttributeError:
    device = torch.device("cpu")
    # Initialize the source at the origin and the center of the detector plane on the positive z-axis
    source = torch.tensor([[0.0, 0.0, 0.0]], device=device)
    center = torch.tensor([[0.0, 0.0, 1.0]], device=device) * self.sdd
    # Use the standard basis for the detector plane
    basis = torch.tensor([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], device=device)
    # Construct the detector plane with different offsets for even or odd heights
    h_off = 1.0 if self.height % 2 else 0.5
    w_off = 1.0 if self.width % 2 else 0.5
    # Construct equally spaced points along the basis vectors
    t = (
    torch.arange(-self.height // 2, self.height // 2, device=device) + h_off
    ) * self.delx
    s = (
    torch.arange(-self.width // 2, self.width // 2, device=device) + w_off
    ) * self.dely
    if self.reverse_x_axis:
    s = -s
    coefs = torch.cartesian_prod(t, s).reshape(-1, 2)
    target = torch.einsum("cd,nc->nd", basis, coefs)
    target += center
    # Batch source and target
    source = source.unsqueeze(0)
    target = target.unsqueeze(0)
    # Apply principal point offset
    target[..., 1] -= self.x0
    target[..., 0] -= self.y0
    if self.n_subsample is not None:
    sample = torch.randperm(self.height * self.width)[: int(self.n_subsample)]
    target = target[:, sample, :]
    self.subsamples.append(sample.tolist())
    return source, target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant