Page 42 -
P. 42

16    Chapter 1 ■ Practical Aspects of a Vision System


                                 img = newimage (x->height, x->width);
                                 else if ((x->depth==8) && (x->nChannels==3)) //Color
                                 {
                                     color = 1;
                                     img = newimage (x->height, x->width);
                                 }
                                 else return 0;

                                 for (i=0; i<x->height; i++)
                                 {
                                     for (j=0; j<x->width; j++)
                                     {
                                         s = cvGet2D (x, i, j);
                                         if (color)
                                           k= (unsigned char)  ((s.val[0]+s.val[1]+s.val[2])/3);
                                         else k = (unsigned char)(s.val[0]);
                                           img->data[i][j] = k;
                                      }
                                  }
                                  return img;
                             }

                             The two functions toOpenCV and fromOpenCV do the job of allowing the
                           image-processing routines developed here to be used with OpenCV. As a
                           demonstration, here is the main routine only for a program that thresholds
                           an image using the method of grey-level histograms devised by Otsu and
                           presented in Chapter 4. It is very much like the program for thresholding
                           written earlier in Section 1.2.4, but instead uses the AIPCV library function
                           thr_glh to find the threshold and apply it.

                             int main(int argc, char *argv[])
                             {
                               IplImage* img=0;
                               IplImage* img2=0;
                               IMAGE x;
                               int height,width,step,channels;
                               uchar *data;
                               int mean=0,count=0;

                               if(argc<1){
                                 printf(“Usage: main <image-file-name>\n\7“);
                                 exit(0);
                               }
                               // load an image
                               img=cvLoadImage(“H:/AIPCV/marchA062.jpg“);
   37   38   39   40   41   42   43   44   45   46   47