Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [AlgorithmInfo("Stretch del contrasto", Category = "FEI")]
- public class ContrastStretch : ImageOperation<Image<byte>, Image<byte>>
- {
- private int stretchDiscardPerc;
- [AlgorithmParameter]
- [DefaultValue(0)]
- public int StretchDiscardPercentage
- {
- get { return stretchDiscardPerc; }
- set
- {
- if (value < 0 || value > 100)
- throw new ArgumentOutOfRangeException("Inserire un valore fra 0 and 100.");
- stretchDiscardPerc = value;
- }
- }
- public ContrastStretch()
- {
- }
- public ContrastStretch(Image<byte> inputImage)
- : base(inputImage)
- {
- StretchDiscardPercentage = 0;
- }
- public ContrastStretch(Image<byte> inputImage, int stretchDiscard)
- : base(inputImage)
- {
- StretchDiscardPercentage = stretchDiscard;
- }
- public override void Run()
- {
- var hist = new BioLab.ImageProcessing.HistogramBuilder(InputImage).Execute();
- int nd = InputImage.PixelCount * StretchDiscardPercentage / 100;
- int i1 = 0;
- int sum = 0;
- while (sum < nd)
- {
- sum += hist[i1++];
- }
- int i2 = 255;
- sum = 0;
- while (sum < nd)
- {
- sum += hist[i2--];
- }
- int diff = i2 - i1;
- if (diff == 0)
- diff = 1;
- Result = new LookupTableTransform<byte>(InputImage,
- p => ((p - i1) * 255 / diff).ClipToByte()).Execute();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement