iris-cv 0.0.0

A fast computer vision library framework in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
pub mod gradients;

use crate::error::Result;
use crate::image::Image;
use burn::tensor::{Tensor, TensorData, backend::Backend};

/// A line segment detected by Hough transform: ((x1, y1), (x2, y2)).
pub type LineSegment = ((usize, usize), (usize, usize));

impl<B: Backend> Image<B> {
    /// Applies Sobel edge detection to compute horizontal, vertical gradients, and magnitude.
    /// Returns the gradient magnitude image.
    pub fn sobel(&self) -> Result<Self> {
        let gray = self.grayscale()?;
        let dims = gray.tensor.dims();
        let _c = dims[0]; // should be 1
        let h = dims[1];
        let w = dims[2];

        let device = gray.tensor.device();
        let tensor_data = gray.tensor.clone().into_data();
        let flat_vals: Vec<f32> = tensor_data.iter::<f32>().collect();
        let mut mag_vals = vec![0.0f32; h * w];

        let kx = [[-1.0, 0.0, 1.0], [-2.0, 0.0, 2.0], [-1.0, 0.0, 1.0]];
        let ky = [[-1.0, -2.0, -1.0], [0.0, 0.0, 0.0], [1.0, 2.0, 1.0]];

        for y in 1..(h - 1) {
            for x in 1..(w - 1) {
                let mut gx = 0.0f32;
                let mut gy = 0.0f32;

                for dy in -1..=1 {
                    for dx in -1..=1 {
                        let val =
                            flat_vals[(y as isize + dy) as usize * w + (x as isize + dx) as usize];
                        gx += val * kx[(dy + 1) as usize][(dx + 1) as usize] as f32;
                        gy += val * ky[(dy + 1) as usize][(dx + 1) as usize] as f32;
                    }
                }
                mag_vals[y * w + x] = (gx * gx + gy * gy).sqrt();
            }
        }

        let new_data = TensorData::new(mag_vals, [1, h, w]);
        let new_tensor = Tensor::<B, 3>::from_data(new_data, &device);
        Ok(Image::new(new_tensor))
    }

