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
|
typedef struct _ConCompo {
//Bounding Box position
int min_x,max_x;
int min_y,max_y;
//Center position
int x,y;
//Area
int area;
} ConCompo;
void fillConCompo(ConCompo *blobs,const int nof_blobs, const IplImage *im) {
int x,y;
uchar *line, id; //Pointer on the image
if (im->nChannels!=1 || im->depth!=IPL_DEPTH_8U) {
printf("Image input is not valid\n");
fflush(stdout);
return;
}
//Itarate over the whole image
for (y=0; y<im->height; y++) {
line=(uchar*)(im->imageData+y*im->widthStep);
for (x=0; x<im->width; x++) {
//If pixel is not black
if (line[x]) {
//The labels start from 1 but the array of blobs from 0
id=line[x]-1;
if(id < nof_blobs) {
//Set Max Values (Bounding Box)
if(blobs[id].max_x < x) blobs[id].max_x = x;
if(blobs[id].max_y < y) blobs[id].max_y = y;
//Set Min Values (Bouding Box)
if(blobs[id].min_x > x) blobs[id].min_x = x;
if(blobs[id].min_y > y) blobs[id].min_y = y;
//Set Center Position
blobs[id].x+=x;
blobs[id].y+=y;
//Set The area
blobs[id].area++;
}
}
}
}
for(id=0;id<nof_blobs;id++) {
blobs[id].x/=blobs[id].area;
blobs[id].y/=blobs[id].area;
}
} |
Partager