197 lines
5.3 KiB
Markdown
197 lines
5.3 KiB
Markdown
# Data Extraction from Diagram Images
|
||
|
||
This toolkit extracts data points from PNG diagrams containing plots with x and y axes and curves/waves.
|
||
|
||
## Files Overview
|
||
|
||
- **extract_data.py** - Main extraction script that detects axes and extracts curve data points
|
||
- **calibrate_and_visualize.py** - Visualization and calibration tool for converting normalized coordinates to actual values
|
||
- **extracted_data.json** - JSON file containing all extracted data points in normalized coordinates (0-1 range)
|
||
- **1_extracted.csv / 3_extracted.csv** - CSV files with extracted normalized data points
|
||
|
||
## Quick Start
|
||
|
||
### Step 1: Extract Data from Your Images
|
||
|
||
The extraction script has already been run. It processes all PNG files in the directory and creates:
|
||
|
||
```bash
|
||
python extract_data.py
|
||
```
|
||
|
||
This will generate:
|
||
- `extracted_data.json` - Complete extraction results
|
||
- `{image_name}_extracted.csv` - CSV files with normalized coordinates
|
||
|
||
### Step 2: Calibrate and Visualize
|
||
|
||
The extracted data is in **normalized coordinates** (x and y range from 0 to 1). To convert these to actual values from your diagrams:
|
||
|
||
```bash
|
||
python calibrate_and_visualize.py
|
||
```
|
||
|
||
This tool will:
|
||
1. Generate visualization plots showing extracted points
|
||
2. Ask you to calibrate the axes by providing real-world values
|
||
3. Convert normalized coordinates to actual values
|
||
4. Save calibrated data to CSV files
|
||
|
||
## Understanding the Extracted Data
|
||
|
||
### Normalized Coordinates
|
||
The extraction produces coordinates in the range [0, 1]:
|
||
- `x=0` is the left edge of the plot area
|
||
- `x=1` is the right edge of the plot area
|
||
- `y=0` is the bottom of the plot area
|
||
- `y=1` is the top of the plot area
|
||
|
||
Example from extracted_data.json:
|
||
```json
|
||
"normalized_data_points": [
|
||
{"x": 0.0120, "y": 0.6439},
|
||
{"x": 0.0130, "y": 0.5682},
|
||
{"x": 0.0130, "y": 0.6061}
|
||
]
|
||
```
|
||
|
||
### Calibration to Real Values
|
||
|
||
To convert normalized coordinates to actual values, you need to know what the axes represent.
|
||
|
||
If your diagram shows:
|
||
- **X-axis**: ranges from 0 to 100 Hz
|
||
- **Y-axis**: ranges from -10 to 10 dB
|
||
|
||
Then calibration would be:
|
||
```
|
||
X-axis minimum: 0
|
||
X-axis maximum: 100
|
||
Y-axis minimum: -10
|
||
Y-axis maximum: 10
|
||
```
|
||
|
||
And normalized point `{x: 0.5, y: 0.5}` would become `{x: 50, y: 0}` (middle of both axes)
|
||
|
||
## Extraction Results Summary
|
||
|
||
### 1.png
|
||
- **Image size**: 500×286 pixels
|
||
- **Points extracted**: 269 data points
|
||
- **Plot area dimensions**: 460×264 pixels
|
||
|
||
### 3.png
|
||
- **Image size**: 971×666 pixels
|
||
- **Points extracted**: 1188 data points
|
||
- **Plot area dimensions**: 893×612 pixels
|
||
|
||
## Output Files
|
||
|
||
### Normalized Data (Raw Extraction)
|
||
- **extracted_data.json** - Complete extraction with plot boundaries and raw coordinates
|
||
- **{image}_extracted.csv** - Quick reference CSV format
|
||
|
||
Format:
|
||
```
|
||
x,y
|
||
0.0120,0.6439
|
||
0.0130,0.5682
|
||
...
|
||
```
|
||
|
||
### Calibrated Data (After Running calibrate_and_visualize.py)
|
||
- **calibrated_data.json** - Extraction with calibration parameters and actual values
|
||
- **{image}_calibrated.csv** - Calibrated data in CSV format
|
||
- **calibration_info.json** - Calibration parameters used
|
||
- **comparison_{image}.png** - Visualization plots
|
||
|
||
## Advanced Usage
|
||
|
||
### Manual Calibration
|
||
|
||
To manually calibrate without the interactive prompt, edit `calibrate_and_visualize.py`:
|
||
|
||
```python
|
||
calibration = {
|
||
'x_min': 0,
|
||
'x_max': 100,
|
||
'y_min': -10,
|
||
'y_max': 10
|
||
}
|
||
```
|
||
|
||
### Working with the JSON Data
|
||
|
||
Load and process the extracted data programmatically:
|
||
|
||
```python
|
||
import json
|
||
|
||
with open('extracted_data.json', 'r') as f:
|
||
data = json.load(f)
|
||
|
||
for image_name, results in data.items():
|
||
print(f"Image: {image_name}")
|
||
print(f"Points: {results['num_points']}")
|
||
|
||
# Access the data points
|
||
for point in results['normalized_data_points'][:5]:
|
||
print(f" x={point['x']:.4f}, y={point['y']:.4f}")
|
||
```
|
||
|
||
## Technical Details
|
||
|
||
### Detection Method
|
||
|
||
1. **Axis Detection**: Uses morphological operations to find horizontal and vertical lines
|
||
2. **Edge Detection**: Applies Canny edge detection to find curve boundaries
|
||
3. **Point Extraction**: Converts edge pixels to normalized coordinates
|
||
4. **Clustering**: Groups nearby pixels into single data points using DBSCAN
|
||
5. **Sorting**: Orders points by x-coordinate for consistency
|
||
|
||
### Parameters
|
||
|
||
Key parameters in `extract_data.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 |
|
||
|
||
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
|
||
- Check that curves aren't too light/faint
|
||
|
||
### Too many noisy points
|
||
- Increase DBSCAN eps value
|
||
- Increase Canny high threshold
|
||
|
||
### Axes not detected properly
|
||
- Check that axes are clearly visible dark lines
|
||
- May need to adjust morphological kernel sizes
|
||
|
||
## Dependencies
|
||
|
||
- opencv-python
|
||
- numpy
|
||
- pillow
|
||
- scipy
|
||
- scikit-learn
|
||
- matplotlib
|
||
|
||
All dependencies are pre-installed in the virtual environment.
|
||
|
||
## Questions or Issues?
|
||
|
||
The extraction is based on image processing techniques:
|
||
- If your diagrams have unusual axis formats or colors, the detection may need adjustment
|
||
- Scanned/photographed diagrams with poor quality may not extract well
|
||
- Digital diagrams work best
|
||
|