修改opencv中的svm,輸出top n匹配的結果。

來源:互聯網
上載者:User

最近想用svm來輸出top n 匹配的結果。看了下opencv中的svm,predict,只能輸出top 1 。

有3個predict函數可選。其中有一個用了cv::parallel_for,應該是並行最佳化了,查不到相應的解釋文檔。

最後選了

float CvSVM::predict( const float* row_sample, int row_len, bool returnDFVal ) const

如果不是one——class問題,returnDFVal是不起作用的。

其實也沒得選,因為其他的幾個都是基於這個。。。。

原來的代碼有如下內容,這個就是輸出最大值的。先排序,然後就可以輸出第n個值了。
  
        for( i = 1, k = 0; i < class_count; i++ )
        {
            if( vote[i] > vote[k] )
                k = i;
        }
        result = returnDFVal && class_count == 2 ? (float)sum : (float)(class_labels->data.i[k]);
    }
    else
        CV_Error( CV_StsBadArg, "INTERNAL ERROR: Unknown SVM type, "
                                "the SVM structure is probably corrupted" );

    return result;
}

如果不改原始碼,就要自己訓練(n*n-1)/2個svm,然後returnDFVal設定為true,自己比較輸出結果。

 最後修改如下

/*
        for( i = 1, k = 0; i < class_count; i++ )
        {
            if( vote[i] > vote[k] )
                k = i;
        }
  result = returnDFVal && class_count == 2 ? (float)sum : (float)(class_labels->data.i[k]);
  */
  /*修改代碼,以便可以輸出第n大的數,作為候選結果*/
  float xx=0;
  cv::Mat vote2=cv::Mat(class_count,1,CV_32FC1,vote);
  cv::Mat vote3;
  cv::sortIdx(vote2,vote3,CV_SORT_EVERY_COLUMN+CV_SORT_DESCENDING);
  k=vote3.at<int>(n-1,0);
  result = returnDFVal && class_count == 2 ? (float)sum : (float)(class_labels->data.i[k]);
      結果有待驗證。

改了hpp和cpp後,檢查下是否引用了hpp,如果改的和引用的不一致就悲劇了,我就碰到了,還好休息的時候馬上想到了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.