目標檢測中的候選框的迴歸修正c++版

目標檢測中的候選框的迴歸修正c++版

簡介

在目標檢測算法中,都會需要框出目標,其中的實現過程是一個關於候選框的迴歸,迴歸以後才能得到更加準確的框的座標信息,和框的長寬。具體過程可以參考下面給出的源碼,每一步都我給出了相應的註釋,大家應該能看懂。

源碼

typedef struct FaceInfo {
    float score;//置信度
    int x[2];
    int y[2];//框左下和右上的座標點
    float area;//框的面積
    float regreCoord[4];//4個座標的修正信息,返回的是框的比例
    int landmark[10];//人臉的5個特徵點
} FaceInfo;
refine(vector<FaceInfo> &bboxs, int height, int width, bool flag)
{
    if (bboxs.empty())
        return;
    for (auto it = bboxs.begin(); it != bboxs.end(); it++)
    {
        float bw = it->x[1] - it->x[0] + 1;
        float bh = it->y[1] - it->y[0] + 1;//得到候選框的長寬
        float x0 = it->x[0] + it->regreCoord[0] * bw;
        float y0 = it->y[0] + it->regreCoord[1] * bh;
        float x1 = it->x[1] + it->regreCoord[2] * bw;
        float y1 = it->y[1] + it->regreCoord[3] * bh;//修正爲候選框的比例

        if (flag)
        {
            float w = x1 - x0 + 1;
            float h = y1 - y0 + 1;//防止寬高爲0
            float m = (h > w) ? h : w;
            x0 = x0 + w * 0.5 - m * 0.5;
            y0 = y0 + h * 0.5 - m * 0.5;//讓x0或y0更小
            x1 = x0 + m - 1;
            y1 = y0 + m - 1;//讓x1或y1更大
            //效果相當於將候選框以兩個點向外拉大了
        }//這裏的目的是讓框儘可能的大一些,爲了更好的將目標置於框中
        it->x[0] = round(x0);
        it->y[0] = round(y0);
        it->x[1] = round(x1);
        it->y[1] = round(y1);//取整賦值

        if (it->x[0] < 0) it->x[0] = 0;
        if (it->y[0] < 0) it->y[0] = 0;
        if (it->x[1] > width) it->x[1] = width - 1;
        if (it->y[1] > height) it->y[1] = height - 1;//限制座標在圖片裏面
        
        it->area = (it->x[1] - it->x[0]) * (it->y[1] - it->y[0]);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章