Skip to main content

scirs2_vision/
lib.rs

1//! # SciRS2 Computer Vision
2//!
3//! **scirs2-vision** provides comprehensive computer vision and image processing capabilities
4//! built on SciRS2's scientific computing infrastructure, offering torchvision/OpenCV-compatible
5//! APIs with Rust's performance and safety.
6//!
7//! ## 🎯 Key Features
8//!
9//! - **Feature Detection**: SIFT, ORB, Harris corners, LoG blob detection
10//! - **Image Segmentation**: Watershed, region growing, graph cuts, semantic segmentation
11//! - **Edge Detection**: Sobel, Canny, Prewitt, Laplacian
12//! - **Image Registration**: Homography, affine, perspective transformations
13//! - **Color Processing**: Color space conversions, histogram operations
14//! - **Advanced Processing**: Super-resolution, HDR, denoising
15//! - **Object Tracking**: DeepSORT, Kalman filtering
16//! - **Performance**: SIMD acceleration, parallel processing, GPU support
17//!
18//! ## 📦 Module Overview
19//!
20//! | Module | Description | Python Equivalent |
21//! |--------|-------------|-------------------|
22//! | [`feature`] | Feature detection and matching (SIFT, ORB, Harris) | OpenCV features2d |
23//! | [`segmentation`] | Image segmentation algorithms | scikit-image.segmentation |
24//! | [`preprocessing`] | Image filtering and enhancement | torchvision.transforms |
25//! | [`color`] | Color space conversions | OpenCV color |
26//! | [`transform`] | Geometric transformations | torchvision.transforms |
27//! | [`registration`] | Image registration and alignment | scikit-image.registration |
28//! | [`streaming`] | Real-time video processing | OpenCV VideoCapture |
29//!
30//! ## 🚀 Quick Start
31//!
32//! ### Installation
33//!
34//! Add to your `Cargo.toml`:
35//!
36//! ```toml
37//! [dependencies]
38//! scirs2-vision = "0.1.2"
39//! ```
40//!
41//! ### Feature Detection (Harris Corners)
42//!
43//! ```rust,no_run
44//! use scirs2_vision::harris_corners;
45//! use image::open;
46//!
47//! fn main() -> Result<(), Box<dyn std::error::Error>> {
48//!     // Load image
49//!     let img = open("image.jpg")?;
50//!
51//!     // Detect Harris corners
52//!     let corners = harris_corners(&img, 3, 0.04, 100.0)?;
53//!     println!("Detected Harris corners");
54//!
55//!     Ok(())
56//! }
57//! ```
58//!
59//! ### SIFT Feature Detection
60//!
61//! ```rust,no_run
62//! use scirs2_vision::detect_and_compute;
63//! use image::open;
64//!
65//! fn main() -> Result<(), Box<dyn std::error::Error>> {
66//!     let img = open("image.jpg")?;
67//!
68//!     // Detect SIFT keypoints and compute descriptors
69//!     let descriptors = detect_and_compute(&img, 500, 0.03)?;
70//!     println!("Detected {} SIFT features", descriptors.len());
71//!
72//!     Ok(())
73//! }
74//! ```
75//!
76//! ### Edge Detection (Sobel)
77//!
78//! ```rust,no_run
79//! use scirs2_vision::sobel_edges;
80//! use image::open;
81//!
82//! fn main() -> Result<(), Box<dyn std::error::Error>> {
83//!     let img = open("image.jpg")?;
84//!
85//!     // Detect edges using Sobel operator
86//!     let edges = sobel_edges(&img, 0.1)?;
87//!     println!("Edge map computed");
88//!
89//!     Ok(())
90//! }
91//! ```
92//!
93//! ### Image Segmentation (Watershed)
94//!
95//! ```rust,no_run
96//! use scirs2_vision::watershed;
97//! use image::open;
98//!
99//! fn main() -> Result<(), Box<dyn std::error::Error>> {
100//!     let img = open("image.jpg")?;
101//!
102//!     // Perform watershed segmentation
103//!     let segments = watershed(&img, None, 8)?;
104//!     println!("Segmented into regions");
105//!
106//!     Ok(())
107//! }
108//! ```
109//!
110//! ### Color Space Conversion
111//!
112//! ```rust,no_run
113//! use scirs2_vision::{rgb_to_grayscale, rgb_to_hsv};
114//! use image::open;
115//!
116//! fn main() -> Result<(), Box<dyn std::error::Error>> {
117//!     let img = open("image.jpg")?;
118//!
119//!     // Convert to grayscale
120//!     let gray = rgb_to_grayscale(&img, None)?;
121//!
122//!     // Convert to HSV
123//!     let hsv = rgb_to_hsv(&img)?;
124//!
125//!     Ok(())
126//! }
127//! ```
128//!
129//! ### Image Registration (Homography)
130//!
131//! ```rust,no_run
132//! use scirs2_vision::find_homography;
133//!
134//! fn main() -> Result<(), Box<dyn std::error::Error>> {
135//!     // Source and destination points
136//!     let src_points = vec![(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)];
137//!     let dst_points = vec![(10.0, 10.0), (110.0, 5.0), (105.0, 105.0), (5.0, 110.0)];
138//!
139//!     // Find homography matrix
140//!     let (_h, _inliers) = find_homography(&src_points, &dst_points, 3.0, 0.99)?;
141//!
142//!     // Warp image using homography
143//!     // let warped = warp_perspective(&img, &h, (width, height))?;
144//!
145//!     Ok(())
146//! }
147//! ```
148//!
149//! ### Super-Resolution
150//!
151//! ```rust,no_run
152//! use scirs2_vision::{SuperResolutionProcessor, SuperResolutionMethod};
153//!
154//! fn main() -> Result<(), Box<dyn std::error::Error>> {
155//!     let processor = SuperResolutionProcessor::new(
156//!         2, // scale factor
157//!         SuperResolutionMethod::ESRCNN
158//!     )?;
159//!
160//!     // Upscale image
161//!     // let upscaled = processor.process(&low_res_image)?;
162//!
163//!     Ok(())
164//! }
165//! ```
166//!
167//! ### Object Tracking (DeepSORT)
168//!
169//! ```rust,no_run
170//! use scirs2_vision::{DeepSORT, Detection, TrackingBoundingBox};
171//!
172//! fn main() -> Result<(), Box<dyn std::error::Error>> {
173//!     let mut tracker = DeepSORT::new();
174//!
175//!     // For each frame
176//!     let bbox = TrackingBoundingBox::new(10.0, 10.0, 50.0, 50.0, 0.9, 0);
177//!     let detections = vec![Detection::new(bbox)];
178//!
179//!     let tracks = tracker.update(detections)?;
180//!     for track in tracks {
181//!         println!("Track ID: {}, Position: {:?}", track.id, track.get_bbox());
182//!     }
183//!
184//!     Ok(())
185//! }
186//! ```
187//!
188//! ## 🧠 Feature Detection Methods
189//!
190//! ### Classical Features
191//!
192//! - **Harris Corners**: Fast corner detection algorithm
193//! - **SIFT**: Scale-Invariant Feature Transform (rotation and scale invariant)
194//! - **ORB**: Oriented FAST and Rotated BRIEF (efficient alternative to SIFT)
195//! - **LoG Blobs**: Laplacian of Gaussian blob detection
196//!
197//! ### Neural Features
198//!
199//! - **SuperPoint**: Learned keypoint detector and descriptor
200//! - **Learned SIFT**: Neural network-enhanced SIFT
201//!
202//! ### Feature Matching
203//!
204//! - **Brute-force matching**: Exhaustive descriptor comparison
205//! - **Neural matching**: Attention-based feature matching
206//!
207//! ## 🎨 Segmentation Methods
208//!
209//! ### Classical Segmentation
210//!
211//! - **Watershed**: Marker-based segmentation
212//! - **Region Growing**: Similarity-based region merging
213//! - **Graph Cuts**: Energy minimization segmentation
214//! - **Threshold**: Otsu, adaptive thresholding
215//!
216//! ### Advanced Segmentation
217//!
218//! - **Semantic Segmentation**: Deep learning-based pixel classification
219//! - **Instance Segmentation**: Object detection + segmentation
220//!
221//! ## 🚄 Performance
222//!
223//! scirs2-vision leverages multiple optimization strategies:
224//!
225//! - **SIMD**: Vectorized operations for pixel processing
226//! - **Parallel**: Multi-threaded execution for large images
227//! - **GPU**: CUDA/OpenCL support for accelerated operations
228//! - **Memory Efficient**: Zero-copy views, chunked processing
229//!
230//! ### Thread Safety
231//!
232//! All functions are thread-safe and can be called concurrently on different images.
233//! When the `parallel` feature is enabled, many algorithms automatically utilize
234//! multiple CPU cores:
235//!
236//! - Gradient computations in edge detection
237//! - Pixel-wise operations in preprocessing
238//! - Feature detection across image regions
239//! - Segmentation clustering steps
240//!
241//! Note: Mutable image data should not be shared between threads without proper synchronization.
242//!
243//! ## 📊 Comparison with Other Libraries
244//!
245//! | Feature | OpenCV | torchvision | scirs2-vision |
246//! |---------|--------|-------------|---------------|
247//! | Feature Detection | ✅ | ❌ | ✅ |
248//! | Segmentation | ✅ | ⚠️ (limited) | ✅ |
249//! | Geometric Transforms | ✅ | ✅ | ✅ |
250//! | GPU Support | ✅ | ✅ | ✅ (limited) |
251//! | Type Safety | ❌ | ❌ | ✅ |
252//! | Memory Safety | ❌ | ⚠️ | ✅ |
253//! | Pure Rust | ❌ | ❌ | ✅ |
254//!
255//! ## 🔗 Integration with SciRS2 Ecosystem
256//!
257//! - **scirs2-ndimage**: Low-level image operations
258//! - **scirs2-linalg**: Matrix operations for geometric transforms
259//! - **scirs2-neural**: Deep learning models for advanced tasks
260//! - **scirs2-stats**: Statistical analysis of image features
261//! - **scirs2-cluster**: Clustering for segmentation
262//!
263//! ## 🔒 Version
264//!
265//! Current version: **0.1.2** (Released January 15, 2026)
266
267#![warn(missing_docs)]
268
269// Re-export image crate with the expected name
270// (using standard import instead of deprecated extern crate)
271
272pub mod color;
273pub mod error;
274pub mod feature;
275pub mod gpu_ops;
276/// Image preprocessing functionality
277///
278/// Includes operations like filtering, histogram manipulation,
279/// and morphological operations.
280pub mod preprocessing;
281pub mod quality;
282pub mod registration;
283pub mod segmentation;
284pub mod simd_ops;
285pub mod streaming;
286
287// Advanced mode enhancements - cutting-edge computer vision
288pub mod ai_optimization;
289pub mod neuromorphic_streaming;
290pub mod quantum_inspired_streaming;
291pub mod transform;
292
293// Advanced Advanced-mode modules - future development features
294pub mod activity_recognition;
295pub mod scene_understanding;
296pub mod visual_reasoning;
297pub mod visual_slam;
298
299// Cross-module Advanced coordination
300/// Advanced Integration - Cross-Module AI Coordination
301///
302/// This module provides the highest level of AI integration across all SciRS2 modules,
303/// combining quantum-inspired processing, neuromorphic computing, advanced AI optimization,
304/// and cross-module coordination into a unified Advanced processing framework.
305///
306/// # Features
307///
308/// * **Cross-Module Coordination** - Unified Advanced across vision, clustering, spatial, neural
309/// * **Global Optimization** - Multi-objective optimization across all modules
310/// * **Unified Meta-Learning** - Cross-module transfer learning and adaptation
311/// * **Resource Management** - Optimal allocation of computational resources
312/// * **Performance Tracking** - Comprehensive monitoring and optimization
313pub mod integration;
314
315/// Advanced Performance Benchmarking for Advanced Mode
316///
317/// This module provides comprehensive performance benchmarking capabilities
318/// for all Advanced mode features, including quantum-inspired processing,
319/// neuromorphic computing, AI optimization, and cross-module coordination.
320///
321/// # Features
322///
323/// * **Comprehensive Benchmarking** - Full performance analysis across all Advanced features
324/// * **Statistical Analysis** - Advanced statistical metrics and trend analysis
325/// * **Resource Monitoring** - Detailed resource usage tracking and optimization
326/// * **Quality Assessment** - Accuracy, consistency, and quality metrics
327/// * **Scalability Analysis** - Performance scaling with different workloads
328/// * **Comparative Analysis** - Speedup and advantage measurements vs baseline
329pub mod performance_benchmark;
330
331// Comment out problematic modules during tests to focus on fixing other issues
332#[cfg(not(test))]
333/// Private transform module for compatibility
334///
335/// Contains placeholder modules that help maintain compatibility
336/// with external code that might reference these modules directly.
337pub mod _transform {
338    /// Non-rigid transformation compatibility module
339    pub mod non_rigid {}
340    /// Perspective transformation compatibility module
341    pub mod perspective {}
342}
343
344// Re-export commonly used items
345pub use error::{Result, VisionError};
346
347// Re-export feature functionality (select items to avoid conflicts)
348pub use feature::{
349    array_to_image,
350    descriptor::{detect_and_compute, match_descriptors, Descriptor, KeyPoint},
351    find_homography,
352    harris_corners,
353    image_to_array,
354    laplacian::{laplacian_edges, laplacian_of_gaussian},
355    log_blob::{log_blob_detect, log_blobs_to_image, LogBlob, LogBlobConfig},
356    orb::{detect_and_compute_orb, match_orb_descriptors, OrbConfig, OrbDescriptor},
357    prewitt::prewitt_edges,
358    sobel::sobel_edges_simd,
359    sobel_edges,
360    AdvancedDenoiser,
361    AppearanceExtractor,
362    AttentionFeatureMatcher,
363    DeepSORT,
364    DenoisingMethod,
365    Detection,
366    // Advanced enhancement features
367    HDRProcessor,
368    KalmanFilter,
369    LearnedSIFT,
370    NeuralFeatureConfig,
371    NeuralFeatureMatcher,
372    SIFTConfig,
373    // Neural features
374    SuperPointNet,
375    SuperResolutionMethod,
376    SuperResolutionProcessor,
377    ToneMappingMethod,
378    Track,
379    TrackState,
380    // Advanced tracking features
381    TrackingBoundingBox,
382    TrackingMetrics,
383};
384// Re-export with unique name to avoid ambiguity
385pub use feature::homography::warp_perspective as feature_warp_perspective;
386
387// Re-export segmentation functionality
388pub use segmentation::*;
389
390// Re-export preprocessing functionality
391pub use preprocessing::*;
392
393// Re-export color functionality
394pub use color::*;
395
396// Re-export transform functionality (select items to avoid conflicts)
397pub use transform::{
398    affine::{estimate_affine_transform, warp_affine, AffineTransform, BorderMode},
399    non_rigid::{
400        warp_elastic, warp_non_rigid, warp_thin_plate_spline, ElasticDeformation, ThinPlateSpline,
401    },
402    perspective::{correct_perspective, BorderMode as PerspectiveBorderMode, PerspectiveTransform},
403};
404// Re-export with unique name to avoid ambiguity
405pub use transform::perspective::warp_perspective as transform_warp_perspective;
406
407// Re-export SIMD operations
408pub use simd_ops::{
409    check_simd_support, simd_convolve_2d, simd_gaussian_blur, simd_histogram_equalization,
410    simd_normalize_image, simd_sobel_gradients, SimdPerformanceStats,
411};
412
413// Re-export GPU operations
414pub use gpu_ops::{
415    gpu_batch_process, gpu_convolve_2d, gpu_gaussian_blur, gpu_harris_corners, gpu_sobel_gradients,
416    GpuBenchmark, GpuMemoryStats, GpuVisionContext,
417};
418
419// Re-export streaming operations
420pub use streaming::{
421    AdvancedStreamPipeline, AdvancedStreamProcessor, BlurStage, EdgeDetectionStage, Frame,
422    FrameMetadata, GrayscaleStage, PipelineMetrics, ProcessingStage, SimplePerformanceMonitor,
423    StreamPipeline, StreamProcessor, VideoStreamReader,
424};
425
426// Re-export Advanced mode enhancements
427pub use quantum_inspired_streaming::{
428    ProcessingDecision, QuantumAdaptiveStreamPipeline, QuantumAmplitude, QuantumAnnealingStage,
429    QuantumEntanglementStage, QuantumProcessingState, QuantumStreamProcessor,
430    QuantumSuperpositionStage,
431};
432
433pub use neuromorphic_streaming::{
434    AdaptiveNeuromorphicPipeline, EfficiencyMetrics, EventDrivenProcessor, EventStats,
435    NeuromorphicEdgeDetector, NeuromorphicMode, NeuromorphicProcessingStats, PlasticSynapse,
436    SpikingNeuralNetwork, SpikingNeuron,
437};
438
439pub use ai_optimization::{
440    ArchitecturePerformance, GeneticPipelineOptimizer, NeuralArchitectureSearch, PerformanceMetric,
441    PipelineGenome, PredictiveScaler, ProcessingArchitecture, RLParameterOptimizer,
442    ScalingRecommendation, SearchStrategy,
443};
444
445// Re-export advanced Advanced-mode features
446pub use scene_understanding::{
447    analyze_scene_with_reasoning, ContextualReasoningEngine, DetectedObject as SceneObject,
448    SceneAnalysisResult, SceneGraph, SceneUnderstandingEngine, SpatialRelation,
449    SpatialRelationType, TemporalInfo,
450};
451
452pub use visual_reasoning::{
453    perform_advanced_visual_reasoning, QueryType, ReasoningAnswer, ReasoningStep,
454    UncertaintyQuantification, VisualReasoningEngine, VisualReasoningQuery, VisualReasoningResult,
455};
456
457pub use activity_recognition::{
458    monitor_activities_realtime, recognize_activities_comprehensive, ActivityRecognitionEngine,
459    ActivityRecognitionResult, ActivitySequence, ActivitySummary as ActivitySceneSummary,
460    DetectedActivity, MotionCharacteristics, PersonInteraction, TemporalActivityModeler,
461};
462
463pub use visual_slam::{
464    process_visual_slam, process_visual_slam_realtime, CameraPose, CameraTrajectory, LoopClosure,
465    Map3D, SLAMResult, SLAMSystemState, SemanticMap, VisualSLAMSystem,
466};
467
468// Re-export Advanced integration functionality
469pub use integration::{
470    batch_process_advanced, process_with_advanced_mode, realtime_advanced_stream,
471    AdvancedProcessingResult, CrossModuleAdvancedProcessingResult, EmergentBehaviorDetection,
472    FusionQualityIndicators, NeuralQuantumHybridProcessor, PerformanceMetrics,
473    UncertaintyQuantification as AdvancedUncertaintyQuantification,
474};
475
476// Re-export performance benchmarking functionality
477pub use performance_benchmark::{
478    AdvancedBenchmarkSuite, BenchmarkConfig, BenchmarkResult, ComparisonMetrics,
479    PerformanceMetrics as BenchmarkPerformanceMetrics, QualityMetrics, ResourceUsage,
480    ScalabilityMetrics, StatisticalSummary,
481};