特征检测算法的实现
Implementation of feature detection algorithm
我对编程相当陌生,想知道如何开始在c++中实现以下算法,
给定一幅二值图像,其中强度为255的像素显示边缘,强度为0的像素显示背景,在图像中找到比n
像素长的线段。t
是一个计数器,显示没有找到一行的迭代次数,tm
是退出程序之前允许的最大迭代次数。
- 让
t=0
。 - 从图像中随机取两个边缘点,求直线通过的方程通过他们。
- 查找
m
,图像中在距离d像素内的其他边缘点的数量。 -
如果是
m > n
,请转步骤5。否则(
m ≤ n
),t
加1,如果t < tm
转到步骤2如果t ≥ tm
退出程序 - 绘制直线,并移除距离其
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,查看文档,尝试这些东西,直到它起作用。
你的下一步可能是
- 创建一个距离函数,欧几里得应该足够了
- 根据距离函数 查找线附近(或点附近,这更容易)的所有点
试一试,如果你还需要帮助,再来找我。
相关文章:
- std::unordered_map 搜索算法是如何实现的?
- 如何实现高效的算法来计算大型数据集的多个不同值?
- 了解算法的性能差异(如果以不同的编程语言实现)
- 它是否定义了哪些算法可以接受可变 lambda 的实现?
- 为什么在此排序算法实现中,向量明显比数组慢?
- 快速实现 PRIM 算法
- 如何知道用于实现标准代码段的确切数据结构和算法,例如在C++STL中?
- 在 Eigen3 中实现 Bartels-Stewart 算法 -- 仅实矩阵?
- 在 c++ 或 python 中生成一个体面的视差图以在 Raspberry Pi 上实现的最佳方法(算法或函数)是什么
- 字母C++实现中的 Prim 算法
- 尝试实现二叉搜索算法,似乎无法使其工作
- C++17 并行算法已经实现了吗?
- AES_XCBC算法的实现
- C++ GDAL 算法 GDALFillNodata() 的实现
- 如何检查 CNG Windows API 是否返回符合 FIPS 的算法实现
- OpenMP:共享同一算法的单线程和多线程实现
- 使用L1CPUCache实现c++算法的Python
- 实现算法(气泡排序)来组织阵列
- 实现O(n)算法来确定字符串是否所有字符都是唯一的
- 我应该如何去实现算法与黑白和彩色图像使用