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.1.tar.gz (19.2 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.1-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nightfocus-0.1.1.tar.gz
  • Upload date:
  • Size: 19.2 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.1.tar.gz
Algorithm Hash digest
SHA256 77223e3650a523ef791f627bbaf88d8552873bf221759098e3f1e31d63f156ba
MD5 fcd7c52c048c1508521db1888b07d177
BLAKE2b-256 e4b92116acdb985f9744e0426b0b1df8790450a28b2e49a24fb0675da1ef323a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nightfocus-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f932ae3f9e0427bf5a8c7910f9a074a921476d8867ef1e85558f35adbc2858c4
MD5 eb70bf0f09f431fd5ed0355664c62e2e
BLAKE2b-256 f6d5586ab0a1eab5e7e90fb24af97be6e3035abfd69148c134499a028a3652b3

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