OpenCV 检测点是否沿着线/平面

发布于 2024-10-31 02:35:28 字数 767 浏览 4 评论 0原文

我正在研究一种光学设备的自动校准,目前是手动执行的。校准的第一部分是确定光束是否照亮了一组“校准”点。

我正在使用 OpenCV,并对图像进行阈值处理和裁剪,只留下可能的相关点。我知道想要确定这些点是否沿着直线(水平);如果数量足够,光束就处于正确的位置! (这些点位于一条直线上,但光束通常是弯曲的,因此击中大多数点就足够了,有 21 个点在阈值化时显示为白色圆圈)。

我尝试使用直方图,但在阈值图像上,结果不正确,现在正在查看霍夫线,但这会检测来自边缘的直线,因为我想确定检测到的点是否位于一条线上。

这是我使用的阈值代码:

cvThreshold(output, output, 150, 256, CV_THRESH_BINARY);

从 1 到 640 个 bin(图像宽度)的任何位置的直方图结果都是 0 处的两条线,大约是接近最大值的 2/3。不是在没有阈值处理的情况下预期或获得的分布。

一些图片试图说明这一点(注意“嘈杂”的光点,这是系统设置的一个功能,无法克服):

12 个点在一条直线上彼此相邻(光束处于正确位置)

想要的输出类型(为了说明,如果点在线上,这就是我需要知道的全部!)

任何帮助将不胜感激。一种想法是提取点的坐标并进行比较,但我不知道该怎么做。

I am working on a form of autocalibration for an optics device which is currently performed manually. The first part of the calibration is to determine whether a light beam has illuminated the set of 'calibration' points.

I am using OpenCV and have thresholded and cropped the image to leave only the possible relevant points. I know want to determine if these points lie along a stright (horizontal) line; if they a sufficient number do the beam is in the correct position! (The points lie in a straight line but the beam is often bent so hitting most of the points suffices, there are 21 points which show up as white circles when thresholded).

I have tried using a histogram but on the thresholded image the results are not correct and am now looking at Hough lines, but this detects straight lines from edges wwhere as I want to establish if detected points lie on a line.

This is the threshold code I use:

cvThreshold(output, output, 150, 256, CV_THRESH_BINARY);

The histogram results with anywhere from 1 to 640 bins (image width) is two lines at 0 and about 2/3rds through of near max value. Not the distribution expected or obtained without thresholding.

Some pictures to try to illistrate the point (note the 'noisy' light spots which are a feature of the system setup and cannot be overcome):

12 points in a stright line next to one another (beam in correct position)

The sort of output wanted (for illistration, if the points are on the line this is all I need to know!)

Any help would be greatly appreciated. One thought was to extract the co-ordinates of the points and compare them but I don't know how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉梦枕江山 2024-11-07 02:35:28

如果它对任何人都有帮助,这里是我使用的一些简单线性回归代码的非常基本(初稿)。

    // Calculate the averages of arrays x and y
double xa = 0, ya = 0;

for(int i = 0; i < n; i++) 
{
    xa += x[i];
    ya += y[i];
}   
xa /= n;
ya /= n;

// Summation of all X and Y values
double sumX = 0;
double sumY = 0;
// Summation of all X*Y values
double sumXY = 0;
// Summation of all X^2 and Y^2 values
double sumXs = 0;
double sumYs = 0;

for(int i = 0; i < n; i++)
{
    sumX = sumX + x[i];
    sumY = sumY + y[i];

    sumXY = sumXY + (x[i] * y[i]);

    sumXs = sumXs + (x[i] * x[i]);
    sumYs = sumYs + (y[i] * y[i]);
}
// (X^2) and (Y^2) sqaured
double Xs = sumX * sumX;
double Ys = sumY * sumY;

// Calculate slope, m
slope = (n * sumXY - sumX * sumY) / (n* sumXs - Xs);    

// Calculate intercept
intercept = ceil((sumY - slope * sumX) / n);

