123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace OTSExtremum.Business
- {
- public class Classify
- {
- public enum Type
- {
- A = 0,
- B = 1,
- C = 2,
- D = 3,
- DS = 4
- }
- public static Dictionary<int, List<double>> getClassByGroup(string path)
- {
- Data.ParticleData particleData = new Data.ParticleData(path);
- DataTable particles = particleData.GetParticleListAndEm();
- List<double> A = new List<double>();
- List<double> B = new List<double>();
- List<double> C = new List<double>();
- List<double> D = new List<double>();
- List<double> DS = new List<double>();
- DataTable BD = particles.Clone();
- foreach (DataRow item in particles.Rows)
- {
- if (Convert.ToInt32(item["RectWidth"]) == 0)
- {
- continue;
- }
- //获取最大长度和最小宽度
- double h = Convert.ToDouble(item["DMAX"]);
- double w = Convert.ToDouble(item["DMIN"]);
- double dLengthWidthRatio = h / w;
- if (dLengthWidthRatio < 1)
- {
- dLengthWidthRatio = 1 / dLengthWidthRatio;
- }
- if (dLengthWidthRatio >= 3)//长宽比大于3的颗粒,根据化学元素不同,分为A类和C类
- {
- //A or C class
- string element = item["Element"].ToString();
- if (element.IndexOf("S-") > -1)
- {
- A.Add(Convert.ToDouble(item["PERIMETER"]));
- }
- else if (element.IndexOf("O-") > -1)
- {
- C.Add(Convert.ToDouble(item["PERIMETER"]));
- }
- }
- else//长宽比小于3的颗粒,有3种情况,一种是串条状的B类颗粒,一种是单独的D类颗粒,如果费雷特直径大于13则为DS类颗粒
- {
- // B, or D or DS
- // compute Feret's diameter
- double dFeretDiameter = Convert.ToDouble(item["PERIMETER"]);
- if (dFeretDiameter >= 13)
- {
- // DS
- DS.Add(Convert.ToDouble(item["PERIMETER"]));
- }
- else
- {
- //不能确定是B或D,先设为INVALID
- BD.Rows.Add(item.ItemArray);
- }
- }
- }
- foreach (DataRow item in BD.Rows)
- {
- double xs = 1142 / 1024;
- int left = Convert.ToInt32(item["RectLeft"]);
- int top = Convert.ToInt32(item["RectTop"]);
- int width = Convert.ToInt32(item["RectWidth"]);
- int height = Convert.ToInt32(item["RectHeight"]);
- int cenX = left + width / 2;
- int Bottom = top - height;
- bool isline = false;
- for (int i = 0; i < BD.Rows.Count; i++)
- {
- int leftB = Convert.ToInt32(item["RectLeft"]);
- int topB = Convert.ToInt32(item["RectTop"]);
- int widthB = Convert.ToInt32(item["RectWidth"]);
- int heightB = Convert.ToInt32(item["RectHeight"]);
- int cenBX = leftB + widthB / 2;
- int BottomB = topB - heightB;
- double dd = 0, ds = 0;
- ds = Math.Abs(cenX - cenBX);
- if (ds * xs < 10)//认为两个颗粒在一条竖直线上,但不在一起
- {
- if (Bottom > topB)//current particle is on the above
- {
- dd = Bottom - topB;
- if (dd * xs < 40)//认为这两个颗粒在一个串条上
- {
- isline = true;
- break;
- }
- }
- else if (BottomB > top) //current particle is on the below
- {
- dd = BottomB - top;
- if (dd * xs < 40)
- {
- isline = true;
- break;
- }
- }
- }
- }
- if (isline)
- {
- B.Add(Convert.ToDouble(item["PERIMETER"]));
- }
- else
- {
- D.Add(Convert.ToDouble(item["PERIMETER"]));
- }
- }
- SetZeroBackfill(particles.Rows.Count, ref A);
- SetZeroBackfill(particles.Rows.Count, ref B);
- SetZeroBackfill(particles.Rows.Count, ref C);
- SetZeroBackfill(particles.Rows.Count, ref D);
- SetZeroBackfill(particles.Rows.Count, ref DS);
- Dictionary<int, List<double>> dic = new Dictionary<int, List<double>>();
- dic.Add((int)Type.A, A);
- dic.Add((int)Type.B, B);
- dic.Add((int)Type.C, C);
- dic.Add((int)Type.D, D);
- dic.Add((int)Type.DS, DS);
- return dic;
- }
- public static double[] getClass(string path)
- {
- Data.ParticleData particleData = new Data.ParticleData(path);
- DataTable particles = particleData.GetParticleListAndEm();
- double A=0, B=0, C=0, D=0, DS = 0;
- DataTable BD = particles.Clone();
- foreach (DataRow item in particles.Rows)
- {
- if (Convert.ToInt32(item["RectWidth"]) == 0)
- {
- continue;
- }
- //获取最大长度和最小宽度
- double h = Convert.ToDouble(item["DMAX"]);
- double w = Convert.ToDouble(item["DMIN"]);
- double dLengthWidthRatio = h / w;
- if (dLengthWidthRatio < 1)
- {
- dLengthWidthRatio = 1 / dLengthWidthRatio;
- }
- if (dLengthWidthRatio >= 3)//长宽比大于3的颗粒,根据化学元素不同,分为A类和C类
- {
- //A or C class
- string element = item["Element"].ToString();
- if (element.IndexOf("S-") > -1)
- {
- if (Convert.ToDouble(item["PERIMETER"]) > A)
- {
- A = Convert.ToDouble(item["PERIMETER"]);
- }
-
- }
- else if (element.IndexOf("O-") > -1)
- {
- if (Convert.ToDouble(item["PERIMETER"]) > C)
- {
- C = Convert.ToDouble(item["PERIMETER"]);
- }
- }
- }
- else//长宽比小于3的颗粒,有3种情况,一种是串条状的B类颗粒,一种是单独的D类颗粒,如果费雷特直径大于13则为DS类颗粒
- {
- // B, or D or DS
- // compute Feret's diameter
- double dFeretDiameter = Convert.ToDouble(item["PERIMETER"]);
- if (dFeretDiameter >= 13)
- {
- // DS
- if (Convert.ToDouble(item["PERIMETER"]) > DS)
- {
- DS = Convert.ToDouble(item["PERIMETER"]);
- }
- }
- else
- {
- //不能确定是B或D,先设为INVALID
- BD.Rows.Add(item.ItemArray);
- }
- }
- }
- foreach (DataRow item in BD.Rows)
- {
- double xs = 1142 / 1024;
- int left = Convert.ToInt32(item["RectLeft"]);
- int top = Convert.ToInt32(item["RectTop"]);
- int width = Convert.ToInt32(item["RectWidth"]);
- int height = Convert.ToInt32(item["RectHeight"]);
- int cenX = left + width / 2;
- int Bottom = top- height;
- bool isline = false;
- for (int i = 0; i < BD.Rows.Count; i++)
- {
- int leftB = Convert.ToInt32(item["RectLeft"]);
- int topB = Convert.ToInt32(item["RectTop"]);
- int widthB = Convert.ToInt32(item["RectWidth"]);
- int heightB = Convert.ToInt32(item["RectHeight"]);
- int cenBX = leftB + widthB / 2;
- int BottomB = topB - heightB;
- double dd = 0, ds = 0;
- ds =Math.Abs(cenX - cenBX);
- if (ds* xs < 10)//认为两个颗粒在一条竖直线上,但不在一起
- {
- if (Bottom > topB)//current particle is on the above
- {
- dd = Bottom - topB;
- if (dd* xs < 40)//认为这两个颗粒在一个串条上
- {
- isline= true;
- break;
- }
- }
- else if (BottomB > top) //current particle is on the below
- {
- dd = BottomB - top;
- if (dd* xs < 40)
- {
- isline= true;
- break;
- }
- }
- }
- }
- if (isline)
- {
- if (Convert.ToDouble(item["PERIMETER"]) > B)
- {
- B = Convert.ToDouble(item["PERIMETER"]);
- }
- }
- else
- {
- if (Convert.ToDouble(item["PERIMETER"]) > D)
- {
- D = Convert.ToDouble(item["PERIMETER"]);
- }
- }
- }
- return new double[] {A,B,C,D,DS};
- }
- static void SetZeroBackfill(int count,ref List<double> ll)
- {
- for (int i = ll.Count; i < count; i++)
- {
- ll.Add(0);
- }
- ll.Sort((x, y) => -x.CompareTo(y));
- }
- }
-
- }
|