简体   繁体   English

OpenCV 检测点是否沿线/平面分布

[英]OpenCV detect if points lie along line/plane

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.我正在使用 OpenCV 并对图像进行阈值处理和裁剪,只留下可能的相关点。 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). (这些点位于一条直线上,但光束经常弯曲,因此击中大部分点就足够了,有 21 个点在设置阈值时显示为白色圆圈)。

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.具有 1 到 640 个 bin(图像宽度)的直方图结果是两条线,位于 0 处和接近最大值的大约 2/3 处。 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)一条直线上的 12 个点并排(光束在正确位置)

The sort of output wanted (for illistration, if the points are on the line this is all I need to know!)想要的输出类型(对于illustration,如果点在线,这就是我需要知道的!)

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.一个想法是提取点的坐标并进行比较,但我不知道如何做到这一点。

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.有更多有效的方法可以做到这一点(请参阅 CodeCogs 或 AGLIB),但作为快速修复,此代码似乎有效。

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为了检测 OpenCV 中的圆,我从这篇文章中删除了霍夫变换和适应代码: 检测图像上的硬币(和拟合椭圆)

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.然后是细化坐标(去除任何异常值等)以确定圆是否位于回归的斜率和截距值的水平线上的情况。

Obtain the x,y coordinates of the thresholded points, then perform a linear regression to find a best-fit line.获取阈值点的 x,y 坐标,然后执行线性回归以找到最佳拟合线。 With that line, you can determine the r^2 value which effectively gives you the quality of fit.使用该线,您可以确定 r^2 值,该值有效地为您提供拟合质量。 Based on that fitness measure, you can determine your calibration success.根据该适应度测量,您可以确定校准成功与否。

Here is a good discussion. 是一个很好的讨论。

you could do something like this, altough it is an aproximation:你可以做这样的事情,虽然它是一个近似值:

var dw = decide a medium dot width in pixels 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 would be the best result... maxdots 将是最好的结果......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM