autofocus algorithms for clear night sky
Project description
NightFocus
What it is
Simple python package for automated focus optimization of astrophotography image patches.
How to use it
- 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
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nightfocus-0.1.2.tar.gz.
File metadata
- Download URL: nightfocus-0.1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a741aa6d099b1bc7f87cb290fc429dfa5d7c488e37f1af8f1af0f1d264091920
|
|
| MD5 |
5e86c5fd10a914e3fceb6ea3ac984e92
|
|
| BLAKE2b-256 |
03aff24f7aa2070fcd8f2c2b8836186b7cc6df1e0cf76286f637138bbd2c2922
|
File details
Details for the file nightfocus-0.1.2-py3-none-any.whl.
File metadata
- Download URL: nightfocus-0.1.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ec111047f3c0b6ac62d27ef954af7fb7bfa6bc00c9d29ac4e187d18b1504688
|
|
| MD5 |
a95b0886181ab67e4f0bf21724289b40
|
|
| BLAKE2b-256 |
eb986755baf4904e3b484c91c8d3c1ba2c923b42ff8b3da0a467e3a347b6ea40
|