1.open cv3.0版本中没有了CvSVM类的定义,而是将其写入到一个document中,但是使用也并不复杂,代码示例如下:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">ml<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::SVM</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::Params</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>svmType <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> ml<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::SVM</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::C_SVC</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>kernelType <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> ml<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::SVM</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::POLY</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>gamma <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>; Ptr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span>ml<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::SVM</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span> svm <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> ml<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::SVM</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::create</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span>); Mat trainData; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 每行为一个样本</span> Mat labels; svm<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>train( trainData , ml<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::ROW_SAMPLE</span> , labels ); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// ...</span> Mat query; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 输入, 1个通道</span> Mat res; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 输出</span> svm<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>predict(query, res);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>
另外,svm的定义和训练可以写到一起:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">Ptr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span>SVM<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span> svm<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>StatModel<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::train</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span>SVM<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>(trainData,ml<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::ROW_SAMPLE</span>,labels,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">params</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
2.使用statModel::train(针对所有的机器学习 )的时候,要注意里面的参数(训练样本、训练标签)格式为InputArray,这个格式可以从Mat格式自动生成,但是不能从CvMat格式生成。
3.因此,必要的时候要进行两者的转换。当CvMat->Mat时:
<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">Mat Matfeatures</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">cv::cvarrToMat(CvMatFeatures)</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
PS: 从IplImage格式转成Mat格式时也是调用此函数,从Mat转到IplImage格式时:
<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">Mat mat</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">&IplImage(img);//其中img是IplImage格式数据</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
不知道这个方法对从Mat到CvMat的转换是否同样适用(估计是可以的)
4.由于3.0不存在为机器学习定义的类,因此不能用调用函数的方法对分类器进行操作,而代之使用:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">svm<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>save(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"...."</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//文件形式为xml,可以保存在txt或者xml文件中</span> svm<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>getVarCount();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取SVM支持向量的维数</span> svm<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>getSupportVectors;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回Mat类型,获取SVM的支持向量</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
5.可以对保存在xml或txt文件中的分类器进行加载。
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-constant" style="box-sizing: border-box;">Ptr</span><<span class="hljs-constant" style="box-sizing: border-box;">SVM</span>> svm=<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">statModel:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:load<SVM></span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"...."</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
6.在编写程序过程中,使用hog.compute()计算会报内存错误,折磨了一整天,最后将“项目属性”-“链接器”-“输入”中的”附加依赖项”中的ts300.lib和world300.lib删除掉了,只保留ts300d.lib和world300d.lib。具体原因,不详。
7.遇到”error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function “public: double & __thiscall std::vector…..”这种错误时,需要将” C/C++”->”代码生成”的运行库由 “多线程调试(/MTd)” 改为“多线程调试DLL (/MDd)”
8.构建字符串时有时需要将“1,2,3……23,24,25…….”显示成为“001,002,003…..023,024”,可使用’%3d’,如:
<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">spintf(neName,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"person_<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%3d</span>.bmp"</span>,i);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
9.使用OpenCv里面的机器学习算法时,要保证给的labelData的数据格式为”有符号的整型数”,”CV_32FC1”就是错误的一种形式,应该使用”CV_32SC1”
-
顶
- 0
-
踩