Commit ca84d094 SK

获取元件面积时使用直方图二值化

1 个父辈 9e086d51
...@@ -251,9 +251,6 @@ namespace Acc.Img ...@@ -251,9 +251,6 @@ namespace Acc.Img
} }
} }
private static void LabelBlobsInCircle(ref string[] labels, List<CvBlob> blobList, double centerX, double centerY, double radius) private static void LabelBlobsInCircle(ref string[] labels, List<CvBlob> blobList, double centerX, double centerY, double radius)
{ {
int labelCount = 0; int labelCount = 0;
...@@ -382,9 +379,13 @@ namespace Acc.Img ...@@ -382,9 +379,13 @@ namespace Acc.Img
return Math.Sqrt(avgArea); return Math.Sqrt(avgArea);
} }
private static CvBlobs AutoThreshBlobs(ref Mat imageMat, int blobArea = -1)
private static CvBlobs AutoThreshBlobs(ref Mat imageMat, int blobArea)
{ {
if (blobArea == -1)
{
//获取元器件特征时,假定的blob面积
blobArea = 3;
}
Mat[] mats = new Mat[] { imageMat };//一张图片,初始化为panda Mat[] mats = new Mat[] { imageMat };//一张图片,初始化为panda
Mat hist = new Mat();//用来接收直方图 Mat hist = new Mat();//用来接收直方图
int[] channels = new int[] { 0 };//一个通道,初始化为通道0 int[] channels = new int[] { 0 };//一个通道,初始化为通道0
...@@ -406,7 +407,7 @@ namespace Acc.Img ...@@ -406,7 +407,7 @@ namespace Acc.Img
for (int i = 0; i < 256; i++)//直方图 for (int i = 0; i < 256; i++)//直方图
{ {
double len = hist.Get<double>(i); double len = hist.Get<double>(i);
if(len > 100) if (len > 100)
{//灰度值的像素数小于100的忽略 {//灰度值的像素数小于100的忽略
percent = percent + len / total; percent = percent + len / total;
if (startIndex == -1) if (startIndex == -1)
...@@ -422,56 +423,37 @@ namespace Acc.Img ...@@ -422,56 +423,37 @@ namespace Acc.Img
} }
} }
int avgIndex = (startIndex + endIndex) / 2; //int avgIndex = (startIndex + endIndex) / 2;
//Mat threshMat = new Mat();
//Cv2.Threshold(imageMat, threshMat, avgIndex, 255, ThresholdTypes.BinaryInv);
//CvBlobs resultBlobs = new CvBlobs();
//resultBlobs.Label(threshMat);
//List<CvBlob> autoBlobList = resultBlobs.Values.Where(b => b.Area > blobArea).ToList();
//int blobCount = resultBlobs.Count();
Mat threshMat = new Mat(); Mat threshMat = new Mat();
Cv2.Threshold(imageMat, threshMat, avgIndex, 255, ThresholdTypes.BinaryInv); int blobCount = 0;
CvBlobs resultBlobs = new CvBlobs(); CvBlobs resultBlobs = new CvBlobs();
resultBlobs.Label(threshMat); int threshIndex = (startIndex + endIndex) / 2;
List<CvBlob> autoBlobList = resultBlobs.Values.Where(b => b.Area > blobArea).ToList();
int blobCount = resultBlobs.Count();
int threshIndex = avgIndex;
double theArea = blobArea * 0.8; double theArea = blobArea * 0.8;
if (theArea < 1) theArea = 1; if (theArea < 1) theArea = 1;
Console.WriteLine("Avg Thresh: " + threshIndex);
while (true) for (int index = startIndex; index < endIndex; index++)
{ {
//阈值向下走,找满足条件Blob数量最多的 Mat tempThreshMat = new Mat();
threshIndex = threshIndex - 1; Cv2.Threshold(imageMat, tempThreshMat, threshIndex, 255, ThresholdTypes.BinaryInv);
Cv2.Threshold(imageMat, threshMat, threshIndex, 255, ThresholdTypes.BinaryInv);
CvBlobs blobs = new CvBlobs(); CvBlobs blobs = new CvBlobs();
blobs.Label(threshMat); blobs.Label(tempThreshMat);
List<CvBlob> blobList = blobs.Values.Where(b => b.Area > theArea).ToList(); List<CvBlob> blobList = blobs.Values.Where(b => b.Area > theArea).ToList();
if (blobList.Count > blobCount) if (blobList.Count > blobCount)
{ {
threshMat = tempThreshMat;
threshIndex = index;
resultBlobs = blobs; resultBlobs = blobs;
blobCount = blobList.Count; blobCount = blobList.Count;
} }
else
{
break;
}
}
threshIndex = avgIndex;
while (true)
{
//阈值向上走,找满足条件Blob数量最多的
threshIndex = threshIndex + 1;
Cv2.Threshold(imageMat, threshMat, threshIndex, 255, ThresholdTypes.BinaryInv);
CvBlobs blobs = new CvBlobs();
blobs.Label(threshMat);
List<CvBlob> blobList = blobs.Values.Where(b => b.Area > theArea).ToList();
if (blobList.Count > blobCount)
{
resultBlobs = blobs;
blobCount = blobList.Count;
}
else
{
break;
}
} }
imageMat = threshMat; imageMat = threshMat;
Console.WriteLine(threshIndex + "==== Blob: " + blobCount + " Area:" + theArea); Console.WriteLine("result thresh: " + threshIndex + "==== Blob: " + blobCount + " Area:" + theArea);
return resultBlobs; return resultBlobs;
} }
...@@ -1055,12 +1037,16 @@ namespace Acc.Img ...@@ -1055,12 +1037,16 @@ namespace Acc.Img
Mat dst = new Mat(); Mat dst = new Mat();
Cv2.CvtColor(imageMat, dst, ColorConversionCodes.RGB2GRAY); Cv2.CvtColor(imageMat, dst, ColorConversionCodes.RGB2GRAY);
//全局二值化 //全局二值化
Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.BinaryInv); //Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.BinaryInv);
//image = BitmapConverter.ToBitmap(dst);
//CvBlobs blobs = new CvBlobs();
//blobs.Label(dst);
CvBlobs blobs = AutoThreshBlobs(ref dst);
image = BitmapConverter.ToBitmap(dst); image = BitmapConverter.ToBitmap(dst);
CvBlobs blobs = new CvBlobs();
blobs.Label(dst);
int blobArea = -1; int blobArea = -1;
foreach(CvBlob blob in blobs.Values) { foreach (CvBlob blob in blobs.Values) {
if (blob.Rect.Contains(new OpenCvSharp.Point(markX, markY))) if (blob.Rect.Contains(new OpenCvSharp.Point(markX, markY)))
{ {
if (blob.Area < blobArea || blobArea == -1) if (blob.Area < blobArea || blobArea == -1)
......
...@@ -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.4) if (diff/avgRadius <= 0.5)
{ {
isValid = true; isValid = true;
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!