FAST改进方法另一种

在原有方法改进的基础上进行改进

原来的方法n取10
现在的方法 n取12

第二个图代码如下 (除以平均值的这个)

#include
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <stdlib.h>

using namespace std;
using namespace cv;

int getSum(uchar *p, int length)
{
int sum = 0;
for(int i=0;i<length; i++)
{
sum += *(p+i);
}
return sum;
}

void bubbleSortUp(int *p)
{
uchar flag = 1;
//for(int i=0; i < 10 && flag; i++)
for(int i=0; i < 12 && flag; i++)
{
flag = 0;
//for(int j=9; j > i; j–)
for(int j=11; j > i; j–)
{
if(p[j] < p[j-1])
{
uchar tmp = p[j];
p[j] = p[j-1];
p[j-1] = tmp;
flag = 1;
}
}
}
}

void bubbleSortDown(int *p)
{
uchar flag = 1;
//for(int i=0; i < 10 && flag; i++)
for(int i=0; i < 12 && flag; i++)
{
flag = 0;
//for(int j=9; j > i; j–)
for(int j=11; j > i; j–)
{
if(p[j] > p[j-1])
{
uchar tmp = p[j];
p[j] = p[j-1];
p[j-1] = tmp;
flag = 1;
}
}
}
}

void printArray(int *p, int len)
{
for(int i=0; i<len; i++)
{
cout<<p[i]<<" ";
}
cout<<endl;
}

void binaryInsertUp(int *p, uchar value)
{
int left = 0;
//int right = 9;
int right = 11;
int mid;
if(value < p[0])
return;
//if(value > p[9])
if(value > p[11])
{
//for(int i=0; i<9; i++)
for(int i=0; i<11; i++)
{
p[i] = p[i+1];

    }
    //p[9] = value;
      p[11] = value;
    return;
}

while(left < right)
{
    mid = (left + right)/2;
    if(mid == left || mid == right)
        break;
    if(value < p[mid])
        right = mid;
    else
        left = mid;
}
for(int i = 0; i < left; i++)
{
    p[i] = p[i+1];
}
p[left] = value;

}

void binaryInsertDown(int *p, uchar value)
{
int left = 0;
//int right = 9;
int right = 11;
int mid;
if(value > p[0])
return;
//if(value < p[9])
if(value < p[11])
{
//for(int i=0; i<9; i++)
for(int i=0; i<11; i++)
{
p[i] = p[i+1];

    }
    //p[9] = value;
      p[11] = value;
    return;
}

while(left < right)
{
    mid = (left + right)/2;
    if(mid == left || mid == right)
        break;
    if(value < p[mid])
        left = mid;
    else
        right = mid;
}
for(int i = 0; i < left; i++)
{
    p[i] = p[i+1];
}
p[left] = value;

}

int main(int argc, char* argv[])
{
/* 1.读入图像 */
Mat image = imread(“…/church01.png”, 0);
if(!image.data)
return 0;

namedWindow("Original Image");
imshow("Original Image", image);

Mat fastImg(image.size(), CV_8U, Scalar(0));//用于保存Fast检测的候选点
Mat fastScore(image.size(), CV_32F, Scalar(0));//用于计算候选点score
vector<Point> points;
int rows, cols, threshold;
rows = image.rows;
cols = image.cols;
threshold = 50;
int maxValues[12], minValues[12];

/* 2.计算Fast算法中的阈值参数 */
Mat_<uchar>::const_iterator it = image.begin<uchar>();
int count = 0;///
long sum=0;
int ave =0;
while(count < 12)
{
    maxValues[count] = *it;
    minValues[count] = *it;
    count++;
   sum+= *it;
    it++;
}
bubbleSortUp(maxValues);
bubbleSortDown(minValues);

while(it != image.end<uchar>())
{
    int value = *it;
    binaryInsertUp(maxValues, value);
    binaryInsertDown(minValues, value);
    sum+= *it;
    it++;
}


ave = sum/long(rows*cols);

cout<<" sum "<<sum<<" ave "<<ave<<endl;/
printArray(maxValues, 12);
printArray(minValues, 12);

int diff = 0;
for(int i = 0; i < 12; i++)
{
    diff += maxValues[i] - minValues[i];
}
threshold = (int)(20.0f*diff/12.0/ave);

cout<<" threshold "<<threshold<<endl;
/* 3.使用Fast算法检测得到候选点 */
for(int x = 3; x < rows - 3; x++)
{
for(int y = 3; y < cols - 3; y++)
{
uchar delta[16] = {0};
uchar diff[16] = {0};
delta[0] = (diff[0] = abs(image.at(x,y) - image.at(x, y-3))) > threshold;
delta[8] = (diff[8] = abs(image.at(x,y) - image.at(x, y+3))) > threshold;
if(getSum(delta, 16) == 0)
continue;
else
{
delta[12] = (diff[12] = abs(image.at(x,y) - image.at(x-3, y))) > threshold;
delta[4] = (diff[4] = abs(image.at(x,y) - image.at(x+3, y))) > threshold;
if(getSum(delta, 16) < 3)
continue;

            else
            {
                delta[1] = (diff[1] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+1, y-3))) > threshold;
                delta[2] = (diff[2] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+2, y-2))) > threshold;
                delta[3] = (diff[3] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+3, y-1))) > threshold;

                delta[5] = (diff[5] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+3, y+1))) > threshold;
                delta[6] = (diff[6] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+2, y+2))) > threshold;
                delta[7] = (diff[7] = abs(image.at<uchar>(x,y) - image.at<uchar>(x+1, y+3))) > threshold;

                delta[9] = (diff[9] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-1, y+3))) > threshold;
                delta[10] = (diff[10] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-2, y+2))) > threshold;
                delta[11] = (diff[11] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-3, y+1))) > threshold;

                delta[13] = (diff[13] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-3, y-1))) > threshold;
                delta[14] = (diff[14] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-2, y-2))) > threshold;
                delta[15] = (diff[15] = abs(image.at<uchar>(x,y) - image.at<uchar>(x-1, y-3))) > threshold;

                if(getSum(delta, 16) >= 12)
                {
                    points.push_back(Point(y,x));
                    fastScore.at<float>(x,y) = getSum(diff, 16);
                    fastImg.at<uchar>(x,y) = 255;
                }
            }
        }
    }

}

vector<Point>::const_iterator itp = points.begin();
while(itp != points.end())
{
    circle(image, *itp, 3, Scalar(255), 1);
    ++itp;
}
namedWindow("Fast Image");
imshow("Fast Image", image);//Fast检测候选点在原图中标记

/* 4.局部非极大值抑制 */
image = imread("../church01.png", 0);
Mat dilated(fastScore.size(), CV_32F, Scalar(0));
Mat localMax;
//Mat element(7, 7, CV_8U, Scalar(1));
dilate(fastScore, dilated, Mat());
compare(fastScore, dilated, localMax, CMP_EQ);
bitwise_and(fastImg, localMax, fastImg);

for(int x = 0;x < fastImg.cols; x++)
{
    for(int y = 0; y < fastImg.rows; y++)
    {
        if(fastImg.at<uchar>(y,x))
        {
            circle(image, Point(x,y), 3, Scalar(255), 1);
        }
    }
}
namedWindow("局部非极大值抑制");
imshow("局部非极大值抑制", image);//局部非极大值抑制后候选点在原图中标记

/* 5.计算Hessian矩阵去除边缘点和不稳定点 */
image = imread("../church01.png", 0);
Mat cornerStrength(fastScore.size(), CV_64F, Scalar(0));
for(int x = 0;x < fastImg.rows; x++)
{
    for(int y = 0; y < fastImg.cols; y++)
    {
        if(fastImg.at<uchar>(x,y))
        {
            if((x>0) && (x<fastImg.rows-1)
                && (y>0) && (y<fastImg.cols-1))
            {
                double Ixx = image.at<uchar>(x+1,y) + image.at<uchar>(x-1,y)-2*image.at<uchar>(x,y);
                double Iyy = image.at<uchar>(x,y+1) + image.at<uchar>(x,y-1)-2*image.at<uchar>(x,y);
                double Ixy = image.at<uchar>(x+1,y+1) + image.at<uchar>(x,y)-image.at<uchar>(x,y+1)-image.at<uchar>(x+1,y);
                cornerStrength.at<double>(x,y) = (Ixx+Iyy)*(Ixx+Iyy)/(Ixx*Iyy-Ixy*Ixy);

            }
        }
    }
}
int t = 10;
Mat cornerMap;
cornerMap = cornerStrength > t;
image = imread("../church01.png", 0);

for(int x = 0;x < cornerMap.cols; x++)
{
    for(int y = 0; y < cornerMap.rows; y++)
    {
        if(cornerMap.at<uchar>(y,x))
        {
            circle(image, Point(x,y), 3, Scalar(255), 1);

        }
    }
}
namedWindow("improvedFast");
imshow("improvedFast", image);//最终检测得到的角点在原图中标记

waitKey();
return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值