Commit cc99ba63 SK

指针遍历distanceMat

1 个父辈 8511f266
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
......
...@@ -709,15 +709,15 @@ namespace Acc.Img ...@@ -709,15 +709,15 @@ namespace Acc.Img
/// <param name="oneBlobWidth"></param> /// <param name="oneBlobWidth"></param>
/// <param name="oneBlobRadius"></param> /// <param name="oneBlobRadius"></param>
/// <returns></returns> /// <returns></returns>
public static SplitItem findCircleInBlob(ref double[,] matDistanceArr, CvBlobs blobs, CvBlob blob, Point2d reelCenter, double oneBlobWidth= -1, double oneBlobRadius = -1) public static SplitItem findCircleInBlob(double[,] matDistanceArr, CvBlobs blobs, CvBlob blob, Point2d reelCenter, double oneBlobWidth= -1, double oneBlobRadius = -1)
{ {
SplitItem item = new SplitItem(); SplitItem item = new SplitItem();
while (true) while (true)
{ {
bool hasFind = false; bool hasFind = false;
for (int y = blob.MinY; y < blob.MaxY; y++) for (int x = blob.MinX; x< blob.MaxX; x++)
{ {
for (int x = blob.MinX; x < blob.MaxX; x++) for (int y = blob.MinY; y < blob.MaxY; y++)
{ {
double distance = matDistanceArr[x, y]; double distance = matDistanceArr[x, y];
if (distance > 0) if (distance > 0)
...@@ -726,7 +726,7 @@ namespace Acc.Img ...@@ -726,7 +726,7 @@ namespace Acc.Img
if (label != blob.Label) if (label != blob.Label)
{ {
//不是当前Blob的像素 //不是当前Blob的像素
matDistanceArr[x, y] = 0; //matDistanceArr[x, y] = 0;
continue; continue;
} }
if (!item.isEnd) if (!item.isEnd)
...@@ -787,13 +787,31 @@ namespace Acc.Img ...@@ -787,13 +787,31 @@ namespace Acc.Img
Mat distanceMat = new Mat(); Mat distanceMat = new Mat();
Cv2.DistanceTransform(threshMat, distanceMat, DistanceTypes.L2, DistanceMaskSize.Mask3); Cv2.DistanceTransform(threshMat, distanceMat, DistanceTypes.L2, DistanceMaskSize.Mask3);
double[,] distanceArr = new double[threshMat.Cols, threshMat.Rows]; double[,] distanceArr = new double[threshMat.Cols, threshMat.Rows];
IntPtr dddd = distanceMat.Data;
Console.WriteLine("Start to distance array"); Console.WriteLine("Start to distance array");
unsafe
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int y = 0; y < threshMat.Rows; y++) for (int y = 0; y < threshMat.Rows; y++)
{ {
for (int x = 0; x < threshMat.Cols; x++) for (int x = 0; x < threshMat.Cols; x++)
{ {
distanceArr[x,y] = distanceMat.At<float>(y, x); float* dd = (float*)distanceMat.Ptr(y, x);
distanceArr[x, y] = dd[0];
//float atValue = distanceMat.At<float>(y, x);
//distanceArr[x, y] = atValue;
//if (atValue != dd[0])
//{
// Console.WriteLine("rho=" + dd[0].ToString() + " atValue=" + atValue);
//}
}
} }
sw.Stop();
Console.WriteLine("=" + sw.ElapsedMilliseconds + " ms");
} }
Dictionary<int, SplitItem> blobCircles = new Dictionary<int, SplitItem>(); Dictionary<int, SplitItem> blobCircles = new Dictionary<int, SplitItem>();
...@@ -838,7 +856,7 @@ namespace Acc.Img ...@@ -838,7 +856,7 @@ namespace Acc.Img
maxWidth = blob.Rect.Height; maxWidth = blob.Rect.Height;
} }
SplitItem item = findCircleInBlob(ref distanceArr, blobs, blob, reelCenter); SplitItem item = findCircleInBlob(distanceArr, blobs, blob, reelCenter);
foreach (Circle c in item.circles) foreach (Circle c in item.circles)
{ {
...@@ -850,7 +868,8 @@ namespace Acc.Img ...@@ -850,7 +868,8 @@ namespace Acc.Img
} }
} }
} }
//放大宽度,防止误判断
maxWidth = maxWidth * 1.02;
Console.WriteLine("Start count"); Console.WriteLine("Start count");
int totalCount = 0; int totalCount = 0;
foreach (CvBlob blob in blobs.Values) foreach (CvBlob blob in blobs.Values)
...@@ -873,7 +892,7 @@ namespace Acc.Img ...@@ -873,7 +892,7 @@ namespace Acc.Img
} }
} }
//多个元器件,查找 所有圆 //多个元器件,查找 所有圆
SplitItem item = findCircleInBlob(ref distanceArr, blobs, blob, reelCenter, maxWidth, maxRadius); SplitItem item = findCircleInBlob(distanceArr, blobs, blob, reelCenter, maxWidth, maxRadius);
//对所有圆进行分组 //对所有圆进行分组
List<List<Circle>> groupCircles = item.groupCircles(maxWidth, maxRadius, reelCenter); List<List<Circle>> groupCircles = item.groupCircles(maxWidth, maxRadius, reelCenter);
...@@ -884,11 +903,10 @@ namespace Acc.Img ...@@ -884,11 +903,10 @@ namespace Acc.Img
Circle c = groupCircle[0]; Circle c = groupCircle[0];
srcMat.Circle(c.x, c.y, (int)c.radius/2, Scalar.Yellow); srcMat.Circle(c.x, c.y, (int)c.radius/2, Scalar.Yellow);
totalCount = totalCount + 1; totalCount = totalCount + 1;
//Scalar color = Scalar.RandomColor(); foreach (Circle cg in groupCircle)
//foreach (Circle c in groupCircle) {
//{ srcMat.Circle(cg.x, cg.y, (int)c.radius, color);
// srcMat.Circle(c.x, c.y, (int)c.radius, color); }
//}
} }
} }
} }
......
...@@ -31,7 +31,7 @@ namespace AccImage ...@@ -31,7 +31,7 @@ namespace AccImage
{ {
//与平均半径差值在10%以内,认为OK //与平均半径差值在10%以内,认为OK
double diff = avgRadius - currentMaxRadius; double diff = avgRadius - currentMaxRadius;
if (diff/avgRadius <= 0.2) if (diff/avgRadius <= 0.3)
{ {
isValid = true; isValid = true;
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!