Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crop an image with OpenCV in Rust?

Tags:

opencv

rust

I am implementing a backend in Rust to run ONNX inferences on images using the opencv crate. Everything's working great but I have some doubts about a function I have written to crop an image at a specified location:

// assume coordinates are always valid
img
.col_range(&opencv::core::Range::new(xmin, xmax).unwrap()).unwrap()
.row_range(&opencv::core::Range::new(ymin, ymax).unwrap()).unwrap();

Is this the only way to accomplish a simple Mat crop? Hope I am not missing a better and more efficient solution.

like image 850
mascIT Avatar asked Oct 22 '25 05:10

mascIT


1 Answers

There's nothing inefficient with what you've written. OpenCV Mats use data-sharing by default so there's no worry that you're needlessly copying large portions of original image. There's many ways to "slice and dice" the data and what you've chosen is a sensible way to crop an image. In fact, its the primary way you would do this in other languages (from Cropping an Image using OpenCV):

  • Python: cropped_image = img[80:280, 150:330]
  • C++: Mat cropped_image = img(Range(80,280), Range(150,330));

What I would recommend, which may be more clear and direct in Rust, is to use Mat::roi (a.k.a. the "region of interest" constructor):

let cropped_image = Mat::roi(&img, opencv::core::Rect {
    x: xmin,
    y: ymin,
    width: xmax - xmin,
    height: ymax - ymin,
}).unwrap();

See also:

  • How to crop a CvMat in OpenCV?
  • How to crop an image in OpenCV using Python
like image 150
kmdreko Avatar answered Oct 24 '25 12:10

kmdreko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!