2 u010203404 u010203404 于 2014.12.20 20:44 提问

OpenCV KeyPoint 位置 坐标 改变 错误

KeyPoint
位置 坐标 错误
怎么改变

class Patch {

public:
Patch() :angle(0)
{
}
float operator-(Patch rhs)
{
float sum = 0.f;
for (int i = 0; i < 16 * 16; ++i)
{
if (this->patches[i] - rhs.patches[i]>0)
sum += (this->patches[i] - rhs.patches[i]);
else
sum -= (this->patches[i] - rhs.patches[i]);
}
return (sum);
}

/*Patch(Patch &rhs)
{
    for (int i = 0; i < 16 * 16; ++i)
        this->patches[i] = rhs.patches[i];
    this->x = rhs.x;
    this->y = rhs.y;
    this->flag = rhs.flag;
    this->octave = rhs.octave;
    this->angle = rhs.angle;
}*/

float patches[256];
float x, y;
int flag;
int octave;
float angle;
float size;

};

void Operation::getPatchCoarse()
{
float y, x, _x, _y;
int sample_x, sample_y;
float co, si;
num = 0;

cv::KeyPoint *p = &keypoints[index];

x = p->pt.x;
y = p->pt.y;

for (int i = -24; i <= 24; i += 2)
for (int j = -24; j <= 24; j += 2)
{
    count = 0;
    float temp;
    _x = x + i;
    _y = y + j;
    temp = _x > 0 ? _x : 0;
    temp = temp < grayImg.cols ? temp : grayImg.cols;
    patchC[num].x = temp;
    temp = _y > 0 ? _y : 0;
    temp = temp < grayImg.rows ? temp : grayImg.rows;
    patchC[num].y = temp;
    co = cos(p->angle);
    si = sin(p->angle);

    //psize = p->size;

    for (int k = -8; k <= 8; ++k)
    {
        for (int l = -8; l <= 8; ++l)
        {
            //Get coords of sample point on the rotated axis
            sample_x = cvRound(_x + (-l*si + k*co));
            sample_y = cvRound(_y + (l*co + k*si));
            sample_x = sample_x > 0 ? sample_x : 0;
            sample_y = sample_y > 0 ? sample_y : 0;
            sample_x = sample_x < grayImg.cols ? sample_x : grayImg.cols;
            sample_y = sample_y < grayImg.rows ? sample_y : grayImg.rows;

            patchC[num].patches[count++] = grayImg.at<unsigned char>(sample_y, sample_x);
        }
    }
    num++;
}

}
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
void Operation::getPatchFine()
{
float y, x;
int sample_x, sample_y;
float co, si, _x, _y;
num = 0;

rhs;
x = rhs.x;
y = rhs.y;

for (float i = -1.75; i <= 1.75; i += 0.25)
for (float j = -1.75; j <= 1.75; j += 0.25)
{
    count = 0;
    float temp;
    _x = x + i;
    _y = y + j;
    temp = _x > 0 ? _x : 0;
    temp = temp < grayImg.cols ? temp : grayImg.cols;
    patchF[num].x = temp;
    temp = _y > 0 ? _y : 0;
    temp = temp < grayImg.rows ? temp : grayImg.rows;
    patchF[num].y = temp;
    co = cos(rhs.angle);
    si = sin(rhs.angle);

    psize = rhs.size;

    for (int k = -8; k <= 8; ++k)
    {
        for (int l = -8; l <= 8; ++l)
        {
            //Get coords of sample point on the rotated axis
            sample_x = cvRound(_x + (-l*si + k*co));
            sample_y = cvRound(_y + (l*co + k*si));

            sample_x = sample_x > 0 ? sample_x : 0;
            sample_y = sample_y > 0 ? sample_y : 0;
            sample_x = sample_x < grayImg.cols ? sample_x : grayImg.cols;
            sample_y = sample_y < grayImg.rows ? sample_y : grayImg.rows;

            patchF[num].patches[count++] = grayImg.at<unsigned char>(sample_y, sample_x);
        }
    }
    num++;
}

}

////////////////////////////////////////////////////////////
void Operation::propagation()
{
float minSAD = FLT_MAX, SAD, thresh = 7200;
unsigned int minIdx = 0, min_Idx = 0;

size = int(keypoints.size());

for (index = 0; index < size; index++)
{
    if (!patches[index].flag)continue;

    getPatchCoarse();

    for (unsigned int i = 0; i < 625; i++)
    {
        SAD = patches[index] - patchC[i];
        if (SAD < minSAD)
        {
            minSAD = SAD;
            minIdx = i;
        }
    }
    rhs = patchC[minIdx];
    getPatchFine();

    for (unsigned int j = 0; j < 225; j++)
    {
        SAD = patches[index] - patchF[j];
        if (SAD < minSAD)
        {
            minSAD = SAD;
            min_Idx = j;
        }
    }
    rhs = patchF[min_Idx];

    if (minSAD < thresh)
    {
        for (int k = 0; k < 16 * 16; ++k)
            patches[index].patches[k] = rhs.patches[k];
        keypoints[index].pt = cv::Point2f(rhs.x, rhs.y);
        patches[index].x = rhs.x;
        patches[index].y = rhs.y;
    }
    else
    {
        keypoints[index].class_id = 0;
        patches[index].flag = 0;
    }           
}

}

void Operation::getPatch()
{
int sample_x, sample_y, count;
float co, si, y, x;

patches.clear();

size = unsigned int (keypoints.size());
patches.resize(size);
for (index = 0; index < unsigned int(keypoints.size()); index++)
{

    cv::KeyPoint *kp = &keypoints[index];

    count = 0;
    newPatch.flag = 1;
    newPatch.octave = kp->octave;
    newPatch.angle = kp->angle;
    x = kp->pt.x;
    y = kp->pt.y;

    psize = kp->size;
    newPatch.size = psize;

    newPatch.x = kp->pt.x;
    newPatch.y = kp->pt.y;
    co = cos(kp->angle);
    si = sin(kp->angle);

    for (int k = -8; k <= 8; ++k)
    {
        for (int l = -8; l <= 8; ++l)
        {
            //Get coords of sample point on the rotated axis
            sample_x = cvRound(x + (-l*si + k*co));
            sample_y = cvRound(y + (l*co + k*si));

            sample_x = sample_x > 0 ? sample_x : 0;
            sample_y = sample_y > 0 ? sample_y : 0;
            sample_x = sample_x < grayImg.cols ? sample_x : grayImg.cols;
            sample_y = sample_y < grayImg.rows ? sample_y : grayImg.rows;

            newPatch.patches[count] = grayImg.at<unsigned char>(sample_y, sample_x);
            count++;
            if (count>size)break;
        }
    }
    patches[index] = newPatch;
}

}

2个回答

u010203404
u010203404   2014.12.20 21:08

改变后,关键点集中显示在主对角线上半部分, 关键点坐标 的 x与y 很相近

u014443759
u014443759 我觉得你晒图的时候应该在程序中注释一下——说明整个程序的作用及各个函数各自的作用。
大约 3 年之前 回复
u010203404
u010203404   2014.12.20 21:33

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!