Skip to main content

autofocus algorithms for clear night sky

Project description

NightFocus

Tests

What it is

Simple python package for automated focus optimization of astrophotography image patches.

How to use it

  1. create a sublcass of Camera for your camera. It must implement the take_picture method:
from nightfocus.camera import Camera

class MyCamera(Camera):
    def take_picture(self, focus: int) -> np.ndarray:
        raise NotImplementedError
  1. perform focus optimization:
from nightfocus import optimize_focus

best_focus, _ = optimize_focus(camera, bounds=(0, 100))

Under the hood

NightFocus uses Bayesian optimization to find the best focus position. The default metric used is the tenengrad focus measure:

def tenengrad(image: np.ndarray, ksize: int = 3) -> float:
    """
    Tenengrad focus measure based on gradient magnitude.
    Works well for star images and is computationally efficient.
    """
    if len(image.shape) == 3:
        gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    else:
        gray = image

    gx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=ksize)
    gy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=ksize)
    return float(np.mean(gx**2 + gy**2))

Other metrics are available in the nightfocus.focus_metrics module:

from nightfocus.focus_metrics import FOCUS_MEASURES

FOCUS_MEASURES = {
    "tenengrad": tenengrad,
    "modified_laplacian": modified_laplacian,
    "normalized_variance": normalized_variance,
    "spectral_energy": spectral_energy,
    "brenner_gradient": brenner_gradient,
    "threshold_count": threshold_count,
    "fast_entropy": fast_entropy,
    "wavelet_measure": wavelet_measure,
}

How we know it works

It could find the correct focus when running on the datasets located in the images folder. A dataset file corresponds to a corresponding tiff image on which increasing values of blur where applied.

Command Line Interface

NightFocus includes a CLI for common tasks:

nightfocus --help

It provides this commands:

  • crops: Create random crops from an image.
  • dataset: Generate blurred dataset from TIFF files with increasing blur.
  • evaluate: Evaluate focus scoring on a dataset.
  • evaluate-directory: Evaluate focus scoring on a directory of images.
  • evaluate-metrics: Evaluate multiple focus metrics on a dataset and print results.
  • view: View images from a dataset file with their focus values.

Installation

pip install nightfocus

Author

Vincent Berenz, Max Planck Institute for Intelligent Systems, Tuebingen, Germany

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nightfocus-0.1.0.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nightfocus-0.1.0-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

Details for the file nightfocus-0.1.0.tar.gz.

File metadata

  • Download URL: nightfocus-0.1.0.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/6.8.0-59-generic

File hashes

Hashes for nightfocus-0.1.0.tar.gz
Algorithm Hash digest
SHA256 350ce486d95df604860cb6d87a2e04312f4690b87266961b5a4d027d68858f3d
MD5 dccd298ce6ef069181149f81d8b48898
BLAKE2b-256 680ff148182a6d091dcb679a7d4470c9f948add2f2b90a85865dbe98bf83ac22

See more details on using hashes here.

File details

Details for the file nightfocus-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nightfocus-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.3 Linux/6.8.0-59-generic

File hashes

Hashes for nightfocus-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a18f0ef20617e9dbb5c3b048b2e18cb360846f4981c432403d8bc68a5b6a3ac7
MD5 86f046c92a93f8078b6b8c17e5d815db
BLAKE2b-256 94d2f2bf92963a1c8a40d68941f8f42f441292df6a2c443cb13244fa2041e9a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page