From: http://blog.csdn.net/hxgqh/archive/2011/02/23/6202001.aspx
You can obtain the pixel value for more flexible image processing. In opencv, you can add a cvscalar struct to the cvget2d () and cvset2d () functions.
In opencv, The cvscalar structure is:
Typedef struct cvscalar
{
Double Val [4];
}
Cvscalar;
Four double-type variables will not be forced type conversion during algorithm processing and reduce the accuracy.
Let's take a look at the definition of the read/write function:
Cvget2d obtains the value of a certain vertex. idx0 = the value of the hight row and idx1 = the value of the width column.
Cvapi (cvscalar) cvget2d (const cvarr * arr, int idx0, int idx1 );
Cvset2d assigns a value to a vertex.
Cvapi (void) cvset2d (cvarr * arr, int idx0, int idx1, cvscalar value );
As shown in the preceding figure, the cvget2d return type and the Value Type in cvset2d are both cvscalar. In this way, it is OK to define a cvscalar variable and then call the function.
Example of pixel reading and writing in opencv:
Int main (INT argc, char ** argv)
{
Iplimage * IMG = cvloadimage (argv [1], 1 );
Cvscalar pixel;
For (INT I = 0; I height; ++ I)
{
For (Int J = 0; j width; ++ J)
{
// Obtain and display the RGB values of pixels. Note that the storage sequence in the memory is BGR.
Pixel = cvget2d (IMG, I, j );
Printf ("B = % F, G = % F, R = % F/T", pixel. val [0], pixel. val [1], pixel. val [2]);
// Modify the value of each point
Pixel. Val [0] = 0;
Pixel. Val [1] = 0;
Pixel. Val [2] = 0;
Cvset2d (IMG, I, j, pixel );
}
}
Cvnamedwindow ("image", 1 );
Cvshowimage ("image", IMG );
Cvwaitkey (0 );
Cvdestroywindow ("image ");
Cvreleaseimage (& IMG );
Return 0;
}