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.3.tar.gz (20.9 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.3-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nightfocus-0.1.3.tar.gz
  • Upload date:
  • Size: 20.9 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.3.tar.gz
Algorithm Hash digest
SHA256 6c66eb8a9ab96dae08cf81d8d940528f7c36729ef3bea5e6b8f09e509b31913a
MD5 f0eea00e5ba8db512445ca401b99779f
BLAKE2b-256 e0a61bcd875fb66bc9cfc5a57e16c066ac0cebaaae97e6fa2e3d22f7a6e66862

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nightfocus-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 25.5 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 21ff0d358407160cdb6ac4ddeee676b124365b464194a4c42036726589337eeb
MD5 60c68bd82273bdb4c9e5a584385aab3b
BLAKE2b-256 d18ac71657f62cb73131ff2499ceba9eb8a4f416a87bc1ca77da251366fc5c32

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