You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to own a part of array, just like what OpenCV does? #1097
Hi thanks for the lib! I want to own a part of array, just like what OpenCV does. Consider the (simplified) code:
fn read_and_crop_image() -> ??? {
let big_image: Array2<u8> = read_the_image();
let cropped = big_image.slice(s![0..3000, 0..4000]);
cropped // ERROR!!
}
However it errors, since ArrayView should not outlive the Array it belongs to. I have tried ArcArray but it does not seem to support .slice() as well.
With OpenCV I can do:
fn read_and_crop_image() -> Mat {
let big_image: Mat = read_the_image();
let cropped = Mat::roi([0..3000, 0..4000], big_image);
cropped // no problem, it is another Mat, and underlying it is reference counting
}
You could also do something similar with ArcArray. The key is to return an owned array type (Array or ArcArray), rather than a view of an array which will be dropped at the end of the function body.
Another option is to copy the relevant portion, which may be beneficial if the original image is much larger than the cropped portion:
Activity
jturner314 commentedon Oct 29, 2021
You could use
.slice_move(),.slice_collapse(), or other similar methods, and return an ownedArray2<u8>, e.g.:You could also do something similar with
ArcArray. The key is to return an owned array type (ArrayorArcArray), rather than a view of an array which will be dropped at the end of the function body.Another option is to copy the relevant portion, which may be beneficial if the original image is much larger than the cropped portion:
fzyzcjy commentedon Oct 29, 2021
Thank you!