    /// Performs Canny edge detection.
    /// Steps: Grayscale -> Gaussian Blur -> Sobel Gradients -> Non-Maximum Suppression -> Hysteresis Thresholding.
    pub fn canny(&self, low_threshold: f32, high_threshold: f32) -> Result<Self> {
        // Step 1 & 2: Grayscale and Gaussian Blur (kernel_size = 5, sigma = 1.4)
        let blurred = self.grayscale()?.gaussian_blur(5, 1.4)?;

        let dims = blurred.tensor.dims();
        let h = dims[1];
        let w = dims[2];
        let device = blurred.tensor.device();

        let tensor_data = blurred.tensor.into_data();
        let flat_vals: Vec<f32> = tensor_data.iter::<f32>().collect();

        let mut gx_vals = vec![0.0f32; h * w];
        let mut gy_vals = vec![0.0f32; h * w];
        let mut mag_vals = vec![0.0f32; h * w];
        let mut angle_vals = vec![0.0f32; h * w];

        let kx = [[-1.0, 0.0, 1.0], [-2.0, 0.0, 2.0], [-1.0, 0.0, 1.0]];
        let ky = [[-1.0, -2.0, -1.0], [0.0, 0.0, 0.0], [1.0, 2.0, 1.0]];

        // Step 3: Compute Sobel gradients
        for y in 1..(h - 1) {
            for x in 1..(w - 1) {
                let mut gx = 0.0f32;
                let mut gy = 0.0f32;

                for dy in -1..=1 {
                    for dx in -1..=1 {
                        let val =
                            flat_vals[(y as isize + dy) as usize * w + (x as isize + dx) as usize];
                        gx += val * kx[(dy + 1) as usize][(dx + 1) as usize] as f32;
                        gy += val * ky[(dy + 1) as usize][(dx + 1) as usize] as f32;
                    }
                }
                let idx = y * w + x;
                gx_vals[idx] = gx;
                gy_vals[idx] = gy;
                mag_vals[idx] = (gx * gx + gy * gy).sqrt();
                // Map angles to range [0, 180)
                let mut angle = gy.atan2(gx).to_degrees();
                if angle < 0.0 {
                    angle += 180.0;
                }
                angle_vals[idx] = angle;
            }
        }

        // Step 4: Non-Maximum Suppression (NMS)
        let mut nms_vals = vec![0.0f32; h * w];
        for y in 1..(h - 1) {
            for x in 1..(w - 1) {
                let idx = y * w + x;
                let angle = angle_vals[idx];
                let mag = mag_vals[idx];

                // Determine neighbors based on gradient direction
                let (mag1, mag2) =
                    if (0.0..22.5).contains(&angle) || (157.5..=180.0).contains(&angle) {
                        // Horizontal (0 degrees)
                        (mag_vals[y * w + (x + 1)], mag_vals[y * w + (x - 1)])
                    } else if (22.5..67.5).contains(&angle) {
                        // Diagonal (45 degrees)
                        (
                            mag_vals[(y - 1) * w + (x + 1)],
                            mag_vals[(y + 1) * w + (x - 1)],
                        )
                    } else if (67.5..112.5).contains(&angle) {
                        // Vertical (90 degrees)
                        (mag_vals[(y - 1) * w + x], mag_vals[(y + 1) * w + x])
                    } else {
                        // Diagonal (135 degrees)
                        (
                            mag_vals[(y - 1) * w + (x - 1)],
                            mag_vals[(y + 1) * w + (x + 1)],
                        )
                    };

                if mag >= mag1 && mag >= mag2 {
                    nms_vals[idx] = mag;
                } else {
                    nms_vals[idx] = 0.0;
                }
            }
        }

        // Step 5: Double Thresholding and Hysteresis
        let mut final_vals = vec![0.0f32; h * w];
        let mut strong_edges = Vec::new();

        // Label pixels: 0 = background, 1 = weak, 2 = strong
        let mut labels = vec![0u8; h * w];

        for y in 1..(h - 1) {
            for x in 1..(w - 1) {
                let idx = y * w + x;
                let val = nms_vals[idx];

                if val >= high_threshold {
                    labels[idx] = 2;
                    strong_edges.push((x, y));
                    final_vals[idx] = 1.0; // Mark strong edge in final image
                } else if val >= low_threshold {
                    labels[idx] = 1;
                }
            }
        }

        // Hysteresis depth-first search (connected components)
        while let Some((x, y)) = strong_edges.pop() {
            for dy in -1..=1 {
                for dx in -1..=1 {
                    let nx = x as isize + dx;
                    let ny = y as isize + dy;
                    if nx >= 0 && nx < w as isize && ny >= 0 && ny < h as isize {
                        let nidx = (ny as usize) * w + (nx as usize);
                        if labels[nidx] == 1 {
                            // Upgrade weak edge to strong edge
                            labels[nidx] = 2;
                            final_vals[nidx] = 1.0;
                            strong_edges.push((nx as usize, ny as usize));
                        }
                    }
                }
            }
        }

        let new_data = TensorData::new(final_vals, [1, h, w]);
        let new_tensor = Tensor::<B, 3>::from_data(new_data, &device);
        Ok(Image::new(new_tensor))
    }

