星辰大海StarSea 2014-12-20 12:44 采纳率: 0%
浏览 5000

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;
}

}

  • 写回答

1条回答

  • 星辰大海StarSea 2014-12-20 13:08
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败