Page 32 -
P. 32

6     Chapter 1 ■ Practical Aspects of a Vision System


                           double in type. The function cvGet2D does this; getting the pixel value at i,j
                           for the image above is simply
                             p = cvGet2D (img, i, j);

                             The variable p is of type CvScalar,which is

                                        struct CvScalar
                                        {
                                          double val[4];
                                        }
                             If the pixel has only a single value (i.e., grey), then p.val[0] is that value. If it
                           is RGB, then the color components of the pixel are as follows:
                                Blue is p.val[0]
                                Green is p.val[1]

                                Red is p.val[2]
                             Modifying the pixel value is done as follows:

                                       p.val[0] = 0;          // Blue
                                       p.val[1] = 255;        // Green
                                       p.val[2] = 255;        // Red
                                       cvSet2D(img,i,j,p);    // Set the (i,j) pixel to yellow
                             This is referred to as indirect access in OpenCV documentation and is slower
                           than other means of accessing pixels. It is, on the other hand, clean and clear.


                           1.2.2    Reading and Writing Images
                           The basic function for image input has already been seen; cvLoadImage reads
                           an image from a file, given a path name to that file. It can read images in JPEG,
                           BMP, PNM, PNG, and TIF formats, and does so automatically, without the
                           need to specify the file type. This is determined from the data on the file itself.
                           Once read, a pointer to an IplImage structure is returned that will by default
                           be forced into a 3-channel RGB form, such as has been described previously.
                           So, the call
                                        img = cvLoadImage (filename);

                           returns an IplImage* value that is an RGB image, unless the file name indicated
                           by the string variable filename can’t be read, in which case the function returns
                           0 (null). A second parameter can be used to change the default return image.
                           The call
                                        img = cvLoadImage (filename, f);
   27   28   29   30   31   32   33   34   35   36   37