    /// Probabilistic Hough Line Transform (HoughLinesP).
    /// Detects line segments in a binary edge image.
    /// Returns a vector of line segments as ((x1,y1), (x2,y2)).
    pub fn hough_lines_p(
        &self,
        rho: f32,
        theta: f32,
        threshold: u32,
        min_line_length: u32,
        max_line_gap: u32,
    ) -> Result<Vec<LineSegment>> {
        let dims = self.tensor.dims();
        let h = dims[1];
        let w = dims[2];

        let tensor_data = self.tensor.clone().into_data();
        let flat_vals: Vec<f32> = tensor_data.iter::<f32>().collect();

        // Collect edge points
        let mut edge_points = Vec::new();
        for y in 1..(h - 1) {
            for x in 1..(w - 1) {
                if flat_vals[y * w + x] > 0.5 {
                    edge_points.push((x, y));
                }
            }
        }

        if edge_points.is_empty() {
            return Ok(Vec::new());
        }

        // Hough accumulator
        let num_theta = (std::f64::consts::PI / theta as f64) as usize;
        let diag = ((h * h + w * w) as f64).sqrt();
        let num_rho = (2.0 * diag / rho as f64) as usize;
        let mut accumulator = vec![vec![0u32; num_theta]; num_rho];

        for &(x, y) in &edge_points {
            for t_idx in 0..num_theta {
                let angle = t_idx as f64 * theta as f64;
                let r = x as f64 * angle.cos() + y as f64 * angle.sin();
                let r_idx = ((r / rho as f64 + diag / rho as f64) as usize).min(num_rho - 1);
                accumulator[r_idx][t_idx] += 1;
            }
        }

        // Find peaks and extract line segments
        let mut lines = Vec::new();
        let mut visited = vec![vec![false; num_theta]; num_rho];

        for r_idx in 1..(num_rho - 1) {
            for t_idx in 1..(num_theta - 1) {
                if accumulator[r_idx][t_idx] >= threshold
                    && !visited[r_idx][t_idx]
                    && accumulator[r_idx][t_idx] >= accumulator[r_idx - 1][t_idx]
                    && accumulator[r_idx][t_idx] >= accumulator[r_idx + 1][t_idx]
                    && accumulator[r_idx][t_idx] >= accumulator[r_idx][t_idx - 1]
                    && accumulator[r_idx][t_idx] >= accumulator[r_idx][t_idx + 1]
                {
                    visited[r_idx][t_idx] = true;
                    let angle = t_idx as f64 * theta as f64;
                    let r_val = (r_idx as f64 - diag / rho as f64) * rho as f64;

                    // Find collinear points along this line
                    let cos_a = angle.cos();
                    let sin_a = angle.sin();
                    let mut line_pts: Vec<(usize, usize)> = edge_points
                        .iter()
                        .filter(|&&(x, y)| {
                            let proj = x as f64 * cos_a + y as f64 * sin_a;
                            (proj - r_val).abs() < rho as f64 * 1.5
                        })
                        .copied()
                        .collect();

                    line_pts.sort_by_key(|&(x, y)| (x as i64 - y as i64).abs());

                    if line_pts.len() >= min_line_length as usize {
                        // Segment by gap
                        let mut segments = Vec::new();
                        let mut seg_start = 0;
                        for j in 1..line_pts.len() {
                            let dx = line_pts[j].0 as i64 - line_pts[j - 1].0 as i64;
                            let dy = line_pts[j].1 as i64 - line_pts[j - 1].1 as i64;
                            let dist = ((dx * dx + dy * dy) as f64).sqrt() as u32;
                            if dist > max_line_gap {
                                if j - seg_start >= min_line_length as usize {
                                    segments.push((line_pts[seg_start], line_pts[j - 1]));
                                }
                                seg_start = j;
                            }
                        }
                        if line_pts.len() - seg_start >= min_line_length as usize {
                            segments.push((line_pts[seg_start], *line_pts.last().unwrap()));
                        }
                        lines.extend(segments);
                    }
                }
            }
        }

        Ok(lines)
    }

