improved point detection
This commit is contained in:
@@ -10,10 +10,12 @@ from matplotlib.patches import Rectangle
|
||||
from .extractor import DiagramExtractor
|
||||
|
||||
|
||||
def process_all_images(directory, calibration=None, prompt_axis_override=False):
|
||||
def process_all_images(
|
||||
directory, calibration=None, prompt_axis_override=False, max_points=None
|
||||
):
|
||||
"""Process all PNG images in the directory"""
|
||||
|
||||
extractor = DiagramExtractor(calibration)
|
||||
extractor = DiagramExtractor(calibration, max_points=max_points)
|
||||
all_results = {}
|
||||
|
||||
for filename in os.listdir(directory):
|
||||
@@ -201,10 +203,26 @@ def main():
|
||||
print(f" X-axis: {calibration['x_min']} to {calibration['x_max']}")
|
||||
print(f" Y-axis: {calibration['y_min']} to {calibration['y_max']}\n")
|
||||
|
||||
# Get sampling density preference
|
||||
print("SAMPLING DENSITY:")
|
||||
print("-" * 40)
|
||||
max_points_input = input("Maximum points to extract (blank for auto): ").strip()
|
||||
max_points = None
|
||||
if max_points_input:
|
||||
try:
|
||||
max_points = int(max_points_input)
|
||||
print(f"Will extract up to {max_points} points per image.\n")
|
||||
except ValueError:
|
||||
print("Invalid input. Using automatic sampling.\n")
|
||||
else:
|
||||
print("Using automatic sampling (typically 50-200 points).\n")
|
||||
|
||||
override_choice = input("Override detected axes per image? (y/n): ").strip().lower()
|
||||
prompt_axis_override = override_choice in ("y", "yes")
|
||||
|
||||
results = process_all_images(images_dir, calibration, prompt_axis_override)
|
||||
results = process_all_images(
|
||||
images_dir, calibration, prompt_axis_override, max_points
|
||||
)
|
||||
|
||||
if results:
|
||||
print("\n" + "=" * 70)
|
||||
|
||||
@@ -2,19 +2,21 @@
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from sklearn.cluster import DBSCAN
|
||||
|
||||
|
||||
class DiagramExtractor:
|
||||
"""Extract data from diagram images with direct real-world coordinate conversion"""
|
||||
|
||||
def __init__(self, calibration=None):
|
||||
def __init__(self, calibration=None, max_points=None):
|
||||
"""
|
||||
Initialize extractor with optional calibration parameters
|
||||
|
||||
Args:
|
||||
calibration: Dict with x_min, x_max, y_min, y_max
|
||||
If None, will use default values
|
||||
max_points: Maximum number of points to extract (int)
|
||||
If None, uses automatic sampling (min 50, or 1/5 of detected points)
|
||||
Set to a lower number (e.g., 20, 50, 100) for sparser data
|
||||
"""
|
||||
self.calibration = calibration or {
|
||||
"x_min": 0,
|
||||
@@ -22,6 +24,7 @@ class DiagramExtractor:
|
||||
"y_min": 0,
|
||||
"y_max": 1,
|
||||
}
|
||||
self.max_points = max_points
|
||||
|
||||
def set_calibration(self, x_min, x_max, y_min, y_max):
|
||||
"""Update calibration parameters"""
|
||||
@@ -209,28 +212,38 @@ class DiagramExtractor:
|
||||
# Sort by x coordinate
|
||||
points = points[points[:, 0].argsort()]
|
||||
|
||||
# Sample points uniformly by x coordinate
|
||||
n_samples = min(max(50, len(points) // 5), len(points))
|
||||
indices = np.linspace(0, len(points) - 1, n_samples, dtype=int)
|
||||
sampled_points = points[indices]
|
||||
# Group points by x-coordinate bins and take median y-value
|
||||
# This centers the line even when edges are detected on both sides
|
||||
x_min_pt, x_max_pt = points[:, 0].min(), points[:, 0].max()
|
||||
|
||||
# Further smoothing: group very close points
|
||||
if len(sampled_points) > 1:
|
||||
clustering = DBSCAN(eps=3, min_samples=1).fit(sampled_points)
|
||||
labels = clustering.labels_
|
||||
|
||||
# Get cluster centers
|
||||
unique_labels = np.unique(labels)
|
||||
final_points = []
|
||||
for label in unique_labels:
|
||||
cluster_points = sampled_points[labels == label]
|
||||
center = cluster_points.mean(axis=0)
|
||||
final_points.append(center)
|
||||
|
||||
final_points = np.array(final_points)
|
||||
final_points = final_points[final_points[:, 0].argsort()]
|
||||
# Determine number of bins based on desired samples
|
||||
if self.max_points is not None:
|
||||
n_bins = self.max_points
|
||||
else:
|
||||
final_points = sampled_points
|
||||
n_bins = min(
|
||||
max(50, len(points) // 5), len(points), x_max_pt - x_min_pt + 1
|
||||
)
|
||||
|
||||
# Create bins
|
||||
bin_width = max(1, (x_max_pt - x_min_pt) / n_bins)
|
||||
centered_points = []
|
||||
|
||||
for i in range(int(n_bins)):
|
||||
bin_start = x_min_pt + i * bin_width
|
||||
bin_end = bin_start + bin_width
|
||||
|
||||
# Find all points in this x-range
|
||||
mask = (points[:, 0] >= bin_start) & (points[:, 0] < bin_end)
|
||||
bin_points = points[mask]
|
||||
|
||||
if len(bin_points) > 0:
|
||||
# Use median x and median y to find line center
|
||||
x_center = np.median(bin_points[:, 0])
|
||||
y_center = np.median(bin_points[:, 1])
|
||||
centered_points.append([x_center, y_center])
|
||||
|
||||
final_points = np.array(centered_points)
|
||||
final_points = final_points[final_points[:, 0].argsort()]
|
||||
|
||||
# Convert pixel coordinates to real-world data values
|
||||
x_scale = self.calibration["x_max"] - self.calibration["x_min"]
|
||||
|
||||
Reference in New Issue
Block a user