DNN检测矩阵保持值大于1

时间:2019-04-27 04:47:08

标签: c++ opencv

我正在尝试使用opencv DNN通过opencv随附的caffe文件检测面部。该模型适用于大多数图像,但是对于较大的图像,我的代码将失败。检测矩阵的返回值似乎存在问题,有时返回值大于1。如果我按照某些网站上的建议将图像调整为300x300尺寸,则该模型将完全失效,无法检测到任何东西。代码在下面,在此之前未编辑图像。

std::vector<Rect> detectDNN(Mat image) {
const std::string caffeConfigFile = "deployFinal.prototxt";
const std::string caffeWeightFile = "res10_300x300_ssd_iter_140000.caffemodel";
std::vector<Rect> rects;

Net net = cv::dnn::readNetFromCaffe(caffeConfigFile, caffeWeightFile); 
cv::Mat inputBlob = cv::dnn::blobFromImage(image, 1.0, cv::Size(image.cols, image.rows), Scalar(155,155,155), false, false);

net.setInput(inputBlob, "data");
cv::Mat detection = net.forward("detection_out");

cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());

for (int i = 0; i < detectionMat.rows; i++)
{
    float confidence = detectionMat.at<float>(i, 2);

    if (confidence >.45)
    {
        int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * image.cols);
        int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * image.rows);
        int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * image.cols);
        int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * image.rows);

        printf("FIRST: %f Second: %f Third: %f Fourth %f\n", detectionMat.at<float>(i, 3), detectionMat.at<float>(i, 4), detectionMat.at<float>(i, 5), detectionMat.at<float>(i, 6));

        cv::rectangle(image, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0), 2, 4);
        Rect found(Point(x1, y1), Point(x2, y2));
        rects.push_back(found);
    }
}
return rects;

}

0 个答案:

没有答案