// Calculate regression index, r^2
double r_top = (n * sumXY - sumX * sumY); 
double r_bottom = sqrt((n* sumXs - Xs) * (n* sumYs - Ys));
double r = 0;

// Check line is not perfectly vertical or horizontal
if(r_top == 0 || r_bottom == 0)
    r = 0;
else
    r = r_top/ r_bottom;

有更有效的方法可以做到这一点(请参阅 CodeCogs 或 AGLIB),但作为快速修复,此代码似乎可以工作。

为了在 OpenCV 中检测圆,我从这篇文章中删除了霍夫变换和改编的代码:
检测图像上的硬币(和适合的椭圆)

然后是细化坐标(删除任何异常值等)的情况,以确定圆是否位于回归的斜率和截距值的水平线上。

Incase it helps anyone here is a very basic (the first draft) of some simple linaear regression code I used.

    // Calculate the averages of arrays x and y
double xa = 0, ya = 0;

for(int i = 0; i < n; i++) 
{
    xa += x[i];
    ya += y[i];
}   
xa /= n;
ya /= n;

// Summation of all X and Y values
double sumX = 0;
double sumY = 0;
// Summation of all X*Y values
double sumXY = 0;
// Summation of all X^2 and Y^2 values
double sumXs = 0;
double sumYs = 0;

for(int i = 0; i < n; i++)
{
    sumX = sumX + x[i];
    sumY = sumY + y[i];

    sumXY = sumXY + (x[i] * y[i]);

    sumXs = sumXs + (x[i] * x[i]);
    sumYs = sumYs + (y[i] * y[i]);
}
// (X^2) and (Y^2) sqaured
double Xs = sumX * sumX;
double Ys = sumY * sumY;

// Calculate slope, m
slope = (n * sumXY - sumX * sumY) / (n* sumXs - Xs);    

// Calculate intercept
intercept = ceil((sumY - slope * sumX) / n);

// Calculate regression index, r^2
double r_top = (n * sumXY - sumX * sumY); 
double r_bottom = sqrt((n* sumXs - Xs) * (n* sumYs - Ys));
double r = 0;

// Check line is not perfectly vertical or horizontal
if(r_top == 0 || r_bottom == 0)
    r = 0;
else
    r = r_top/ r_bottom;

There are more efficeint ways of doing this (see CodeCogs or AGLIB) but as quick fix this code seems to work.

To detect Circles in OpenCV I dropped the Hough Transform and adapeted codee from this post:
Detection of coins (and fit ellipses) on an image

It is then a case of refining the co-ordinates (removing any outliers etc) to determine if the circles lie on a horizontal line from the slope and intercept values of the regression.

白鸥掠海 2024-11-07 02:35:28

获取阈值点的 x,y 坐标,然后执行线性回归以找到最佳拟合线。通过该线,您可以确定 r^2 值,从而有效地为您提供拟合质量。根据该健康测量,您可以确定校准是否成功。

这里是一个很好的讨论。

Obtain the x,y coordinates of the thresholded points, then perform a linear regression to find a best-fit line. With that line, you can determine the r^2 value which effectively gives you the quality of fit. Based on that fitness measure, you can determine your calibration success.

Here is a good discussion.

若水微香 2024-11-07 02:35:28

你可以做这样的事情,尽管它是一个近似值:

var dw = 决定以像素为单位的中等点宽度

maxdots = 0;
for each line of the image {
  var dots = 0;
  scan by incrementing x by dw {
     if (color==dotcolor) dots++;
  }
  if (dots>maxdots) maxdots=dots;
}

maxdots 将是最好的结果......

you could do something like this, altough it is an aproximation:

var dw = decide a medium dot width in pixels

maxdots = 0;
for each line of the image {
  var dots = 0;
  scan by incrementing x by dw {
     if (color==dotcolor) dots++;
  }
  if (dots>maxdots) maxdots=dots;
}

maxdots would be the best result...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文