Advertisement
Guest User

borisko_porisko

a guest
Oct 30th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4.  
  5. namespace DigitalImage.util
  6. {
  7. public class RasterRegion
  8. {
  9. public int regId = 0;
  10. public List<Point> points = new List<Point>();
  11.  
  12. public Boolean momentiOdredjeni = false;
  13. public int n = 0; // ukupan broj tacaka
  14.  
  15. public double c20 = 0;
  16. public double c11 = 0;
  17. public double c02 = 0;
  18.  
  19. #region osnovne statisticke osobine regiona
  20. public double xM = 0;
  21. public double yM = 0;
  22. public double theta = 0;
  23. public double eccentricity = 0;
  24. public double majorAxisLength = 0;
  25. public double minorAxisLength = 0;
  26. #endregion
  27.  
  28. public void odrediMomente()
  29. {
  30. if (momentiOdredjeni == true)
  31. return;
  32. n = points.Count;
  33. foreach (Point pp in points)
  34. {
  35. xM += pp.X;
  36. yM += pp.Y;
  37. }
  38. xM = xM / n;
  39. yM = yM / n;
  40. foreach (Point pp in points)
  41. {
  42. c20 += (pp.X - xM) * (pp.X - xM);
  43. c11 += (pp.X - xM) * (pp.Y - yM);
  44. c02 += (pp.Y - yM) * (pp.Y - yM);
  45. }
  46. // sad imamo vrednosti covariance matrix
  47. // c20 c11
  48. // c11 c02
  49. // odrediti karakteristicne vrednosti
  50. double a = 1;
  51. double b = -(c20 + c02);
  52. double c = c20 * c02 - c11 * c11;
  53. double D = b * b - 4 * c;
  54. double alfa1 = 0;
  55. double alfa2 = 0;
  56. if (D > 0)
  57. {
  58. D = Math.Sqrt(D);
  59. alfa1 = (-b + D) / 2 * a;
  60. alfa2 = (-b - D) / 2 * a;
  61. double temp1 = Math.Max(alfa1, alfa2);
  62. double temp2 = Math.Min(alfa1, alfa2);
  63. alfa1 = temp1;
  64. alfa2 = temp2;
  65. if (alfa1 != 0)
  66. eccentricity = alfa2 / alfa1;
  67. majorAxisLength = alfa1;
  68. minorAxisLength = alfa2;
  69. }
  70. theta = 0.5 * Math.Atan2(2 * c11, (c20 - c02));
  71. }
  72. public byte[,] odrediSliku()
  73. {
  74. int minX = int.MaxValue;
  75. int maxX = int.MinValue;
  76. int minY = int.MaxValue;
  77. int maxY = int.MinValue;
  78.  
  79. for (int i = 0; i < points.Count; i++)
  80. {
  81. if (points[i].X < minX)
  82. {
  83. minX = points[i].X;
  84. }
  85. if(points[i].X > maxX)
  86. {
  87. maxX = points[i].X;
  88. }
  89. if (points[i].Y < minY)
  90. {
  91. minY = points[i].Y;
  92. }
  93. if (points[i].Y > maxY)
  94. {
  95. maxY = points[i].Y;
  96. }
  97. }
  98. byte[,] slika = new byte[maxY - minY+2, maxX - minX+2];
  99. /*for (int i = 0; i < maxX - minX; i++)
  100. {
  101. for (int j = 0; j < maxY - minY; j++)
  102. {
  103. for (int p = 0; p < points.Count; p++)
  104. {
  105. if (i == points[p].X - minX && j == points[p].Y - minY)
  106. {
  107. slika[i, j] = 0;
  108. }
  109. else
  110. slika[i, j] = 255;
  111. }
  112. }
  113. }*/
  114. for (int i = 0; i < maxY - minY +2; i++)
  115. {
  116. for (int j = 0; j < maxX - minX+2; j++)
  117. {
  118. slika[i, j] = 255;
  119. }
  120. }
  121. for (int i = 0; i < points.Count; i++)
  122. {
  123. slika[points[i].Y-minY, points[i].X-minX] = 0;
  124. }
  125.  
  126. return slika;
  127. }
  128.  
  129. public byte[,] odrediNSliku()
  130. {
  131. List<Point> points1 = new List<Point>();
  132. double alfa = Math.PI/2-Math.Abs(theta);
  133.  
  134. for (int i = 0; i < points.Count; i++)
  135. {
  136.  
  137. int x = (int)(Math.Cos(alfa) * (points[i].X - xM) - Math.Sin(alfa) * (points[i].Y - yM) + xM);
  138. int y = (int)(Math.Sin(alfa) * (points[i].X - xM) + Math.Cos(alfa) * (points[i].Y - yM) + yM);
  139. Point point = new Point(x,y);
  140. points1.Add(point);
  141.  
  142. }
  143. points = points1;
  144. return odrediSliku();
  145. }
  146. }
  147.  
  148.  
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement