Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Returns number of holes within digit
- // 25/04/2017
- #include <stdio.h>
- #include "captcha.h"
- 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){
- int copyholes[box_height][box_width];
- int y = box_height-1, x = 0, i = 0, y2 = box_height-1, x2 = 0;
- int hole_count = 0, hole_pixels = 0;
- double hole_density = 0;
- int check = 0;
- int hole_top = 0, hole_bottom = 0;
- double hole_centre = 0;
- //creates a duplicate array of the bounding box of the digit
- //printf("%d, %d \n", box_height, box_width);
- copy_pixels(height, width, pixels, start_row, start_column, box_height, box_width, copyholes);
- //print_image(box_height, box_width, copyholes);
- //printf("%d %d\n", box_height, box_width);
- //filling border of bounding box
- while(y >= 0){
- while(x <= box_width-1){
- if(y == box_height-1 && copyholes[y][x] == 0){
- copyholes[y][x] = 2;
- }
- if(x == 0 && copyholes[y][x] == 0){ //fills left column
- copyholes[y][x] = 2;
- }
- if(y == height-1 && copyholes[y][x] == 0){ //fills top row
- copyholes[y][x] = 2;
- }
- if(x == box_width-1 && copyholes[y][x] == 0){ //fills right column
- copyholes[y][x] = 2;
- }
- if(y == 0 && copyholes[y][x] == 0){ //fill bottom row
- copyholes[y][x] = 2;
- }
- x++;
- }
- x = 0;
- y--;
- }
- while(i < height){
- y = box_height-1;
- x = 0;
- while(y >= 0){
- while(x <= box_width-1){
- if(copyholes[y][x] == 0 && copyholes[y][x+1] == 2){
- copyholes[y][x] = 2;
- }
- x++;
- }
- x = 0;
- y--;
- }
- i++;
- }
- i = 0;
- while(i < height){
- y = box_height-1;
- x = 1;
- while(y >= 1){
- while(x <= box_width-1){
- 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)){
- copyholes[y][x] = 2;
- }
- x++;
- }
- x = 0;
- y--;
- }
- i++;
- }
- //count holes
- y = box_height-1;
- x = 0;
- i = 0;
- while(y >= 1){
- while(x <= box_width-1){
- if(copyholes[y][x] == 0){ //detects first hole pixel
- copyholes[y][x] = 2;
- hole_pixels ++;
- hole_count++;
- y2 = y;
- x2 = x;
- while(i<height){
- y2 = y;
- x2 = x;
- while(y2 >= 1){
- while(x2 <= box_width-1){
- 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
- copyholes[y2][x2] = 2;
- if(check == 0){
- hole_bottom = y2;
- check = 1;
- }
- if(y2 < hole_bottom){
- hole_bottom = y2;
- }
- if(y2 > hole_top){
- hole_top = y2;
- }
- hole_pixels ++;
- }
- x2++;
- }
- x2 = 0;
- y2--;
- }
- i++;
- }
- i = 0;
- }
- x++;
- }
- x = 0;
- y--;
- }
- hole_centre = (((hole_top*1.0+hole_bottom*1.0)/2.0)/(box_height*1.0));
- hole_density = hole_pixels/(box_width*box_height*1.0);
- //printf("%d\n%d\n", hole_top, hole_bottom);
- //printf("%.3lf\n", hole_centre);
- //print_image(box_height, box_width, copyholes); //debugging, checks if pixels are changed correctly
- //printf("%d holes, %d hole pixels \n", hole_count, hole_pixels); //debugging, checks if number of holes is detected correctly
- return hole_count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement