Commit 60a3f895 cuiya

optimize

1 个父辈 86c5ac4c
......@@ -12,6 +12,7 @@ namespace Acc.Img
public class ImageUtil
{
public static bool selectB = false;
/// <summary>
/// 读取图片,,支持格式*.raw,*.bmp;*.gif;*.jpg;*.png
/// </summary>
......@@ -28,7 +29,6 @@ namespace Acc.Img
if (images != null)
{
image = images[1];
string ss = "改版10";
}
}
else
......@@ -119,6 +119,84 @@ namespace Acc.Img
}
return 0;
}
public static int GetItemFeatureAuto(Image image, int markX = -1, int markY = -1, int thresh = -1, bool inv = true)
{
Mat imageMat = BitmapConverter.ToMat(new Bitmap(image));
List<CvBlob> blobList = GetBlobs(imageMat, thresh, inv);
List<int> sampleList = new List<int>();
CvBlob srcBlob = new CvBlob();
srcBlob.Area = -1;
int blobCount = blobList.Count;
int selectIndex = blobCount / 2+blobCount/16;
for (int i = 0; i < blobList.Count; i++)
{
if (srcBlob.Area == -1)
{
srcBlob = blobList[i];
if (srcBlob.Area < 40)
{
srcBlob.Area = -1;
}
}
else
{
if (blobList[i].Area < srcBlob.Area && blobList[i].Area > 40)
{
srcBlob = blobList[i];
}
}
}
//srcBlob.Area += (int)(srcBlob.Area * 0.2);
for (int i = 0; i < blobList.Count; i++)
{
if (srcBlob.Area < blobList[i].Area)
{
double num = (double)blobList[i].Area / srcBlob.Area;
if (num < 2)
{
sampleList.Add(blobList[i].Area);
}
}
if (sampleList.Count == blobList.Count-1 || i == blobList.Count-1)
{
int nums = 0;
for (int j = 0; j < sampleList.Count; j++)
{
nums += sampleList[j];
}
double area = (double)nums / sampleList.Count;
double ss = area * 0.35;
int areaI = (int)Math.Round(area + ss);
//int sss = (int)Math.Round(areaI);
return areaI;
}
}
//if (markX != -1 && markY != -1)
//{
// //查找标记的Blob
// int markIndex = -1;
// for (int i = 0; i < blobCount; i++)
// {
// CvBlob blob = blobList[i];
// if (blob.Rect.Contains(new OpenCvSharp.Point(markX, markY)))
// {
// if (markIndex == -1 || blobList[i].Area < blobList[markIndex].Area)
// {
// markIndex = i;
// }
// }
// }
// if (markIndex != -1)
// {
// int area = blobList[markIndex].Area;
// area = area * 5 / 3 - 23;
// return area;
// }
//}
return srcBlob.Area;
}
/// <summary>
/// 根据元器件特征统计图片中的元器件数量
......@@ -131,14 +209,17 @@ namespace Acc.Img
public static int CountItems(ref Image image, int itemFeature, int thresh = -1, bool inv = true)
{
Mat imageMat = BitmapConverter.ToMat(new Bitmap(image));
Mat grayMat = BitmapConverter.ToMat(new Bitmap(image));
Cv2.CvtColor(grayMat, grayMat, ColorConversionCodes.RGBA2RGB);
Cv2.Threshold(imageMat, imageMat, 0, 255, ThresholdTypes.Binary);
List<CvBlob> blobList = GetBlobs(imageMat, thresh, inv);
int itemArea = (itemFeature + 23) * 3 /5;
if(itemArea <= 0)
{
itemArea = 3;
}
int totalCount = CountBlobs(blobList, itemArea, ref imageMat);
image = BitmapConverter.ToBitmap(imageMat);
int totalCount = CountBlobs(blobList, itemArea, ref grayMat);
image = BitmapConverter.ToBitmap(grayMat);
return totalCount;
}
......@@ -304,53 +385,87 @@ namespace Acc.Img
}
return Math.Sqrt(avgArea);
}
/// <summary>
/// 获取Blob个数
/// </summary>
/// <param name="blobList"></param>
/// <param name="avgArea"></param>
/// <param name="srcMat"></param>
/// <returns></returns>
private static int CountBlobs(List<CvBlob> blobList, int avgArea, ref Mat srcMat)
{
List<CvBlob> filterBlobList = blobList.Where(b => b.Area > 0.3 * avgArea).ToList();
//List<CvBlob> filterBlobList = blobList.Where(b => b.Area > 0.3 * avgArea).ToList();
List<CvBlob> filterBlobList = blobList.Where(b => b.Area > 0).ToList();
if (blobList.Count == 0)
{
return 0;
}
CvBlob markBlob = null;
double labelStep = GetLabelStep(blobList, avgArea, out markBlob);
//CvBlob markBlob = null;
//double labelStep = GetLabelStep(blobList, avgArea, out markBlob);
//string[] labels = new string[filterBlobList.Count];
string[] labels = new string[filterBlobList.Count];
//double markBlobX = markBlob.Centroid.X;
//double markBlobY = markBlob.Centroid.Y;
//LabelBlobsInCircle(ref labels, filterBlobList, markBlobX, markBlobY, labelStep);
double markBlobX = markBlob.Centroid.X;
double markBlobY = markBlob.Centroid.Y;
LabelBlobsInCircle(ref labels, filterBlobList, markBlobX, markBlobY, labelStep);
//int totalCount = 0;
//for (int i = 0; i < labels.Length; i++)
//{
// if (labels[i] != null)
// {
// CvBlob blob = filterBlobList[i];
// Scalar color = Scalar.Red;
// int count = BlobHasItem(avgArea, blob);
// if (count > 0)
// {
// if (count == 1)
// {
// color = Scalar.Green;
// }
// else if (count == 2)
// {
// color = Scalar.Blue;
// //Cv2.PutText(srcMat, count + "", blob.Centroid, HersheyFonts.HersheySimplex, 0.5, color);
// }
// else if (count >= 3)
// {
// color = Scalar.Red;
// Point2d center = blob.Centroid;
// Cv2.PutText(srcMat, count + "", new OpenCvSharp.Point(center.X, center.Y), HersheyFonts.HersheySimplex, 0.5, color);
// }
// totalCount = totalCount + count;
// blob.Contour.Render(srcMat, color);
// }
// }
//}
int totalCount = 0;
for (int i = 0; i < labels.Length; i++)
foreach (CvBlob blob in filterBlobList)
{
if (labels[i] != null)
Scalar color = Scalar.Red;
if (blob.MaxX - blob.MinX > 450 && blob.MaxY - blob.MinY > 450) continue;
int count = BlobHasItem(avgArea, blob);
if (count > 0)
{
CvBlob blob = filterBlobList[i];
Scalar color = Scalar.Red;
int count = BlobHasItem(avgArea, blob);
if (count > 0)
if (count == 1)
{
if (count == 1)
{
color = Scalar.Green;
}
else if (count == 2)
{
color = Scalar.Blue;
//Cv2.PutText(srcMat, count + "", blob.Centroid, HersheyFonts.HersheySimplex, 0.5, color);
}
else if (count >= 3)
{
color = Scalar.Red;
Point2d center = blob.Centroid;
Cv2.PutText(srcMat, count + "", new OpenCvSharp.Point(center.X, center.Y), HersheyFonts.HersheySimplex, 0.5, color);
}
totalCount = totalCount + count;
blob.Contour.Render(srcMat, color);
color = Scalar.Green;
}
else if (count == 2)
{
color = Scalar.Blue;
//Cv2.PutText(srcMat, count + "", blob.Centroid, HersheyFonts.HersheySimplex, 0.5, color);
}
else if (count >= 3)
{
color = Scalar.Red;
Point2d center = blob.Centroid;
Cv2.PutText(srcMat, count + "", new OpenCvSharp.Point(center.X, center.Y), HersheyFonts.HersheySimplex, 0.5, color);
}
totalCount = totalCount + count;
blob.Contour.Render(srcMat, color);
}
}
string countText = "Count: " + totalCount;
......@@ -375,21 +490,54 @@ namespace Acc.Img
{
Mat dst = new Mat();
Cv2.CvtColor(imageMat, dst, ColorConversionCodes.RGB2GRAY);
if (thresh == -1)
//if (selectB)
//{
// //全局二值化
// Cv2.Threshold(dst, dst, 30, 255, ThresholdTypes.Binary);
//}
//else
//{
// //全局二值化
// Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Binary);
//}
if (selectB)
{
//自动局部二值化
Binarizer.Sauvola(dst, dst, 21, 0.2, 32);
if (thresh == -1)
{
//自动局部二值化
Binarizer.Sauvola(dst, dst, 21, 0.2, 32);
}
else
{
//全局二值化
Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Binary);
}
if (inv)
{
Cv2.Threshold(dst, dst, 0, 150, ThresholdTypes.BinaryInv);
}
}
else
{
//全局二值化
Cv2.Threshold(dst, dst, thresh, 255, ThresholdTypes.Binary);
Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Binary);
Cv2.Threshold(dst, dst, 0, 150, ThresholdTypes.BinaryInv);
}
if (inv)
{
Cv2.Threshold(dst, dst, thresh, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
}
//if (thresh == -1)
//{
// //自动局部二值化
// Binarizer.Sauvola(dst, dst, 21, 0.2, 32);
//}
//else
//{
// //全局二值化
// Cv2.Threshold(dst, dst, 0, 255, ThresholdTypes.Binary);
//}
//if (inv)
//{
// Cv2.Threshold(dst, dst, 0, thresh, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
//}
return dst;
}
/// <summary>
......@@ -404,14 +552,12 @@ namespace Acc.Img
Cv2.CvtColor(imageMat, imageMat, ColorConversionCodes.RGBA2BGR);
//Cv2.CvtColor(imageMat, imageMat, ColorConversionCodes.RGB2GRAY);
Mat dst = Threshhold(imageMat, thresh, inv);
Mat k1 = Mat.Ones(new OpenCvSharp.Size(3, 3), MatType.CV_8UC1);
Mat k1 = Mat.Ones(new OpenCvSharp.Size(1, 1), MatType.CV_8UC1);
Cv2.MorphologyEx(dst, dst, MorphTypes.Open, k1);
CvBlobs blobs = new CvBlobs();
blobs.Label(dst);
List<CvBlob> blobList = blobs.Values.Where(b => b.Area > 3).ToList();
List<CvBlob> blobList = blobs.Values.Where(b => b.Area > 0).ToList();
return blobList;
}
......@@ -432,7 +578,7 @@ namespace Acc.Img
}
if (blobArea < minArea)
{
return 0;
return 1;
}
//if (blobArea >= 0.5 * averageArea && blobArea <= 1.5 * averageArea)
//{
......@@ -451,11 +597,11 @@ namespace Acc.Img
{
count = 1;
}
if (count <= 50)
if (count <= 5000)
{
return count;
}
return -1;
return 1;
}
/// <summary>
......
......@@ -22,7 +22,7 @@ namespace Acc.Demo
InitializeComponent();
}
private void OpenImage()
{
using (FileDialog dialog = new OpenFileDialog())
......@@ -119,6 +119,11 @@ namespace Acc.Demo
sw.Start();
int itemFeature = int.Parse(textBoxFeature.Text);
Image image = orginalImage;
if (!ImageUtil.selectB)
{
itemFeature = ImageUtil.GetItemFeatureAuto(image, markX, markY, theshValue);
}
ImageUtil.selectB = false;
int count = ImageUtil.CountItems(ref image, itemFeature, theshValue);
sw.Stop();
labelTime.Text = "耗时:" + sw.ElapsedMilliseconds + " ms";
......@@ -152,6 +157,7 @@ namespace Acc.Demo
{
theshValue = int.Parse(textBoxThesh.Text);
}
ImageUtil.selectB = true;
int feature = ImageUtil.GetItemFeature(orginalImage, markX, markY, theshValue);
labelFeature.Text = "特征值:" + feature + "";
textBoxFeature.Text = feature + "";
......
......@@ -30,7 +30,6 @@
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.imageBox = new Cyotek.Windows.Forms.ImageBox();
this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
this.打开图片ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.原图ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
......@@ -49,24 +48,11 @@
this.labelTime = new System.Windows.Forms.Label();
this.textBoxFeature = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.imageBox = new Cyotek.Windows.Forms.ImageBox();
this.contextMenuStrip.SuspendLayout();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// imageBox
//
this.imageBox.AllowDrop = true;
this.imageBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.imageBox.Location = new System.Drawing.Point(16, 52);
this.imageBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.imageBox.Name = "imageBox";
this.imageBox.Size = new System.Drawing.Size(817, 458);
this.imageBox.TabIndex = 0;
this.imageBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.imageBox_MouseDown);
//
// contextMenuStrip
//
this.contextMenuStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
......@@ -145,6 +131,7 @@
this.buttonThesh.TabIndex = 3;
this.buttonThesh.Text = "二值化";
this.buttonThesh.UseVisualStyleBackColor = true;
this.buttonThesh.Visible = false;
this.buttonThesh.Click += new System.EventHandler(this.buttonThesh_Click);
//
// textBoxThesh
......@@ -155,6 +142,7 @@
this.textBoxThesh.Size = new System.Drawing.Size(60, 25);
this.textBoxThesh.TabIndex = 4;
this.textBoxThesh.Text = "30";
this.textBoxThesh.Visible = false;
//
// checkBoxAutoThresh
//
......@@ -166,6 +154,7 @@
this.checkBoxAutoThresh.TabIndex = 5;
this.checkBoxAutoThresh.Text = "自动";
this.checkBoxAutoThresh.UseVisualStyleBackColor = true;
this.checkBoxAutoThresh.Visible = false;
this.checkBoxAutoThresh.CheckedChanged += new System.EventHandler(this.checkBoxAutoThresh_CheckedChanged);
//
// panel1
......@@ -236,6 +225,7 @@
this.textBoxFeature.Size = new System.Drawing.Size(60, 25);
this.textBoxFeature.TabIndex = 8;
this.textBoxFeature.Text = "200";
this.textBoxFeature.Visible = false;
//
// label1
//
......@@ -246,22 +236,26 @@
this.label1.Size = new System.Drawing.Size(82, 15);
this.label1.TabIndex = 9;
this.label1.Text = "元件特征:";
this.label1.Visible = false;
//
// button1
// imageBox
//
this.button1.Location = new System.Drawing.Point(263, 18);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 10;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.imageBox.AllowDrop = true;
this.imageBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.imageBox.Location = new System.Drawing.Point(16, 52);
this.imageBox.Margin = new System.Windows.Forms.Padding(4);
this.imageBox.Name = "imageBox";
this.imageBox.Size = new System.Drawing.Size(817, 458);
this.imageBox.TabIndex = 0;
this.imageBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.imageBox_MouseDown);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(851, 544);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBoxFeature);
this.Controls.Add(this.panel1);
......@@ -303,7 +297,6 @@
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ToolStripMenuItem 计数ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 保存当前图片ToolStripMenuItem;
private System.Windows.Forms.Button button1;
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!