image-rs / image

Encoding and decoding images in Rust
Apache License 2.0
4.98k stars 618 forks source link

Option to normalize images #1913

Open vsuryamurthy opened 1 year ago

vsuryamurthy commented 1 year ago

Hello,

I would like to be able normalize images given a minimum and maximum bound. For instance, normalizing a Luma from 0-255 to 0-1 (or) Luma with its own min and max value.

The reasoning is that several image filters require a normalized image as an input before applying the filter.

I would like to first hear your opinion on if it makes sense to add it to the image crate. I would like to atleast have an implementation for single channel grayscale images as RGB requires a separate discussion on how the normalize should happen over multiple channels. Based on your response, I can create a PR if necessary.

fintelia commented 1 year ago

I'm generally hesitant to add methods that can be implemented with the existing APIs in a line or two. I think what you're describing is a matter of:

ImageBuffer::from_fn(img.width(), img.height(), |x, y| {
    Luma([(img.get_pixel(x,y).0[0] - minimum) as f32 / (maximum - minimum) as f32])
})

This don't mean we should never add such methods, but we should first consider how commonly the specific operation is likely to be used

vsuryamurthy commented 1 year ago

Agreed, it is a very small piece of code. However, I believe there are several cases, filters mainly, that require normalization. Maybe because of opencv, I am used to interpreting a grayscale as both 0-255 or 0-1. Anyways, I am not sure how to verify the use case of normalization given that there are several operations out there.

willhansen commented 1 year ago

I found my way here because I was expecting some sort of normalized function to already exist. Had to look up the find_extremes function too, because apparently ImageBuffers don't have min and max convenience methods.

For the sake of ease-of-use, please include all of those one-liner convenience functions, especially if someone else is offering to do the work. It goes miles for people new to using the library.