JuliaImages / Images.jl

An image library for Julia
http://juliaimages.org/
Other
535 stars 143 forks source link

add a channelwise macro #817

Closed johnnychen94 closed 3 years ago

johnnychen94 commented 5 years ago

I don't have a specific implementation in mind, but the following usage is really common:

cv_x = channelview(x)
cv_y = channelview(y)
cv_z = foo(cv_x, cv_y)
z = colorview(colortype(eltype(x)), cv_z)

If we could replace this as something like...

@channelwise z = foo(x, y)

Similarly, replace

cv_x = channelview(x)
cv_y = channelview(y)
rst = psnr(cv_x, cv_y)

as

rst = @channelwise psnr(x, y)

This is a very draft idea.

Tokazama commented 5 years ago

Is the main advantage here more concise code?

johnnychen94 commented 5 years ago

That's one and the main advantage.

To support N-D array, in JuliaImages we take pixels as array elements. This is a great design, however, this would cause confusion when someone directly calls a function without channelview it.

For example, for RGB images, Minkowski{2} distance isn't equivalent to Euclidean distance. https://github.com/JuliaImages/ImageDistances.jl/issues/16#issuecomment-519754529

Introducing this macro would make this situation easy to explain and easy to avoid. "Okay, you forgot to use it channelwisely"

If both channelwised version and non-channelwised version can be called in one line; it's much easier to spell 😃 .

AliMalik9599 commented 4 years ago

Hey @johnnychen94 I am planning on working on this if it hasn't been added. I am relatively new to Julia, any tips on getting started? Thanks!

johnnychen94 commented 4 years ago

I believe some understanding of the color systems of JuliaImages, and the metaprogramming would be sufficient for you to start with. Actually, there's a WIP PR for this https://github.com/JuliaImages/ImageCore.jl/pull/105 that you can use as a reference. Part of the reason I didn't continue it is because I wasn't very familiar with macros and I am still not. :cry:

Speaking of performance, this @channelview is probably not a good option (see the comments https://github.com/JuliaImages/ImageCore.jl/pull/105#issuecomment-569506758) , but I should say we need benchmarks to say so.

johnnychen94 commented 3 years ago

Abusing channelview has the tendency to goes against our best practice and thus hurt the performance so now I realize that we shouldn't provide this.

I'm closing this in favor of the docs https://github.com/JuliaImages/juliaimages.github.io/issues/206 and alternative https://github.com/JuliaImages/ImageCore.jl/issues/170