-
-
Notifications
You must be signed in to change notification settings - Fork 886
Add AutoLevel Processor #1619
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
Add AutoLevel Processor #1619
Conversation
| Rectangle sourceRectangle) | ||
| : base(configuration, source, sourceRectangle) | ||
| { | ||
| // TODO I don't know how to get channel bit depth for non-grayscale types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know what to do here
Codecov Report
@@ Coverage Diff @@
## master #1619 +/- ##
==========================================
+ Coverage 84.30% 84.31% +0.01%
==========================================
Files 816 818 +2
Lines 35919 35977 +58
Branches 4185 4189 +4
==========================================
+ Hits 30280 30334 +54
- Misses 4813 4815 +2
- Partials 826 828 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
| : base(configuration, source, sourceRectangle) | ||
| { | ||
| // TODO I don't know how to get channel bit depth for non-grayscale types | ||
| if (!(typeof(TPixel) == typeof(L16) || typeof(TPixel) == typeof(L8))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be ok to take the PixelType.BitsPerPixel and divide it by Unsafe.SizeOf<T>(). That will give you a per-pixel-component average (since all our pixel formats are blittable) which should be close enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I would try that with L16
Unsafe.SizeOf<L16>() would return 2
BitsPerPixel would return 16
The result is half of what it needs to be? (L16 is only 1 component?)
Or did you mean it's an operation only for non-luminescence types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right yep, my bad We need a ChannelCount property on PixelTypeInfo.
@antonfirsov This would be a breaking change (which we can probably allow for 1.1 release) but do you have any ideas how we could introduce it in a non breaking way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A hack you could do just now is something similar to what we do in the png encoder, I.E use a switch for known types and a default for unknown.
ImageSharp/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs
Lines 159 to 174 in a8cb711
| return typeof(TPixel) switch | |
| { | |
| Type t when t == typeof(A8) => PngColorType.GrayscaleWithAlpha, | |
| Type t when t == typeof(Argb32) => PngColorType.RgbWithAlpha, | |
| Type t when t == typeof(Bgr24) => PngColorType.Rgb, | |
| Type t when t == typeof(Bgra32) => PngColorType.RgbWithAlpha, | |
| Type t when t == typeof(L8) => PngColorType.Grayscale, | |
| Type t when t == typeof(L16) => PngColorType.Grayscale, | |
| Type t when t == typeof(La16) => PngColorType.GrayscaleWithAlpha, | |
| Type t when t == typeof(La32) => PngColorType.GrayscaleWithAlpha, | |
| Type t when t == typeof(Rgb24) => PngColorType.Rgb, | |
| Type t when t == typeof(Rgba32) => PngColorType.RgbWithAlpha, | |
| Type t when t == typeof(Rgb48) => PngColorType.Rgb, | |
| Type t when t == typeof(Rgba64) => PngColorType.RgbWithAlpha, | |
| Type t when t == typeof(RgbaVector) => PngColorType.RgbWithAlpha, | |
| _ => PngColorType.RgbWithAlpha |
|
Looking through the ImageMagick implementation, they also support autolevel on the color channels (I thought it would just create a grayscale image but its not), but I must admit I'm not to sure how to implement that, nor do I have a use for it. Would full color support be required for this to be merged? |
@CoenraadS: in my opinion, its ok if its only works on grayscale image. This is also the case for the other HistogramEqualization processors. Maybe add a note in the Processor Summary text, that it only works with gray images, so the user know what to expect. |
|
What's the science behind working for all color channels? Might it be a case of using the resultant normalized luminance as some sort of multiplier against each pixel? I'm not convinced greyscale only is an adequate implementation for the base library (I dearly wish our existing Histogram equalization operations supported full color) simply because every use case for automated equalization I've ever seen is in the application against full color photos in an album. |
Im not convinced that applying the histogram equalization to each individual color channel of an RGB image makes much sense. One approach which sounds reasonable to me is converting the image from the RGB color space to the LAB color space and then apply the histogram equalization to the L channel. Then convert it back to RGB. In my opinion applying the equalization to all color channels should not be the scope of this PR. Maybe its worth looking into how ImageMagic does it, but that could be something for a future PR. edit: I have checked the AutoLevel source code of ImageMagick and they do apply it to each channel separately. So maybe I was wrong in saying it does not make sense to do it this way. (But its stated in the comment of the source code, that applying the histogram stretch individually will cause a color distortion.) I still believe the method i mentioned above would be a better approach. |
Closes #87
Prerequisites
Description
Add AutoLevel Normalization (only works for L8, L16 types)
Test Image generated from following command (ImageMagick)
cd ImageSharp\tests\Images\Input\Jpg\baselinemagick 640px-Unequalized_Hawkes_Bay_NZ.jpg -auto-level 640px-Unequalized_Hawkes_Bay_NZ.png