Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cropping a Mat #563

Closed
Dirleye opened this issue Apr 17, 2024 · 3 comments
Closed

Cropping a Mat #563

Dirleye opened this issue Apr 17, 2024 · 3 comments

Comments

@Dirleye
Copy link

Dirleye commented Apr 17, 2024

How would you go about cropping a Mat from a Rect?

The simplest way I can think of currently is by creating a whole new Mat and moving it back over (I haven't actually tested this and could be completely misunderstanding what assign_to_def() does):

let region: Rect = get_regeion_from_somewhere();
let mut target_mat: Mat = get_mat_from_somewhere();

let mut cropped: Mat = Mat::default();
target_mat.roi(region).unwrap().assign_to_def(&mut cropped).unwrap();
target_mat = cropped;

But that feels like too much work for something this simple.
Am I missing something really obvious?

Thanks for any help.

@twistedfall
Copy link
Owner

twistedfall commented Apr 18, 2024

If you need an independent Mat then something like this should work:

let cropped = target_mat.roi(region)?.try_clone()?;
let target_mat = cropped;

@Dirleye
Copy link
Author

Dirleye commented Apr 18, 2024

Ah so cloning into a new Mat is definitely needed. That is simpler though so thank you.

@Dirleye Dirleye closed this as completed Apr 18, 2024
@twistedfall
Copy link
Owner

Yeah, I don't think that OpenCV supports in-place crop at all, so one or the other form of copying is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants