Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. Vec3b mycolor = img(x,y);
  2. Vec3b green = Vec3b(0, 255, 0);
  3. Vec3b red = Vec3b(0, 0, 255);
  4. Mat_<Vec3b> cloneArea = Mat(img.rows,img.cols,CV_8UC3);
  5. Mat_<Vec3b> clonePerimeterThiness = Mat(img.rows, img.cols, CV_8UC3);
  6. Mat_<Vec3b> cloneCenterOfMass = Mat(img.rows, img.cols, CV_8UC3);
  7. Mat_<Vec3b> cloneAxisOfElongation = Mat(img.rows, img.cols, CV_8UC3);
  8. Mat_<Vec3b> cloneAspectRatio = Mat(img.rows, img.cols, CV_8UC3);
  9. Mat_<Vec3b> cloneProjectionHor = Mat(img.rows, img.cols, CV_8UC3);
  10. Mat_<Vec3b> cloneProjectionVert = Mat(img.rows, img.cols, CV_8UC3);
  11.  
  12. cloneArea = img.clone();
  13. clonePerimeterThiness = img.clone();
  14. cloneAspectRatio = img.clone();
  15. cloneAxisOfElongation = img.clone();
  16. cloneCenterOfMass = img.clone();
  17. int objectLabel = 0;
  18. Point2i centerOfMass;
  19. int objectRows = 0;
  20. int objectCols = 0;
  21. int objectArea = 0;
  22. int denominator = 0;
  23. int numerator1 = 0;
  24. int numerator2 = 0;
  25. int cmax = 0;
  26. int cMin = INT_MAX;
  27. int rmax = 0;
  28. int rmin = INT_MAX;
  29. for (int i = 0; i < img.rows; i++) {
  30. for (int j = 0; j < img.cols; j++) {
  31. if (img(i,j) == mycolor) {
  32. cloneArea(i, j) = green;
  33. objectArea++;
  34. objectRows += i;
  35. objectCols += j;
  36. if(cmax < j){
  37. cmax = j;
  38. }
  39. if (cMin > j) {
  40. cMin = j;
  41. }
  42. if (rmax < i) {
  43. rmax = i;
  44. }
  45. if (rmin > i) {
  46. rmin = i;
  47. }
  48. }
  49. }
  50. }
  51. imshow("Show area", cloneArea);
  52. waitKey();
  53. int cmRow = objectRows / objectArea;
  54. int cmCols = objectCols / objectArea;
  55. //colorCenterOfMass
  56. cloneCenterOfMass(cmRow,cmCols) = green;
  57. cloneCenterOfMass(cmRow - 1, cmCols) = green;
  58. cloneCenterOfMass(cmRow + 1, cmCols) = green;
  59. cloneCenterOfMass(cmRow, cmCols - 1) = green;
  60. cloneCenterOfMass(cmRow, cmCols + 1) = green;
  61. imshow("Center of Mass", cloneCenterOfMass);
  62. waitKey();
  63. for (int i = 0; i < img.rows; i++) {
  64. for (int j = 0; j < img.cols; j++) {
  65. if (img(i,j) == mycolor) {
  66. cloneProjectionHor(i, j) = red;
  67. cloneProjectionVert(i, j) = red;
  68. denominator += (i - cmRow)*(j - cmCols);
  69. numerator1 += (j - cmCols)*(j - cmCols);
  70. numerator2 += (i - cmRow)*(i - cmRow);
  71. }
  72. }
  73. }
  74.  
  75. float arcTanElong = atan2(2.0 * (float)denominator, (float)numerator1 - (float)numerator2) / 2;
  76. float distance = 100;
  77. Point2i newPointCenter;
  78. newPointCenter.y = cmRow + distance * sin(arcTanElong);
  79. newPointCenter.x = cmCols + distance * cos(arcTanElong);
  80. Point2i newCenter;
  81. newCenter.x = cmCols;
  82. newCenter.y = cmRow;
  83. line(cloneAxisOfElongation,newCenter, newPointCenter, green, 2);
  84. imshow("Axis of Elongation", cloneAxisOfElongation);
  85. waitKey();
  86.  
  87. int di8[8] = { -1, 0, 1, 0, 1, -1, 1 , -1 };
  88. int dj8[8] = { 0, -1, 0, 1, 1, 1 , -1 , -1 };
  89. int neighboursi;
  90. int neighboursj;
  91. int perimeter = 0;
  92. int check = 0;
  93.  
  94. for (int i = 0; i < img.rows; i++) {
  95. for (int j = 0; j < img.cols; j++) {
  96. if (img(i,j) == mycolor) {
  97. check = 0;
  98. for (int k = 0; k < 8; k++) {
  99. neighboursi = i + di8[k];
  100. neighboursj = j + dj8[k];
  101. if (isInside(img,neighboursi,neighboursj) && img(neighboursi, neighboursj) != mycolor) {
  102. check = 1;
  103. clonePerimeterThiness(i,j) = green;
  104. }
  105. }
  106. perimeter += check;
  107. }
  108. }
  109. }
  110.  
  111. imshow("Perimeter", clonePerimeterThiness);
  112. waitKey();
  113.  
  114. float thinessRatio = 0.0;
  115. thinessRatio = 4.0 * PI*((float)objectArea / ((float)perimeter * (float)perimeter));
  116.  
  117. float aspectRatio = 0.0;
  118. aspectRatio = ((float)cmax - (float)cMin + 1.0) / ((float)rmax - (float)rmin + 1.0);
  119.  
  120. Point2i leftTop;
  121. leftTop.x = cMin;
  122. leftTop.y = rmin;
  123. Point2i leftBott;
  124. leftBott.x = cMin;
  125. leftBott.y = rmax;
  126. Point2i rightTop;
  127. rightTop.x = cmax;
  128. rightTop.y = rmin;
  129. Point2i rightBott;
  130. rightBott.x = cmax;
  131. rightBott.y = rmax;
  132. line(cloneAspectRatio, leftTop, rightTop, green, 1);
  133. line(cloneAspectRatio, leftTop, leftBott, green, 1);
  134. line(cloneAspectRatio, rightTop, rightBott, green, 1);
  135. line(cloneAspectRatio, leftBott, rightBott, green, 1);
  136. imshow("AspectRatio", cloneAspectRatio);
  137. waitKey();
  138.  
  139. std::vector<int> pixelOnRows(img.rows);
  140. std::vector<int> pixelOnCols(img.cols);
  141. for (int i = 0; i < img.rows; i++) {
  142. pixelOnRows[i] = 0;
  143. }
  144. for (int j = 0; j < img.cols; j++) {
  145. pixelOnCols[j] = 0;
  146. }
  147.  
  148.  
  149. for (int i = 0; i < img.rows; i++) {
  150. int pixelCount = 0;
  151. for (int j = 0; j < img.cols; j++) {
  152. if (img(i,j) == mycolor) {
  153. pixelCount++;
  154. }
  155. }
  156. pixelOnRows[i] = pixelCount;
  157. }
  158.  
  159. for (int i = 0; i < img.cols; i++) {
  160. int pixelCount = 0;
  161. for (int j = 0; j < img.rows; j++) {
  162. if (img(j,i) == mycolor) {
  163. pixelCount++;
  164. }
  165. }
  166. pixelOnCols[i] = pixelCount;
  167. }
  168.  
  169. for (int i = 0; i < img.rows; i++) {
  170. Point2i finalPoint;
  171. finalPoint.y = i;
  172. finalPoint.x = img.cols;
  173. Point2i startPoint;
  174. startPoint.y = i;
  175. startPoint.x = img.cols - pixelOnRows[i];
  176. if (pixelOnRows[i] != 0) {
  177. line(cloneProjectionVert, startPoint, finalPoint, green, 2);
  178. }
  179. }
  180.  
  181. for (int i = 0; i < img.cols; i++) {
  182. Point2i finalPoint;
  183. finalPoint.y = img.rows;
  184. finalPoint.x = i;
  185. Point2i startPoint;
  186. startPoint.y = img.rows - pixelOnCols[i];
  187. startPoint.x = i;
  188. if (pixelOnCols[i] != 0) {
  189. line(cloneProjectionHor, startPoint, finalPoint, green, 2);
  190. }
  191. }
  192.  
  193. imshow("ProjectionVert", cloneProjectionVert);
  194. waitKey();
  195. imshow("ProjectionHor", cloneProjectionHor);
  196. waitKey();
  197.  
  198. std::cout << "Object area: " << objectArea << std::endl;
  199. std::cout << "Center of mass: " << centerOfMass.y << " " << centerOfMass.x << std::endl;
  200. std::cout << "Axis of Elongation " << arcTanElong << std::endl;
  201. std::cout << "Perimeter: " << perimeter << std::endl;
  202. std::cout << "Thiness ration: " << thinessRatio << std::endl;
  203. std::cout << "Aspect Ratio: " << aspectRatio << std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement