opencv cnn 模块 detection 返回的四维数组在mat中 循环获取每个人脸的信息
时间: 2023-11-27 19:52:29 浏览: 30
OpenCV中的CNN模块可以用来进行对象检测,其中返回的四维数组表示了检测到的对象的位置和置信度信息。如果你想获取每个人脸的信息,可以按照以下步骤进行:
1. 遍历返回的四维数组,找到所有置信度大于一个阈值的人脸位置信息。
```
float confThreshold = 0.5; // 置信度阈值
for (int i = 0; i < detections.size[2]; ++i)
{
float confidence = detections.at<float>(0, 0, i, 2);
if (confidence > confThreshold)
{
// 处理此人脸位置信息
}
}
```
2. 对于每个满足条件的人脸位置信息,从四维数组中获取其位置和大小信息,并将其转换为OpenCV的Rect类型。
```
int xLeftBottom = static_cast<int>(detections.at<float>(0, 0, i, 3) * frame.cols);
int yLeftBottom = static_cast<int>(detections.at<float>(0, 0, i, 4) * frame.rows);
int xRightTop = static_cast<int>(detections.at<float>(0, 0, i, 5) * frame.cols);
int yRightTop = static_cast<int>(detections.at<float>(0, 0, i, 6) * frame.rows);
Rect faceRect(xLeftBottom, yLeftBottom, xRightTop - xLeftBottom, yRightTop - yLeftBottom);
```
3. 可以将人脸位置信息保存到一个数组或容器中,以便后续使用。
```
vector<Rect> faces; // 存储所有人脸位置信息
faces.push_back(faceRect); // 将当前人脸位置信息加入数组中
```
这样,你就可以获取到所有检测到的人脸的位置和大小信息,并进行后续处理。