123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using OpenCvSharp;
- using Point = System.Drawing.Point;
- using Size = OpenCvSharp.Size;
- namespace PaintDotNet.DedicatedAnalysis.GrainSizeStandard.IntegrationClass
- {
- internal class InParameter
- {
- public int iMethod;
- public Mat MGryImg;
- public Mat MHesOutImage;
- public Mat MValley;
- public int iDimA;
- public int iDimB;
- public int iXiHua;
- public int iLianJie;
- public Mat MEdgeOutImage;
- }
- public class NewSeg
- {
- private Mat m_MOrgImg;
- private Mat m_MOutImg;
- private double[] m_adOutParam;
- private InParameter m_InParam;
- public int DoSeg(Mat _MOrgImg, Mat _MOutImg, double[] _adOutParam, Vec4b value)
- {
- m_MOrgImg = _MOrgImg;
- m_MOutImg = _MOutImg;
- m_adOutParam = _adOutParam;
- PrepareParameter();
- if (2 == m_InParam.iMethod)
- {
- DetectEdge(value);
- return 2;
- }
- ImgHessian();
- FindValley();
- FindShed(value);
- return 1;
- }
- private int DetectEdge(Vec4b value)
- {
- Mat mat = new Mat();
- Cv2.Resize(m_InParam.MGryImg, mat, new Size((double)m_InParam.MGryImg.Cols * 0.2, (double)m_InParam.MGryImg.Rows * 0.2));
- Cv2.Sobel(mat, m_InParam.MEdgeOutImage, -1, 1, 1);
- Cv2.Threshold(m_InParam.MEdgeOutImage, m_InParam.MEdgeOutImage, 20.0, 255.0, ThresholdTypes.Binary);
- Mat mat2 = new Mat();
- Cv2.Canny(mat, mat2, 50.0, 50.0);
- m_InParam.MEdgeOutImage = (Mat)(m_InParam.MEdgeOutImage | mat2);
- Mat structuringElement = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(7, 7), new OpenCvSharp.Point(-1, -1));
- Cv2.MorphologyEx(m_InParam.MEdgeOutImage, m_InParam.MEdgeOutImage, MorphTypes.Close, (InputArray)structuringElement);
- Mat mat3 = new Mat();
- Cv2.Threshold(mat, mat3, 150.0, 255.0, ThresholdTypes.BinaryInv);
- m_InParam.MEdgeOutImage = (Mat)(m_InParam.MEdgeOutImage & mat3);
- ConnectionBoundary(m_InParam.MEdgeOutImage, 108);
- Mat mat4 = new Mat();
- Cv2.BitwiseNot(m_InParam.MEdgeOutImage, mat4);
- structuringElement = Cv2.GetStructuringElement(MorphShapes.Cross, new Size(7, 7), new OpenCvSharp.Point(-1, -1));
- Cv2.MorphologyEx(mat4, mat4, MorphTypes.Erode, (InputArray)structuringElement);
- Mat mat5 = new Mat();
- Cv2.Resize(m_MOrgImg, mat5, new Size(mat4.Cols, mat4.Rows));
- Mat mat6 = new Mat(mat4.Size(), 4);
- Cv2.FindContours(mat4, out OpenCvSharp.Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple);
- for (int i = 0; i < hierarchy.Length; i++)
- {
- Cv2.DrawContours(mat6, contours, i, Scalar.All(i + 1), 1, LineTypes.Link8, hierarchy);
- }
- Cv2.Watershed(mat5, mat6);
- Mat mat7 = new Mat(mat5.Rows, mat5.Cols, MatType.CV_8UC1, Scalar.All(0.0));
- int rows = mat6.Rows;
- int cols = mat6.Cols;
- int[] array = new int[2]
- {
- 0,
- 0
- };
- while (array[0] < rows)
- {
- array[1] = 0;
- while (array[1] < cols)
- {
- if (0 >= mat6.At<int>(array[0], array[1]))
- {
- mat7.Set(array, byte.MaxValue);
- }
- array[1]++;
- }
- array[0]++;
- }
- Cv2.Resize(mat7, mat7, new Size(m_InParam.MGryImg.Cols, m_InParam.MGryImg.Rows));
- Cv2.Resize(mat7, mat7, new Size(m_InParam.MGryImg.Cols, m_InParam.MGryImg.Rows));
- //Vec3b value = new Vec3b(0, 0, byte.MaxValue);
- rows = m_MOutImg.Rows;
- cols = m_MOutImg.Cols;
- array[0] = 0;
- while (array[0] < rows)
- {
- array[1] = 0;
- while (array[1] < cols)
- {
- if (120 < mat7.At<byte>(array[0], array[1]))
- {
- m_MOutImg.Set(array, value);
- }
- array[1]++;
- }
- array[0]++;
- }
- return 1;
- }
- public int LTQY()
- {
- Mat mat = new Mat();
- Mat mat2 = new Mat();
- Mat mat3 = new Mat();
- Mat mEdgeOutImage = m_InParam.MEdgeOutImage;
- int num = Cv2.ConnectedComponentsWithStats(mEdgeOutImage, mat, mat2, mat3);
- int[] array = new int[num];
- for (int i = 1; i < num; i++)
- {
- array[i] = 255;
- if (mat2.At<int>(i, 4) < 20)
- {
- array[i] = 0;
- }
- }
- Mat mat4 = new Mat(mEdgeOutImage.Rows, mEdgeOutImage.Cols, MatType.CV_8UC1, Scalar.All(0.0));
- for (int j = 0; j < mat4.Rows; j++)
- {
- for (int k = 0; k < mEdgeOutImage.Cols; k++)
- {
- int num2 = mat.At<int>(j, k);
- mat4.Set(j, k, (byte)array[num2]);
- }
- }
- return 0;
- }
- public void ImgThin(Mat src, int maxIterations = -1)
- {
- Cv2.Threshold(src, src, 100.0, 1.0, ThresholdTypes.Binary);
- int rows = src.Rows;
- int cols = src.Cols;
- int num = 0;
- while (true)
- {
- num++;
- if (maxIterations != -1 && num > maxIterations)
- {
- break;
- }
- List<int[]> list = new List<int[]>();
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- src.At<byte>(i, j);
- int num2 = (i != 0) ? src.At<byte>(i - 1, j) : 0;
- int num3 = (i != 0 && j != cols - 1) ? src.At<byte>(i - 1, j + 1) : 0;
- int num4 = (j != cols - 1) ? src.At<byte>(i, j + 1) : 0;
- int num5 = (i != rows - 1 && j != cols - 1) ? src.At<byte>(i + 1, j + 1) : 0;
- int num6 = (i != rows - 1) ? src.At<byte>(i + 1, j) : 0;
- int num7 = (i != rows - 1 && j != 0) ? src.At<byte>(i + 1, j - 1) : 0;
- int num8 = (j != 0) ? src.At<byte>(i, j - 1) : 0;
- int num9 = (i != 0 && j != 0) ? src.At<byte>(i - 1, j - 1) : 0;
- if (num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 >= 2 && num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 <= 6)
- {
- int num10 = 0;
- if (num2 == 0 && num3 == 1)
- {
- num10++;
- }
- if (num3 == 0 && num4 == 1)
- {
- num10++;
- }
- if (num4 == 0 && num5 == 1)
- {
- num10++;
- }
- if (num5 == 0 && num6 == 1)
- {
- num10++;
- }
- if (num6 == 0 && num7 == 1)
- {
- num10++;
- }
- if (num7 == 0 && num8 == 1)
- {
- num10++;
- }
- if (num8 == 0 && num9 == 1)
- {
- num10++;
- }
- if (num9 == 0 && num2 == 1)
- {
- num10++;
- }
- if (num10 == 1 && num2 * num4 * num6 == 0 && num4 * num6 * num8 == 0)
- {
- list.Add(new int[2]
- {
- i,
- j
- });
- }
- }
- }
- }
- list.ForEach(delegate (int[] aiP)
- {
- src.Set(aiP, (byte)0);
- });
- if (list.Count() == 0)
- {
- break;
- }
- list.Clear();
- for (int k = 0; k < rows; k++)
- {
- for (int l = 0; l < cols; l++)
- {
- int num11 = src.At<byte>(k, l);
- if (num11 != 1)
- {
- continue;
- }
- int num12 = (k != 0) ? src.At<byte>(k - 1, l) : 0;
- int num13 = (k != 0 && l != cols - 1) ? src.At<byte>(k - 1, l + 1) : 0;
- int num14 = (l != cols - 1) ? src.At<byte>(k, l + 1) : 0;
- int num15 = (k != rows - 1 && l != cols - 1) ? src.At<byte>(k + 1, l + 1) : 0;
- int num16 = (k != rows - 1) ? src.At<byte>(k + 1, l) : 0;
- int num17 = (k != rows - 1 && l != 0) ? src.At<byte>(k + 1, l - 1) : 0;
- int num18 = (l != 0) ? src.At<byte>(k, l - 1) : 0;
- int num19 = (k != 0 && l != 0) ? src.At<byte>(k - 1, l - 1) : 0;
- if (num12 + num13 + num14 + num15 + num16 + num17 + num18 + num19 >= 2 && num12 + num13 + num14 + num15 + num16 + num17 + num18 + num19 <= 6)
- {
- int num20 = 0;
- if (num12 == 0 && num13 == 1)
- {
- num20++;
- }
- if (num13 == 0 && num14 == 1)
- {
- num20++;
- }
- if (num14 == 0 && num15 == 1)
- {
- num20++;
- }
- if (num15 == 0 && num16 == 1)
- {
- num20++;
- }
- if (num16 == 0 && num17 == 1)
- {
- num20++;
- }
- if (num17 == 0 && num18 == 1)
- {
- num20++;
- }
- if (num18 == 0 && num19 == 1)
- {
- num20++;
- }
- if (num19 == 0 && num12 == 1)
- {
- num20++;
- }
- if (num20 == 1 && num12 * num14 * num18 == 0 && num12 * num16 * num18 == 0)
- {
- list.Add(new int[2]
- {
- k,
- l
- });
- }
- }
- }
- }
- list.ForEach(delegate (int[] aiP)
- {
- src.Set(aiP, (byte)0);
- });
- if (list.Count() == 0)
- {
- break;
- }
- list.Clear();
- }
- Cv2.Threshold(src, src, 0.0, 255.0, ThresholdTypes.Binary);
- }
- public void ConnectionBoundary(Mat Img, int iDistance)
- {
- Cv2.Threshold(Img, Img, 100.0, 1.0, ThresholdTypes.Binary);
- Mat mat = Connectivity(Img);
- List<int[]> list = new List<int[]>();
- List<int[]> list2 = new List<int[]>();
- int[] array = new int[2]
- {
- 1,
- 0
- };
- while (array[0] < Img.Rows - 1)
- {
- array[1] = 1;
- while (array[1] < Img.Cols - 1)
- {
- if (1 <= Img.At<byte>(array[0], array[1]) && mat.At<byte>(array[0], array[1]) == 0)
- {
- Img.Set(array, (byte)0);
- }
- if (1 <= Img.At<byte>(array[0], array[1]) && 1 == mat.At<byte>(array[0], array[1]))
- {
- list.Add(new int[2]
- {
- array[0],
- array[1]
- });
- int num = 0;
- int i = 0;
- for (num = -1; num <= 1; num++)
- {
- bool flag = false;
- for (i = -1; i <= 1; i++)
- {
- if ((num != 0 || i != 0) && Img.At<byte>(array[0] + num, array[1] + i) >= 1)
- {
- flag = true;
- break;
- }
- }
- if (flag)
- {
- break;
- }
- }
- list2.Add(new int[2]
- {
- -num,
- -i
- });
- }
- array[1]++;
- }
- array[0]++;
- }
- int num2 = list.Count();
- List<int[]>[] array2 = new List<int[]>[num2];
- List<int> list3 = new List<int>();
- List<int> list4 = new List<int>();
- for (int j = 0; j < num2; j++)
- {
- list3.Add(j);
- int[] array3 = list[j];
- array2[j] = new List<int[]>();
- array2[j].Add(new int[2]
- {
- array3[0],
- array3[1]
- });
- }
- Mat mat2 = new Mat(Img.Size(), MatType.CV_32SC1);
- array[0] = 0;
- while (array[0] < Img.Rows)
- {
- array[1] = 0;
- while (array[1] < Img.Cols)
- {
- int value = Img.At<byte>(array[0], array[1]);
- mat2.Set(array, value);
- array[1]++;
- }
- array[0]++;
- }
- int[,] array4 = new int[260, 2]
- {
- {
- -1,
- 0
- },
- {
- 0,
- 1
- },
- {
- 1,
- 0
- },
- {
- 0,
- 1
- },
- {
- -1,
- 1
- },
- {
- 1,
- 1
- },
- {
- 1,
- -1
- },
- {
- -1,
- -1
- },
- {
- -2,
- 0
- },
- {
- 0,
- 2
- },
- {
- 2,
- 0
- },
- {
- 0,
- -2
- },
- {
- -2,
- 1
- },
- {
- -1,
- 2
- },
- {
- 1,
- 2
- },
- {
- 2,
- 1
- },
- {
- 2,
- -1
- },
- {
- 1,
- -2
- },
- {
- -1,
- -2
- },
- {
- -2,
- -1
- },
- {
- -2,
- 2
- },
- {
- 2,
- 2
- },
- {
- 2,
- -2
- },
- {
- -2,
- -2
- },
- {
- -3,
- 0
- },
- {
- 0,
- 3
- },
- {
- 3,
- 0
- },
- {
- 0,
- -3
- },
- {
- -3,
- 1
- },
- {
- -1,
- 3
- },
- {
- 1,
- 3
- },
- {
- 3,
- 1
- },
- {
- 3,
- -1
- },
- {
- 1,
- -3
- },
- {
- -1,
- -3
- },
- {
- -3,
- -1
- },
- {
- 2,
- 3
- },
- {
- -3,
- 2
- },
- {
- -2,
- -3
- },
- {
- -2,
- 3
- },
- {
- -3,
- -2
- },
- {
- 3,
- 2
- },
- {
- 3,
- -2
- },
- {
- 2,
- -3
- },
- {
- 4,
- 0
- },
- {
- 0,
- 4
- },
- {
- -4,
- 0
- },
- {
- 0,
- -4
- },
- {
- 1,
- 4
- },
- {
- 4,
- -1
- },
- {
- 4,
- 1
- },
- {
- -1,
- -4
- },
- {
- -4,
- 1
- },
- {
- -4,
- -1
- },
- {
- -1,
- 4
- },
- {
- 1,
- -4
- },
- {
- 3,
- 3
- },
- {
- 3,
- -3
- },
- {
- -3,
- 3
- },
- {
- -3,
- -3
- },
- {
- -4,
- -2
- },
- {
- -2,
- -4
- },
- {
- -4,
- 2
- },
- {
- 4,
- 2
- },
- {
- -2,
- 4
- },
- {
- 4,
- -2
- },
- {
- 2,
- -4
- },
- {
- 2,
- 4
- },
- {
- 4,
- -3
- },
- {
- -3,
- 4
- },
- {
- 4,
- 3
- },
- {
- 5,
- 0
- },
- {
- 0,
- 5
- },
- {
- 0,
- -5
- },
- {
- -5,
- 0
- },
- {
- 3,
- 4
- },
- {
- -3,
- -4
- },
- {
- -4,
- -3
- },
- {
- 3,
- -4
- },
- {
- -4,
- 3
- },
- {
- 5,
- -1
- },
- {
- -1,
- 5
- },
- {
- -1,
- -5
- },
- {
- 5,
- 1
- },
- {
- 1,
- -5
- },
- {
- -5,
- 1
- },
- {
- -5,
- -1
- },
- {
- 1,
- 5
- },
- {
- -2,
- 5
- },
- {
- 2,
- -5
- },
- {
- 2,
- 5
- },
- {
- 5,
- 2
- },
- {
- -5,
- 2
- },
- {
- -2,
- -5
- },
- {
- 5,
- -2
- },
- {
- -5,
- -2
- },
- {
- -4,
- 4
- },
- {
- 4,
- -4
- },
- {
- 4,
- 4
- },
- {
- -4,
- -4
- },
- {
- 3,
- -5
- },
- {
- -5,
- 3
- },
- {
- -3,
- 5
- },
- {
- 5,
- -3
- },
- {
- 5,
- 3
- },
- {
- -5,
- -3
- },
- {
- -3,
- -5
- },
- {
- 3,
- 5
- },
- {
- 0,
- 6
- },
- {
- 0,
- -6
- },
- {
- -6,
- 0
- },
- {
- 6,
- 0
- },
- {
- -6,
- 1
- },
- {
- 1,
- 6
- },
- {
- -6,
- -1
- },
- {
- 1,
- -6
- },
- {
- -1,
- 6
- },
- {
- -1,
- -6
- },
- {
- 6,
- -1
- },
- {
- 6,
- 1
- },
- {
- 2,
- 6
- },
- {
- -2,
- -6
- },
- {
- 6,
- 2
- },
- {
- -6,
- 2
- },
- {
- 2,
- -6
- },
- {
- -6,
- -2
- },
- {
- -2,
- 6
- },
- {
- 6,
- -2
- },
- {
- -5,
- -4
- },
- {
- 5,
- 4
- },
- {
- 4,
- 5
- },
- {
- -4,
- 5
- },
- {
- 4,
- -5
- },
- {
- -5,
- 4
- },
- {
- 5,
- -4
- },
- {
- -4,
- -5
- },
- {
- -3,
- 6
- },
- {
- -6,
- 3
- },
- {
- -3,
- -6
- },
- {
- -6,
- -3
- },
- {
- 3,
- -6
- },
- {
- 6,
- 3
- },
- {
- 3,
- 6
- },
- {
- 6,
- -3
- },
- {
- 0,
- 7
- },
- {
- 7,
- 0
- },
- {
- 0,
- -7
- },
- {
- -7,
- 0
- },
- {
- 5,
- -5
- },
- {
- 1,
- 7
- },
- {
- -1,
- 7
- },
- {
- -5,
- -5
- },
- {
- -7,
- -1
- },
- {
- -7,
- 1
- },
- {
- -1,
- -7
- },
- {
- 1,
- -7
- },
- {
- 7,
- 1
- },
- {
- -5,
- 5
- },
- {
- 5,
- 5
- },
- {
- 7,
- -1
- },
- {
- 6,
- 4
- },
- {
- -6,
- 4
- },
- {
- 6,
- -4
- },
- {
- -6,
- -4
- },
- {
- -4,
- -6
- },
- {
- 4,
- 6
- },
- {
- 4,
- -6
- },
- {
- -4,
- 6
- },
- {
- 7,
- -2
- },
- {
- 7,
- 2
- },
- {
- 2,
- 7
- },
- {
- -7,
- -2
- },
- {
- -2,
- -7
- },
- {
- -2,
- 7
- },
- {
- 2,
- -7
- },
- {
- -7,
- 2
- },
- {
- 7,
- -3
- },
- {
- -3,
- 7
- },
- {
- -3,
- -7
- },
- {
- 3,
- -7
- },
- {
- -7,
- 3
- },
- {
- 3,
- 7
- },
- {
- 7,
- 3
- },
- {
- -7,
- -3
- },
- {
- 6,
- -5
- },
- {
- 5,
- 6
- },
- {
- 5,
- -6
- },
- {
- 6,
- 5
- },
- {
- -5,
- 6
- },
- {
- -5,
- -6
- },
- {
- -6,
- 5
- },
- {
- -6,
- -5
- },
- {
- 8,
- 0
- },
- {
- 0,
- -8
- },
- {
- -8,
- 0
- },
- {
- 0,
- 8
- },
- {
- -4,
- 7
- },
- {
- 4,
- -7
- },
- {
- -8,
- -1
- },
- {
- -1,
- 8
- },
- {
- -7,
- 4
- },
- {
- -1,
- -8
- },
- {
- 7,
- 4
- },
- {
- 1,
- -8
- },
- {
- 1,
- 8
- },
- {
- 8,
- 1
- },
- {
- 8,
- -1
- },
- {
- -4,
- -7
- },
- {
- -7,
- -4
- },
- {
- 7,
- -4
- },
- {
- -8,
- 1
- },
- {
- 4,
- 7
- },
- {
- -2,
- 8
- },
- {
- 2,
- -8
- },
- {
- -2,
- -8
- },
- {
- 8,
- 2
- },
- {
- -8,
- 2
- },
- {
- -8,
- -2
- },
- {
- 2,
- 8
- },
- {
- 8,
- -2
- },
- {
- -6,
- 6
- },
- {
- 6,
- 6
- },
- {
- 6,
- -6
- },
- {
- -6,
- -6
- },
- {
- 3,
- -8
- },
- {
- -3,
- 8
- },
- {
- 8,
- 3
- },
- {
- 8,
- -3
- },
- {
- 3,
- 8
- },
- {
- -3,
- -8
- },
- {
- -8,
- 3
- },
- {
- -8,
- -3
- },
- {
- 7,
- -5
- },
- {
- -7,
- -5
- },
- {
- 5,
- 7
- },
- {
- 7,
- 5
- },
- {
- 5,
- -7
- },
- {
- -7,
- 5
- },
- {
- -5,
- -7
- },
- {
- -5,
- 7
- },
- {
- 8,
- -4
- },
- {
- -4,
- -8
- },
- {
- -8,
- -4
- },
- {
- 8,
- 4
- },
- {
- 4,
- 8
- },
- {
- -8,
- 4
- },
- {
- 4,
- -8
- },
- {
- -4,
- 8
- },
- {
- 0,
- -9
- },
- {
- 0,
- 9
- },
- {
- -9,
- 0
- },
- {
- 9,
- 0
- },
- {
- 9,
- -1
- },
- {
- -9,
- -1
- },
- {
- 1,
- -9
- },
- {
- -1,
- 9
- },
- {
- -1,
- -9
- },
- {
- -9,
- 1
- },
- {
- 1,
- 9
- },
- {
- 9,
- 1
- }
- };
- while (list3.Count() != 0)
- {
- int num3 = list3[0];
- list3.RemoveAt(0);
- int index = array2[num3].Count() - 1;
- int[] array5 = array2[num3][index];
- int[] pnt = new int[2]
- {
- array5[0],
- array5[1]
- };
- int[] array6 = list2[num3];
- bool flag2 = false;
- for (int k = 0; k < iDistance; k++)
- {
- if (0 > array6[0] * array4[k, 0] || 0 > array6[1] * array4[k, 1])
- {
- continue;
- }
- pnt[0] = array5[0] + array4[k, 0];
- pnt[1] = array5[1] + array4[k, 1];
- if (0 > pnt[0] || Img.Rows <= pnt[0] || 0 > pnt[1] || Img.Cols <= pnt[1])
- {
- continue;
- }
- int num4 = mat2.At<int>(pnt[0], pnt[1]);
- if (1 < num4 && num3 != num4)
- {
- list3.Remove(num4);
- if (!list4.Contains(num4))
- {
- int num5 = array2[num4].FindIndex((int[] t) => t[0] == pnt[0] && t[1] == pnt[1]);
- int num6 = array2[num4].Count() - 1;
- if (1 <= num6 - num5)
- {
- for (int l = num5 + 1; l <= num6; l++)
- {
- mat2.Set(array2[num4][l], 0);
- }
- array2[num4].RemoveRange(num5 + 1, num6 - num5);
- }
- list4.Add(num4);
- list4.Add(num3);
- }
- ConnectTwoPnt(array5, pnt, array2[num3]);
- flag2 = true;
- break;
- }
- if (1 == num4)
- {
- ConnectTwoPnt(array5, pnt, array2[num3]);
- flag2 = true;
- break;
- }
- }
- if (!flag2)
- {
- pnt[0] = array5[0] + array6[0];
- pnt[1] = array5[1] + array6[1];
- if (0 <= pnt[0] && Img.Rows > pnt[0] && 0 <= pnt[1] && Img.Cols > pnt[1])
- {
- array2[num3].Add(pnt);
- mat2.Set(pnt, num3);
- list3.Add(num3);
- }
- }
- }
- for (int m = 0; m < num2; m++)
- {
- array2[m].ForEach(delegate (int[] aiP)
- {
- Img.Set(aiP, (byte)1);
- });
- }
- Cv2.Threshold(Img, Img, 0.0, 255.0, ThresholdTypes.Binary);
- }
- public void ConnectTwoPnt(int[] pntA, int[] pntB, List<int[]> DevelopmentA)
- {
- int num = pntA[0];
- int num2 = pntA[1];
- int num3 = pntB[0] - num;
- int num4 = pntB[1] - num2;
- int num5 = (num3 != 0) ? (num3 / Math.Abs(num3)) : 0;
- int num6 = (num4 != 0) ? (num4 / Math.Abs(num4)) : 0;
- while (num3 != 0 || num4 != 0)
- {
- if (Math.Abs(num3) > Math.Abs(num4))
- {
- num += num5;
- }
- else
- {
- num2 += num6;
- }
- DevelopmentA.Add(new int[2]
- {
- num,
- num2
- });
- num3 = pntB[0] - num;
- num4 = pntB[1] - num2;
- }
- }
- public Mat Connectivity(Mat Img)
- {
- Mat mat = new Mat(Img.Size(), MatType.CV_8UC1);
- int[] array = new int[2]
- {
- 1,
- 0
- };
- while (array[0] < Img.Rows - 1)
- {
- array[1] = 0;
- while (array[1] < Img.Cols)
- {
- byte b = Img.At<byte>(array[0] - 1, array[1]);
- b = (byte)(b + Img.At<byte>(array[0], array[1]));
- b = (byte)(b + Img.At<byte>(array[0] + 1, array[1]));
- mat.Set(array, b);
- array[1]++;
- }
- array[0]++;
- }
- Mat mat2 = new Mat(Img.Size(), MatType.CV_8UC1);
- array[0] = 1;
- while (array[0] < Img.Rows - 1)
- {
- array[1] = 1;
- while (array[1] < Img.Cols - 1)
- {
- byte b2 = mat.At<byte>(array[0], array[1] - 1);
- b2 = (byte)(b2 + mat.At<byte>(array[0], array[1]));
- b2 = (byte)(b2 + mat.At<byte>(array[0], array[1] + 1));
- b2 = (byte)(b2 - Img.At<byte>(array[0], array[1]));
- mat2.Set(array, b2);
- array[1]++;
- }
- array[0]++;
- }
- mat = null;
- return mat2;
- }
- private int FindShed(Vec4b value)
- {
- Mat mat = new Mat(m_InParam.MValley.Size(), 4);
- Cv2.FindContours(m_InParam.MValley, out OpenCvSharp.Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple);
- for (int i = 0; i < hierarchy.Length; i++)//# 相同的条件,这里比demo慢?
- {
- Cv2.DrawContours(mat, contours, i, Scalar.All(i + 1), 1, LineTypes.Link8, hierarchy);
- }
- Cv2.Watershed(m_MOrgImg, mat);
- int rows = mat.Rows;
- int cols = mat.Cols;
- int[] array = new int[2];
- //Vec3b value = new Vec3b(0, 0, byte.MaxValue);
- array[0] = 0;
- while (array[0] < rows)
- {
- array[1] = 0;
- while (array[1] < cols)
- {
- if (0 >= mat.At<int>(array[0], array[1]))
- {
- m_MOutImg.Set(array, value);
- }
- array[1]++;
- }
- array[0]++;
- }
- return 1;
- }
- private int FindValley()
- {
- Mat structuringElement = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(m_InParam.iDimA, m_InParam.iDimA), new OpenCvSharp.Point(-1, -1));
- Cv2.MorphologyEx(m_InParam.MHesOutImage, m_InParam.MHesOutImage, MorphTypes.Close, (InputArray)structuringElement);
- Cv2.BitwiseNot(m_InParam.MHesOutImage, m_InParam.MValley);
- Size size = m_InParam.MValley.Size();
- Mat mat = (Mat)Mat.Zeros(size.Height + 2, size.Width + 2, m_InParam.MValley.Type());
- m_InParam.MValley.CopyTo(mat.SubMat(new Range(1, size.Height + 1), new Range(1, size.Width + 1)));
- Cv2.FloodFill(mat, new OpenCvSharp.Point(0, 0), new Scalar(255.0));
- Mat mat2 = new Mat();
- mat.SubMat(new Range(1, size.Height + 1), new Range(1, size.Width + 1)).CopyTo(mat2);
- Cv2.BitwiseNot(mat2, mat2);
- m_InParam.MValley = (Mat)(m_InParam.MValley | mat2);
- return 1;
- }
- private int PrepareParameter()
- {
- m_InParam = new InParameter();
- m_InParam.MGryImg = new Mat();
- if (3 == m_MOrgImg.Channels())
- {
- Cv2.CvtColor(m_MOrgImg, m_InParam.MGryImg, ColorConversionCodes.BGR2GRAY/*RGB2GRAY*/);
- }
- else
- {
- if (1 != m_MOrgImg.Channels())
- {
- return -1;
- }
- m_InParam.MGryImg = m_MOrgImg.Clone();
- }
- m_InParam.iMethod = 1;
- m_InParam.iDimA = 3;
- m_InParam.iDimB = 1;
- bool flag = false;
- int num = 0;
- double num2 = 1.0;
- double num3 = 1.0;
- double num4 = 0.0;
- m_InParam.iXiHua = 0;
- m_InParam.iLianJie = 0;
- if (m_adOutParam != null)
- {
- if (m_adOutParam[0] > 1.5 && m_adOutParam[0] < 2.5)
- {
- m_InParam.iMethod = 2;
- }
- if (m_adOutParam[1] > 1.5 && m_adOutParam[1] < 2.5)
- {
- flag = true;
- }
- if (m_adOutParam[2] > 0.5 && m_adOutParam[2] < 100.5)
- {
- m_InParam.iDimA = (int)(m_adOutParam[2] + 0.5);
- }
- if (m_adOutParam[3] > 0.5 && m_adOutParam[3] < 100.5)
- {
- m_InParam.iDimB = (int)(m_adOutParam[3] + 0.5);
- }
- if (m_adOutParam[4] > 1.5 && m_adOutParam[4] < 2.5)
- {
- num = 2;
- num2 = Math.Max(m_adOutParam[5], 1.0);
- }
- else if (m_adOutParam[4] > 2.5 && m_adOutParam[4] < 3.5)
- {
- num = 3;
- num2 = Math.Max(m_adOutParam[5], 1.0);
- }
- else if (m_adOutParam[4] > 3.5 && m_adOutParam[4] < 4.5)
- {
- num = 4;
- num3 = m_adOutParam[6];
- num4 = m_adOutParam[7];
- }
- if (m_adOutParam[8] > 1.5 && m_adOutParam[8] < 2.5)
- {
- m_InParam.iXiHua = 2;
- }
- if (m_adOutParam[9] > 1.5 && m_adOutParam[9] < 2.5)
- {
- m_InParam.iLianJie = 2;
- }
- }
- if (flag || num > 1)
- {
- int rows = m_InParam.MGryImg.Rows;
- int cols = m_InParam.MGryImg.Cols;
- int[] array = new int[2]
- {
- 0,
- 0
- };
- while (array[0] < rows)
- {
- array[1] = 0;
- while (array[1] < cols)
- {
- byte b = m_InParam.MGryImg.At<byte>(array);
- if (flag)
- {
- b = (byte)(255 - b);
- }
- if (2 == num)
- {
- b = (byte)((double)(int)b / num2 + 0.5);
- }
- if (3 == num)
- {
- double num5 = 255.0 - (double)(int)b;
- num5 /= num2;
- b = (byte)(255.0 - num5 + 0.5);
- }
- if (4 == num)
- {
- double val = num3 * (double)(int)b + num4;
- val = Math.Min(Math.Max(val, 0.0), 255.0);
- b = (byte)(val + 0.5);
- }
- m_InParam.MGryImg.Set(array, b);
- array[1]++;
- }
- array[0]++;
- }
- }
- m_InParam.MHesOutImage = new Mat();
- m_InParam.MValley = new Mat();
- m_InParam.MEdgeOutImage = new Mat();
- return 1;
- }
- private int ImgHessian()
- {
- Mat mat = new Mat();
- m_InParam.MGryImg.ConvertTo(mat, 5);
- int rows = mat.Rows;
- int cols = mat.Cols;
- int num = 5;
- double num2 = 1.2;
- Mat mat2 = new Mat(2 * num + 1, 2 * num + 1, MatType.CV_32FC1, Scalar.All(0.0));
- Mat mat3 = new Mat(2 * num + 1, 2 * num + 1, MatType.CV_32FC1, Scalar.All(0.0));
- Mat mat4 = new Mat(2 * num + 1, 2 * num + 1, MatType.CV_32FC1, Scalar.All(0.0));
- for (int i = -num; i <= num; i++)
- {
- for (int j = -num; j <= num; j++)
- {
- double num3 = 1.0 - (double)(i * i) / (num2 * num2);
- double num4 = Math.Exp((double)(-1 * (i * i + j * j)) / (2.0 * num2 * num2));
- double num5 = -1.0 / (Math.PI * 2.0 * Math.Pow(num2, 4.0));
- double num6 = num3 * num4 * num5;
- float value = (float)num6;
- mat2.Set(i + num, j + num, value);
- mat4.Set(i + num, j + num, (float)((1.0 - (double)(j * j) / (num2 * num2)) * Math.Exp((double)(-1 * (i * i + j * j)) / (2.0 * num2 * num2)) * (-1.0 / (Math.PI * 2.0 * Math.Pow(num2, 4.0)))));
- mat3.Set(i + num, j + num, (float)((double)(i * j) * Math.Exp((double)(-1 * (i * i + j * j)) / (2.0 * num2 * num2)) * (1.0 / (Math.PI * 2.0 * Math.Pow(num2, 6.0)))));
- }
- }
- Mat mat5 = new Mat(rows, cols, MatType.CV_32FC1, Scalar.All(0.0));
- Mat mat6 = new Mat(rows, cols, MatType.CV_32FC1, Scalar.All(0.0));
- Mat mat7 = new Mat(rows, cols, MatType.CV_32FC1, Scalar.All(0.0));
- Cv2.Filter2D(mat, mat5, mat5.Depth(), mat2);//# 相同的条件,这里比demo慢
- Cv2.Filter2D(mat, mat6, mat6.Depth(), mat4);
- Cv2.Filter2D(mat, mat7, mat7.Depth(), mat3);
- byte[,] array = new byte[rows, cols];
- float[] array2 = new float[rows * cols];
- float[] array3 = new float[rows * cols];
- float[] array4 = new float[rows * cols];
- Marshal.Copy(mat5.Data, array2, 0, rows * cols);
- Marshal.Copy(mat6.Data, array3, 0, rows * cols);
- Marshal.Copy(mat7.Data, array4, 0, rows * cols);
- for (int k = 0; k < rows; k++)
- {
- for (int l = 0; l < cols; l++)
- {
- float num7 = array2[k * cols + l];
- float num8 = array3[k * cols + l];
- float num9 = array4[k * cols + l];
- double num10 = (num7 + num8) * (num7 + num8) - 4f * (num7 * num8 - num9 * num9);
- if (!(num10 < 0.0))
- {
- num10 = Math.Sqrt(num10);
- double num11 = ((double)(num7 + num8) + num10) / 2.0;
- double value2 = ((double)(num7 + num8) - num10) / 2.0;
- if (num11 > 0.0 && Math.Abs(num11) > 1.0 + Math.Abs(value2))
- {
- array[k, l] = byte.MaxValue;
- }
- }
- }
- }
- m_InParam.MHesOutImage = new Mat(rows, cols, MatType.CV_8UC1, array, 0L);
- return 0;
- }
- }
- /////
- /////
- class OUTsegmsurf
- {
- public Mat cellbw;
- public Mat wat;
- public Mat imsegm;
- public Mat minima;
- public Mat minimacell;
- //kkkkk public NEW_TYPE_SegInf info;
- }
- class In5segmsurf
- {
- public string smoothim_method;
- public int filterridges;
- public double gaussian_stdev;
- public string LEVEL;
- public double classifycells_convexarea;
- public double classifycells_convexperim;
- public Mat OrgImg;
- public In5segmsurf Clone()
- {
- return this.MemberwiseClone() as In5segmsurf;
- }
- }
- class In6segmsurf
- {
- public string prmfile;//Path to parameter file
- public Mat minima;//All markers, for instance from manual drawings
- public Mat minimacell;//; for cells, for instance from manual drawings
- public Mat imnucl;//Nucleus image for defining markers
- }
- class PMergefragments//区域合并
- {
- public int optlog;
- public int optint;
- public double intint;
- public double conv;
- public PMergefragments Clone()
- {
- return this.MemberwiseClone() as PMergefragments;
- }
- }
- class PSmoothim//平滑
- {
- public string method;
- public double[] h;
- public int gpu;
- public int dim;
- public int ced_maxniter;
- public PSmoothim Clone()
- {
- return this.MemberwiseClone() as PSmoothim;
- }
- }
- class PClassifycells//区域分类
- {
- public double convexarea;
- public double convexperim;
- public double[] h;
- public double minvolfull;
- public double maxvolfull;
- public string method;
- public PClassifycells Clone()
- {
- return this.MemberwiseClone() as PClassifycells;
- }
- }
- class In4smoothim
- {
- public int filterridges;
- public string LEVEL;
- public double gaussian_stdev;
- public double[] getminima_h;
- public double getminima_minvolfull;
- public double getminima_maxvolfull;
- public double getminima_level;
- //public string getminima_method;
- public PMergefragments mergefragments;
- public PSmoothim smoothim;
- public PClassifycells classifycells;
- public int[] dim;
- public double minvol;
- public double minvolvox;
- public double maxvol;
- public double maxvolvox;
- public double watminvolfull;
- public double watmaxvolfull;
- public double minvolfull;
- public double maxvolfull;
- public double[] h;
- public double just;
- public int gpu;
- public int illum;
- public double illumdiameter;
- public int merge;
- public int iInterpolationMethod;//zzzzz new, 1 for Cubic spline interpolation, 2 for Bilinear interpolation, 3 for Nearest neighbor interpolation
- }
- class OUTgetminima
- {
- public Mat minimacell;
- public Mat minima;
- }
- class OUTclassifycells
- {
- public Mat cellbw;
- public PClassifycells classifycells;
- }
- class OUTmaxfilt3
- {
- public List<float>[,] maxim;
- public float[,] sumim;
- public int numpoints;
- public float[][][] minim;
- }
- class MyFilter
- {
- public double[] x;
- public double[] y;
- public double[] z;
- }
- class OLDcellsegm
- {
- public const double m_PI = 3.1415926535897931;
- void mf_mergeinputpar(In4smoothim prm, In5segmsurf prmin)
- {
- prm.smoothim.method = prmin.smoothim_method;
- prm.filterridges = prmin.filterridges;
- prm.gaussian_stdev = prmin.gaussian_stdev;
- prm.LEVEL = (string)prmin.LEVEL.Clone();
- prm.classifycells.convexarea = prmin.classifycells_convexarea;
- prm.classifycells.convexperim = prmin.classifycells_convexperim;
- }
- void mf_cellsize(double minvol, double maxvol, In4smoothim prm, int ThreeDimNO)
- {
- if (0 == prm.h[0] * prm.h[1] * prm.h[2])
- {
- Console.WriteLine("There is zero voxel size");
- }
- double minr = Math.Pow((3 * minvol) / (4 * m_PI), 1 / 3);
- //maxr = ((3*maxvol)/(4*pi))^(1/3);
- //maxz = maxr/h(3);
- double minz = minr / prm.h[2];
- if (ThreeDimNO < minz)
- {
- //shrinink a sphere to a circle
- double r = Math.Pow((3 * maxvol) / (4 * m_PI), 1 / 3);
- maxvol = Math.Pow(ThreeDimNO, prm.just) * m_PI * r * r;
- r = Math.Pow((3 * minvol) / (4 * m_PI), 1 / 3);
- minvol = Math.Pow(ThreeDimNO, prm.just) * m_PI * r * r;
- }
- double voxelvol = prm.h[0] * prm.h[1] * prm.h[2];
- double minvolvox = Math.Round(minvol / voxelvol, MidpointRounding.AwayFromZero);
- double maxvolvox = Math.Round(maxvol / voxelvol, MidpointRounding.AwayFromZero);
- prm.minvol = minvol;
- prm.minvolvox = minvolvox;
- prm.maxvol = maxvol;
- prm.maxvolvox = maxvolvox;
- }
- public OUTsegmsurf mf_segmsurf_progress_auto(Mat GryImg, Color phaseColor, int adOutParam0, int adOutParam1, int adOutParam2, int adOutParam3, int adOutParam4, double adOutParam5, int adOutParam6)
- {
- long start = Cv2.GetTickCount();
- OUTsegmsurf rtn = new OUTsegmsurf();
- //ttttt Cv2.FindContours(watermark, out contour, out hier, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple, null);
- rtn.wat = new Mat(GryImg.Size(), MatType.CV_8UC4, new Scalar(phaseColor.B, phaseColor.G, phaseColor.R, 0));
- //Mat OutImg = new Mat(GryImg.Rows, GryImg.Cols, MatType.CV_8UC3, Scalar.All(0));
- NewSeg NSeg = new NewSeg();
- //转为为灰度?
- if (/*3 == GryImg.Channels() || */4 == GryImg.Channels())
- {
- Cv2.CvtColor(GryImg, GryImg, ColorConversionCodes.BGRA2BGR);
- }
- else if (1 == GryImg.Channels())
- {
- Cv2.CvtColor(GryImg, GryImg, ColorConversionCodes.GRAY2BGR);
- }
- //Cv2.ImShow("oriImg", GryImg);
- //Cv2.WaitKey();
- double time_v2 = (Cv2.GetTickCount() - start) / Cv2.GetTickFrequency();
- System.Console.WriteLine("晶界重现_V2_执行时间1:" + time_v2);
- double[] adOutParam = new double[10];
- adOutParam[0] = adOutParam0;// 2;//方法1原方法,方法2模拟软件方法
- adOutParam[1] = adOutParam1;// 0;//默认为暗晶界,为2时为亮晶界
- adOutParam[2] = adOutParam2;// 3;//尺度系数,默认为3
- adOutParam[3] = adOutParam3;// 1;//晶界宽度系数,默认为1
- adOutParam[4] = adOutParam4;// 0;//亮度调整,为0不调整,为1自动调整,为2整体调暗,为3整体调亮,为4线性调整
- adOutParam[5] = adOutParam5;// 1;//adOutParam[4]为2或3时adOutParam[5]有意义,表示调整强度,必须大于1,等于1无变化
- adOutParam[6] = adOutParam6==0 ? 1 : 0;// 1;//adOutParam[4]为4时adOutParam[6]有意义,线性变换斜率。
- adOutParam[7] = adOutParam6 == 1 ? 1 : 0;// 0;//adOutParam[4]为4时adOutParam[7]有意义,线性变换截距。
- adOutParam[8] = 0;//为2时表示边界必须细化
- adOutParam[9] = 0;//为2时表示边界必须连接
- NSeg.DoSeg(GryImg.Clone()/*OrgImg*/, rtn.wat, adOutParam, new Vec4b(phaseColor.B, phaseColor.G, phaseColor.R, 255));
- ////ttttt Cv2.DrawContours(rtn.wat, contour, -1, Scalar.Red);
- //int Rows = OutImg.Rows;
- //int Cols = OutImg/*watermark*/.Cols;
- //int[] point = new int[2];
- //Vec4b redcol;
- //if (phaseColor != null)
- //{
- // redcol = new Vec4b(phaseColor.B, phaseColor.G, phaseColor.R, 255);
- //}
- //else
- // redcol = new Vec4b(0, 0, 255/*255*/, 255);
- //for (point[0] = 0; point[0] < Rows; point[0]++)
- //{
- // for (point[1] = 0; point[1] < Cols; point[1]++)
- // {
- // if (0 >= watermark.At<int>(point[0], point[1]))
- // {
- // rtn.wat.Set<Vec4b>(point[0], point[1], redcol);//###
- // }
- // }
- //}
- time_v2 = (Cv2.GetTickCount() - start) / Cv2.GetTickFrequency();
- System.Console.WriteLine("晶界重现_V2_执行时间2:" + time_v2);
- ////Cv2.ImShow("contour", rtn.wat);//kkkkk
- ////Cv2.WaitKey(0);//kkkkk
- //control.Cursor = cursor;// System.Windows.Forms.Cursors.Default;
- return rtn;
- }
-
- OUTclassifycells classifycells(Mat watermark, Mat imsegmini, PClassifycells prmclf, Mat minimacell = null)
- {
- OUTclassifycells rtn = new OUTclassifycells();
- return rtn;
- }
- OUTgetminima getminima(Mat imsegm, Mat minimacell, In5segmsurf prmin)
- {
- OUTgetminima rtn = new OUTgetminima();
- rtn.minima = null;
- rtn.minimacell = null;
- if (null == minimacell)
- {
- rtn.minima = automated(imsegm, prmin);
- }
- return rtn;
- }
- Mat automated(Mat imsegm, In5segmsurf prmin)
- {
- Mat mat_mean = new Mat(), mat_stddev = new Mat();
- double m, s, d;
- //Cv2.MeanStdDev(imsegm, mat_mean, mat_stddev);
- //m = mat_mean.At<double>(0, 0);
- //s = mat_stddev.At<double>(0, 0);
- //d = 0;//mmmmm
- double th;
- //th = Math.Min(Math.Max(m+d*s, m/2),1.5*m);
- Mat minima = new Mat(), minimacell = new Mat(imsegm.Size(), MatType.CV_8UC3);
- //Cv2.Threshold(imsegm, minimacell, th, 255, ThresholdTypes.Binary);
- int iRad = 39;
- Mat matTempA = new Mat();
- Mat matTempB = new Mat();
- Cv2.MedianBlur(imsegm, matTempA, iRad);
- Cv2.AddWeighted(imsegm, 1, matTempA, -1, 0, matTempB);
- Cv2.MeanStdDev(matTempB, mat_mean, mat_stddev);
- m = mat_mean.At<double>(0, 0);
- s = mat_stddev.At<double>(0, 0);
- d = 0;//mmmmm
- th = m + d * s;
- Cv2.Threshold(matTempB, minimacell, th, 255, ThresholdTypes.Binary);
- int diam;//mmmmm
- Mat kernel;
- diam = 9;
- kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new Size(diam, diam), new OpenCvSharp.Point(-1, -1));
- Cv2.Dilate(minimacell, minimacell, kernel);
- diam = 3;
- kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(diam, diam), new OpenCvSharp.Point(-1, -1));
- Cv2.Erode(minimacell, minimacell, kernel);
- Cv2.MorphologyEx(minimacell, minimacell, MorphTypes.Open, kernel);
- //zzzzz Cv2.ImShow("bianjie", minimacell);
- //zzzzz Cv2.WaitKey(0);
- Cv2.BitwiseNot(minimacell, minima);
- Size bianjieSize = minima.Size();
- Mat Temp = Mat.Zeros(bianjieSize.Height + 2, bianjieSize.Width + 2, minimacell.Type());//延展图像
- minima.CopyTo(Temp.SubMat(new Range(1, bianjieSize.Height + 1), new Range(1, bianjieSize.Width + 1)));
- Cv2.FloodFill(Temp, new OpenCvSharp.Point(0, 0), new Scalar(255));
- Mat cutImg = new Mat();
- Temp.SubMat(new Range(1, bianjieSize.Height + 1), new Range(1, bianjieSize.Width + 1)).CopyTo(cutImg);
- Cv2.BitwiseNot(cutImg, cutImg);
- minima = minima | cutImg;
- diam = 9;
- kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(diam, diam), new OpenCvSharp.Point(-1, -1));
- Cv2.MorphologyEx(minima, minima, MorphTypes.Open, kernel);
- //zzzzz Cv2.ImShow("neibu", minima);//kkkkk
- //zzzzz Cv2.WaitKey(0);//kkkkk
- return minima;
- }
- Mat smoothim(Mat im, string method, string prmflg, In4smoothim prm)
- {
- //diameter of filter
- int iPrmD = 13;
- Mat rst = new Mat();
- Cv2.ConvertScaleAbs(dircohenh(im, iPrmD, prm.h, prm.iInterpolationMethod), rst);
- return rst;
- }
- Mat dircohenh(Mat im, int iPrmD, double[] adPrmH, int iInterpolationMethod)
- {
- int[] dim = new int[3] { im.Rows, im.Cols, 1 };
- if (3 == im.Dims)
- {
- dim[2] = im.Height;
- }
- //number of elements to remove before one takes the Olympic average. Set to
- //1-->3
- int numremele = 2;
- int stepxy = 30;
- int numdeghere = (180 - stepxy) / stepxy + 1;
- int[] deghere = new int[numdeghere];
- deghere[0] = 0;
- for (int i = 1; i < numdeghere; i++)
- {
- deghere[i] = deghere[i - 1] + stepxy;
- }
- double[,] deg2D = new double[numdeghere, 2];
- for (int i = 1; i < numdeghere; i++)
- {
- deg2D[i, 0] = (double)(deghere[i]);
- deg2D[i, 1] = 90;
- }
- double[,] deg;
- deg = deg2D;
- if (1 != dim[2])
- {
- double[,] deg3D = new double[numdeghere + 1, 2];
- for (int i = 1; i < numdeghere; i++)
- {
- deg3D[i, 0] = deg2D[i, 0];
- deg3D[i, 1] = deg2D[i, 1];
- }
- deg3D[numdeghere, 0] = 0;
- deg3D[numdeghere, 1] = 0;
- deg = deg3D;
- }
- //the half filter size
- int p = iPrmD / 2;
- //make filter
- MyFilter[] c = makefilter(deg, p);
- //number of directions
- int numdir = c.Length;
- //inisization
- Mat filtim = new Mat(im.Rows, im.Cols, MatType.CV_32F, 0.0);
- //zzzzz
- //201118 for Acceleration Mat imRs = new Mat();
- Mat imRs = null;
- float[] afImRs = null;
- int iProportion = 5;
- if (1 == iInterpolationMethod)
- {
- imRs = new Mat();//201118 for Acceleration
- Mat imFt = new Mat();
- im.ConvertTo(imFt, MatType.CV_32F);
- Cv2.Resize(imFt, imRs, new Size(imFt.Cols * iProportion, imFt.Rows * iProportion), 0, 0, InterpolationFlags.Cubic);
- //afImRs = new float[imRs.Rows, imRs.Cols];
- afImRs = new float[imRs.Rows * imRs.Cols];
- Marshal.Copy(imRs.Data, afImRs, 0, imRs.Rows * imRs.Cols);
- /*
- for (int i = 0; i < imRs.Rows; i++)
- {
- for (int j = 0; j < imRs.Cols; j++)
- {
- afImRs[i, j] = imRs.At<float>(i, j);
- }
- */
- imRs = null;
- }
- for (int i = 0; i < numdir; i++)
- {
- //coordinates of points in this direction after rotation of filter
- MyFilter chere = c[i];
- //filter image for max value
- //zzzzz
- OUTmaxfilt3 fltrst;
- if (1 == iInterpolationMethod)
- {
- //201118 for Acceleration fltrst = maxfilt3ByCubicSpline(im, chere, numremele/*, dim*/, imRs, iProportion);
- fltrst = maxfilt3ByCubicSpline(im, chere, numremele/*, dim*/, afImRs, iProportion);
- }
- else if (2 == iInterpolationMethod)
- {
- fltrst = maxfilt3ByBilinear(im, chere, numremele/*, dim*/);
- }
- else
- {
- fltrst = maxfilt3ByNearest(im, chere, numremele/*, dim*/);
- }
- //structural filtering
- //filtim = max(filtim,(sumim - sum(maxim,4))/ (numpoints-numremele));
- int[] point = new int[2];
- for (point[0] = 0; point[0] < filtim.Rows; point[0]++)
- {
- for (point[1] = 0; point[1] < filtim.Cols; point[1]++)
- {
- float fTemp = filtim.At<float>(point[0], point[1]);
- /*201118 for Acceleration
- fTemp = Math.Max(fTemp,
- (fltrst.sumim[point[0], point[1]] - (fltrst.maxim[point[0], point[1]]).Sum()) / (fltrst.numpoints - numremele)
- );
- */
- //(fltrst.minim[point[0], point[1]]).Sum()
- fTemp = fTemp = Math.Max(fTemp, (fltrst.minim[point[0]][point[1]].Take<float>(fltrst.numpoints - numremele)).Sum() / (fltrst.numpoints - numremele));
- filtim.Set<float>(point, fTemp);
- }
- }
- }
- return filtim;
- }
- OUTmaxfilt3 maxfilt3ByNearest(Mat im, MyFilter chere, int numremele/*, int[] dim*/)
- {
- /////Mat imRs = new Mat();
- /////im.ConvertTo(imRs, MatType.CV_32F);
- int BigRows = im.Rows;
- int BigCols = im.Cols;
- OUTmaxfilt3 rst = new OUTmaxfilt3();
- rst.sumim = new float[im.Rows, im.Cols];
- rst.maxim = new List<float>[im.Rows, im.Cols];
- for (int i = 0; i < im.Rows; i++)
- {
- for (int j = 0; j < im.Cols; j++)
- {
- (rst.sumim)[i, j] = 0;
- List<float> lisTmp = new List<float>();
- for (int k = 0; k < numremele; k++)
- {
- lisTmp.Add(0);
- }
- (rst.maxim)[i, j] = lisTmp;
- }
- }
- rst.numpoints = chere.x.Length;
- for (int i = 0; i < rst.numpoints; i++)
- {
- double xhere;
- double yhere;
- int[] point = new int[2];
- for (point[0] = 0; point[0] < im.Rows; point[0]++)
- {
- for (point[1] = 0; point[1] < im.Cols; point[1]++)
- {
- xhere = (point[0] + (chere.x)[i]);
- yhere = (point[1] + (chere.y)[i]);
- int iX = Math.Min(Math.Max((int)(xhere + 0.5), 0), BigRows - 1);
- int iY = Math.Min(Math.Max((int)(yhere + 0.5), 0), BigCols - 1);
- float fTemp = im.At<byte>(iX, iY);
- (rst.sumim)[point[0], point[1]] += fTemp;
- (rst.maxim)[point[0], point[1]].Add(fTemp);
- (rst.maxim)[point[0], point[1]].Sort();
- (rst.maxim)[point[0], point[1]].RemoveAt(0);
- (rst.maxim)[point[0], point[1]].Reverse();
- }
- }
- }
- return rst;
- }
- OUTmaxfilt3 maxfilt3ByBilinear(Mat im, MyFilter chere, int numremele/*, int[] dim*/)
- {
- /////Mat imRs = new Mat();
- /////im.ConvertTo(imRs, MatType.CV_32F);
- int BigRows = im.Rows;
- int BigCols = im.Cols;
- OUTmaxfilt3 rst = new OUTmaxfilt3();
- rst.sumim = new float[im.Rows, im.Cols];
- rst.maxim = new List<float>[im.Rows, im.Cols];
- for (int i = 0; i < im.Rows; i++)
- {
- for (int j = 0; j < im.Cols; j++)
- {
- (rst.sumim)[i, j] = 0;
- List<float> lisTmp = new List<float>();
- for (int k = 0; k < numremele; k++)
- {
- lisTmp.Add(0);
- }
- (rst.maxim)[i, j] = lisTmp;
- }
- }
- rst.numpoints = chere.x.Length;
- for (int i = 0; i < rst.numpoints; i++)
- {
- double xhere;
- double yhere;
- int[] point = new int[2];
- for (point[0] = 0; point[0] < im.Rows; point[0]++)
- {
- for (point[1] = 0; point[1] < im.Cols; point[1]++)
- {
- xhere = point[0] + (chere.x)[i];
- yhere = point[1] + (chere.y)[i];
- int iXS = Math.Min(Math.Max((int)(xhere), 0), BigRows - 1);
- int iYS = Math.Min(Math.Max((int)(yhere), 0), BigCols - 1);
- int iXB = Math.Min(Math.Max((int)(xhere + 1.0), 0), BigRows - 1);
- int iYB = Math.Min(Math.Max((int)(yhere + 1.0), 0), BigCols - 1);
- double dWXB = Math.Min(Math.Max((xhere - (double)iXS), 0.0), 1.0);
- double dWXS = 1.0 - dWXB;
- double dWYB = Math.Min(Math.Max((yhere - (double)iYS), 0.0), 1.0);
- double dWYS = 1.0 - dWYB;
- double dTempA = im.At<byte>(iXS, iYS);
- double dTempB = im.At<byte>(iXS, iYB);
- double dTempC = im.At<byte>(iXB, iYS);
- double dTempD = im.At<byte>(iXB, iYB);
- double dTempAB = dTempA * dWYS + dTempB * dWYB;
- double dTempCD = dTempC * dWYS + dTempD * dWYB;
- float fTemp = (float)(dTempAB * dWXS + dTempCD * dWXB);
- (rst.sumim)[point[0], point[1]] += fTemp;
- (rst.maxim)[point[0], point[1]].Add(fTemp);
- (rst.maxim)[point[0], point[1]].Sort();
- (rst.maxim)[point[0], point[1]].RemoveAt(0);
- (rst.maxim)[point[0], point[1]].Reverse();
- }
- }
- }
- return rst;
- }
- //201118 for Acceleration OUTmaxfilt3 maxfilt3ByCubicSpline(Mat im, MyFilter chere, int numremele/*, int[] dim*/, Mat imRs, int iProportion)
- OUTmaxfilt3 maxfilt3ByCubicSpline(Mat im, MyFilter chere, int numremele, float[] afImRs, int iProportion)
- {
- /////Mat imFt = new Mat();
- /////Mat imRs = new Mat();
- /////im.ConvertTo(imFt, MatType.CV_32F);
- /////int iProportion = 5;
- /////Cv2.Resize(imFt, imRs, new Size(imFt.Cols * iProportion, imFt.Rows * iProportion), 0, 0, InterpolationFlags.Cubic);
- //201118 for Acceleration int BigRows = imRs.Rows;
- //201118 for Acceleration int BigCols = imRs.Cols;
- int BigRows = im.Rows * iProportion;
- int BigCols = im.Cols * iProportion;
- /*
- double[,] x = new double[dim[0], dim[1]];
- double[,] y = new double[dim[0], dim[1]];
- for (int i = 0; i < dim[0]; i++)
- {
- for (int j = 0; j < dim[1]; j++)
- {
- x[i, j] = (double)i;
- y[i, j] = (double)j;
- }
- }
- */
- OUTmaxfilt3 rst = new OUTmaxfilt3();
- //rst.maxim = new Mat[numremele];
- //201118 for Acceleration rst.sumim = new float[im.Rows, im.Cols];
- //201118 for Acceleration rst.maxim = new List<float>[im.Rows, im.Cols];
- rst.numpoints = chere.x.Length;
- rst.minim = new float[im.Rows][][];
- for (int i = 0; i < im.Rows; i++)
- {
- rst.minim[i] = new float[im.Cols][];
- for (int j = 0; j < im.Cols; j++)
- {
- rst.minim[i][j] = new float[rst.numpoints];
- //201118 for Acceleration (rst.sumim)[i, j] = 0;
- //201118 for Acceleration List<float> lisTmp = new List<float>();
- //201118 for Acceleration for (int k = 0; k < numremele; k++)
- //201118 for Acceleration {
- //201118 for Acceleration lisTmp.Add(0);
- //201118 for Acceleration }
- //201118 for Acceleration (rst.maxim)[i, j] = lisTmp;
- //201118 for Acceleration List<float> lisTmpMin = new List<float>();
- //201118 for Acceleration (rst.minim)[i, j] = lisTmpMin;
- }
- }
- //Mat imhere = new Mat(imFt.Rows, imFt.Cols, MatType.CV_32F, 0.0);
- /* 201118 for Acceleration
- for (int i = 0; i < rst.numpoints; i++)
- {
- //the coordinate where we want to find the values
- //NB!!!!!!!!!
- //Must not do this for z direction, then it goes wrong.
- //Do NOOOOOT replacestr any Inf or NaN by image values, then the filter
- //does not work anymore!!!!!!
- //xhere(xhere < h(1)) = h(1);xhere(xhere > dim(1)*h(1)) = dim(1)*h(1);
- //yhere(yhere < h(1)) = h(1);yhere(yhere > dim(2)*h(1)) = dim(2)*h(2);
- //double[,] xhere = new double[dim[0], dim[1]];
- //double[,] yhere = new double[dim[0], dim[1]];
- double xhere;
- double yhere;
- int[] point = new int[2];
- for (point[0] = 0; point[0] < im.Rows; point[0]++)
- {
- for (point[1] = 0; point[1] < im.Cols; point[1]++)
- {
- //xhere[i, j] = Math.Min(Math.Max(x[i, j]+(chere.x)[t],1),dim[0]);
- //yhere[i, j] = Math.Min(Math.Max(y[i, j]+(chere.y)[t],1),dim[1]);
- xhere = (point[0] + (chere.x)[i]) * iProportion;
- yhere = (point[1] + (chere.y)[i]) * iProportion;
- int iX = Math.Min(Math.Max((int)(xhere + 0.5), 0), BigRows - 1);
- int iY = Math.Min(Math.Max((int)(yhere + 0.5), 0), BigCols - 1);
- float fTemp = imRs.At<float>(iX, iY);
- //imhere.Set<float>(point, fTemp);
- (rst.sumim)[point[0], point[1]] += fTemp;
- (rst.maxim)[point[0], point[1]].Add(fTemp);
- (rst.maxim)[point[0], point[1]].Sort();
- (rst.maxim)[point[0], point[1]].RemoveAt(0);
- (rst.maxim)[point[0], point[1]].Reverse();
- }
- }
- }
- */
- for (int ir = 0; ir < im.Rows; ir++)
- {
- for (int ic = 0; ic < im.Cols; ic++)
- {
- double xhere;
- double yhere;
- for (int ip = 0; ip < rst.numpoints; ip++)
- {
- xhere = (ir + (chere.x)[ip]) * iProportion;
- yhere = (ic + (chere.y)[ip]) * iProportion;
- int iX = Math.Min(Math.Max((int)(xhere + 0.5), 0), BigRows - 1);
- int iY = Math.Min(Math.Max((int)(yhere + 0.5), 0), BigCols - 1);
- //201118 for Acceleration float fTemp = imRs.At<float>(iX, iY);
- float fTemp = afImRs[iX * BigCols + iY];
- //201118 for Acceleration (rst.sumim)[ir, ic] += fTemp;
- //201118 for Acceleration (rst.maxim)[ir, ic].Add(fTemp);
- (rst.minim)[ir][ic][ip] = fTemp;
- }
- //201118 for Acceleration (rst.maxim)[ir, ic].Sort((x, y) => -x.CompareTo(y));
- //201118 for Acceleration (rst.maxim)[ir, ic].RemoveRange(numremele, rst.numpoints - numremele);
- Array.Sort((rst.minim)[ir][ic]);
- //201118 for Acceleration (rst.minim)[ir, ic].Sort();
- //201118 for Acceleration (rst.minim)[ir, ic].RemoveRange(rst.numpoints - numremele,numremele);
- }
- }
- return rst;
- }
- MyFilter[] makefilter(double[,] deg, int p)
- {
- int iNum = deg.GetLength(0);
- MyFilter[] c = new MyFilter[iNum];
- //r = linspace(-p,p,7);
- int iArrLength = 7;
- double[] r = new double[iArrLength];
- double dStep = p * 2.0 / (iArrLength - 1.0);
- for (int i = 0; i < 7; i++)
- {
- r[i] = -1.0 * p + i * dStep;
- }
- /*
- for i = 1 : size(deg,1)
- % the degrees
- theta = deg(i,1);
- phi = deg(i,2);
- % the positions
- c(i).x = r*sind(phi)*cosd(theta);
- c(i).y = r*sind(phi)*sind(theta);
- c(i).z = r*cosd(phi);
- end;
- */
- for (int i = 0; i < iNum; i++)
- {
- double theta = deg[i, 0];
- double phi = deg[i, 1];
- c[i] = new MyFilter();
- c[i].x = new double[iArrLength];
- c[i].y = new double[iArrLength];
- c[i].z = new double[iArrLength];
- for (int j = 0; j < iArrLength; j++)
- {
- (c[i].x)[j] = r[j] * Math.Sin(phi) * Math.Cos(theta); ;
- (c[i].y)[j] = r[j] * Math.Sin(phi) * Math.Sin(theta); ;
- (c[i].z)[j] = r[j] * Math.Cos(phi);
- }
- }
- return c;
- }
- public Mat ProFroStandardImage(Mat GryImg, int iConnect, Color phaseColor)
- {
- //Mat GryImg = new Mat();
- ////转为为灰度?
- //if (3 == OrgImg.Channels())
- //{
- // Cv2.CvtColor(OrgImg, GryImg, ColorConversionCodes.RGB2GRAY);
- //}
- //else
- //{
- // if (1 == OrgImg.Channels())
- // {
- // GryImg = OrgImg.Clone();
- // }
- // else
- // {
- // return null;
- // }
- //}
- Mat CrystalImg = new Mat();
- Mat BoundaryImg = new Mat();
- Mat mat_mean = new Mat(), mat_stddev = new Mat();
- double m, s, d = 0.5;
- Cv2.MeanStdDev(GryImg, mat_mean, mat_stddev);
- m = mat_mean.At<double>(0, 0);
- s = mat_stddev.At<double>(0, 0);
- //Cv2.Threshold(GryImg, CrystalImg, (int)Math.Min(m+d*s,230), 255, ThresholdTypes.Binary);
- //Cv2.Threshold(GryImg, CrystalImg, 0, 1, ThresholdTypes.Otsu);
- Cv2.Threshold(GryImg, CrystalImg, m, 255, ThresholdTypes.Binary);
- //Cv2.ImShow("src", CrystalImg);//kkkkk
- //Cv2.WaitKey(0);//kkkkk
- int diam = 3;//mmmmm
- Mat kernel = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(diam, diam), new OpenCvSharp.Point(-1, -1));
- //Cv2.Erode(CrystalImg, CrystalImg, kernel);
- Cv2.MorphologyEx(CrystalImg, CrystalImg, MorphTypes.Open, kernel);
- Cv2.BitwiseNot(CrystalImg, BoundaryImg);
- //kernel = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(diam, diam), new Point(-1, -1));
- //Cv2.MorphologyEx(BoundaryImg, BoundaryImg, MorphTypes.Open, kernel);
- //Cv2.MorphologyEx(BoundaryImg, BoundaryImg, MorphTypes.Close, kernel);
- //Cv2.Dilate(BoundaryImg, BoundaryImg, kernel);
- Class12 TCAlgo = new Class12();
- TCAlgo.ImgThin(BoundaryImg);
- //string filename = "D:\\temp.jpg";
- //Cv2.ImWrite(filename, BoundaryImg);
- if (1 == iConnect)
- {
- TCAlgo.ConnectionBoundary(BoundaryImg, 108);
- }
- //Cv2.ImShow("wai", BoundaryImg);//kkkkk
- //Cv2.WaitKey(0);//kkkkk
- //Cv2.BitwiseNot(BoundaryImg, CrystalImg);
- //diam = 9;
- //kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new Size(diam, diam), new Point(-1, -1));
- //Cv2.MorphologyEx(BoundaryImg, BoundaryImg, MorphTypes.Close, kernel);
- //Cv2.Erode(CrystalImg, CrystalImg, kernel);
- //Cv2.MorphologyEx(CrystalImg, CrystalImg, MorphTypes.Open, kernel);
- //Cv2.MorphologyEx(CrystalImg, CrystalImg, MorphTypes.Close, kernel);
- //Cv2.ImShow("nei", CrystalImg);//kkkkk
- //Cv2.WaitKey(0);//kkkkk
- /*
- Mat watermark = new Mat(CrystalImg.Size(), MatType.CV_32S);
- Point[][] contour;
- HierarchyIndex[] hier;
- Cv2.FindContours(CrystalImg, out contour, out hier, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple, null);
- for (int i = 0; i < hier.Length; i++)
- {
- Cv2.DrawContours(watermark, contour, i, Scalar.All(i + 1), 1, LineTypes.Link8, hier);
- }
- Cv2.Watershed(OrgImg, watermark);
- BoundaryImg = new Mat(watermark.Size(), MatType.CV_8UC3);
- //ttttt Cv2.DrawContours(rtn.wat, contour, -1, Scalar.Red);
- */
- /*
- Mat NewWat = new Mat();
- Cv2.BitwiseNot(CrystalImg, NewWat);
- Cv2.ImShow("bian", NewWat);//kkkkk
- Cv2.WaitKey(0);//kkkkk
- ImgThin(NewWat);
- int Rows = NewWat.Rows;
- int Cols = NewWat.Cols;
- */
- int[] point = new int[2];
- Vec4b redcol;
- if (phaseColor != null)
- {
- redcol = new Vec4b(phaseColor.B, phaseColor.G, phaseColor.R, 255);
- }
- else
- redcol = new Vec4b(0, 0, 255/*255*/, 255);
- Mat WAT = new Mat(BoundaryImg.Size(), MatType.CV_8UC4);
- for (point[0] = 0; point[0] < WAT.Rows; point[0]++)
- {
- for (point[1] = 0; point[1] < WAT.Cols; point[1]++)
- {
- if (0 < BoundaryImg.At<byte>(point[0], point[1]))
- {
- WAT.Set<Vec4b>(point, redcol);
- }
- }
- }
- return WAT;
- }
- }
- }
|