Commit 61eb66e4 张士柳

1 个父辈 0361ce5a
......@@ -853,9 +853,21 @@ namespace eyemLib_Sharp
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
//边缘查找
private static extern int eyemEdge1dGenPosRect(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int iTransition, double dSigma, double dAmpThresh, out MeasureHandle hObject);
//findLine
/// <summary>
/// 寻找边缘
/// </summary>
/// <param name="tpImage">图像</param>
/// <param name="tpLineSt">卡尺起点</param>
/// <param name="tpLineEd">卡尺终点</param>
/// <param name="iWhRoi">卡尺宽度</param>
/// <param name="nCalipers">卡尺数量</param>
/// <param name="iSearchDirec">搜索方向(起点到终点方向的垂直方向)</param>
/// <param name="dAmpThreshold">最小边缘幅度</param>
/// <param name="ccTransition">灰度值过渡的类型以确定边缘'first','all', 'negative', 'positive'</param>
/// <param name="hObject">结果</param>
/// <returns></returns>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemEdge1dFindLine(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int iTransition, out MeasureHandle hObject);
private static extern int eyemEdge1dFindLine(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int nCalipers, int iSearchDirec, double dAmpThreshold, string ccTransition, out MeasureHandle hObject);
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
private static extern int eyemSkeleton(EyemImage tpImage);
//边缘
......@@ -1284,18 +1296,22 @@ namespace eyemLib_Sharp
#endregion
#region Test 1DEdge
//EyemOcsDXY tpLineSt = new EyemOcsDXY();
//tpLineSt.dX = 947;
//tpLineSt.dY = 1;
//EyemOcsDXY tpLineEd = new EyemOcsDXY();
//tpLineEd.dX = 149;
//tpLineEd.dY = 548;
//MeasureHandle hObject;
//eyemEdge1dGenMeasureRect(ucpImage, tpLineSt, tpLineEd, 10, "all", 0, 0.9, 30, out hObject);
//eyemEdge1dGenPosRect(ucpImage, tpLineSt, tpLineEd, 50, 0, 0.9, 30, out hObject);
//eyemEdge1dFindLine(ucpImage, tpLineSt, tpLineEd, 15, 0, out hObject);
///hObject.Dispose();
EyemOcsDXY tpLineSt = new EyemOcsDXY();
tpLineSt.dX = 671;
tpLineSt.dY = 718;
EyemOcsDXY tpLineEd = new EyemOcsDXY();
tpLineEd.dX = 858;
tpLineEd.dY = 716;
MeasureHandle hObject;
//eyemEdge1dGenMeasureRect(image, tpLineSt, tpLineEd, 10, "all", 0, 0.9, 30, out hObject);
//eyemEdge1dGenPosRect(image, tpLineSt, tpLineEd, 50, 0, 0.9, 30, out hObject);
sw.Restart();
eyemEdge1dFindLine(image, tpLineSt, tpLineEd, 25, 35, -1, 15, "negative", out hObject);
sw.Stop();
Console.WriteLine("时间:" + sw.ElapsedMilliseconds.ToString());
return;
//hObject.Dispose();
#endregion
#region Test Matrix
......@@ -1341,29 +1357,29 @@ namespace eyemLib_Sharp
#region Test RobustFitLine
EyemOcsDXY taPoint = new EyemOcsDXY();
List<EyemOcsDXY> taPoints = new List<EyemOcsDXY>();
//EyemOcsDXY taPoint = new EyemOcsDXY();
//List<EyemOcsDXY> taPoints = new List<EyemOcsDXY>();
//x^2/4+y^2/9=1
for (double i = -2.0; i <= 2.0; i += 0.1)
{
taPoint.dX = i;
taPoint.dY = 3 * Math.Sqrt(1 - Math.Pow(i, 2) / 4);
taPoints.Add(taPoint);
//for (double i = -2.0; i <= 2.0; i += 0.1)
//{
// taPoint.dX = i;
// taPoint.dY = 3 * Math.Sqrt(1 - Math.Pow(i, 2) / 4);
// taPoints.Add(taPoint);
taPoint.dX = i;
taPoint.dY = -3 * Math.Sqrt(1 - Math.Pow(i, 2) / 4);
taPoints.Add(taPoint);
}
// taPoint.dX = i;
// taPoint.dY = -3 * Math.Sqrt(1 - Math.Pow(i, 2) / 4);
// taPoints.Add(taPoint);
//}
taPoint.dX = 0;
taPoint.dY = 2;
taPoints.Add(taPoint);
//taPoint.dX = 0;
//taPoint.dY = 2;
//taPoints.Add(taPoint);
taPoint.dX = 2;
taPoint.dY = 1;
taPoints.Add(taPoint);
//taPoint.dX = 2;
//taPoint.dY = 1;
//taPoints.Add(taPoint);
//taPoint.dX = 1;
//taPoint.dY = -1;
......@@ -1418,12 +1434,12 @@ namespace eyemLib_Sharp
//EyemOcsDABCD tpPlane = new EyemOcsDABCD();
IntPtr tpPoint = eyemStructArray2IntPtr(taPoints.ToArray());
//eyemRobustFitPlane(taPoints.Count, tpPoint, 6, 0, ref tpPlane);
EyemOcsDXYLSQ tpEllipse = new EyemOcsDXYLSQ();
eyemRobustFitEllipse(taPoints.Count, tpPoint, 6, 0, ref tpEllipse);
//IntPtr tpPoint = eyemStructArray2IntPtr(taPoints.ToArray());
////eyemRobustFitPlane(taPoints.Count, tpPoint, 6, 0, ref tpPlane);
//EyemOcsDXYLSQ tpEllipse = new EyemOcsDXYLSQ();
//eyemRobustFitEllipse(taPoints.Count, tpPoint, 6, 0, ref tpEllipse);
return;
//return;
//eyemFitLine(taPoints.Count, tpPoint, 10, ref tpLine);
//eyemRobustFitLine(taPoints.Count, tpPoint, 2, 0, ref tpLine);
//eyemFitLineRANSAC(taPoints.Count, tpPoint, 5, ref tpLine);
......
......@@ -744,146 +744,36 @@ int eyemRobustFitPlane(int iPtnNum, EyemOcsDXYZ *taPoint, int iCalcMode, double
static void fitEllipse_wods(int iPtnNum, const EyemOcsDXY *taPoints, float *weights, float *linebuf)
{
//计算系数
double sa[25], sb[5], sx[5];
cv::Mat a = cv::Mat(5, 5, CV_64F, sa), b = cv::Mat(5, 1, CV_64F, sb);
//系数矩阵A*X=B;
cv::Mat x = cv::Mat(5, 1, CV_64F, sx);
memset(sa, 0, sizeof(sa));
memset(sb, 0, sizeof(sb));
memset(sx, 0, sizeof(sx));
for (int i = 0; i < iPtnNum; i++)
{
sa[0] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dX*taPoints[i].dX;
sa[1] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dX*taPoints[i].dY;
sa[2] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dY*taPoints[i].dY;
sa[3] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dX;
sa[4] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dY;
sa[5] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dX*taPoints[i].dY;
sa[6] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dY*taPoints[i].dY;
sa[7] += (double)weights[i] * taPoints[i].dX*taPoints[i].dY*taPoints[i].dY*taPoints[i].dY;
sa[8] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dY;
sa[9] += (double)weights[i] * taPoints[i].dX*taPoints[i].dY*taPoints[i].dY;
sa[10] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dY*taPoints[i].dY;
sa[11] += (double)weights[i] * taPoints[i].dX*taPoints[i].dY*taPoints[i].dY*taPoints[i].dY;
sa[12] += (double)weights[i] * taPoints[i].dY*taPoints[i].dY*taPoints[i].dY*taPoints[i].dY;
sa[13] += (double)weights[i] * taPoints[i].dX*taPoints[i].dY*taPoints[i].dY;
sa[14] += (double)weights[i] * taPoints[i].dY*taPoints[i].dY*taPoints[i].dY;
sa[15] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dX;
sa[16] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dY;
sa[17] += (double)weights[i] * taPoints[i].dX*taPoints[i].dY*taPoints[i].dY;
sa[18] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX;
sa[19] += (double)weights[i] * taPoints[i].dX*taPoints[i].dY;
sa[20] += (double)weights[i] * taPoints[i].dX*taPoints[i].dX*taPoints[i].dY;
sa[21] += (double)weights[i] * taPoints[i].dX*taPoints[i].dY*taPoints[i].dY;
sa[22] += (double)weights[i] * taPoints[i].dY*taPoints[i].dY*taPoints[i].dY;
sa[23] += (double)weights[i] * taPoints[i].dX*taPoints[i].dY;
sa[24] += (double)weights[i] * taPoints[i].dY*taPoints[i].dY;
sb[0] += (double)weights[i] * (-taPoints[i].dX*taPoints[i].dX);
sb[1] += (double)weights[i] * (-taPoints[i].dX*taPoints[i].dY);
sb[2] += (double)weights[i] * (-taPoints[i].dY*taPoints[i].dY);
sb[3] += (double)weights[i] * (-taPoints[i].dY);
sb[3] += (double)weights[i] * (-taPoints[i].dX);
//6%左右的点不要
std::vector<cv::Point2f> taPoint;
for (int i = 0; i < iPtnNum; i++) {
if (weights[i]) {
taPoint.push_back(cv::Point2f((float)taPoints[i].dX, (float)taPoints[i].dY));
}
}
cv::solve(a, b, x, cv::DECOMP_SVD);
//计算椭圆参数Ax^2+Bxy+Cy^2+Dx+Ey+F=0 F=1
float s, l, t, x0, y0, A, B, C, D, E, F;
//对应的
A = (float)sx[0], B = (float)sx[1], C = (float)sx[2], D = (float)sx[3], E = (float)sx[4], F = 1.0f;
//中心x
x0 = (B*E - 2.0f * C*D) / (4.0f * A*C - B*B);
//中心y
y0 = (B*D - 2.0f * A*E) / (4.0f * A*C - B*B);
//倾角
if (abs(B / A) <= 0.0001f&&1.0f < C / A)
t = 0.0f;
else if (abs(B / A) <= 0.0001f&&1.0f > C / A)
t = (float)PI_BY_2;
else if (1.0f < C / A)
t = 0.5f*atan((B / A) / (1.0f - C / A));
else t = (float)PI_BY_2 + 0.5f*atan((B / A) / (1.0f - C / A));
//长轴半径
l = sqrt(2.0f*(A*x0*x0 + C*y0*y0*B*x0*y0 - 1.0f) / (A + C - sqrt((A - C)*(A - C) + B*B)));
//短轴半径
s = sqrt(2.0f*(A*x0*x0 + C*y0*y0*B*x0*y0 - 1.0f) / (A + C + sqrt((A - C)*(A - C) + B*B)));
//拟合椭圆
auto rbox = cv::fitEllipse(taPoint);
//结果
linebuf[0] = x0; linebuf[1] = y0; linebuf[2] = cv::max(l, s); linebuf[3] = cv::min(l, s); linebuf[4] = t;
linebuf[0] = rbox.center.x; linebuf[1] = rbox.center.y; linebuf[2] = cv::max(rbox.size.width / 2.0f, rbox.size.height / 2.0f); linebuf[3] = cv::min(rbox.size.width / 2.0f, rbox.size.height / 2.0f); linebuf[4] = rbox.angle*(float)PI / 180.0f;
}
int eyemRobustFitEllipse(int iPtnNum, EyemOcsDXY * taPoint, int iCalcMode, double dRobustCoef, EyemOcsDXYLSQ & tpEllipse)
{
double min_err = DBL_MAX, err = 0;
void(*calc_weights_param) (float *, int, float *, float) = 0;
float linebuf[5] = { .0f };
memset(linebuf, 0, 5 * sizeof(float));
std::vector<float> weights(iPtnNum, 1.0);
switch (iCalcMode)
{
case EYEM_DIST_L1:
calc_weights_param = weightL1;
break;
case EYEM_DIST_L12:
calc_weights_param = weightL12;
break;
case EYEM_DIST_FAIR:
calc_weights_param = weightFair;
break;
case EYEM_DIST_WELSCH:
calc_weights_param = weightWelsch;
break;
case EYEM_DIST_HUBER:
calc_weights_param = weightHuber;
break;
case EYEM_DIST_TUKEY:
calc_weights_param = weightTukey;
break;
case EYEM_DIST_CAUCHY:
calc_weights_param = weightCauchy;
break;
case EYEM_DIST_LOGISTIC:
calc_weights_param = weightLogistic;
break;
case EYEM_DIST_ANDREWS:
calc_weights_param = weightAndrews;
break;
case EYEM_DIST_ATLWORTH:
calc_weights_param = weightTalworth;
break;
case EYEM_DIST_USER:
calc_weights_param = weightUser;
break;
default:
break;
}
//迭代计算
for (int n = 0; n < 100; n++)
{
float sum_dist = .0, sum_w = .0;
fitEllipse_wods(iPtnNum, taPoint, &weights[0], linebuf);
//椭圆参数
float x0 = linebuf[0], y0 = linebuf[1], l = linebuf[2], s = linebuf[3], t = linebuf[4], c = sqrt(abs(l*l - s*s));
//焦点
cv::Point2f f1(x0 - c*cos(t), y0 - c*sin(t)), f2(x0 + c*cos(t), y0 + c*sin(t));
cv::AutoBuffer<float> dist(iPtnNum);
for (int k = 0; k < iPtnNum; k++) {
dist[k] = fabs(((float)cv::norm(cv::Point2f((float)taPoint[k].dX, (float)taPoint[k].dY) - f1) + (float)cv::norm(cv::Point2f((float)taPoint[k].dX, (float)taPoint[k].dY) - f2)) - 2.0f*l);
sum_dist += dist[k];
}
err = sum_dist;
if (sum_dist < FLT_EPSILON)
break;
/*calculate weight*/
calc_weights_param(dist, iPtnNum, &weights[0], (float)dRobustCoef);
if (std::abs(err - min_err) < FLT_EPSILON)
break;
min_err = err;
float sum_dist = .0, sum_w = .0;
fitEllipse_wods(iPtnNum, taPoint, &weights[0], linebuf);
//椭圆参数
float x0 = linebuf[0], y0 = linebuf[1], l = linebuf[2], s = linebuf[3], t = linebuf[4], c = sqrt(abs(l*l - s*s));
//焦点
cv::Point2f F1(x0 - c*cos(t), y0 - c*sin(t)), F2(x0 + c*cos(t), y0 + c*sin(t));
std::vector<float> dist(iPtnNum);
for (int k = 0; k < iPtnNum; k++) {
dist[k] = fabs(((float)cv::norm(cv::Point2f((float)taPoint[k].dX, (float)taPoint[k].dY) - F1) + (float)cv::norm(cv::Point2f((float)taPoint[k].dX, (float)taPoint[k].dY) - F2)) - 2.0f*l);
sum_dist += dist[k];
}
tpEllipse.dL = linebuf[2], tpEllipse.dS = linebuf[3], tpEllipse.dQ = linebuf[4], tpEllipse.dXo = linebuf[0], tpEllipse.dYo = linebuf[1];
return FUNC_OK;
......
......@@ -645,7 +645,7 @@ extern "C" {
EXPORTS int eyemEdge1dGenPosRect(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int iTransition, double dSigma, double dAmpThresh, IntPtr *hObject);
EXPORTS int eyemEdge1dFitLine(IntPtr hObject, int iClippingEndPoints, int iMaxIterations, double dRobustCoef, EyemOcsDABC *tpLine);
EXPORTS int eyemEdge1dFitCircle(IntPtr hObject, int iClippingEndPoints, int iMaxIterations, double dRobustCoef, EyemOcsDXYR *tpCircle);
EXPORTS int eyemEdge1dFindLine(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int iTransition, IntPtr *hObject);
EXPORTS int eyemEdge1dFindLine(EyemImage tpImage, EyemOcsDXY tpLineSt, EyemOcsDXY tpLineEd, int iWhRoi, int nCalipers, int iSearchDirec, double dAmpThreshold, const char *ccTransition, IntPtr *hObject);
EXPORTS bool eyemEdge1dGenMeasureFree(IntPtr hObject);
#ifdef __cplusplus
......
......@@ -85,6 +85,7 @@
<LinkIncremental>false</LinkIncremental>
<IncludePath>D:\opencv420\build\include;D:\opencv420\build\include\opencv2;D:\tbb2017_20170604oss\include;D:\zxing-cpp-master\core\src;D:\zxing-cpp-master\opencv\src;$(IncludePath)</IncludePath>
<LibraryPath>D:\opencv420\build\x64\vc14\lib;D:\tbb2017_20170604oss\lib\intel64\vc14;D:\zxing-cpp-master\build\Release;$(LibraryPath)</LibraryPath>
<TargetExt>.dll</TargetExt>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!