| 1 | /* |
| 2 | This file is part of the CVD Library. |
| 3 | |
| 4 | Copyright (C) 2005 The Authors |
| 5 | |
| 6 | This library is free software, see LICENSE file for details |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | \page tutorial1 Tutorial |
| 11 | |
| 12 | \section images Images |
| 13 | |
| 14 | Images are represented using the templated CVD#Image type. The template |
| 15 | parameter determines the number of channels (i.e. whether the image is greyscale, |
| 16 | RGB, etc) and precision of the image (8 bit, floating point, etc). |
| 17 | |
| 18 | \subsection loadsave1 Loading and saving images from file. |
| 19 | |
| 20 | @code |
| 21 | #include <cvd/image_io.h> |
| 22 | using namespace CVD; |
| 23 | |
| 24 | int main() |
| 25 | { |
| 26 | Image<byte> in; //Declares an 8 bit greyscale image |
| 27 | |
| 28 | in = img_load("test_image.jpg"); |
| 29 | img_save(in, "new_image.png"); |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | @endcode |
| 35 | |
| 36 | CVD#img_load can be given either a filename or an istream. In this case, the image |
| 37 | will be converted in to an 8 bit geryscale image automatically to match the type of |
| 38 | <code>in</code>. |
| 39 | |
| 40 | CVD#img_save can be given a filename or an ostream to save to. It can also be given an |
| 41 | image type. In the absence of a type, it will use the extension of the filename to |
| 42 | deduce the image type. It will choose the optimum number of channels and bits per channel |
| 43 | based on the type of <code>in</code> and the availability of features in the image |
| 44 | file format being used. Consequently, the resulting image in <code>new_image.png</code> |
| 45 | will be 8 bit greyscale. |
| 46 | |
| 47 | |
| 48 | @code |
| 49 | #include <cvd/image_io.h> |
| 50 | using namespace CVD; |
| 51 | |
| 52 | int main() |
| 53 | { |
| 54 | Image<Rgb<float> > in; //Declares a floating rgb image |
| 55 | |
| 56 | in = img_load("test_image.jpg"); |
| 57 | img_save(in, "new_image.png"); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | @endcode |
| 62 | |
| 63 | This example loads and saves a file via a floating point RGB image. In this |
| 64 | case, the file saved will be an RGB PNG image with 16 bits per channel, since |
| 65 | that is the best available match to <code>Image<Rgb<byte> > </code> Images of |
| 66 | any builtin type can be made, loaded in to and saved from as gery scale images. |
| 67 | RGB images can be used via CVD#Rgb and RGBA images via CVD#Rgba. |
| 68 | |
| 69 | |
| 70 | \subsection imagedisplay Basic image visualisation |
| 71 | @code |
| 72 | #include <cvd/gl_helpers.h> //OpenGL wrappers for various CVD types |
| 73 | #include <cvd/image_io.h> |
| 74 | #include <cvd/videodisplay.h> //Very cheap and cheerful X window with OpenGL capabilities |
| 75 | using namespace CVD; |
| 76 | |
| 77 | int main() |
| 78 | { |
| 79 | Image<Rgb<byte> > in; |
| 80 | in = img_load("test_image.jpg"); |
| 81 | |
| 82 | VideoDisplay window(in.size()); //Create an OpenGL window with the dimensions of `in' |
| 83 | glDrawPixels(in); |
| 84 | |
| 85 | glColor3f(1,0,0); |
| 86 | glBegin(GL_LINES); |
| 87 | glVertex(in.size()/2); |
| 88 | glVertex(in.size()); |
| 89 | glEnd(); |
| 90 | glFlush(); |
| 91 | |
| 92 | std::cin.get(); |
| 93 | return 0; |
| 94 | } |
| 95 | @endcode |
| 96 | |
| 97 | This program loads and displays an image, then draws a red diagonal line from the center to the bottom |
| 98 | right corner of the image. The OpenGL coordinate system is set up so that (0,0) corresponds to pixel |
| 99 | (0,0) in the image. Note that OpenGL wrappers exist for CVD's size objects (known as CVD#ImageRef). |
| 100 | |
| 101 | |
| 102 | \subsection imageref Pixel Access |
| 103 | |
| 104 | Images can be accessed in two ways, either by row, then column or with an CVD#ImageRef object. |
| 105 | |
| 106 | @code |
| 107 | #include <cvd/image_io.h> |
| 108 | using namespace CVD; |
| 109 | using namespace std; |
| 110 | |
| 111 | int main() |
| 112 | { |
| 113 | Image<Rgb<byte> > in; |
| 114 | |
| 115 | in = img_load("test_image.jpg"); |
| 116 | |
| 117 | ImageRef pos; |
| 118 | |
| 119 | pos.x = 0; |
| 120 | pos.y = in.size().y / 2; |
| 121 | |
| 122 | cout << "Pixel at x=10, y=3 = " << in[3][10] << endl; |
| 123 | cout << "Pixel at x=0, and half way down = " << in[pos] << endl; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | @endcode |
| 128 | |
| 129 | |
| 130 | \subsection image2 Images in a bit more detail |
| 131 | |
| 132 | Images behave like smart pointers. Copying images using = or copy construction |
| 133 | copies a pointer, but still references the same underlying data. Images are |
| 134 | reference counted, so when the last reference to an image is destructed, then the data is |
| 135 | deleted. This allows images to be efficiently returned from functions and stored in STL |
| 136 | containers, since copying is fast. |
| 137 | |
| 138 | Making duplicate copies of the data must be done explicitly: |
| 139 | |
| 140 | @code |
| 141 | Image<byte> a, b; |
| 142 | ... |
| 143 | ... |
| 144 | //fill in a with somethng |
| 145 | ... |
| 146 | ... |
| 147 | |
| 148 | //Duplicate a's data |
| 149 | b.copy_from(a); |
| 150 | |
| 151 | @endcode |
| 152 | |
| 153 | \subsubsection helpers Useful Methods |
| 154 | |
| 155 | There are a number of useful helper methods for images: |
| 156 | - Image(ImageRef) |
| 157 | - Construct an image of a given size |
| 158 | - Image(ImageRef, T) |
| 159 | - Construct an image of a given size and filled with a certain value |
| 160 | - resize(ImageRef) |
| 161 | - This resizes an image. |
| 162 | - The image now points to a new block of uninitialized data |
| 163 | - All other copies of the data are unaffected |
| 164 | - zero() |
| 165 | - Fills the memory with all zeros |
| 166 | - Only use this on POD types |
| 167 | - fill(T) |
| 168 | - Fills the image with a specific value |
| 169 | - make_unique() |
| 170 | - Causes this image to point to a fresh copy of the data. |
| 171 | - All other references to the data are unaffected |
| 172 | - copy_from_me() |
| 173 | - This causes a copy to occur on assignment. |
| 174 | - This is complementary to copy_from. |
| 175 | - a = b.copy_from_me(); |
| 176 | |
| 177 | |
| 178 | \subsubsection imstl Images and the STL |
| 179 | Images can be efficientlu stored in STL containers. |
| 180 | If you want to make a bag of 10 images, then |
| 181 | the code |
| 182 | @code |
| 183 | vector<Image<byte> > some_images(an_image, 13); |
| 184 | @endcode |
| 185 | Will not make 13 copies of an_image images, it will make 13 references to the same |
| 186 | block of data, since copying images does not copy the data. The following code can be |
| 187 | used instead: |
| 188 | @code |
| 189 | vector<Image<byte> > some_images(CreateImagesBegin(an_image), CreateImagesEnd(an_image, 13)); |
| 190 | @endcode |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | \section video1 Video |
| 196 | |
| 197 | Basic video access and display is demonstrated by the following code: |
| 198 | @code |
| 199 | #include <cvd/gl_helpers.h> |
| 200 | #include <cvd/videodisplay.h> |
| 201 | #include <cvd/videosource.h> |
| 202 | |
| 203 | using namespace CVD; |
| 204 | |
| 205 | int main() |
| 206 | { |
| 207 | VideoBuffer<Rgb<byte> > * video_buffer = open_video_source<Rgb<byte> >("v4l2:///dev/video0"); |
| 208 | VideoDisplay disp(video_buffer->size()); |
| 209 | |
| 210 | while(1) |
| 211 | { |
| 212 | VideoFrame<Rgb<byte> > *frame = video_buffer->get_frame(); |
| 213 | glDrawPixels(*frame); |
| 214 | video_buffer->put_frame(frame); |
| 215 | } |
| 216 | } |
| 217 | @endcode |
| 218 | |
| 219 | Video is obtained from CVD#VideoBuffer's, which are templated on the type of the frame (like CVD#Image). |
| 220 | CVD#open_video_source is the most convinient way to open a video, and uses a URL like syntax. For example: |
| 221 | - v4l2://v4l2 device file |
| 222 | - This opens a Video4Linux2 device |
| 223 | - Example: v4l2:///dev/video0 |
| 224 | - dc1394://camera number |
| 225 | - Opens a camera obeying the IEEE1394, DC spec (eg firewire webcams, and high end firewire video cameras). |
| 226 | - The DC1394 camera number selectd the device |
| 227 | - Example: dc1394://0 |
| 228 | - files://glob |
| 229 | - Opens a list of images given by a unix style glob |
| 230 | - glob is relative to the current directory |
| 231 | - Example files://img*.jpg |
| 232 | - file://file |
| 233 | - Opens AVI or MPEG files (or anything else compatible with ffmpeg) |
| 234 | - Example file://video.mpg |
| 235 | |
| 236 | CVD#open_video_source is a convinience function. More flexibility can be gained by using the underlying CVD#VideoBuffer classes |
| 237 | such as CVD#DVBuffer2. |
| 238 | |
| 239 | |
| 240 | VideoBuffers return CVD#VideoFrame objects, instead of images. These share much of the same functionality |
| 241 | with images, but they do not perform reference counting. However, they do have features specific to video, such |
| 242 | as a timestamp. Images and VideoFrames actually share the same base class. |
| 243 | |
| 244 | After use, video frames must be returned to the video buffer. The number of frames which can be held |
| 245 | out simultaneously depends on the buffer type. This number tends to be very limited if videos come from |
| 246 | a hardware source. |
| 247 | |
| 248 | |
| 249 | \section errors Errors (or Help! My program aborts!) |
| 250 | |
| 251 | CVD uses exception throughout for error handling. If your program aborts, then it may be due to an |
| 252 | uncaught exception. All CVD exceptions belong to the same heirachy, so the following code will print |
| 253 | a useful diagnostic error message: |
| 254 | |
| 255 | @code |
| 256 | #include <cvd/image_io.h> |
| 257 | using namespace CVD; |
| 258 | |
| 259 | int main() |
| 260 | { |
| 261 | try |
| 262 | { |
| 263 | Image<byte> in = img_load("this_file_does_not_exist"); |
| 264 | } |
| 265 | catch(Exceptions::All error) |
| 266 | { |
| 267 | std::cerr << "Error: " << error.what << std::endl; |
| 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | @endcode |
| 273 | |
| 274 | **/ |