2015-10-13 92 views
1

我是相当新的编程,并想知道如何开始实施在C++下面的算法,特征检测算法的实现

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

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

    否则(m ≤ n),递增1 t并且如果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(); 
} 
+0

检查OpenCV的例子和功能。可以使用[HoughLines函数](http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html)。 OpenCV还提供了检测边缘,[features](http://docs.opencv.org/doc/tutorials/features2d/feature_detection/feature_detection.html)等功能。 – wendelbsilva

+1

我知道houghlines,并houghlinesP检测线,但我想尝试实现上述算法。 – TheAmateur

回答

1

让我们假设你有一个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内的其他边缘点的数量。

您需要点之间的界限。一些搜索产量为this fine answer,这导致

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

您是否在此处看到模式?将步骤分成子步骤,询问谷歌,看看the documentation,尝试东西,直到它的工作。

你的下一个步骤可能是

  • 创建distance function,欧几里德应该足够
  • 找到所有点旁边基于距离函数线(或旁边的一个点,这是比较容易)

如果您仍然需要帮助,请尝试一些并返回。

+0

我需要这样做后我加载图像文件的权利? – TheAmateur

+0

现在我可以加载和显示图像,但是如何检测图像中的行?,下面给出的是代码,我只需要知道在“情况3:”中应该做什么。 – TheAmateur