2015-10-13 118 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