Ошибка в коде преобразования изображения - C#

Узнай цену своей работы

Формулировка задачи:

есть вот такой код есть ошибка помогите разобраться в этом
Листинг программы
  1. using System.Drawing;
  2. using Image.Contracts;
  3. using System.Windows.Forms;
  4. using System.Drawing.Imaging;
  5. namespace Binarization.Transformation
  6. {
  7. public class BinarizationTransformation : IImageTransformation
  8. {
  9. public BinarizationTransformation()
  10. {
  11. BinarizationThreshold = 122;
  12. }
  13. public string Name
  14. {
  15. get
  16. {
  17. return "Binarization";
  18. }
  19. }
  20. public string Description
  21. {
  22. get
  23. {
  24. return "Transfroms the input image into binary image.";
  25. }
  26. }
  27. public int BinarizationThreshold { get; set; }
  28. public Control UI {
  29. get
  30. {
  31. return _ui ?? (_ui = new BinarizationParameters { numThreshhold = { Value = BinarizationThreshold } });
  32. }
  33. }
  34. private BinarizationParameters _ui;
  35.  
  36. private Bitmap GetArgbCopy(System.Drawing.Image sourceImage)
  37. {
  38. var bmpNew = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
  39. using (Graphics graphics = Graphics.FromImage(bmpNew))
  40. {
  41. graphics.DrawImage(sourceImage, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height)
  42. , new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), GraphicsUnit.Pixel);
  43. graphics.Flush();
  44. }
  45. return bmpNew;
  46. }
  47. private Bitmap ApplyColorMatrix(System.Drawing.Image sourceImage, ColorMatrix colorMatrix)
  48. {
  49. Bitmap bmp32BppSource = GetArgbCopy(sourceImage);
  50. Bitmap bmp32BppDest = new Bitmap(bmp32BppSource.Width, bmp32BppSource.Height, PixelFormat.Format32bppArgb);
  51. using (Graphics graphics = Graphics.FromImage(bmp32BppDest))
  52. {
  53. ImageAttributes bmpAttributes = new ImageAttributes();
  54. bmpAttributes.SetColorMatrix(colorMatrix);
  55. graphics.DrawImage(bmp32BppSource, new Rectangle(0, 0, bmp32BppSource.Width, bmp32BppSource.Height),
  56. 0, 0, bmp32BppSource.Width, bmp32BppSource.Height, GraphicsUnit.Pixel, bmpAttributes);
  57. }
  58. bmp32BppSource.Dispose();
  59. return bmp32BppDest;
  60. }
  61. public Bitmap brg(System.Drawing.Image sourceImage, int threshold)
  62. {
  63. var k = threshold;
  64. ColorMatrix colorMatrix = new ColorMatrix(new float[][]
  65. {
  66. new float[] {k, k, k, 0, 0},
  67. new float[] {k, k, k, 0, 0},
  68. new float[] {k, k, k, 0, 0},
  69. new float[] {0, 0, 0, 1, 0},
  70. new float[] {-k, -k, -k, 0, 0}
  71. });
  72. return ApplyColorMatrix(sourceImage, colorMatrix);
  73. }
  74. public Bitmap Apply(Bitmap bmpImg)
  75. {
  76. /*
  77. Bitmap BinarizationImage = new Bitmap(bmpImg.Width, bmpImg.Height);
  78. BinarizationImage = (brg(BinarizationImage, BinarizationThreshold));
  79. return BinarizationImage;
  80. */ где-то тут ошибка как я думаю
  81. }
  82. }
  83. }
на формах так работает
Листинг программы
  1. private void Apply_Click(object sender, EventArgs e)
  2. {
  3. var pic =new Bitmap(pictureBox1.Image);
  4. var RGBFilters = new ExtBitmap();
  5. if (pictureBox1.Image != null)
  6. {
  7. pictureBox2.Image = (RGBFilters.ColorBalance(pic,(byte)trcRed.Value, (byte)trcGreen.Value, (byte)trcRed.Value));
  8. }
с выводом решено но теперь проблема с BinarizationThreshold он при выполнение не учитывается
Листинг программы
  1. public Bitmap Apply(Bitmap bmpImg)
  2. {
  3. Bitmap BinarizationImage = new Bitmap(bmpImg.Width, bmpImg.Height);
  4. BinarizationImage = (brg(bmpImg,BinarizationThreshold ));
  5.  
  6. return BinarizationImage;
  7. }

Решение задачи: «Ошибка в коде преобразования изображения»

textual
Листинг программы
  1.  public Control UI
  2.         {
  3.             get
  4.             {
  5.                 if (_ui == null)
  6.                 {
  7.                     _ui = new BinarizationParameters {numThreshhold = {Value = BinarizationThreshold}};
  8.                     _ui.numThreshhold.ValueChanged += (o, args) => { BinarizationThreshold = (int) _ui.numThreshhold.Value; };
  9.                 }
  10.                 return _ui;
  11.             }
  12.         }
  13.  
  14.         private BinarizationParameters _ui;

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут