Feature: Expand OpenCV API Bindings - Add Core and Image Processing Functions #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR significantly expands the API surface of
node-opencvby implementing 10 new high-value OpenCV functions across core operations and image processing, along with 20+ constants. All implementations follow the existing async task pattern using napi-rs and include comprehensive documentation.🎯 What's New
Core Operations (
core_funcs.rs)flip()- Flip images horizontally, vertically, or bothrotate()- Rotate images by 90°, 180°, or 270° with dedicated constantsmerge()- Combine multiple single-channel Mats into a multi-channel Matsplit()- Separate a multi-channel Mat into individual channel MatsinRange()- Create binary masks for pixels within specified value rangesImage Processing (
imgproc.rs)canny()- Canny edge detection with configurable thresholdsgaussianBlur()- Apply Gaussian blur with customizable kernel size and sigmaadaptiveThreshold()- Adaptive thresholding for varying lighting conditionsfindContours()- Detect contours in binary images with multiple retrieval modesdrawContours()- Draw contours on images with customizable colors and thicknessConstants & Type Definitions
Added 20+ essential OpenCV constants:
THRESH_BINARY,THRESH_OTSU,THRESH_TRIANGLE, etc.ADAPTIVE_THRESH_MEAN_C,ADAPTIVE_THRESH_GAUSSIAN_CRETR_EXTERNAL,RETR_LIST,RETR_CCOMP,RETR_TREECHAIN_APPROX_NONE,CHAIN_APPROX_SIMPLE, etc.ROTATE_90_CLOCKWISE,ROTATE_180,ROTATE_90_COUNTERCLOCKWISE📚 Documentation
Added comprehensive
README.mdwith:🧪 Testing
All implementations are thoroughly tested:
Test results:
🚀 Performance
All operations:
AsyncTaskAbortSignalfor cancellation💡 Technical Implementation
release()📊 Impact
🔗 Addresses
Closes #[issue_number] - Systematically expands the OpenCV API bindings with core functionality and image processing operations, making the library more powerful and comprehensive for Node.js developers.
Breaking Changes: None - all additions are backward compatible
Future Work: This provides a solid foundation for implementing remaining features from the original issue (ORB detector, BFMatcher, CascadeClassifier, Hough transforms).
Original prompt
This section details on the original issue you should resolve
<issue_title>Feature: Expand OpenCV API Bindings</issue_title>
<issue_description>### Description
The goal of this issue is to systematically expand the API surface of
node-opencv, making the library more powerful and comprehensive for NodeJS developers. This involves identifying high-value OpenCV functions, wrapping them from the underlyingopencv-rustcrate, and exposing them to JavaScript usingnapi-rs.This task can be broken down into implementing functions from several key OpenCV modules. Contributors can pick a single function or a whole module to work on.
To-Do List: API Modules to Implement
Please check off items as you begin working on them and link your Pull Request to this issue.
कोर Functionality (
core)cv::core::flip()- Flip an image horizontally, vertically, or both.cv::core::rotate()- Rotate an image by 90, 180, or 270 degrees.cv::core::merge()- Merge several single-channel Mats into a multi-channel Mat.cv::core::split()- Split a multi-channel Mat into several single-channel Mats.cv::core::in_range()- Check if array elements lie between the elements of two other arrays.Image Processing (
imgproc)cv::imgproc::gaussian_blur()- (Already exists, but could be a reference).cv::imgproc::threshold()- Apply a fixed-level threshold to a single-channel array. (Different threshold types likeTHRESH_BINARY,THRESH_OTSUshould be supported).cv::imgproc::adaptive_threshold()- Apply an adaptive threshold to an array.cv::imgproc::find_contours()- Find contours in a binary image. This will be complex, as it needs to return an array of points.cv::imgproc::draw_contours()- Draw contours on an image.cv::imgproc::canny()- Find edges in an image using the Canny algorithm.cv::imgproc::hough_lines_p()- Find line segments in a binary image using the probabilistic Hough transform.Feature Detection and Description (
features2d)This module is more complex as it often involves classes.
cv::features2d::ORBdetector.ORB::create()- A static method to create an ORB detector instance.ORB::detect_and_compute()- To find keypoints and compute descriptors. The return value should be a JS object like{ keypoints: KeyPoint[], descriptors: Buffer }.cv::features2d::BFMatcher(Brute-Force Matcher).BFMatcher::create()BFMatcher::knn_match()- To find the k best matches for each descriptor.Object Detection (
objdetect)cv::objdetect::CascadeClassifierCascadeClassifier::new()- To create a new classifier.CascadeClassifier::load()- To load a pre-trained model from an XML file (e.g., Haar cascades). This function should accept a file path as a string.CascadeClassifier::detect_multi_scale()- To detect objects of different sizes in the input image. Should return an array of rectangles (Rect).Implementation Workflow for a Single Function
Here is the general process for wrapping a new function:
Select a Function: Pick an unchecked item from the to-do list above.
Research the Signature: Look up the function in the opencv-rust documentation to understand its parameters and return types.
Create the N-API Wrapper:
src/imgproc.rs), create a newpub fnand annotate it with#[napi].Bufferfor image data,i32for numbers,Stringfor paths).napi::Resultcontaining the JS-compatible output.Example Template (
canny):Write a Test:
__test__/directory (e.g.,imgproc.spec.ts).Bufferis not empty and has the expected length.Document the API:
Fixes #2
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.