1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| void SobelF( Mat* p_img, int thresholdValue )
{
int h = p_img->rows;
int w = p_img->cols;
printf("height = %d, width = %d \n", h, w);
int gradInter[4] = {0,0,0,0};
int gMax = 0;
int nbPixelsAnalyses = 0;
//Bruit gaussien pour éliminer le bruit
GaussianBlur( *p_img, *p_img, Size(3,3), 0, 0, BORDER_DEFAULT );
for( int x = 1; x < w-1; x++ )
{
for( int y = 1; y < h-1; y++ )
{
//Initialisation de la matrice de travail extraite de l'image initiale
uchar kernel[9] = {(*p_img).at<uchar>(y-1, x-1),
(*p_img).at<uchar>(y, x-1),
(*p_img).at<uchar>(y+1, x-1),
(*p_img).at<uchar>(y-1, x),
(*p_img).at<uchar>(y, x),
(*p_img).at<uchar>(y+1, x),
(*p_img).at<uchar>(y-1, x+1),
(*p_img).at<uchar>(y, x+1),
(*p_img).at<uchar>(y+1, x+1)};
//Calcul de la convolution avec les 4 matrices
//Pour les formes rectangulaires
gradInter[0] = abs((-1) * kernel[0] +
(-2) * kernel[3] +
(-1) * kernel[6] +
kernel[2] +
2 * kernel[5] +
kernel[8]);
gradInter[1] = abs(kernel[0] +
2 * kernel[1] +
kernel[2] +
(-1) * kernel[6] +
(-2) * kernel[7] +
(-1) * kernel[8]);
//Pour les formes circulaires
gradInter[2] = abs(kernel[1] +
2 * kernel[2] +
(-1) * kernel[3] +
kernel[5] +
(-2) * kernel[6] +
(-1) * kernel[7]);
gradInter[3] = abs(2 * kernel[0] +
kernel[1] +
kernel[3] +
(-1) * kernel[5] +
(-1) * kernel[7] +
(-2) * kernel[8]);
//Recherche du maximum
gMax = 0;
for( int i = 0; i < 4; i++ )
{
if( gradInter[i] > gMax )
gMax = gradInter[i];
}
//Seuillage
if( gMax > thresholdValue )
{
circle( *p_img, Point(y,x), 0, Scalar(255), -1, 8, 0 );
nbPixelsAnalyses++;
}
else
{
circle( *p_img, Point(y,x), 0, Scalar(0), -1, 8, 0 );
nbPixelsAnalyses++;
}
}
}
printf("nbPixelsAnalyses = %d, nbPixelsTotal = heigth*width = %d\n", nbPixelsAnalyses, ((h-2)*(w-2)) );
printf("Sobel done!\n");
} |