improved point detection

This commit is contained in:
2026-02-13 00:01:27 +03:30
parent 44d35ebc86
commit f20fbc9d0b
4 changed files with 98 additions and 45 deletions

View File

@@ -74,6 +74,25 @@ for point in result['data_points']:
print(f"x={point['x']:.4f}, y={point['y']:.4f}")
```
#### Controlling Point Density
To extract fewer points (less dense sampling):
```python
from extract_plot_data import DiagramExtractor
# Extract only 30 points (sparse sampling)
extractor = DiagramExtractor(
calibration={'x_min': 0, 'x_max': 100, 'y_min': -10, 'y_max': 10},
max_points=30
)
result = extractor.extract('path/to/diagram.png')
print(f"Extracted {len(result['data_points'])} points") # Will be 30 or fewer
```
Without `max_points`, automatic sampling extracts 50-200 points depending on curve complexity.
## Files Overview
- **src/extract_plot_data/** - Main package directory
@@ -97,10 +116,11 @@ uv run extract-plot-data
The tool will:
1. Ask for an images folder (defaults to the current directory)
2. Ask for axis calibration values (min/max for X and Y axes)
3. Optionally prompt per image to override detected axis pixel positions
4. Process all PNG files in the chosen images folder
5. Extract data points and convert to real-world coordinates
6. Save results to JSON and CSV files in `extracted_output/`
3. Ask for maximum points to extract (blank for automatic sampling)
4. Optionally prompt per image to override detected axis pixel positions
5. Process all PNG files in the chosen images folder
6. Extract data points and convert to real-world coordinates
7. Save results to JSON and CSV files in `extracted_output/`
### Step 3: Review Results
@@ -178,9 +198,11 @@ for image_name, results in data.items():
```python
from extract_plot_data import DiagramExtractor
# Create extractor with custom calibration
extractor = DiagramExtractor()
extractor.set_calibration(x_min=0, x_max=100, y_min=-10, y_max=10)
# Create extractor with custom calibration and sampling density
extractor = DiagramExtractor(
calibration={'x_min': 0, 'x_max': 100, 'y_min': -10, 'y_max': 10},
max_points=50 # Extract only 50 points for less dense sampling
)
# Process multiple images
import glob
@@ -204,34 +226,36 @@ result = extractor.extract(
1. **Axis Detection**: Uses Hough line detection (with morphology fallback) to find axes
2. **Edge Detection**: Applies Canny edge detection to find curve boundaries
3. **Point Extraction**: Converts edge pixels to real-world coordinates using calibration
4. **Clustering**: Groups nearby pixels into single data points using DBSCAN
3. **Point Extraction**: Divides x-axis into bins and takes median of detected points in each bin
4. **Line Centering**: Uses median y-values to center points on thick lines (handles edges on both sides)
5. **Sorting**: Orders points by x-coordinate for consistency
6. **Coordinate Conversion**: Direct transformation from pixels to real-world values
### Parameters
Key parameters in `extract_data.py`:
Key parameters in `extractor.py`:
| Parameter | Purpose | Default |
|-----------|---------|---------|
|-----------|---------|---------|
| `Canny low/high thresholds` | Edge detection sensitivity | 50, 150 |
| `DBSCAN eps` | Clustering radius for point grouping | 3 pixels |
| `Kernel sizes` | Morphological operation sizes | 50×1, 1×50 |
| `max_points` | Maximum number of points to extract | Auto (50-200) |
Adjust these if extraction quality is poor for your specific images.
## Troubleshooting
### Too few points extracted
- Increase Canny edge detection thresholds lower
- Reduce DBSCAN eps value for finer clustering
- Increase the `max_points` parameter or leave blank for automatic sampling
- Lower Canny edge detection thresholds
- Check that curves aren't too light/faint
### Too many noisy points
- Increase DBSCAN eps value
### Too many noisy or dense points
- Set `max_points` to a lower value (e.g., 20, 30, 50) to get sparser sampling
- Increase Canny high threshold
### Points not centered on line
- The algorithm now uses median binning to automatically center points on thick lines
- If still having issues, try adjusting Canny thresholds or check image quality
### Axes not detected properly
- Check that axes are clearly visible dark lines
- May need to adjust morphological kernel sizes
@@ -245,7 +269,6 @@ All dependencies are automatically managed by UV and specified in [pyproject.tom
- pillow - Image manipulation
- matplotlib - Visualization
- scipy - Scientific computing
- scikit-learn - Machine learning (DBSCAN clustering)
Install with:
```bash