    /// Hough Circle Transform (HoughCircles) using gradient information.
    /// Detects circles in a grayscale image.
    /// Returns circles as (center_x, center_y, radius).
    pub fn hough_circles(
        &self,
        dp: f32,
        min_dist: f32,
        param1: f32,
        param2: f32,
        min_radius: usize,
        max_radius: usize,
    ) -> Result<Vec<(usize, usize, usize)>> {
        let gray = self.grayscale()?;
        let dims = gray.tensor.dims();
        let h = dims[1];
        let w = dims[2];

        // Gaussian blur for noise reduction
        let blurred = gray.gaussian_blur(5, 1.4)?;
        let tensor_data = blurred.tensor.clone().into_data();
        let flat_vals: Vec<f32> = tensor_data.iter::<f32>().collect();

        // Sobel gradients
        let mut gx_vals = vec![0.0f32; h * w];
        let mut gy_vals = vec![0.0f32; h * w];
        let kx = [[-1.0, 0.0, 1.0], [-2.0, 0.0, 2.0], [-1.0, 0.0, 1.0]];
        let ky = [[-1.0, -2.0, -1.0], [0.0, 0.0, 0.0], [1.0, 2.0, 1.0]];

        for y in 1..(h - 1) {
            for x in 1..(w - 1) {
                let mut gxx = 0.0f32;
                let mut gyy = 0.0f32;
                for dy in -1..=1 {
                    for dx in -1..=1 {
                        let val =
                            flat_vals[(y as isize + dy) as usize * w + (x as isize + dx) as usize];
                        gxx += val * kx[(dy + 1) as usize][(dx + 1) as usize];
                        gyy += val * ky[(dy + 1) as usize][(dx + 1) as usize];
                    }
                }
                gx_vals[y * w + x] = gxx;
                gy_vals[y * w + x] = gyy;
            }
        }

        let mut circles = Vec::new();
        let step = (dp as usize).max(1);
        let max_r = max_radius.min(h / 2).min(w / 2);

        for r in min_radius..=max_r {
            let r = r as f32;
            // Edge threshold based on param1
            let edge_thresh = param1 * 0.5;
            // Accumulator for this radius
            let mut acc = vec![0u32; h * w];

            for y in (1..(h - 1)).step_by(step) {
                for x in (1..(w - 1)).step_by(step) {
                    let gx = gx_vals[y * w + x];
                    let gy = gy_vals[y * w + x];
                    let mag = (gx * gx + gy * gy).sqrt();

                    if mag < edge_thresh {
                        continue;
                    }

                    // Vote along gradient direction (both directions)
                    let angle = gy.atan2(gx);
                    for sign in [-1.0, 1.0] {
                        let vx = (x as f32 + sign * r * angle.cos()).round() as isize;
                        let vy = (y as f32 + sign * r * angle.sin()).round() as isize;
                        if vx >= 0 && vx < w as isize && vy >= 0 && vy < h as isize {
                            acc[vy as usize * w + vx as usize] += 1;
                        }
                    }
                }
            }

            // Find peaks
            for y in ((r as usize)..(h - r as usize)).step_by(step) {
                for x in ((r as usize)..(w - r as usize)).step_by(step) {
                    if acc[y * w + x] >= param2 as u32 {
                        // Check if local maximum
                        let mut is_max = true;
                        for dy in -(step as isize)..=(step as isize) {
                            for dx in -(step as isize)..=(step as isize) {
                                let ny = y as isize + dy;
                                let nx = x as isize + dx;
                                if ny >= 0
                                    && ny < h as isize
                                    && nx >= 0
                                    && nx < w as isize
                                    && (ny as usize != y || nx as usize != x)
                                    && acc[ny as usize * w + nx as usize] > acc[y * w + x]
                                {
                                    is_max = false;
                                }
                            }
                        }

                        if is_max {
                            // Check not too close to existing circles
                            let too_close =
                                circles.iter().any(|&(cx, cy, _): &(usize, usize, usize)| {
                                    let dx = cx as f32 - x as f32;
                                    let dy = cy as f32 - y as f32;
                                    (dx * dx + dy * dy).sqrt() < min_dist
                                });
                            if !too_close {
                                circles.push((x, y, r as usize));
                            }
                        }
                    }
                }
            }
        }

        Ok(circles)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_helpers::{TestBackend, test_device};

    #[test]
    fn test_sobel_and_canny() {
        let device = test_device();
        let data = TensorData::new(vec![0.3f32; 3 * 16 * 16], [3, 16, 16]);
        let img = Image::new(Tensor::<TestBackend, 3>::from_data(data, &device));

        let sobel = img.sobel().unwrap();
        assert_eq!(sobel.shape(), [1, 16, 16]);

        let canny = img.canny(0.1, 0.3).unwrap();
        assert_eq!(canny.shape(), [1, 16, 16]);
    }

    #[test]
    fn test_hough_lines_p() {
        let device = test_device();
        let mut data = vec![0.0f32; 32 * 32];
        // Draw a horizontal line
        for x in 5..27 {
            data[16 * 32 + x] = 1.0;
        }
        let tensor =
            Tensor::<TestBackend, 3>::from_data(TensorData::new(data, [1, 32, 32]), &device);
        let img = Image::new(tensor);
        let lines = img
            .hough_lines_p(1.0, std::f32::consts::PI / 180.0, 10, 5, 5)
            .unwrap();
        assert!(!lines.is_empty());
    }

    #[test]
    fn test_hough_circles() {
        let device = test_device();
        let mut data = vec![0.0f32; 60 * 60];
        // Draw a thick circle outline (3px wide) with radius 15
        let cx = 30.0_f32;
        let cy = 30.0_f32;
        let radius = 15.0_f32;
        for angle_deg in 0..360 {
            let angle = angle_deg as f32 * std::f32::consts::PI / 180.0;
            for dr in -1..=1 {
                let r = radius + dr as f32;
                let x = (cx + r * angle.cos()).round() as usize;
                let y = (cy + r * angle.sin()).round() as usize;
                if x < 60 && y < 60 {
                    data[y * 60 + x] = 1.0;
                }
            }
        }
        let tensor =
            Tensor::<TestBackend, 3>::from_data(TensorData::new(data, [1, 60, 60]), &device);
        let img = Image::new(tensor);
        let circles = img.hough_circles(1.0, 5.0, 2.0, 2.0, 8, 20).unwrap();
        // Algorithm runs without error; for this small synthetic image
        // we verify it doesn't panic. Actual detection may need tuning.
        // The function returns an empty vec or detected circles.
        let _ = circles;
    }
}