Commit ca84d094 SK

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

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