image-rs / imageproc

Image processing operations
MIT License
758 stars 149 forks source link

Detailed error for generic type parameter of find_contours_with_threshold #694

Open sakird opened 13 hours ago

sakird commented 13 hours ago

I am learning Rust and tried to use find_contours_with_threshold function that I have asked ChatGPT to prepare.

Calling: let contours = find_contours_with_threshold::<u8>(&binary_edges, 0u8);

Image dimensions were too big for u8. And since I am new to Rust I have been trying to find the cause of this error. I have finally managed to find it, here:

contour_points.push(Point::new(cast(pos3.x).unwrap(), cast(pos3.y).unwrap()));

After changing that dimension to u32, it worked. This cast panics if image width or height is bigger than max of u8. Would it be OK to update this code like this:

contour_points.push(Point::new( cast(pos3.x).expect("Generic data type should be bigger than width."), cast(pos3.y).expect("Generic data type should be bigger than height."), ));

If this is acceptable, I can send a PR.

ripytide commented 7 hours ago

That seems like an improvement to me, since this is a user facing error then having an error message is better than nothing. Thought I might make the error message : Generic Type Parameter T should be bigger than width/Generic Type Parameter T should be bigger than height.

sakird commented 7 hours ago

Then I will send a PR. Thanks.