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 82 83 84 85 86 87 88 89 90 91
|
int HarrisFilter(Image* img_src, Image* img_dst, int Gauss, float k, int threshold, std::vector<Point> &corner_counter)
{
// basics verification
if(img_src == NULL) return -1;
if(img_dst == NULL) return -1;
if(img_src->GetImageType() != 8) return -1;
if(img_dst->GetImageType() != 8) return -1;
// have dimension
int width_src = img_src->GetWidth();
int height_src = img_src->GetHeight();
int ret = 5;
// variables declaration
Image* Ix_img = new Image(width_src, height_src, 8, "X derivation");
Image* Iy_img = new Image(width_src, height_src, 8, "Y derivation");
ret = SobelXY(img_src, Ix_img, Iy_img);
if(ret == 0){
// gaussian derivation
if(Gauss > 0){
GaussianSmooth(Ix_img, Ix_img, Gauss);
GaussianSmooth(Iy_img, Iy_img, Gauss);
}
double A = 0, B = 0, C = 0;
double responce = 2, harris_max = pow(255*255*9, responce); // maximum value of compute harris responce
Image* response_Harris = new Image(img_dst);
// compute products of derivatives at each pixel and compute responce
int y = 0, x = 0;
// first line set to zero
for(y=0; y<height_src; y++)
response_Harris[y][0]= 0;
for(x=1; x<width_src-1; x++) {
// first column set to zero
response_Harris[0][x]= 0;
for(y=1; y<height_src-1; y++) {
/* H matrix, 2*2 : [ A = Sum(Ix²), B = Sum(Ixy),
B = Sum(Ixy), C = Sum(Iy²) ] */
A = Ix_img[y-1][x-1] * Ix_img[y-1][x-1] + Ix_img[y-1][x ] * Ix_img[y-1][x ] + Ix_img[y-1][x+1] * Ix_img[y-1][x+1] +
Ix_img[y ][x-1] * Ix_img[y ][x-1] + Ix_img[y ][x ] * Ix_img[y ][x ] + Ix_img[y ][x+1] * Ix_img[y ][x+1] +
Ix_img[y+1][x-1] * Ix_img[y+1][x-1] + Ix_img[y+1][x ] * Ix_img[y+1][x ] + Ix_img[y+1][x+1] * Ix_img[y+1][x+1];
C = Iy_img[y-1][x-1] * Iy_img[y-1][x-1] + Iy_img[y-1][x ] * Iy_img[y-1][x ] + Iy_img[y-1][x+1] * Iy_img[y-1][x+1] +
Iy_img[y ][x-1] * Iy_img[y ][x-1] + Iy_img[y ][x ] * Iy_img[y ][x ] + Iy_img[y ][x+1] * Iy_img[y ][x+1] +
Iy_img[y+1][x-1] * Iy_img[y+1][x-1] + Iy_img[y+1][x ] * Iy_img[y+1][x ] + Iy_img[y+1][x+1] * Iy_img[y+1][x+1];
B = Ix_img[y-1][x-1] * Iy_img[y-1][x-1] + Ix_img[y-1][x ] * Iy_img[y-1][x ] + Ix_img[y-1][x+1] * Iy_img[y-1][x+1] +
Ix_img[y ][x-1] * Iy_img[y ][x-1] + Ix_img[y ][x ] * Iy_img[y ][x ] + Ix_img[y ][x+1] * Iy_img[y ][x+1] +
Ix_img[y+1][x-1] * Iy_img[y+1][x-1] + Ix_img[y+1][x ] * Iy_img[y+1][x ] + Ix_img[y+1][x+1] * Iy_img[y+1][x+1];
responce = (A * C - B * B) - k * (A + B) * (A + B);
if(responce > threshold){
responce *= (255/harris_max);
response_Harris[y][x] = responce;
response_Harris[y][x] = responce;
}else
response_Harris[y][x] = 0;
}
// last column set to zero
response_Harris[height][x]= 0;
}
// last line set to zero
for(y=0; y<height_src; y++)
response_Harris[y][width]= 0;
ret = LocalMaxima(response_Harris, img_dst, 2);
if(ret == 0){
corner_counter.resize(0);
// have corner count
for(int y=5; y<height_src-5;y++){
for(int x=5; x<width_src-5; x++){
if(img_dst[y][x] != 0){
corner_counter.push_back(x);
corner_counter.push_back(y);
}
}
}
int counter = corner_counter.size();
}
}
// end
return 0;
} |
Partager