Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. // Returns number of holes within digit
  2. // 25/04/2017
  3.  
  4. #include <stdio.h>
  5. #include "captcha.h"
  6.  
  7.  
  8. int get_hole_count(int height, int width, int box_height, int box_width, int pixels[box_height][box_width], int start_row, int start_column){
  9. int copyholes[box_height][box_width];
  10. int y = box_height-1, x = 0, i = 0, y2 = box_height-1, x2 = 0;
  11. int hole_count = 0, hole_pixels = 0;
  12. double hole_density = 0;
  13. int check = 0;
  14. int hole_top = 0, hole_bottom = 0;
  15. double hole_centre = 0;
  16.  
  17. //creates a duplicate array of the bounding box of the digit
  18.  
  19. //printf("%d, %d \n", box_height, box_width);
  20. copy_pixels(height, width, pixels, start_row, start_column, box_height, box_width, copyholes);
  21. //print_image(box_height, box_width, copyholes);
  22. //printf("%d %d\n", box_height, box_width);
  23.  
  24.  
  25.  
  26. //filling border of bounding box
  27. while(y >= 0){
  28. while(x <= box_width-1){
  29. if(y == box_height-1 && copyholes[y][x] == 0){
  30. copyholes[y][x] = 2;
  31. }
  32. if(x == 0 && copyholes[y][x] == 0){ //fills left column
  33. copyholes[y][x] = 2;
  34. }
  35. if(y == height-1 && copyholes[y][x] == 0){ //fills top row
  36. copyholes[y][x] = 2;
  37. }
  38. if(x == box_width-1 && copyholes[y][x] == 0){ //fills right column
  39. copyholes[y][x] = 2;
  40. }
  41. if(y == 0 && copyholes[y][x] == 0){ //fill bottom row
  42. copyholes[y][x] = 2;
  43. }
  44. x++;
  45. }
  46. x = 0;
  47. y--;
  48. }
  49.  
  50. while(i < height){
  51. y = box_height-1;
  52. x = 0;
  53. while(y >= 0){
  54. while(x <= box_width-1){
  55. if(copyholes[y][x] == 0 && copyholes[y][x+1] == 2){
  56. copyholes[y][x] = 2;
  57. }
  58. x++;
  59. }
  60. x = 0;
  61. y--;
  62. }
  63. i++;
  64. }
  65. i = 0;
  66.  
  67. while(i < height){
  68. y = box_height-1;
  69. x = 1;
  70. while(y >= 1){
  71. while(x <= box_width-1){
  72. if(copyholes[y][x] == 0 && (copyholes[y][x+1] == 2 || copyholes[y+1][x+1] == 2 || copyholes[y-1][x+1] == 2 || copyholes[y][x-1] == 2 || copyholes[y+1][x-1] == 2 || copyholes[y-1][x-1] == 2 || copyholes[y+1][x] == 2 || copyholes[y-1][x] == 2)){
  73. copyholes[y][x] = 2;
  74. }
  75. x++;
  76. }
  77. x = 0;
  78. y--;
  79. }
  80. i++;
  81. }
  82.  
  83.  
  84. //count holes
  85. y = box_height-1;
  86. x = 0;
  87. i = 0;
  88. while(y >= 1){
  89. while(x <= box_width-1){
  90. if(copyholes[y][x] == 0){ //detects first hole pixel
  91. copyholes[y][x] = 2;
  92. hole_pixels ++;
  93. hole_count++;
  94. y2 = y;
  95. x2 = x;
  96. while(i<height){
  97. y2 = y;
  98. x2 = x;
  99. while(y2 >= 1){
  100. while(x2 <= box_width-1){
  101. if(copyholes[y2][x2] == 0 && (copyholes[y2][x2+1] == 2 || copyholes[y2+1][x2+1] == 2 || copyholes[y2-1][x2+1] == 2 || copyholes[y2][x2-1] == 2 || copyholes[y2-1][x2-1] == 2 || copyholes[y2+1][x2-1] == 2 || copyholes[y2+1][x2] == 2 || copyholes[y2-1][x2] == 2)){ // fills pixels around hole pixel
  102. copyholes[y2][x2] = 2;
  103. if(check == 0){
  104. hole_bottom = y2;
  105. check = 1;
  106. }
  107. if(y2 < hole_bottom){
  108. hole_bottom = y2;
  109. }
  110. if(y2 > hole_top){
  111. hole_top = y2;
  112. }
  113. hole_pixels ++;
  114. }
  115. x2++;
  116. }
  117. x2 = 0;
  118. y2--;
  119. }
  120. i++;
  121. }
  122. i = 0;
  123. }
  124. x++;
  125. }
  126. x = 0;
  127. y--;
  128. }
  129.  
  130. hole_centre = (((hole_top*1.0+hole_bottom*1.0)/2.0)/(box_height*1.0));
  131. hole_density = hole_pixels/(box_width*box_height*1.0);
  132. //printf("%d\n%d\n", hole_top, hole_bottom);
  133. //printf("%.3lf\n", hole_centre);
  134. //print_image(box_height, box_width, copyholes); //debugging, checks if pixels are changed correctly
  135. //printf("%d holes, %d hole pixels \n", hole_count, hole_pixels); //debugging, checks if number of holes is detected correctly
  136.  
  137. return hole_count;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement