]> rtime.felk.cvut.cz Git - opencv.git/commitdiff
fixed norm(SparseMat, NORM_INF), SparseMat::convertTo, reading graphs from xml/yml...
authorvp153 <vp153@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Fri, 15 Jan 2010 02:28:36 +0000 (02:28 +0000)
committervp153 <vp153@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Fri, 15 Jan 2010 02:28:36 +0000 (02:28 +0000)
git-svn-id: https://code.ros.org/svn/opencv/trunk@2563 73c94f0f-984f-4a5f-82bc-2d8db8d8ee08

opencv/src/cxcore/cxmatrix.cpp
opencv/src/cxcore/cxpersistence.cpp
opencv/tests/cxcore/src/aarithm.cpp
opencv/tests/cxcore/src/aarray.cpp
opencv/tests/cxcore/src/aio.cpp
opencv/tests/cxcore/src/amath.cpp

index 3b4a67a9b305d31d4603de7ff3d5ad28a838cb0c..ef079d88c4b8a19b338d6c58d3b6e61819cd097d 100644 (file)
@@ -2514,7 +2514,7 @@ void SparseMat::convertTo( SparseMat& m, int rtype, double alpha ) const
     
     CV_Assert(hdr != 0);
     if( hdr != m.hdr )
-        m.create( m.hdr->dims, m.hdr->size, rtype );
+        m.create( hdr->dims, hdr->size, rtype );
     
     SparseMatConstIterator from = begin();
     size_t i, N = nzcount();
