2 FAQ Technical Questions on Library Use
3 --------------------------------------
5 How to access image pixels
6 ^^^^^^^^^^^^^^^^^^^^^^^^^^
8 (The coordinates are 0-based and counted from image origin, either top-left (img->origin=IPL_ORIGIN_TL) or bottom-left (img->origin=IPL_ORIGIN_BL)
10 * Suppose, we have 8-bit 1-channel image I (IplImage* img)::
12 I(x,y) ~ ((uchar*)(img->imageData + img->widthStep*y))[x]
14 * Suppose, we have 8-bit 3-channel image I (IplImage* img)::
16 I(x,y)blue ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3]
17 I(x,y)green ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+1]
18 I(x,y)red ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+2]
20 e.g. increasing brightness of point (100,100) by 30 can be done this way::
22 CvPoint pt = {100,100};
23 ((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3] += 30;
24 ((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3+1] += 30;
25 ((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3+2] += 30;
29 CvPoint pt = {100,100};
30 uchar* temp_ptr = &((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3];
35 * Suppose, we have 32-bit floating point, 1-channel image I (IplImage* img)::
37 I(x,y) ~ ((float*)(img->imageData + img->widthStep*y))[x]
39 * Now, the general case: suppose, we have N-channel image of type T::
41 I(x,y)c ~ ((T*)(img->imageData + img->widthStep*y))[x*N + c]
43 or you may use macro CV_IMAGE_ELEM( image_header, elemtype, y, x_Nc )::
45 I(x,y)c ~ CV_IMAGE_ELEM( img, T, y, x*N + c )
47 There are functions that work with arbitrary (up to 4-channel) images and matrices (cvGet2D, cvSet2D), but they are pretty slow.