[英]Using Opencv to calculate the haar feature
I try to calculate Haar feature using opencv (Given an image). 我尝试使用opencv计算Haar功能 (给出图像)。
Input : an image 输入 :图像
output : haar feature 输出 :haar功能
For that, I am using the FeatureEvaluator from OpenCV. 为此,我使用了OpenCV的FeatureEvaluator。
But I got an exception when I try to calculate one feature. 但是当我尝试计算一个特征时出现了异常 。 Here is how I am doing: 这是我的工作方式:
Ptr<FeatureEvaluator> ptrHaar = FeatureEvaluator::create(FeatureEvaluator::HAAR);
Mat img = imread(image_path); // image of size 2048*1536 correctly loaded
ptrHaar->setImage(img, Size(100, 100));
ptrHaar->setWindow(Point(0, 0));
double res = ptrHaar->calcOrd(0); // get the exception here
Your code is almost right. 您的代码几乎是正确的。 Only is missing is to read the CascadeClassifier previously trained. 唯一缺少的是阅读以前训练过的CascadeClassifier。 You can do this as follow: 您可以按照以下步骤进行操作:
FileStorage fs( "cascade.xml", FileStorage::READ );
//2) Then, create a FileNode to access the features: // 2)然后,创建一个FileNode来访问功能:
FileNode featuresNode = fs["cascade"]["features"];
//3) Create the FeatureEvaluator, as you did in your first line // 3)像在第一行中一样创建FeatureEvaluator
//4) Read the FileNode you have created: // 4)读取您创建的FileNode:
ptrHaar->read(featuresNode);
And continue accordingly your code. 并据此继续执行您的代码。
Note that ptrHaar->calcOrd(0) will read just the first feature rectangle, if you have more to read, you will need a loop, like this: 请注意,ptrHaar-> calcOrd(0)将仅读取第一个要素矩形,如果您需要读取更多内容,则需要一个循环,如下所示:
FileNodeIterator it = featuresNode.begin(), it_end = featuresNode.end();
int idx = 0;
for( ; it != it_end; ==it, idx++ )
{
res = ptrHaar.calcOrd(idx);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.