Link to home
Start Free TrialLog in
Avatar of Besbous
Besbous

asked on

controlling a robot arm problem

Ok this is my first time ! Hi there !
I ve got a problem, I m working now on a experement of controlling Robot arm using image source(camera) that orders to the robert to move when it s sees an object, using the image signal processing. My problem is in the labeling phase, also I have written a program to calculate and draw the center of inertia just of 1 object, but i don t know how to do it for infinite numbers of objects.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#include "robot.h"
#include "img.h"

#define MODE 4
/********************/

extern BYTE *img_work2;
extern BYTE *img_work3;

/********************/
/*********************************************************************************

**********************************************************************************/
void disp_image_while_kbhit(BYTE *img, HDC *hDC); /*(サンプル)*/
void gray2(BYTE *img_src, HDC *hDC);
void get_rid_of_noise(BYTE *img_src, BYTE *img_tmp, HDC *hDC);
double scan_angle(BYTE *img, int g_x, int g_y);
void straight_line(BYTE *img, int g_x, int g_y, double theta, HDC *hDC);
double sqrt( double x );
double atan2( double x,double y);
double tan( double x);

void myfunc(BYTE *img_src , BYTE *img_tmp , HDC *hDC){
  char motor;
  int  count;
  int  x1, y1, z1, x2, y2, z2 ;
  int i, j, s=0;
  int g_x, g_y, x_sum, y_sum, all_sum, threshold;
  double theta;

  switch(MODE){
  /*-- rotate_longの動作確認(1) ----------------------------------------*/
  case 1:
    printf("rotate_longのmovement check(1)\n");
    printf(" motor? ");
    scanf("%c", &motor);

    printf(" count? ");
    scanf("%d", &count);

    rotate_long( motor, count );
    break;
  /*-- rotate_long movement check(2) ----------------------------------------*/
  case 2:
    count = 120;
    motor = 'B';
    printf("rotate_long\n");
    printf("モーター%cを%dだけ回転します。\n",motor,count);
    rotate_long( motor, count );
    getchar();
    printf("モーター%cを%dだけ回転します。",motor,-count);
    printf("元に戻れば、正しく動作しています。\n",motor,-count);
    rotate_long( motor, -count );
    getchar();
    break;
  /*-- move, move3 check-------------------------------------------*/
  /*    自作してください   */
  case 3:
    /* (1)coordonates*/
      printf("x1=\n"); scanf("%d",&x1);
      printf("x2=\n"); scanf("%d",&x2);
      printf("y1=\n"); scanf("%d",&y1);
      printf("y2=\n"); scanf("%d",&y2);
      printf("z1=\n"); scanf("%d",&z1);
      printf("z2=\n"); scanf("%d",&z2);
     
    /* (2)move the arm from Home vision to position1,oepn and close the grip */
      move(XHOME, YHOME, ZHOME, x1, y1, z1);
      grip();
      ungrip();
    /* (3)move the arm from cposition1 to position 2 */
      move(x1, y1, z1, x2, y2, z2);
      grip();
      ungrip();
    /* (4)move the arm back to home vision from position 2、and close back the grip */
      move(x2, y2, z2, XHOME, YHOME, ZHOME);
      home('A');
    /* (5)stop motors */
      stop_all();
    break;
  /*-- move, move3 -------------------------------------------*/

  case 4:
    /*display the image*/
       disp_image_while_kbhit(img_src, hDC);
    /*call the image source*/
        get_image(img_src);
      /*gray the image*/
         copy_image(img_src,img_tmp);
           gray2(img_tmp,hDC);
      /*kill the noise*/
        disp_gray_image(img_tmp,hDC);
         get_rid_of_noise(img_tmp,img_work2,hDC);
          copy_image(img_work2,img_tmp);
       
                show_monitor();
          disp_image(img_tmp,hDC);
        getchar();
      
            /*labeling*/
     
    /* the center of inertia */
      /*Caluculation the Surface :white color = object*/
       for(i=0;i<WIDTH;i++){
            for(j=0;j<HEIGHT;j++){
                   if(gray(img_tmp,j,i)==255){
                   s++;
                   }
             }
      }
    /*get the coordonates of the center of inertia */
     x_sum=0;
     y_sum=0;
     for(i=0;i<WIDTH;i++){
        for(j=0;j<HEIGHT;j++){
            if(gray(img_tmp,j,i)==255){
             x_sum+=i;
             y_sum+=j;
            }
      }
  }
  g_x=x_sum/s;
  g_y=y_sum/s;
  printf(" gx= %d, gy= %d\n", g_x, g_y);
      /*(draw)*/
  for(i=0;i<480;i++){
      rgb(img_tmp,i,g_x,R)=255;
    rgb(img_tmp,i,g_x,G)=0;
      rgb(img_tmp,i,g_x,B)=0;
  }
  for(j=0;j<640;j++){
      rgb(img_tmp,g_y,j,R)=255;
      rgb(img_tmp,g_y,j,G)=0;
      rgb(img_tmp,g_y,j,B)=0;
  }
  show_monitor();
   disp_image(img_tmp,hDC);
    getchar();
   /* get the angle */
  theta = scan_angle(img_tmp, g_x, g_y);
   printf("Angle=(%lf)\n",theta);
    straight_line(img_tmp, g_x, g_y, theta, hDC);
      show_monitor();
     disp_image(img_tmp,hDC);
   getchar();
  /*-------------------------------------------------------------------*/
    break;

  default:
    printf("MODE=%dは有効ではありません。\n",MODE);
  }
}



/*++  キーボード入力があるまでカメラの映像を表示する(サンプル) ++*/
void disp_image_while_kbhit(BYTE *img, HDC *hDC){
  show_monitor();        
  while(!_kbhit()){      
    get_image(img);      
    disp_image(img,hDC);  
  }
  _getch();              
}
 
void gray2(BYTE *img_src, HDC *hDC){
       
        int threshold;
         to_gray(img_src);
            from_gray(img_src);
              disp_image(img_src,hDC);
          show_monitor();
          disp_histgram(img_src,hDC);
        printf("Threshold Range =\n");
        scanf("%d", &threshold);
        binarize(img_src,threshold);
}

void get_rid_of_noise(BYTE *img_src, BYTE *img_tmp, HDC *hDC){
      int k,l;
printf("Enter Dilation EffectNum\n");
        scanf("%d",&k);
        printf("Enter Erosion EffectNum\n");
        scanf("%d",&l);
        copy_image(img_src,img_tmp);      
      erosion(img_tmp,k);
      dilation(img_tmp,l);
      dilation(img_tmp,l);
      erosion(img_tmp,k);
        show_monitor();
        disp_image(img_tmp,hDC);
        getchar();
}

double scan_angle(BYTE *img, int g_x, int g_y){
  double theta, lamda, s11=0, s12=0, s22=0;
   int x, y;
     for(x=0;x<WIDTH;x++){
        for(y=0;y<HEIGHT;y++){
              if(gray(img_work2, y, x)>100){
         s11=(x-g_x)*(x-g_x);
         s12=(x-g_x)*(y-g_y);
         s22=(y-g_y)*(y-g_y);
            }
        }
       }
        lamda=(s11+s22+sqrt(s11*s11+s22*s22-2*s11*s22+4*s12*s12))/2;
            theta=atan2(lamda-s11,s12);
        return theta;
}

void straight_line(BYTE *img_tmp, int g_x, int g_y, double theta, HDC *hDC){
  int x, y;
   for(x=0;x<WIDTH;x++){
              y=(double)(x-g_x)*tan(theta)+g_y;
       if(0>=y && y<HEIGHT){
    rgb(img_tmp,y,x,R)=255;
      rgb(img_tmp,y,x,G)=0;
      rgb(img_tmp,y,x,B)=0;
         }
   }
}
/*********************************************************************************
  *********************************************************************************/
#define LIMIT 90

void rotate_long( char motor, int count ) {

  int  margin, dir;


   if(count>0){
     dir = 1;
   }else{
     dir = -1;
     count =-count;
  }

  while( count > 0 ) {
    /* (2) もし motor がストールしていたら, リターンする */
        if(stalled(motor)==1){
        return;
       }

        margin = LIMIT-distance(motor);
   
      if(count<margin){
      rotate(motor,dir*count);
      count=LIMIT;
      return;
     }
    /* (5) margin が正の場合にのみ次のような処理を行う:
            margin の値だけ移動を指令し, count を margin だけ減らす */
       if(margin>0){
       rotate(motor,dir*margin);
       count=count-margin;
    }
  }
 
}


void      wait_DEF( void ) {
  /* モータ D, E, F の動作終了待ちルーチン
   (D, E, F のエラーレジスタのいずれかが正である間,
   while ループを回り続ける) */
    while(distance('D')>0 || distance('E')>0 || distance('F')>0){
    }
}

void       move(int x1, int y1, int z1, int x2, int y2, int z2) {

  double    a[3],forward[3];

  /* 手先を(x1, y1, z1) から (x2, y2, z2) へ移動させよ. */

  /* (1)move from (x1, y1, z1) to (x2, y2, z2) */
      angle(x1,y1,z1,x2,y2,z2,a);
  /* (2)配列 a に求めた回転角をステップ数に変換し,
      それぞれ配列 forward に収める */
       forward[MOTOR_D]=a[MOTOR_D]/STEP_D;
       forward[MOTOR_E]=a[MOTOR_E]/STEP_E;
       forward[MOTOR_F]=a[MOTOR_F]/STEP_F;
  /* (3)各関節を回転させる */
       rotate_long('D',forward[MOTOR_D]);
       rotate_long('E',forward[MOTOR_E]);
       rotate_long('F',forward[MOTOR_F]);
  /* (4)動作が終了するまで待つ */
       while(distance('D')>0 || distance('E')>0 || distance('F')>0) {
    }


}


void       move3(int x1, int y1, int z1, int x2, int y2, int z2) {
  /* this part i want to move all the motors in the same time. so I wrote this following, not sure wether it s compatible or not */
int delta_x, delta_y, delta_z, dMAX;
  delta_x = x2-x1;
  delta_y = y2-y1;
  delta_z = z2-z2;
  dMAX = abs(delta_x);
  if (dMAX<abs(delta_y) && dMAX>abs(delta_z))
  dMAX = abs(delta_y);
  if(dMAX<abs(delta_z) && dMAX>abs(delta_y))
  dMAX = abs(delta_z);
  if(dMAX<abs(delta_y) && dMAX<abs(delta_z) && abs(delta_z)<abs(delta_y))
  dMAX = abs(delta_y);
  if(dMAX<abs(delta_y) && dMAX<abs(delta_z) && abs(delta_y)<abs(delta_z))
  dMAX = abs(delta_z);
  move(x1, y1, z1, dMAX + x1,  dMAX + y1, dMAX + z1);

}
/****************************************************************************************/

 actually what i want from this program is to display something like the following :
http://www.convict.lu/Jeunes/5%20DOF%20Robot-arm.htm (Get_object_positionsRAC.vi.). so I want my program to display howmany objects r there, and their coordonates of center of inetia, the angle ...!
 ah I think I want too much ! but man I have headach becoz of this program!
Avatar of sunnycoder
sunnycoder
Flag of India image

>I have written a program to calculate and draw the center of inertia just of 1 object, but i don t know how to do it for infinite numbers of objects.

while (1)
{
            //calculate center of inertia
}

apart from that I cant seem to make much from your problem ... what is the issue with the code or what else would you like to add
Avatar of Besbous
Besbous

ASKER

Ok one of my questions is I have the following program for 1 object, but i need for more than one no matter how many objects the camera catches, it should display all the object with their angles and coordonates ...

    /*Caluculation the Surface :white color = object*/
       for(i=0;i<WIDTH;i++){
          for(j=0;j<HEIGHT;j++){
                if(gray(img_tmp,j,i)==255){
                s++;
                }
           }
     }
    /*get the coordonates of the center of inertia */
     x_sum=0;
     y_sum=0;
     for(i=0;i<WIDTH;i++){
       for(j=0;j<HEIGHT;j++){
          if(gray(img_tmp,j,i)==255){
           x_sum+=i;
           y_sum+=j;
          }
     }
  }
  g_x=x_sum/s;
  g_y=y_sum/s;
  printf(" gx= %d, gy= %d\n", g_x, g_y);
     /*(draw)*/
  for(i=0;i<480;i++){
     rgb(img_tmp,i,g_x,R)=255;
    rgb(img_tmp,i,g_x,G)=0;
     rgb(img_tmp,i,g_x,B)=0;
  }
  for(j=0;j<640;j++){
     rgb(img_tmp,g_y,j,R)=255;
     rgb(img_tmp,g_y,j,G)=0;
     rgb(img_tmp,g_y,j,B)=0;
  }
  show_monitor();
   disp_image(img_tmp,hDC);
    getchar();
   /* get the angle */
  theta = scan_angle(img_tmp, g_x, g_y);
   printf("Angle=(%lf)\n",theta);
    straight_line(img_tmp, g_x, g_y, theta, hDC);
      show_monitor();
     disp_image(img_tmp,hDC);
   getchar();
put entire code in this loop

while (1)
{
    //your code for calculations here
    if ( more objects )
          continue;
    else
          break;
}

replace more objects with the test which will determine if there are more objects to work upon
alternatively, if you can determine the number of objects before starting the calculations, then you can use something like

int i;

for ( i = 0; i < count ; i++ ) /* count is the number of objects */
{
      //your calculations here
}

if you have objects stored in an array, you can use the iteration index i for retriving objects from array

Avatar of Besbous

ASKER

I tried using arrays for 4 objects for example, but if it s less then 4 objects it doesn t work !

