Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. [AlgorithmInfo("Stretch del contrasto", Category = "FEI")]
  2. public class ContrastStretch : ImageOperation<Image<byte>, Image<byte>>
  3. {
  4. private int stretchDiscardPerc;
  5.  
  6. [AlgorithmParameter]
  7. [DefaultValue(0)]
  8. public int StretchDiscardPercentage
  9. {
  10. get { return stretchDiscardPerc; }
  11. set
  12. {
  13. if (value < 0 || value > 100)
  14. throw new ArgumentOutOfRangeException("Inserire un valore fra 0 and 100.");
  15. stretchDiscardPerc = value;
  16. }
  17. }
  18.  
  19. public ContrastStretch()
  20. {
  21. }
  22.  
  23. public ContrastStretch(Image<byte> inputImage)
  24. : base(inputImage)
  25. {
  26. StretchDiscardPercentage = 0;
  27. }
  28.  
  29. public ContrastStretch(Image<byte> inputImage, int stretchDiscard)
  30. : base(inputImage)
  31. {
  32. StretchDiscardPercentage = stretchDiscard;
  33. }
  34.  
  35. public override void Run()
  36. {
  37. var hist = new BioLab.ImageProcessing.HistogramBuilder(InputImage).Execute();
  38. int nd = InputImage.PixelCount * StretchDiscardPercentage / 100;
  39. int i1 = 0;
  40. int sum = 0;
  41. while (sum < nd)
  42. {
  43. sum += hist[i1++];
  44. }
  45. int i2 = 255;
  46. sum = 0;
  47. while (sum < nd)
  48. {
  49. sum += hist[i2--];
  50. }
  51. int diff = i2 - i1;
  52. if (diff == 0)
  53. diff = 1;
  54. Result = new LookupTableTransform<byte>(InputImage,
  55. p => ((p - i1) * 255 / diff).ClipToByte()).Execute();
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement