特征检测算法的实现

Implementation of feature detection algorithm

本文关键字:实现 算法 特征检测      更新时间:2023-10-16

我对编程相当陌生,想知道如何开始在c++中实现以下算法,

给定一幅二值图像,其中强度为255的像素显示边缘,强度为0的像素显示背景,在图像中找到比n像素长的线段。t是一个计数器,显示没有找到一行的迭代次数,tm是退出程序之前允许的最大迭代次数。

  1. t=0
  2. 从图像中随机取两个边缘点,求直线通过的方程通过他们。
  3. 查找m,图像中在距离d像素内的其他边缘点的数量。
  4. 如果是m > n,请转步骤5。

    否则(m ≤ n), t加1,如果t < tm转到步骤2如果t ≥ tm退出程序

  5. 绘制直线,并移除距离其d像素的边缘点的形象。然后转到步骤1

基本上,我只是想从图像中随机选择两个点,找到它们之间的距离,如果这个距离太小,我将检测它们之间的一条线。

如果提供一个小代码片段,让我开始,我会很感激。这更像是RANSAC的参数线检测。如果我完成了这篇文章,我也会保持更新。

/* Display Routine */
#include "define.h"
ByteImage bimg;                     //A copy of the image to be viewed
int width, height;                  //Window dimensions
GLfloat zoomx = 1.0, zoomy = 1.0;   //Pixel zoom
int win;                            //Window index
void resetViewer();
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
if ((w!=width) || (h!=height)) {
    zoomx=(GLfloat)w/(GLfloat)bimg.nc;
    zoomy=(GLfloat)h/(GLfloat)bimg.nr;
    glPixelZoom(zoomx,zoomy);
}
width=w; height=h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y) {
glutPostRedisplay();
if((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN) &&
    (zoomx==1.0) && (zoomy==1.0)){
printf(" row=%d, col=%d, int=%d.n", y,x, (int)bimg.image[(bimg.nr-1-y)*bimg.nc+x]);
        glutPostRedisplay();
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);         
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glDrawPixels((GLsizei)bimg.nc,(GLsizei)bimg.nr,   GL_LUMINANCE,GL_UNSIGNED_BYTE, bimg.image);
glutSwapBuffers();
}

让我们假设您有一个int[XDIMENSION][YDIMENSION]

让t = 0。

int t = 0; // ;-)

从图像中随机取两个边缘点,求经过它们的直线的方程。

蛮力:你可以随机搜索图像中的点,并在它们不是边缘点时进行研究

struct Point {
  int x;
  int y;
};
bool is_edge(Point a) {
  return image[a.x][a.y] == 255;
}
int randomUpto(int upto) {
  int r = rand() % upto;
  return r;
}

,它需要通过

初始化伪随机数生成器。
srand(time(NULL));

查找边缘点

  Point a;
  do {
    a.x = randomUpto(XDIMENSION);
    a.y = randomUpto(YDIMENSION);
  } while ( ! is_edge(a) );

查找m,图像中距离该线d像素的其他边缘点的个数

你需要点之间的线。一些搜索得到这个很好的答案,它导致

std::vector<Point> getLineBetween(Point a, Point b) {
  double dx = b.x - a.x;
  double dy = b.y - a.y;
  double dist = sqrt(dx * dx + dy * dy);
  dx /= dist;
  dy /= dist;
  std::vector<Point> points;
  points.push_back(a);
  for ( int i = 0 ; i < 2*dist; i++ ) {
    Point tmp;
    tmp.x = a.x + (int)(i * dx /2.0);
    tmp.y = a.y + (int)(i * dy /2.0);
    if ( tmp.x != points.back().x
     || tmp.y != points.back().y ) {
      points.push_back(tmp);
    }
  }
  return points;
}

你看到其中的规律了吗?将这些步骤分成子步骤,询问google,查看文档,尝试这些东西,直到它起作用。

你的下一步可能是

  • 创建一个距离函数,欧几里得应该足够了
  • 根据距离函数
  • 查找线附近(或点附近,这更容易)的所有点

试一试,如果你还需要帮助,再来找我。