Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/vstarstack/library/stars/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"BORDER_WIDTH" : 10,
"MIN_STAR_R" : 2,
"MAX_STAR_R" : 20,
"SAFETY_THRESHOLD" : 1e-4,
}

def calculate_brightness(image : np.ndarray, x : int, y : int, r : int):
Expand All @@ -36,27 +37,25 @@ def calculate_brightness(image : np.ndarray, x : int, y : int, r : int):
pos_mask = np.zeros(patch.shape)
cv2.circle(pos_mask, (r, r), r, 1, -1)
masked = patch * pos_mask
masked = np.clip(masked, 0, None)
brightness = math.sqrt(np.sum(masked) / math.pi)
return brightness

def _threshold(image, radius, ratio):
kernel = np.zeros((2*radius+1, 2*radius+1))
cv2.circle(kernel, (radius, radius), radius, 1, -1)
kernel = kernel / np.sum(kernel)
filtered = cv2.filter2D(image, ddepth=-1, kernel=kernel)
mask = (image > filtered*ratio).astype('uint8')
def _threshold(image, radius, ratio, safety_threshold):
filtered = cv2.GaussianBlur(image, (2*radius+1, 2*radius+1), 0)
mask = (image > filtered*ratio + safety_threshold).astype('uint8')
return mask

def _find_stars(gray_image : np.ndarray):
"""Find stars on image"""
shape = gray_image.shape

gray_image = cv2.GaussianBlur(gray_image, (3, 3), 0)
gray_image = (gray_image / np.amax(gray_image) * 255).astype('uint8')

thresh = _threshold(gray_image, _detector_cfg["THRESHOLD_BLOCK_SIZE"],
_detector_cfg["THRESHOLD_COEFF"])

_detector_cfg["THRESHOLD_COEFF"],
_detector_cfg["SAFETY_THRESHOLD"])

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
blob = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
Expand Down Expand Up @@ -112,7 +111,8 @@ def configure_detector(*,
max_r = None,
border = None,
thresh_block_size = None,
thresh_coeff = None):
thresh_coeff = None,
safety_threshold = None):
"""Configure detector parameters"""
global _detector_cfg
if min_r is not None:
Expand All @@ -125,3 +125,5 @@ def configure_detector(*,
_detector_cfg["THRESHOLD_BLOCK_SIZE"] = thresh_block_size
if thresh_coeff is not None:
_detector_cfg["THRESHOLD_COEFF"] = thresh_coeff
if safety_threshold is not None:
_detector_cfg["SAFETY_THRESHOLD"] = safety_threshold
2 changes: 2 additions & 0 deletions tests/test_stars_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,7 @@ def test_1():
merged = vstarstack.library.merge.simple_add(source)
layer,_ = merged.get_channel("L")

import matplotlib.pyplot as plt

merged_stars = vstarstack.library.stars.detect.detect_stars(layer)
assert len(merged_stars) == 4