/*Caluculation the Surface = Get the white part(solid)*/
            id=gray(img_tmp,j,i);

       for(i=0;i<WIDTH;i++){
            for(j=0;j<HEIGHT;j++){
            id=gray(img_tmp,j,i);
            if(0<id && id<=4){
            x[id-1]+=i;
            y[id-1]+=j;
            s[id-1]++;
                   }
             }
      }
    /*&#37325;&#24515;&#12398;&#24231;&#27161;]&#12434;&#34920;&#31034;&#12377;&#12427;*/

  for(i=0;i<=4;i++){
  g_x[i]=x[i]/s[i];
  g_y[i]=y[i]/s[i];
  }
  printf(" gx1= %f, gy1= %f\n", g_x[1], g_y[1]);
  printf(" gx2= %f, gy2= %f\n", g_x[2], g_y[2]);
  printf(" gx3= %f, gy2= %f\n", g_x[3], g_y[3]);
  printf(" gx4= %f, gy4= %f\n", g_x[4], g_y[4]);
      /*(&#37325;&#24515;&#12398;&#20013;&#24515;&#12395;&#32218;&#12434;&#24341;&#12367;)*/
  for(i=0;i<WIDTH;i++){
      rgb(img_src,i,g_x[1],G)=255;
      rgb(img_src,i,g_x[2],R)=255;
      rgb(img_src,i,g_x[3],B)=255;
      rgb(img_src,i,g_x[4],G)=255;
  }
  for(j=0;j<HEIGHT;j++){
      rgb(img_src,g_y[1],j,G)=255;
      rgb(img_src,g_y[2],j,R)=255;
      rgb(img_src,g_y[3],j,B)=255;
      rgb(img_src,g_y[4],j,G)=255;
  }
  show_monitor();
  disp_image(img_src,hDC);

 and for the loop suggestion, for printf it wouldn t give all the calculation for all the objects. if you check : http://www.convict.lu/Jeunes/5%20DOF%20Robot-arm.htm (Get_object_positionsRAC.vi.). it shows the hitgram. i have all that but only for 1 object !
>for printf it wouldn t give all the calculation for all the objects
move the printf too inside the loop ... in each iteration it will calculate the figures and print them before moving on to next object

>I tried using arrays for 4 objects for example, but if it s less then 4 objects it doesn t work
how do you determine number of objects ? can you give me some test/expression/statement which will tell the number of objects

Avatar of Besbous

ASKER

how do you determine number of objects ? can you give me some test/expression/statement which will tell the number of objects