@@ -2949,7 +2949,7 @@ double norm( const SparseMat& src, int normType )
     {
         if( normType == NORM_INF )
             for( i = 0; i < N; i++, ++it )
-                result = std::max(result, (double)*(const float*)it.ptr);
+                result = std::max(result, std::abs((double)*(const float*)it.ptr));
         else if( normType == NORM_L1 )
             for( i = 0; i < N; i++, ++it )
                 result += std::abs(*(const float*)it.ptr);
@@ -2964,7 +2964,7 @@ double norm( const SparseMat& src, int normType )
     {
         if( normType == NORM_INF )
             for( i = 0; i < N; i++, ++it )
-                result = std::max(result, *(const double*)it.ptr);
+                result = std::max(result, std::abs(*(const double*)it.ptr));
         else if( normType == NORM_L1 )
             for( i = 0; i < N; i++, ++it )
                 result += std::abs(*(const double*)it.ptr);
index 98e2536da1d5fdf24951fd4c7078c3a54db7c9a5..ab2efa5da1916fdf3f0ceec2843c1ee7dae59b7a 100644 (file)
@@ -4439,7 +4439,8 @@ icvReadGraph( CvFileStorage* fs, CvFileNode* node )
         char* dst_ptr = read_buf;
         int read_max = read_buf_size /MAX(src_elem_size, 1), read_count = 0;
         CvSeqReader reader;
-        cvStartReadRawData( fs, k == 0 ? vtx_node : edge_node, &reader );
+        if(dt)
+            cvStartReadRawData( fs, k == 0 ? vtx_node : edge_node, &reader );
 
         for( i = 0; i < elem_count; i++ )
         {
index 5e82b9d5815ad7b991b58358cb26dbb0c4e6a29c..ab77f83898313345d531ed50591944145627968d 100644 (file)
@@ -952,8 +952,12 @@ void CxCore_CopyTest::run_func()
         cvCopy(test_array[INPUT][0], test_array[INPUT_OUTPUT][0], mask);
     else
     {
+        cv::MatND a = cv::cvarrToMatND(test_array[INPUT][0]);
         cv::MatND c = cv::cvarrToMatND(test_array[INPUT_OUTPUT][0]);
-        cv::cvarrToMatND(test_array[INPUT][0]).copyTo(c, mask ? cv::cvarrToMatND(mask) : cv::MatND());
+        if(!mask)
+            a.copyTo(c);
+        else
+            a.copyTo(c, cv::cvarrToMatND(mask));
     }
 }
 
index d54b7d1a9a2d38852f98c38cfa5f92d2a2f1161a..8e93b3951ea11d2c93ad672d96e7468d7888fe11 100644 (file)
@@ -209,6 +209,7 @@ void CV_ArrayOpTest::run( int /* start_from */)
         int i, k, size[MAX_DIM]={0}, idx[MAX_DIM]={0};
         vector<string> all_idxs;
         vector<double> all_vals;
+        vector<double> all_vals2;
         string sidx, min_sidx, max_sidx;
         double min_val=0, max_val=0;
         
@@ -224,7 +225,8 @@ void CV_ArrayOpTest::run( int /* start_from */)
         int nz0 = (unsigned)rng % max(p/5,10);
         nz0 = min(max(nz0, 1), p);
         all_vals.resize(nz0);
-        Mat_<double> _all_vals(all_vals);
+        all_vals2.resize(nz0);
+        Mat_<double> _all_vals(all_vals), _all_vals2(all_vals2);
         rng.fill(_all_vals, CV_RAND_UNI, Scalar(-1000), Scalar(1000));
         if( depth == CV_32F )
         {
@@ -232,8 +234,18 @@ void CV_ArrayOpTest::run( int /* start_from */)
             _all_vals.convertTo(_all_vals_f, CV_32F);
             _all_vals_f.convertTo(_all_vals, CV_64F);
         }
+        _all_vals.convertTo(_all_vals2, _all_vals2.type(), 2);
+        if( depth == CV_32F )
+        {
+            Mat _all_vals2_f;
+            _all_vals2.convertTo(_all_vals2_f, CV_32F);
+            _all_vals2_f.convertTo(_all_vals2, CV_64F);
+        }
 
         minMaxLoc(_all_vals, &min_val, &max_val);
+        double _norm0 = norm(_all_vals, CV_C);
+        double _norm1 = norm(_all_vals, CV_L1);
+        double _norm2 = norm(_all_vals, CV_L2);
         
         for( i = 0; i < nz0; i++ )
         {
@@ -265,13 +277,29 @@ void CV_ArrayOpTest::run( int /* start_from */)
         Ptr<CvSparseMat> M2 = (CvSparseMat*)M;
         MatND Md;
         M.copyTo(Md);
-        SparseMat M3 = Md;
+        SparseMat M3; SparseMat(Md).convertTo(M3, Md.type(), 2);
         
         int nz1 = M.nzcount(), nz2 = M3.nzcount();
-        if( nz1 != nz0 || nz2 != nz0 )
+        double norm0 = norm(M, CV_C);
+        double norm1 = norm(M, CV_L1);
+        double norm2 = norm(M, CV_L2);
+        double eps = depth == CV_32F ? FLT_EPSILON*100 : DBL_EPSILON*1000;
+        
+        if( nz1 != nz0 || nz2 != nz0)
+        {
+            errcount++;
+            ts->printf(CvTS::LOG, "%d: The number of non-zero elements before/after converting to/from dense matrix is not correct: %d/%d (while it should be %d)\n",
+                       si, nz1, nz2, nz0 );
+            break;
+        }
+        
+        if( fabs(norm0 - _norm0) > fabs(_norm0)*eps ||
+            fabs(norm1 - _norm1) > fabs(_norm1)*eps ||
+            fabs(norm2 - _norm2) > fabs(_norm2)*eps )
         {
             errcount++;
-            ts->printf(CvTS::LOG, "%d: The number of non-zero elements before/after converting to/from dense matrix is not correct: %d/%d (while it should be %d)\n", si, nz1, nz2, nz0 );
+            ts->printf(CvTS::LOG, "%d: The norms are different: %.20g/%.20g/%.20g vs %.20g/%.20g/%.20g\n",
+                       si, norm0, norm1, norm2, _norm0, _norm1, _norm2 );
             break;
         }
         
@@ -298,7 +326,7 @@ void CV_ArrayOpTest::run( int /* start_from */)
             val2 = getValue(M2, idx);
             val3 = getValue(M3, idx, rng);
             
-            if( val1 != val0 || val2 != val0 || val3 != val0 )
+            if( val1 != val0 || val2 != val0 || fabs(val3 - val0*2) > fabs(val0*2)*FLT_EPSILON )
             {
                 errcount++;
                 ts->printf(CvTS::LOG, "SparseMat M[%s] = %g/%g/%g (while it should be %g)\n", sidx.c_str(), val1, val2, val3, val0 );
@@ -342,6 +370,7 @@ void CV_ArrayOpTest::run( int /* start_from */)
         
         int idx1[MAX_DIM], idx2[MAX_DIM];
         double val1 = 0, val2 = 0;
+        M3 = SparseMat(Md);
         minMaxLoc(M3, &val1, &val2, idx1, idx2);
         string s1 = idx2string(idx1, dims), s2 = idx2string(idx2, dims);
         if( val1 != min_val || val2 != max_val || s1 != min_sidx || s2 != max_sidx )
index c031f273cc5f50090d7006d4824b84d4075476c6..b5e868c5cf404af3ee33d899d19fc6d2a907be47 100644 (file)
@@ -180,6 +180,20 @@ void CV_IOTest::run( int )
             test_mat.elemSize(), storage);
         cvSeqPushMulti(seq, test_mat.data, test_mat.cols*test_mat.rows); 
         
+        CvGraph* graph = cvCreateGraph( CV_ORIENTED_GRAPH,
+                                        sizeof(CvGraph), sizeof(CvGraphVtx),
+                                        sizeof(CvGraphEdge), storage );
+        int edges[][2] = {{0,1},{1,2},{2,0},{0,3},{3,4},{4,1}};
+        int i, vcount = 5, ecount = 6;
+        for( i = 0; i < vcount; i++ )
+            cvGraphAddVtx(graph);
+        for( i = 0; i < ecount; i++ )
+        {
+            CvGraphEdge* edge;
+            cvGraphAddEdge(graph, edges[i][0], edges[i][1], 0, &edge);
+            edge->weight = (float)(i+1);
+        }
+        
         depth = cvTsRandInt(rng) % (CV_64F+1);
         cn = cvTsRandInt(rng) % 4 + 1;
         int sz[] = {cvTsRandInt(rng)%10+1, cvTsRandInt(rng)%10+1, cvTsRandInt(rng)%10+1};
@@ -215,6 +229,8 @@ void CV_IOTest::run( int )
         cvWriteComment(*fs, "test comment", 0);
         
         fs.writeObj("test_seq", seq);
+        fs.writeObj("test_graph",graph);
+        CvGraph* graph2 = cvCloneGraph(graph,storage);
         
         fs.release();
         
@@ -306,13 +322,12 @@ void CV_IOTest::run( int )
         
         cvRelease((void**)&m_nd);
             
-        CvSparseMat* m_s = (CvSparseMat*)fs["test_sparse_mat"].readObj();
-        CvSparseMat* _test_sparse_ = (CvSparseMat*)test_sparse_mat;
-        CvSparseMat* _test_sparse = (CvSparseMat*)cvClone(_test_sparse_);
-        cvReleaseSparseMat(&_test_sparse_);
+        Ptr<CvSparseMat> m_s = (CvSparseMat*)fs["test_sparse_mat"].readObj();
+        Ptr<CvSparseMat> _test_sparse_ = (CvSparseMat*)test_sparse_mat;
+        Ptr<CvSparseMat> _test_sparse = (CvSparseMat*)cvClone(_test_sparse_);
         SparseMat m_s2;
         fs["test_sparse_mat"] >> m_s2;
-        CvSparseMat* _m_s2 = (CvSparseMat*)m_s2;
+        Ptr<CvSparseMat> _m_s2 = (CvSparseMat*)m_s2;
         
         if( !m_s || !CV_IS_SPARSE_MAT(m_s) ||
             !cvTsCheckSparse(m_s, _test_sparse,0) ||
@@ -323,11 +338,6 @@ void CV_IOTest::run( int )
             return;
         }
         
-        if( m_s && CV_IS_SPARSE_MAT(m_s))
-            cvReleaseSparseMat(&m_s);
-        cvReleaseSparseMat(&_test_sparse);
-        cvReleaseSparseMat(&_m_s2);
-        
         FileNode tl = fs["test_list"];
         if( tl.type() != FileNode::SEQ || tl.size() != 6 ||
            fabs((double)tl[0] - 0.0000000000001) >= DBL_EPSILON ||
@@ -389,6 +399,29 @@ void CV_IOTest::run( int )
             ts->set_failed_test_info( CvTS::FAIL_INVALID_OUTPUT );
             return;
         }
+        
+        CvGraph* graph3 = (CvGraph*)fs["test_graph"].readObj();
+        if(graph2->active_count != vcount || graph3->active_count != vcount ||
+           graph2->edges->active_count != ecount || graph3->edges->active_count != ecount)
+        {
+            ts->printf( CvTS::LOG, "the cloned or read graph have wrong number of vertices or edges\n" );
+            ts->set_failed_test_info( CvTS::FAIL_INVALID_OUTPUT );
+            return;
+        }
+        
+        for( i = 0; i < ecount; i++ )
+        {
+            CvGraphEdge* edge2 = cvFindGraphEdge(graph2, edges[i][0], edges[i][1]);
+            CvGraphEdge* edge3 = cvFindGraphEdge(graph3, edges[i][0], edges[i][1]);
+            if( !edge2 || edge2->weight != (float)(i+1) ||
+                !edge3 || edge3->weight != (float)(i+1) )
+            {
+                ts->printf( CvTS::LOG, "the cloned or read graph do not have the edge (%d, %d)\n", edges[i][0], edges[i][1] );
+                ts->set_failed_test_info( CvTS::FAIL_INVALID_OUTPUT );
+                return;
+            }
+        }
+        
         fs.release();
         #ifdef _MSC_VER
             _unlink(filename);
index d3307d55a75543595f3cab92ab7fa824e6b44519..e57cf7813b28ff119ded1282ebf03af1c03832bd 100644 (file)
@@ -1271,12 +1271,14 @@ protected:
     void run_func();
     void prepare_to_validation( int test_case_idx );
     CvScalar alpha;
+    bool test_nd;
 };
 
 CxCore_ScaleAddTest::CxCore_ScaleAddTest() :
     CxCore_MatrixTest( "matrix-scaleadd", "cvScaleAdd", 3, 1, false, false, 4 )
 {
     alpha = cvScalarAll(0);
+    test_nd = false;
 }
 
 
@@ -1285,6 +1287,7 @@ void CxCore_ScaleAddTest::get_test_array_types_and_sizes( int test_case_idx, CvS
     CxCore_MatrixTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
     sizes[INPUT][2] = cvSize(1,1);
     types[INPUT][2] &= CV_MAT_DEPTH_MASK;
+    test_nd = cvTsRandInt(ts->get_rng()) % 2 != 0;
 }
 
 
@@ -1304,13 +1307,22 @@ int CxCore_ScaleAddTest::prepare_test_case( int test_case_idx )
     int code = CxCore_MatrixTest::prepare_test_case( test_case_idx );
     if( code > 0 )
         alpha = cvGet1D( &test_mat[INPUT][2], 0 );
+    if( test_nd )
+        alpha.val[1] = 0;
     return code;
 }
 
 
 void CxCore_ScaleAddTest::run_func()
 {
-    cvScaleAdd( test_array[INPUT][0], alpha, test_array[INPUT][1], test_array[OUTPUT][0] );
+    if(!test_nd)
+        cvScaleAdd( test_array[INPUT][0], alpha, test_array[INPUT][1], test_array[OUTPUT][0] );
+    else
+    {
+        cv::MatND c = cv::cvarrToMatND(test_array[OUTPUT][0]);
+        cv::scaleAdd( cv::cvarrToMatND(test_array[INPUT][0]), alpha.val[0],
+                      cv::cvarrToMatND(test_array[INPUT][1]), c);
+    }
 }