Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/
# Include "CV. H "# include" highgui. H "# include <stdio. h> void printmat (cvmat * A); // display matrix void genrandn (cvmat * arr, int seed); // generate a random matrix void genrand (cvmat * arr, int seed); // generate a random matrix of [] Uniform Distribution Static int cmp_func (const void * _ A, const void * _ B, void * userdata ); // Compare function void test_multiply (); // test the matrix multiplication void test_cvgetrawdata (); // enter the cached data in the cvmat array void test_dct (); // calculate the DCT transformation void test_rand (); // generate a random number void test_seqsort (); // second Dimension sequence sorting int main (void) {test_multiply (); test_cvgetrawdata (); test_dct (); test_rand (); test_seqsort (); Return 0 ;} /* matrix multiplication */void test_multiply () {double A [] = {,}; Double B [] = {, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12}; double C [9]; cvmat Ma, MB, MC; printf ("/N = test multiply =/N"); cvinitmatheader (& Ma, 3,4, cv_64fc1, A, cv_autostep); cvinitmatheader (& MB, cv_64fc1, B, cv_autostep); cvinitmat Header (& Mc, 3, 3, cv_64fc1, C, cv_autostep); cvmatmuladd (& Ma, & MB, 0, & Mc); printmat (& Ma); printmat (& MB ); printmat (& Mc);} void test_cvgetrawdata () {float * data; int step; float a [] = {1, 2, 4,-5, 6, 7, 8, 9,-10, -11,12}; cvmat array; cvsize size; int X, Y; printf ("/N = test get raw Dara! ===/N "); cvinitmatheader (& array, 3,4, cv_32fc1, A, cv_autostep); cvgetrawdata (& array, (uchar **) & Data, & step, & size); // obtain the matrix information step/= sizeof (data [0]); printf ("/ncvmat ="); printmat (& array ); printf ("/ndata ="); For (y = 0; y <size. height; y ++, Data + = step) {printf ("/N"); For (x = 0; x <size. width; X ++) {data [x] = (float) FABS (data [x]); printf ("% 8.2f", data [x]);} printf ("/N") ;}} void test_dct () {float data [] = {1, 2, 4, 4, 5, 6, 7, 8}; cvmat A; A = cvmat (2, 4, cv_32fc1, data); printf ("/N = test DCT = "); printf ("/noriginal matrix ="); printmat (& A); cvdct (& A, & A, cv_dxt_forward); printf ("/n2-D DCT = "); printmat (& A); cvdct (& A, & A, cv_dxt_inverse); printf ("/n2-D IDCT ="); printmat (& A);} void test_rand () {cvmat * A = cvcreatemat (10, 6, cv_32f); int I; printf ("/N = test generating random matrix ="); for (I = 0; I <5; I ++) {genran DN (A, I); printmat (a);} cvreleasemat (& A);} void genrand (cvmat * arr, int seed) {cvrandstate RNG; RNG. state = cvrng (0 xffffffff); cvrandinit (& RNG,/* adjust it after using the virtual parameter */seed, /* use a seed */cv_rand_uni/* to specify a uniform distribution type */); cvrandarr (& RNG. state, arr, cv_rand_uni, cvrealscalar (0), cvrealscalar (1);} void genrandn (cvmat * arr, int seed) {cvrandstate RNG; RNG. state = cvrng (0 xffffffff); cvrandinit (& RNG,/* use virtual Parameters Then adjust the number */seed,/* use a seed */cv_rand_normal/* to specify the uniform distribution type */); cvrandarr (& RNG. state, arr, cv_rand_normal, cvrealscalar (0), cvrealscalar (1);} void printmat (cvmat * A) {int I, j; for (I = 0; I <A-> rows; I ++) {printf ("/N"); Switch (cv_mat_depth (a-> type) {Case cv_32f: Case cv_64f: for (j = 0; j <A-> Cols; j ++) printf ("% 9.3f", (float) cvgetreal2d (A, I, j); break; case cv_8u: Case cv_16u: For (j = 0; j <A-> Cols; j ++) printf ("% 6D", (INT) cvgetreal2d (A, I, j); break; default: Break ;}} printf ("/N");} void test_seqsort () {cvmemstorage * storage = cvcreatememstorage (0); cvseq * seq = cvcreateseq (cv_32sc2, sizeof (cvseq), sizeof (cvpoint), storage); int I; printf ("/n === test sequence sorting! = "); For (I = 0; I <10; I ++) {cvpoint pt; PT. X = rand () % 1000; // obtain the random number PT. y = rand () % 1000; cvseqpush (SEQ, & pt); // add elements to the end of the sequence} printf ("/noriginal point set:/N "); for (I = 0; I <seq-> total; I ++) {cvpoint * PT = (cvpoint *) cvgetseqelem (SEQ, I); printf ("(% d, % d)/n ", Pt-> X, Pt-> Y);} cvseqsort (SEQ, cmp_func, 0); printf ("/nafter sorting:/N "); for (I = 0; I <seq-> total; I ++) {cvpoint * PT = (cvpoint *) cvgetseqelem (SEQ, I); printf ("(% d, % d)/n", Pt-> X, Pt-> Y);} cvclearseq (SEQ ); cvreleasememstorage (& storage);} static int cmp_func (const void * _ A, const void * _ B, void * userdata) {cvpoint * A = (cvpoint *) _; cvpoint * B = (cvpoint *) _ B; int y_diff = A-> Y-B-> Y; int x_diff = A-> X-B-> X; return y_diff? Y_diff: x_diff ;}