this statment determines the object

     for(i=0;i<WIDTH;i++){
        for(j=0;j<HEIGHT;j++){
            if(gray(img_tmp,j,i)==255){

 it means when the: gray(img_tmp,j,i)==255 : it means the whole surface is gray exept the object that is white ! if u check :  http://www.convict.lu/Jeunes/5%20DOF%20Robot-arm.htm (Get_object_positionsRAC.vi.).  you  ll see the image !
I checked the link ... there is way too much information on it for me to patiently read through ;o)


>this statment determines the object

>    for(i=0;i<WIDTH;i++){
>      for(j=0;j<HEIGHT;j++){
>         if(gray(img_tmp,j,i)==255){
I assume that if this if statement is true, then it is another object for processing... right ?
In that case

while (1)  /* infinite loop */
{
    for(i=0;i<WIDTH;i++){
      for(j=0;j<HEIGHT;j++){
         if(gray(img_tmp,j,i)==255){
                 /* your processing code here */
                 /* print the results here  */
          } /* if closed here */
          else
               break; /* if we do not have an object get out of the loop*/
} /* end of while */

this loop will go though WIDTH and HEIGHT loop until it encounters first if(gray(img_tmp,j,i)==255) statement which evaluates to false

if you wish to evaluate all the values rather than exit on first failed test, then use

    for(i=0;i<WIDTH;i++){
      for(j=0;j<HEIGHT;j++){
         if(gray(img_tmp,j,i)==255){
                 /* your processing code here */
                 /* print the results here  */
          } /* end of if */
this loop will test all the values
Avatar of Besbous

ASKER

the link is just to see the image ! not to read ! i didn t read :) just the picture of the object in gray then in colors ! and the data of the objects, that s what i want to get from this program !
It s night here in Japan! i ll be checking in the morning :) thx a lot
Avatar of Besbous

ASKER


  what i want from the program, is 1st calculate the area of the objects, then get the coordonates of their center of inertia, then the angles, then draw the lines passing through the center. i wrote before :

 for(i=0;i<WIDTH;i++){
            for(j=0;j<HEIGHT;j++){
                   if(gray(img_tmp,j,i)==255){
                   s++;
                   }
             }
      }
    /*get the coordonates of the center of inertia*/
     x_sum=0;
     y_sum=0;
     for(i=0;i<WIDTH;i++){
        for(j=0;j<HEIGHT;j++){
            if(gray(img_tmp,j,i)==255){
             x_sum+=i;
             y_sum+=j;
            }
      }
  }
  g_x=x_sum/s;
  g_y=y_sum/s;
  printf(" gx= %d, gy= %d\n", g_x, g_y);
      
  for(i=0;i<480;i++){
      rgb(img_tmp,i,g_x,R)=255;   /* drawing the line parallel to X-axis and passing by the center of inertia*/
                rgb(img_tmp,i,g_x,G)=0;
      rgb(img_tmp,i,g_x,B)=0;
  }
  for(j=0;j<640;j++){
      rgb(img_tmp,g_y,j,R)=255;      /* drawing the line parallel to Y-axis and passing by the center of inertia*/
      rgb(img_tmp,g_y,j,G)=0;
      rgb(img_tmp,g_y,j,B)=0;
  }
  show_monitor();
   disp_image(img_tmp,hDC);
    getchar();
   /* get the angle */
  theta = scan_angle(img_tmp, g_x, g_y);
   printf("Angle=(%lf)\n",theta);
    straight_line(img_tmp, g_x, g_y, theta, hDC);
      show_monitor();
     disp_image(img_tmp,hDC);
   getchar();
    break;
/*************************/

 so according to the suggestion, for multiple objects:

            while (1) {      
     x_sum=0;
     y_sum=0;
       for(i=0;i<WIDTH;i++){
            for(j=0;j<HEIGHT;j++){
                   if(gray(img_tmp,j,i)==255){
                   s++;
          x_sum+=i;
          y_sum+=j;
  g_x=x_sum/s;
  g_y=y_sum/s;
  theta = scan_angle(img_tmp, g_x, g_y);
  printf("Area     Pos-x     Pos-y     Angle\n");
  printf(" %d, %d, %d, %lf\n", s, g_x, g_y, theta);

        rgb(img_tmp,i,g_x,R)=255;
    rgb(img_tmp,i,g_x,G)=0;
      rgb(img_tmp,i,g_x,B)=0;

      rgb(img_tmp,g_y,j,R)=255;
      rgb(img_tmp,g_y,j,G)=0;
      rgb(img_tmp,g_y,j,B)=0;

      straight_line(img_tmp, g_x, g_y, theta, hDC);
                   }
       else
             break;
            }
      }
}
   show_monitor();
   disp_image(img_tmp,hDC);
   getchar();
   getchar();

is this correct?
 And would you please tell me how to find the leftX, rightX, UpperY, lowerY of the object (border coordonates)!
 thx a lot !
>is this correct?
yes you got it right ... note that this loop will exit as soon as it finds first case when if(gray(img_tmp,j,i)==255) evaluates to false

>would you please tell me how to find the leftX, rightX, UpperY, lowerY of the object (border coordinates)

for(i=0;i<WIDTH;i++){
         for(j=0;j<HEIGHT;j++){
               if(gray(img_tmp,j,i)==255){
is this scanning pixels... if yes, then as soon as you get a pixel that belongs to an object, note down the co-ordinates... you will get one set of border co-ordinates in a scan while you will have to use comparisons for determining another... I hope I am not confusing you

              a                
                ________
              /                 \
             /                   \
           c ------------------ b

consider the above object... suppose you are scanning pixels from top to bottom in outer loop and left to right in inner loop
i.e. for each row you will check all columns and then move on to next row
the first pixel you detect will be a... this will be the upperY....
the last pixel you see would be lowerY
however, to determine leftX and rightX, you will have to keep track of lowest and highest X encountered so far and compare the values against each pixel
Avatar of Besbous

ASKER

Thx a lot for the shema. yeah  it scans pixel
is the following correct ?

int  leftX, rightX, lowerY,  upperY, id;
for(i=0;i<WIDTH;i++){
         for(j=0;j<HEIGHT;j++){
               if(gray(img_tmp,j,i)==255){
               id = gray(img_tmp,j,i);
               for(gray(img_tmp,j,leftX)<id)
               id = gray(img_tmp,j,leftX);
               for(gray(img_tmp,j,rightX)>id)
               id = gray(img_tmp,j,rightX);
               for(gray(img_tmp,upperY,i)<id)
               id = gray(img_tmp,upperY,i);
               for(gray(img_tmp,lowerY,i)>id)
               id = gray(img_tmp,lowerY,i);
      printf("%d, %d, %d\n",leftX, rightX, UpperY, lowerY);
     }
  }
}

Avatar of Besbous

ASKER

if(gray(img_tmp,j,leftX)<id) not for! sorry for the mistake !
not exactly ... what is the exact functionality of gray()... I am under impression that it returns 255 if the pixel belongs to an object... what other return values and scenarios are possible

I was suggesting something on these lines...

int  leftX, rightX, lowerY,  upperY, flag;
flag = 0;

for( i = 0; i < WIDTH; i++ )
{
        for( j = 0; j < HEIGHT; j++ )
       {
              if( gray(img_tmp,j,i) == 255 )
              {
                     if ( flag == 0 )   /* First object pixel that we found */
                     {
                             leftX = i;
                             flag = 1;
                     }
....
 
Avatar of Besbous

ASKER


 gray() is a function the makes the image looks gray, but the objects look white ! actually 0 means black and 255 means white ! so in between it s all the possible colors.
 the first
int  leftX, rightX, lowerY,  upperY, flag;
flag = 0
for( i = 0; i < WIDTH; i++ ){
        for( j = 0; j < HEIGHT; j++ ){
              if( gray(img_tmp,j,i) == 255 ){
                     if ( flag == 0 ){   /* First object pixel that we found */
                             leftX = i;
                             flag = 1;
                     }
  i couldn t figure out how to proceed with rightX, upperY and lowerY, wether use the same flag for each or different. sorry for asking so much questions !
> i couldn t figure out how to proceed with rightX, upperY and lowerY, wether use the same flag for each or different. sorry for asking so much questions !
the problem here is our miscommunication... while I do not understand your code, you are finding it hard to understand my logic
So I'll do this:
I'll write down the stuff in pure english, the entire algorithm, step by step.... you understand it first and then convert it into C... ok, read carefully

Scenario: given an image which has a single object somwhere in it, we have to determine the leftX, rightX, upperY and lowerY

set flag1 to 0
for each possible value of width
      for each possible value of height
            if the pixel belongs to the object
                  if flag1 is 0
                          set leftx to x co-ordinate of this pixel
                          set flag1 to 1
                          save x co-ordinate of this pixel in temporary variable tempright
                          save y co-ordinate of this pixel in temporary variables tempupper and templower
           if currentx > tempright
                   set tempright = currentx
           if currenty > tempupper
                   set tempupper = currenty
           if currenty < templower
                   set templower = currenty
set rightx = tempright
set uppery = tempupper
set lowery = templower

notice the indentations carefully ... they indicate which statement belongs to which block
Avatar of Besbous

ASKER

Hi sunny! thx a lot for your help, but actually when i wanted to try the program on the robot, it got crazy :)as (gray(img_tmp,y,x)==255) scans pixel, it doesn t give the calculation for the object but for every pixel of the object !  
          while (1) {    
     x_sum=0;
     y_sum=0;
      for(i=0;i<WIDTH;i++){
          for(j=0;j<HEIGHT;j++){
                if(gray(img_tmp,j,i)==255){
                s++;
         x_sum+=i;
         y_sum+=j;
  g_x=x_sum/s;
  g_y=y_sum/s;
  theta = scan_angle(img_tmp, g_x, g_y);
  printf("Area     Pos-x     Pos-y     Angle\n");
  printf(" %d, %d, %d, %lf\n", s, g_x, g_y, theta);

       rgb(img_tmp,i,g_x,R)=255;
    rgb(img_tmp,i,g_x,G)=0;
     rgb(img_tmp,i,g_x,B)=0;

     rgb(img_tmp,g_y,j,R)=255;
     rgb(img_tmp,g_y,j,G)=0;
     rgb(img_tmp,g_y,j,B)=0;

     straight_line(img_tmp, g_x, g_y, theta, hDC);
                }
      else
           break;
          }
     }
}
   show_monitor();
   disp_image(img_tmp,hDC);
   getchar();
   getchar();

this loop is infinite for pixel not for object! so i don t know how to make it work for objects.

 the following shemas: (1) is before labelling (2) is after,  so actually a labeled image show the object with colors (1) is gray and white, (2) is supposed to be black and colored object.
i tried this:
        ________________________________________________>
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                                
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                                  
       |_|_|_|_|            |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                                  
       |_|_|_|_|            |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
       |_|_|_|_|            |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|     |_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|     |_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|     |_|_|_|_|_|_|_|_|
       |_|_|_|_|        |_|_|_|_|_|_|_|_|_|     |_|_|_|_|_|_|_|_|
       |_|_|_|_|_|        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
                                            (1)
 
        ________________________________________________
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                                
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                                  
       |_|_|_|_| 1  1  1 |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                                  
       |_|_|_|_| 1  1  1 |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
       |_|_|_|_| 1  1  1 |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|2 2|_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|2 2|_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|2 2|_|_|_|_|_|_|_|_|
       |_|_|_|_| 3  3  3 |_|_|_|_|_|_|_|_|2 2|_|_|_|_|_|_|_|_|
       |_|_|_|_|_| 3  3  3 |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_| 3  3  3 |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
                                       (2)
 
int select_label(BYTE *img_work3,int n_label,HDC *hDC){
      int x,y,col;
      int color_r[17]={0,255,255,255,  0,255,  0,  0,166,130,255,255,166,255,166,  0,199};
      int color_g[17]={0,  0,  0,255,  0,177,255,255,166,  0,255,160,255,255,166,  0,255};
      int color_b[17]={0,  0,255,  0,255,  0,  0,255,255,130,150,255,  0,255,166,100,199};
      if(n_label<=16){
            for(x=0;x<640;x++){
                  for(y=0;y<480;y++){
                        col=gray(img_work3,y,x);
                        rgb(img_work3,y,x,R)=color_r[col];
                        rgb(img_work3,y,x,G)=color_g[col];
                        rgb(img_work3,y,x,B)=color_b[col];
                  }
            }
         show_monitor();
         disp_image(img_work3,hDC);
         col=0;
         while(1>col||col>n_label){
           scanf("%d",&col);
         }
         return col;
      }else return 0;
}

im not sure if it s correct !
wow!!! you need patience to build figures like that ... you are commendable

Ok back to business... first apologies from my side ... even though I said
>notice the indentations carefully ... they indicate which statement belongs to which block
I myself made an error ... algo should be

set flag1 to 0
for each possible value of width
     for each possible value of height
           if the pixel belongs to the object
                 if flag1 is 0
                         set leftx to x co-ordinate of this pixel
                         set flag1 to 1
                         save x co-ordinate of this pixel in temporary variable tempright
                         save y co-ordinate of this pixel in temporary variables tempupper and templower
                 if currentx > tempright
                        set tempright = currentx
                 if currenty > tempupper
                        set tempupper = currenty
                 if currenty < templower
                       set templower = currenty
set rightx = tempright
set uppery = tempupper
set lowery = templower

the three ifs are to be executed only if the pixel belongs to an object ...

however, you current problem description is better and hopefully we will get a solution this time :o)

Now you are given an image and you wish to find leftX, rightX, upperY and lowerY for each object ... before I proceed, Pls confirm that this is what indeed the objective
Avatar of Besbous

ASKER

thx a lot Mr sunny ! and yeah indeed this what the objective is !

 ok I ll try to compile it n see ! thx a lot !
wait .. the above algo will not work if there are more than 1 objects in the image ... I will post the new algoritm in a while
Avatar of Besbous

ASKER

oh   ok thx !
assuming that objects can be placed anywhere in the room, this is going to be quite complex... anyway here we go

function scan starts:::
for each possible value of width
    for each possible value of height
          if the pixel belongs a new object
                        set leftx to x co-ordinate of this pixel
                        call function found_object with current pixel as parameter
end of function scan


function found_object starts:::
put the current pixel in the queue
while queue is not empty
          remove the front pixel from the queue
          if currentx > tempright
                  set tempright = currentx
          if currenty > tempupper
                  set tempupper = currenty
          if currenty < templower
                   set templower = currenty
          determine all its neighbours
          for each neighbour X that belongs to the object
                    check if all neighbours of X are pixels of the objects.
                    If yes, then
                             discard it
                    else
                             put X at the end of the queue
end of while loop
set rightx = tempright
set uppery = tempupper
set lowery = templower
end of function found_object

The idea is if you find a pixel that does not belong to an object that you have seen so far (how to determine this, I'll tell later) then
1. this is a pixel on the boundary of the object... to be more precise this is the leftX according to our scanning sequence
2. other three bounding co-ordinates will also be pixels on the edges of the object
3. all boundary pixels will have atleast one neighbouring pixel which does not belong to the object

keeping this in mind, re-read the function found_object and try to understand it. It is important that you understand the function because it is complex and coding it wil require more than just a simple translation from english to C.

Last of all... how do you determine if this pixel is part of the object that you have already seen...
there are two approaches
1. maintain additional information for each pixel e.g. an int which if 0 means that we have not seen the pixel yet and if 1 means that we have already seen the pixel
now whenever you see a pixel in either scan or found_object, set it to 1. you will have to modify scan a bit... Why? because, consider this scenario

      |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                                
      |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                                  
      |_|_|_|_|            |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|                                                                                  
      |_|_|_|_|            |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
      |_|_|_|_|   B        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
      |_|_|_|A|_|_|_|_|_|_|_|_|_|_|_|_|     |_|_|_|_|_|_|_|_|
       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|     |_|_|_|_|_|_|_|_|
      |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|     |_|_|_|_|_|_|_|_|
      |_|_|_|_|        |_|_|_|_|_|_|_|_|_|     |_|_|_|_|_|_|_|_|
      |_|_|_|_|_|        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
      |_|_|_|_|_|_|        |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
      |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
                                           (1)
In your scan you first found A... next you will call found_object which will see all the pixels on the boundary of this object but none of the internal objects... when your scan will see B, it wil think that it has not seen this pixel and it belongs to a new object ... thus the algorithm will fail... How to overcome this?
modify scan to

function scan starts:::
for each possible value of width
    for each possible value of height
          if the pixel belongs an object
                        if this pixel was not seen before
                               set leftx to x co-ordinate of this pixel
                               call function found_object with current pixel as parameter
                       else
                               until we get a background pixel increment width and height index(continue scanning)
end of function scan

make sure you understand this thoroughly as small error will make it hard to debug

second approach:
use co-ordinate geometry .... draw a ray from that pixel to any edge of the object... find intersection points of this ray and the object. If there are odd number of interesections then the pixel is internal to the object otherwise it is external to the object.

Good Luck
Avatar of Besbous

ASKER

thx i ll check now if it works !
make sure that you properly understand it before you start coding or else you may land into trouble
Avatar of Besbous

ASKER

Hello sunny, How is the weekend.

 ok I had a hard time figuring out the prog. I came up with this but appearently it s not complere or not working even !
would u please check it : I really need this one .


for(i=0;i<WIDTH;i++){
 for(j=0;j<HEIGHT;j++){
    if(gray(img_work2,j,i)==255){
     x = i;
     found_object(img_work2,i,j);
    }
  }
}

void found_object(BYTE *img, int i, int j){
    int i, j, id;
   
    id = gray(img_work2,j,i);  /* put the current pixel in the queue. I don t understand wether it s this way or not*/
    while(id!=0){
      if(gray(img_work2,j,i+1)=0){
       id!=gray(img_work2,j,i);
       }
             if(i>tempright)
                 tempright = i;
             if(j>tempupper)
                 tempupper = j;  
             if(j<templower)
                 templower = j;
/*check if all the neighbours of X are pixels of the objext*/
    if(gray(img_work2,j,i)==255 && gray(img_work2,j,i+1)=255 && gray(img_work2,j,i-1)=255 && gray(img_work2,j+1,i)=255 && gray(img_work2,j-1,i)=255){
            discard; /* I don t know how to discard*/
           }else{
              put X at the end of the queue;  /* i don t know how to express the queue
     }
    }         rightX = tempright;
             upperY = tempupper;
             lowerY = templower;
                 

}


 also, the following program shows a error, is not a good muching of esle. i couldn t find the error

       for(i=0;i<WIDTH;i++){
            for(j=0;j<HEIGHT;j++){
                   if(gray(img_work2,j,i)==label){
            gray(img_work2,j,i)=255;
                   }else{
            gray(img_work2,j,i)=0;
                   }
   printf("Area   Pos-x   Pos-y   Angle   leftX   rightX   upperY   lowerY\n");
   while (1) {      
     x_sum=0;
     y_sum=0;
       for(i=0;i<WIDTH;i++){
            for(j=0;j<HEIGHT;j++){
                   if(gray(img_work2,j,i)==label){
                   s++;
                  x_sum+=i;
                  y_sum+=j;

  g_x=x_sum/s;     /*&#37325;&#24515;&#12398;&#24231;&#27161;]&#12434;&#34920;&#31034;&#12377;&#12427;*/
  g_y=y_sum/s;
  theta = scan_angle(img_work2, g_x, g_y);  /* get the angle */
  get_rightX_leftX_upperY_lowerY(img_work2, label);
  printf(" %d, %d, %d, %lf, %d, %d, %d, %d\n", s, g_x, g_y, theta, leftX, rightX, upperY, lowerY);
       
     /*(&#37325;&#24515;&#12398;&#20013;&#24515;&#12395;&#32218;&#12434;&#24341;&#12367;)*/
        for(i=0;i<480;i++){
        rgb(img_work2,i,g_x,R)=255;      
    rgb(img_work2,i,g_x,G)=0;
      rgb(img_work2,i,g_x,B)=0;
        }
        for(j=0;j<640;j++){
      rgb(img_work2,g_y,j,R)=255;
      rgb(img_work2,g_y,j,G)=0;
      rgb(img_work2,g_y,j,B)=255;
        }
      straight_line(img_tmp, g_x, g_y, theta, hDC);
      disp_image(img_work2, hDC);
      getchar();
      getchar();
                   } else
            break;
           }
       }       

 i would be thankful if u hel me out in these ! thx a lot
I was expecting that :o)
queues are not what you are thinking them to be... they are special data structures which you will have to implement in your program
http://www.cs.fiu.edu/~weiss/dsaa_c2e/queue.c
http://www.cs.cf.ac.uk/Dave/C/section2_22_14.html

I'll try to post some ad hoc code (untested) in a day or two (cant give too much time on weekdays :o( )
I am posting some semi-complete code ... this should get you an idea...
if you are not able to understand, just ask... you need to complete this ... I may not be able to write the entire thing
also I wrote this code on the fly ... so If you feel anything is inaccurate/missing post back

struct pixel
{
      BYTE * img;
      int seen;
      int i;
      int j
};

struct list
{
        struct pixel    p;
        struct list * next;
};

struct list * start = NULL;

/* initially seen for each pixel should be 0 */

for(i=0;i<WIDTH;i++)
{
      for(j=0;j<HEIGHT;j++)
      {
              if(gray(img_work2,j,i)==255 /* && pixel was not seen before*/)
              {
                     leftx = i;
                     found_object(img_work2,i,j);
              }
              else
              {
                     if (/*pixel was seen before */)
                            while (gray(img_work2,j,i)!=255)  /*proceed until we get a background pixel*/
                             {
                                      if ( j == MAX_HEIGHT)
                                      {
                                               j = 0;
                                               i++;
                                      }
                                       else
                                               j++;                                      
                              }
                      /*to do -- set seen value to 1 */
              }
      }
}

found_object ( BYTE * img, int j, int i )
{
       int uppery, lowery, rightx;
       struct list * temp, * current;

       uppery = lowery = j;
       rightx = i;

       start = (struct list *) malloc ( sizeof (struct list) );
       start->p.img = img;
       start->p.i = i;
       start->p.j = j;
       start->next = NULL;
       temp = start;

       while ( start != NULL )
       {
               current = start;
               if ( current->p.i > rightx)  
                         rightx = current->p.i;
               if ( current->p.j > uppery )
                         uppery = current->p.j;
               if ( current->p.j < lowery )
                         lowery = current->p.j;
               /* to do - get neighbouring pixels of current here i+1,j... i-1,j... i,j+1... i,j-1... i+1,j+1... i+1,j-1... i-1,j+1... i-1,j-1 set all seen values to 1*/
               /* to do - check if they are boundary pixels (atleast one of their neighbours is a boundary pixel)*/
               /* add them to queue as follows (assuming values img1 i1 and j1 --- declare them before using)*/
               temp->next = (struct list *) malloc ( sizeof (struct list) );
               temp = temp->next;
               temp->next = NULL;
               temp->p.img = img1;
               temp->p.i = i1;
               temp->p.j = j1;
               start = start ->next;
               free (current);
      }
       /*to do - you have the boundary co-ordinates in uppery, lowery and rightx ... save/use them*/
}
Avatar of Besbous

ASKER

struct list * start = NULL;

 seen = 0;  /* initially seen for each pixel should be 0 */

for(i=0;i<WIDTH;i++)
{
      for(j=0;j<HEIGHT;j++)
      {
              if(gray(img_work2,j,i)==255 && gray(img_work2,j,i)!=seen)/* && pixel was not seen before*/
              {
                     leftx = i;
                     found_object(img_work2,i,j);
              }
              else
              {
                     if (gray(img_work2,j,i)==seen)/*pixel was seen before */
                            while (gray(img_work2,j,i)!=255)  /*proceed until we get a background pixel*/
                             {
                                      if ( j == MAX_HEIGHT)
                                      {
                                               j = 0;
                                               i++;
                                      }
                                       else
                                               j++;                                      
                              }
                    seen = 1;  /*to do -- set seen value to 1 */
              }
      }
}

found_object ( BYTE * img, int j, int i )
{
       int uppery, lowery, rightx;
       int img1, i1, j1;
       struct list * temp, * current;

       uppery = lowery = j;
       rightx = i;

       start = (struct list *) malloc ( sizeof (struct list) );
       start->p.img = img;
       start->p.i = i;
       start->p.j = j;
       start->next = NULL;
       temp = start;

       while ( start != NULL )
       {
               current = start;
               if ( current->p.i > rightx)  
                         rightx = current->p.i;
               if ( current->p.j > uppery )
                         uppery = current->p.j;
               if ( current->p.j < lowery )
                         lowery = current->p.j;
               /*  get neighbouring pixels of current here i+1,j... i-1,j... i,j+1... i,j-1... i+1,j+1... i+1,j-1... i-1,j+1... i-1,j-1 set all seen values to 1*/
               if(gray(img_work2,current,i)==255 || gray(img_work2,j,current)==255 ){\
               gray(img_work2,j,i+1)=seen;
               gray(img_work2,j,i-1)=seen;
               gray(img_work2,j+1,i)=seen;
               gray(img_work2,j-1,i)=seen;
               gray(img_work2,j+1,i-1)=seen;
               gray(img_work2,j+1,i+1)=seen;
               gray(img_work2,j-1,i+1)=seen;
               gray(img_work2,j-1,i-1)=seen;
               seen = 1;
               /*  check if they are boundary pixels (atleast one of their neighbours is a boundary pixel)*/
                if(gray(img_work2,j,i)==255){
                /** I don t know how to check this :( */

               /* add them to queue as follows (assuming values img1 i1 and j1 --- declare them before using)*/
               /** which queue ? i don t quite know how to do it */

               temp->next = (struct list *) malloc ( sizeof (struct list) );
               temp = temp->next;
               temp->next = NULL;
               temp->p.img = img1;
               temp->p.i = i1;
               temp->p.j = j1;
               start = start ->next;
               free (current);
      }
       /*you have the boundary co-ordinates in uppery, lowery and rightx ... save/use them*/
             rightX = i1;
             upperY = j1;
             lowerY = ?;   /* i don t know about leftx!*/
}


 That s all i could come out with ! I know it might not be correct :(!  would u please correct me this !
this is as good as my skeletal code in the previous post ... I'll try to write the stuff but problem is, I will have to test it too (write thorougly) cuz if I goof up you would not be able to debug (will you be willing to)
and writing that much code is trouble .... How soon do yuo need it ?

btw: happy birthday
what is the data structure in which you store your image ... can you give me some details about it
Avatar of Besbous

ASKER

thx a lot ! :)

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>

#include "robot.h"
#include "img.h"

#define MODE 4
/********************/
extern BYTE *img_work2;
extern BYTE *img_work3;

/* the following explains the structure ! */

    disp_image_while_kbhit(img_src, hDC);
    get_image(img_src);
      /*get the image from the camera*/
    copy_image(img_src,img_tmp);
    gray2(img_tmp,hDC);
      /*make the image gray*/
    disp_gray_image(img_tmp,hDC);
    get_rid_of_noise(img_tmp,img_work2,hDC);
    copy_image(img_work2,img_tmp);

      show_monitor();
    disp_image(img_tmp,hDC);
      /*labeling*/
    label=labelling(img_tmp,img_work2);   /* this gives to each object detected a number ! so it gives the the 1st object detected number 1,  2nd -->2 ...*/
      printf("Max_label=%d\n",label);

      for(i=0;i<WIDTH;i++){        /* for each labe(object) we get the coord of the center of the inertia for each object . */
            for(j=0;j<HEIGHT;j++){
                  int label_id = gray(img_work2,j,i);
                  if(label_id > 0 && label_id <= LABEL){
                      s[label_id]++;
                    x_sum[label_id]+=i;
                    y_sum[label_id]+=j;
                  }
            }
      }

      for(k=1;k<=label;k++){      
        g_x[k]=x_sum[k]/s[k];    
        g_y[k]=y_sum[k]/s[k];
}

i hope this might help a bit !
SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Besbous

ASKER

thx a lot Im working on that now !
Avatar of Besbous

ASKER


 Hi sunny! how  u doing ! I was trying all along the week to compile and recompile and figure out the why there r errors, but still I couldn t see why !
I m not quite familiar with the queue thing !

struct list
{
       int i ;
       int j;
       struct list * next;
};

short int seen[WIDTH][HEIGHT];

struct list * start = NULL;      //keep this declaration global
struct list * temp = NULL;      //keep this declaration global

/* initially seen for each pixel should be 0 */
memset (); // memset seen to all 0s

for(i=0;i<WIDTH;i++){                     /* line 317 */
     for(j=0;j<HEIGHT;j++){               /* line 318 */
             if(gray(img_work2,j,i)==255 && seen[i][j] == 0 ){
                    leftx = i;
                    found_object(img_work2,i,j);
                    seen[i][j] = 1;
             }
             else
             {
                    if ( seen[i][j] == 1 )
                           while (gray(img_work2,j,i)!=255){  /*proceed until we get a background pixel*/
                           
                                     seen[i][j] = 1;
                                     if ( j == MAX_HEIGHT){
                                     
                                              j = 0;
                                              i++;
                                     }
                                      else
                                              j++;                                      
                             }
                     
             }
     }
}

found_object ( BYTE * img, int j, int i ){
      int uppery, lowery, rightx;
      struct list  * current;

      uppery = lowery = j;
      rightx = i;

      start = (struct list *) malloc ( sizeof (struct list) );
      start->p.img = img;
      start->p.i = i;
      start->p.j = j;
      start->next = NULL;
      temp = start;

      while ( start != NULL ){
     
              current = start;
               if ( current->i > rightx)  
                        rightx = current->p.i;
               if ( current->j > uppery )
                         uppery = current->p.j;
              if ( current->j < lowery )
                         lowery = current->p.j;
              seen[i][j] = 1;
              /* to do - get neighbouring pixels of current here i+1,j... i-1,j... i,j+1... i,j-1... i+1,j+1... i+1,j-1... i-1,j+1... i-1,j-1 set all seen values to 1*/
                    if(gray(img_work2,current,i)==255 || gray(img_work2,j,current)==255 ){
               gray(img_work2,j,i+1) = seen[j][j+1];
               gray(img_work2,j,i-1) = seen[j][i-1];
               gray(img_work2,j+1,i) = seen[j+1][i];
               gray(img_work2,j-1,i) = seen[j-1][i];
               gray(img_work2,j+1,i-1) = seen[j+1][i-1];
               gray(img_work2,j+1,i+1) = seen[j+1][i+1];
               gray(img_work2,j-1,i+1) = seen[j-1][i+1];
               gray(img_work2,j-1,i-1) = seen[j-1][i-1];
               seen[i][j] = 1;
                    }

              check ( i, j, 2 );

              /* to do - check if they are boundary pixels (atleast one of their neighbours is a boundary pixel)*/
                /*********** I don t know what to check ! if already check defines a boundary pixel*****************/
              /* add them to queue as follows (assuming values img1 i1 and j1 --- declare them before using)*/
                 /* *******************I don t know how to do that ! i tried but it didn t work8************ */
              start = start ->next;
              free (current);
     }
      /*to do - you have the boundary co-ordinates in uppery, lowery and rightx ... save/use them*/
}

int check ( int i, int j, int level ){
      int ret=0;
        /*i+1,j... i-1,j... i,j+1... i,j-1... i+1,j+1... i+1,j-1... i-1,j+1... i-1,j-1*/
      if ( level == 2 ){
              if ( gray(img_work2, j, i+1)==255 && check ( i+1, j, 1 ) )
                       add ( i+1, j );

              if ( gray(img_work2, j, i-1)==255 && check ( i-1, j, 1 ) )
                       add ( i+1, j );

              if ( gray(img_work2, j+1, i)==255 && check ( i, j+1, 1 ) )
                       add ( i+1, j );

              if ( gray(img_work2, j-1, i)==255 && check ( i, j-1, 1 ) )
                       add ( i+1, j );

              if ( gray(img_work2, j+1, i+1)==255 && check ( i+1, j+1, 1 ) )
                       add ( i+1, j );

              if ( gray(img_work2, j-1, i+1)==255 && check ( i+1, j-1, 1 ) )
                       add ( i+1, j );

              if ( gray(img_work2, j+1, i-1)==255 && check ( i-1, j+1, 1 ) )
                       add ( i+1, j );

              if ( gray(img_work2, j-1, i-1)==255 && check ( i-1, j-1, 1 ) )
                       add ( i+1, j );
              return 0;
      }
      if ( level == 1 )
      {
              ret += check ( i+1, j, 0 );
              ret += check ( i-1, j, 0 );
              ret += check ( i, j+1, 0 );
              ret += check ( i, j-1, 0 );
              ret += check ( i+1, j+1, 0 );
              ret += check ( i+1, j-1, 0 );
              ret += check ( i-1, j+1, 0 );
              ret += check ( i-1, j-1, 0 );
              return ret;
      }
      if ( level == 0)
      {
              if ( gray(img_work2,j,i)==255  )
                      return 1;
      }
}

add ( int i, int j )
{
             temp->next = (struct list *) malloc ( sizeof (struct list) );
              temp = temp->next;
              temp->next = NULL;
              temp->i = i;
              temp->j = j;
}


C:\Rvision\MYFUNC.CPP(317) : error C2143: syntax error : missing ';' before 'for'
C:\Rvision\MYFUNC.CPP(317) : error C2143: syntax error : missing ')' before ';'
C:\Rvision\MYFUNC.CPP(317) : error C2143: syntax error : missing ';' before '<'
C:\Rvision\MYFUNC.CPP(317) : error C2501: 'i' : missing storage-class or type specifiers
C:\Rvision\MYFUNC.CPP(317) : error C2143: syntax error : missing ';' before '<'
C:\Rvision\MYFUNC.CPP(317) : error C2143: syntax error : missing ';' before '++'
C:\Rvision\MYFUNC.CPP(317) : error C2501: 'i' : missing storage-class or type specifiers
C:\Rvision\MYFUNC.CPP(317) : error C2086: 'i' : redefinition
C:\Rvision\MYFUNC.CPP(317) : error C2143: syntax error : missing ';' before '++'
C:\Rvision\MYFUNC.CPP(317) : error C2059: syntax error : ')'
C:\Rvision\MYFUNC.CPP(317) : error C2143: syntax error : missing ';' before '{'
C:\Rvision\MYFUNC.CPP(317) : error C2447: missing function header (old-style formal list?)
C:\Rvision\MYFUNC.CPP(351) : error C2039: 'p' : is not a member of 'list'
        C:\Rvision\MYFUNC.CPP(303) : see declaration of 'list'
C:\Rvision\MYFUNC.CPP(351) : error C2228: left of '.img' must have class/struct/union type
C:\Rvision\MYFUNC.CPP(352) : error C2039: 'p' : is not a member of 'list'
        C:\Rvision\MYFUNC.CPP(303) : see declaration of 'list'
C:\Rvision\MYFUNC.CPP(352) : error C2228: left of '.i' must have class/struct/union type
C:\Rvision\MYFUNC.CPP(353) : error C2039: 'p' : is not a member of 'list'
        C:\Rvision\MYFUNC.CPP(303) : see declaration of 'list'
C:\Rvision\MYFUNC.CPP(353) : error C2228: left of '.j' must have class/struct/union type
C:\Rvision\MYFUNC.CPP(361) : error C2039: 'p' : is not a member of 'list'
        C:\Rvision\MYFUNC.CPP(303) : see declaration of 'list'
C:\Rvision\MYFUNC.CPP(361) : error C2228: left of '.i' must have class/struct/union type
C:\Rvision\MYFUNC.CPP(363) : error C2039: 'p' : is not a member of 'list'
        C:\Rvision\MYFUNC.CPP(303) : see declaration of 'list'
C:\Rvision\MYFUNC.CPP(363) : error C2228: left of '.j' must have class/struct/union type
C:\Rvision\MYFUNC.CPP(365) : error C2039: 'p' : is not a member of 'list'
        C:\Rvision\MYFUNC.CPP(303) : see declaration of 'list'
C:\Rvision\MYFUNC.CPP(365) : error C2228: left of '.j' must have class/struct/union type
C:\Rvision\MYFUNC.CPP(368) : error C2296: '*' : illegal, left operand has type 'struct list *'
C:\Rvision\MYFUNC.CPP(368) : error C2296: '*' : illegal, left operand has type 'struct list *'
C:\Rvision\MYFUNC.CPP(369) : warning C4244: '=' : conversion from 'short' to 'unsigned char', possible loss of data
C:\Rvision\MYFUNC.CPP(370) : warning C4244: '=' : conversion from 'short' to 'unsigned char', possible loss of data
C:\Rvision\MYFUNC.CPP(371) : warning C4244: '=' : conversion from 'short' to 'unsigned char', possible loss of data
C:\Rvision\MYFUNC.CPP(372) : warning C4244: '=' : conversion from 'short' to 'unsigned char', possible loss of data
C:\Rvision\MYFUNC.CPP(373) : warning C4244: '=' : conversion from 'short' to 'unsigned char', possible loss of data
C:\Rvision\MYFUNC.CPP(374) : warning C4244: '=' : conversion from 'short' to 'unsigned char', possible loss of data
C:\Rvision\MYFUNC.CPP(375) : warning C4244: '=' : conversion from 'short' to 'unsigned char', possible loss of data
C:\Rvision\MYFUNC.CPP(376) : warning C4244: '=' : conversion from 'short' to 'unsigned char', possible loss of data
C:\Rvision\MYFUNC.CPP(380) : error C2065: 'check' : undeclared identifier
C:\Rvision\MYFUNC.CPP(389) : warning C4508: 'found_object' : function should return a value; 'void' return type assumed
C:\Rvision\MYFUNC.CPP(391) : error C2373: 'check' : redefinition; different type modifiers
C:\Rvision\MYFUNC.CPP(396) : error C2065: 'add' : undeclared identifier
C:\Rvision\MYFUNC.CPP(440) : error C2373: 'add' : redefinition; different type modifiers

this is what debugging gave !