opencv 4.6 Net.forward 返回的Mat 怎么获取每个人脸的信息
时间: 2023-11-27 20:51:44 浏览: 47
如果你使用的是OpenCV中的DNN模块进行人脸识别,那么在调用Net.forward()方法后,你可以通过以下步骤获取每个检测到的人脸的信息:
1. 获取输出层的名称和形状。
```cpp
std::vector<cv::String> layer_names = net.getLayerNames();
std::string output_layer = layer_names[layer_names.size() - 1];
cv::Mat detection = net.forward(output_layer);
cv::Mat detection_mat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
```
2. 遍历每个检测到的人脸,解析其边界框信息和置信度。
```cpp
for (int i = 0; i < detection_mat.rows; i++)
{
float confidence = detection_mat.at<float>(i, 2);
if (confidence > 0.5)
{
int x1 = static_cast<int>(detection_mat.at<float>(i, 3) * frame.cols);
int y1 = static_cast<int>(detection_mat.at<float>(i, 4) * frame.rows);
int x2 = static_cast<int>(detection_mat.at<float>(i, 5) * frame.cols);
int y2 = static_cast<int>(detection_mat.at<float>(i, 6) * frame.rows);
// do something with the bounding box coordinates and confidence score
}
}
```
3. 可以通过边界框的坐标和置信度进行人脸的进一步处理,例如进行人脸识别、情绪分析等。
注意:以上代码仅供参考,具体实现可能需要根据你的任务需求进行修改和优化。
相关推荐
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)