#include #include #include #include "find_edges.h" #include "Algorithms.h" #define M_PI 3.1416; void lum2RGB(unsigned char* lum_in, unsigned char* rgb_out, int width, int height) { int i, j, rgb_index, lum_index; for(i=0; i 255) gauss = 255; rgb_out[index] = gauss; } } void gaussiano2(unsigned char* rgb_in, unsigned char* rgb_out, int width, int height) { int i, j, k, index, gauss; for(i=2; i 255) gauss = 255; rgb_out[index] = gauss; } } void gaussiano3(unsigned char* rgb_in, unsigned char* rgb_out, int width, int height) { int i, j, k, index, gauss; for(i=3; i 255) gauss = 255; rgb_out[index] = gauss; } } /* void blackAndWhite(unsigned char* img_in, unsigned char* img_out, int width, int height) { int i, j, index; double bw; unsigned char baw; for(i=0; i 255) grad = 255; img_out[index] = grad; } } void prewitt(unsigned char* img_in, unsigned char* img_out, int width, int height) { int i, j, k, index, grad, gradX, gradY; for(i=1; i 255) grad = 255; img_out[index] = grad; } } void sobel(unsigned char* img_in, unsigned char* img_out, int width, int height) { int i, j, k, index, grad, gradX, gradY; for(i=1; i 255) grad = 255; img_out[index] = grad; } } void laplaciano(unsigned char* img_in, unsigned char* img_out, int width, int height) { int i, j, k, index, laplace; for(i=1; i 255) laplace = 255; img_out[index] = (unsigned char)laplace; } } void makeMask(unsigned char* img_in, unsigned char* img_out, int threshold, int width, int height) { int i, j, index, sum; unsigned char res; for(i=0; i threshold) res = 255; else res = 0; img_out[index ] = res; img_out[index+1] = res; img_out[index+2] = res; } } */ void dilatation(unsigned char* lum_in, unsigned char* lum_out, int width, int height) { int i, j, index, count; unsigned char res; for(i=1; i 0) { lum_out[index] = 255; continue; } count = 0; if(lum_in[index+1] > 0) count++; if(lum_in[index-1] > 0) count++; if(lum_in[index+width+1] > 0) count++; if(lum_in[index+width] > 0) count++; if(lum_in[index+width-1] > 0) count++; if(lum_in[index-width+1] > 0) count++; if(lum_in[index-width] > 0) count++; if(lum_in[index-width-1] > 0) count++; if(count>0) res = 255; else res = 0; lum_out[index] = res; } } void erosion(unsigned char* lum_in, unsigned char* lum_out, int width, int height) { int i, j, index, count; unsigned char res; for(i=1; i 0) count++; if(lum_in[index-1] > 0) count++; if(lum_in[index+width+1] > 0) count++; if(lum_in[index+width] > 0) count++; if(lum_in[index+width-1] > 0) count++; if(lum_in[index-width+1] > 0) count++; if(lum_in[index-width] > 0) count++; if(lum_in[index-width-1] > 0) count++; if(count>7) res = 255; else res = 0; lum_out[index] = res; } } void gradienteCanny(unsigned char* rgb_in, unsigned char* lum_out, int width, int height) { int i, j, rgb_index, lum_index, grad, gradX, gradY; for(i=1; i 255) grad = 255; lum_out[lum_index] = grad; } } void canny(unsigned char* rgb_in, unsigned char* lum_out, unsigned char* lum_aux, int width, int height) { int i, j, rgb_index, lum_index, grad, gradX, gradY; unsigned char direction, left, right, res; unsigned char highThreshold = 120; unsigned char lowThreshold = 40; unsigned char P1, P2, P3, P4, P5, P6, P7, P8; double orientation; gradienteCanny(rgb_in, lum_aux, width, height); for(i=0; i=width-3)||(j>=height-3)) { lum_out[lum_index] = 0; // rgb_out[index+1] = 0; // rgb_out[index+2] = 0; continue; } gradX = rgb_in[rgb_index+width*3+3] - rgb_in[rgb_index+width*3-3]; gradX += rgb_in[rgb_index-width*3+3] - rgb_in[rgb_index-width*3-3]; gradX += 2*(rgb_in[rgb_index+3] - rgb_in[rgb_index-3]); gradY = rgb_in[rgb_index-width*3-3] - rgb_in[rgb_index+width*3-3]; gradY += rgb_in[rgb_index-width*3+3] - rgb_in[rgb_index+width*3+3]; gradY += 2*(rgb_in[rgb_index-width*3] - rgb_in[rgb_index+width*3]); grad = abs(gradX) + abs(gradY); if(grad>255) grad = 255; if(gradX == 0) { if(gradY==0) orientation = 0.0; else if(gradY<0) { gradY = -gradY; orientation = 90.0; } else orientation = 90.0; } /* Can't take invtan of angle in 2nd Quad */ else if(gradX<0 && gradY>0) { gradX = -gradX; orientation = 180.0 - ((atan((double)(gradY)/(double)(gradX))) * (180.0/3.1416)); } /* Can't take invtan of angle in 4th Quad */ else if(gradX>0 && gradY<0) { gradY = -gradY; orientation = 180.0 - ((atan((double)(gradY)/(double)(gradX))) * (180.0/3.1416)); } /* else angle is in 1st or 3rd Quad */ else orientation = (atan((double)(gradY)/(double)(gradX))) * (180.0/3.1416); if(orientation < 22.5) direction = 0; else if(orientation < 67.5) direction = 45; else if(orientation < 112.5) direction = 90; else if(orientation < 157.5) direction = 135; else direction = 0; if(direction == 0) { left = lum_aux[lum_index-1]; right = lum_aux[lum_index+1]; } else if(direction == 45) { left = lum_aux[lum_index+width-1]; right = lum_aux[lum_index-width+1]; } else if(direction == 90) { left = lum_aux[lum_index-width]; right = lum_aux[lum_index+width]; } else { left = lum_aux[lum_index-width-1]; right = lum_aux[lum_index+width+1]; } if((grad= highThreshold) res = 255; /* edge */ else if(grad < lowThreshold) res = 0; /* nonedge */ /* SUM is between T1 & T2 */ else { /* Determine values of neighboring pixels */ P1 = rgb_in[rgb_index-width*3-3]; P2 = rgb_in[rgb_index-width*3 ]; P3 = rgb_in[rgb_index-width*3+3]; P4 = rgb_in[rgb_index-3]; P5 = rgb_in[rgb_index+3]; P6 = rgb_in[rgb_index+width*3-3]; P7 = rgb_in[rgb_index+width*3 ]; P8 = rgb_in[rgb_index+width*3+3]; /* Check to see if neighboring pixel values are edges */ if((P1>highThreshold) || (P2>highThreshold) || (P3>highThreshold) || (P4>highThreshold) || (P5>highThreshold) || (P6>highThreshold) || (P7>highThreshold) || (P8>highThreshold)) res = 255; /* make edge */ else res = 0; /* make nonedge */ } } lum_out[lum_index] = res; // rgb_out[index+1] = res; // rgb_out[index+2] = res; } } float calculateArea(int *xs, int *ys, int size) { int i, x0, y0, x1, y1, iaux; iaux = 0; for(i=0; i 255) grad_x = 255; if(grad_y > 255) grad_y = 255; gradX[index ] = grad_x; gradX[index+1] = grad_x; gradX[index+2] = grad_x; gradY[index ] = grad_y; gradY[index+1] = grad_y; gradY[index+2] = grad_y; } for(i=1+k; imax1) max1 = av1; // if(av1max2) max2 = av2; // if(av2 200)||(x==0)||(y==0)||(x==width-1)||(y==height-1)) return; numW2B = 0; for(i=0; i<8; i++) if( (lum[index + dx[i] + dy[i]*width] != 0) && (lum[index + dx[i+1] + dy[i+1]*width] == 0)) numW2B++; if(numW2B < 2) { lum[index] = 0; for(i=0; i<8; i++) if(lum[index + dx[i] + dy[i]*width] != 0) { visitPixel(lum, width, height, x + dx[i], y + dy[i], stackCount+1); } } } void detectCycles(unsigned char* lum, int width, int height) { int i, j, index; for(i=1; i 400)||(x==0)||(y==0)||(x==width-1)||(y==height-1)) return 0; if((!first)&&(x==x_dest)&&(y==y_dest)) { return 1; } firstB2W = -1; for(i=startIndex; i= 0) { if(!first) lum[index] = 0; veAddPoint((float)x, (float)y); return detectCycle(lum, width, height, x + dx[firstB2W], y + dy[firstB2W], (firstB2W+6) % 8, x_dest, y_dest, 0, stackCount+1); } return 0; } int detectCorners(unsigned char* lum, int width, int height, int xs[4], int ys[4]) { int i, j, k, index, size, iscycle; float* vertices; int x1s[20]; int y1s[20]; for(i=1; i=4)&&(size<=20)) { for(k=0; k 150.0) return 1; } // index = (((int)vertices[2*k+1])*width + (int)vertices[2*k]); // lum[index] = 50; // drawPoint(lum, width, height, (int)vertices[2*k], (int)vertices[2*k+1], 100); } } return 0; }