Ceju.cs 732 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OpenCvSharp;
  7. using SmartCoalApplication.Base.CommTool;
  8. namespace SmartCoalApplication.Base.AutoMeasure
  9. {
  10. public class Ceju
  11. {
  12. /// <summary>
  13. /// 对图像进行二值化,得到产品目标区域
  14. /// </summary>
  15. /// <param name="image">单通道图像,一般是红色通道</param>
  16. /// <param name="imageContour">输出二值图</param>
  17. public void GetContour(Mat image, out Mat imageContour)
  18. {
  19. // 均值滤波
  20. Mat imageFilter = new Mat();
  21. Cv2.Blur(image, imageFilter, new OpenCvSharp.Size(5, 5));
  22. // 二值化,获得目标区域
  23. imageContour = imageFilter.Threshold(0, 1, ThresholdTypes.Otsu);
  24. //new Window("contour", WindowMode.Normal, imageContour * 255);
  25. // 闭运算,消除小孔
  26. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 10));// 结构元素
  27. Cv2.MorphologyEx(imageContour, imageContour, MorphTypes.Close, se);
  28. Mat fillContour = new Mat();
  29. Fill(imageContour, out fillContour, 1);
  30. //new Window("close", WindowMode.Normal, imageContour * 255);
  31. //new Window("fill", WindowMode.Normal, fillContour * 255);
  32. //Cv2.WaitKey();
  33. Mat fanse = 1 - fillContour;
  34. Scalar sum = fanse.Sum();
  35. Scalar sumBefore = imageContour.Sum();
  36. Scalar sumAfter = fillContour.Sum();
  37. int areaDifference = (int)sumAfter - (int)sumBefore;
  38. if ((int)sum != 0)//避免整张图全填充
  39. {
  40. if (areaDifference < 100)//避免填充区域过多
  41. imageContour = fillContour;
  42. }
  43. #region[清理内存]
  44. //if (imageFilter != null)
  45. //{
  46. // imageFilter.Dispose();
  47. //}
  48. //if (fillContour != null)
  49. //{
  50. // fillContour.Dispose();
  51. //}
  52. #endregion
  53. }
  54. public void GetContourTongkongShuangceng(Mat image, out Mat imageContour)
  55. {
  56. // 均值滤波
  57. Mat imageFilter = new Mat();
  58. Cv2.Blur(image, imageFilter, new OpenCvSharp.Size(5, 5));
  59. // 二值化,获得目标区域
  60. imageContour = imageFilter.Threshold(0, 1, ThresholdTypes.Otsu);
  61. //new Window("contour", WindowMode.Normal, imageContour * 255);
  62. // 开运算,消除杂质,闭运算,消除小孔
  63. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 10));// 结构元素
  64. Cv2.MorphologyEx(imageContour, imageContour, MorphTypes.Open, se);
  65. Cv2.MorphologyEx(imageContour, imageContour, MorphTypes.Close, se);
  66. Mat fillContour = new Mat();
  67. Fill(imageContour, out fillContour, 1);
  68. //new Window("close", WindowMode.Normal, imageContour * 255);
  69. //new Window("fill", WindowMode.Normal, fillContour * 255);
  70. //Cv2.WaitKey();
  71. Mat fanse = 1 - fillContour;
  72. Scalar sum = fanse.Sum();
  73. Scalar sumBefore = imageContour.Sum();
  74. Scalar sumAfter = fillContour.Sum();
  75. int areaDifference = (int)sumAfter - (int)sumBefore;
  76. if ((int)sum != 0)//避免整张图全填充
  77. {
  78. if (areaDifference < 100)//避免填充区域过多
  79. imageContour = fillContour;
  80. }
  81. }
  82. public void GetShenmangkongContour(Mat image, out Mat imageContour)
  83. {
  84. // 均值滤波
  85. Mat imageFilter = new Mat();
  86. Cv2.Blur(image, imageFilter, new OpenCvSharp.Size(5, 5));
  87. // 二值化,获得目标区域
  88. imageContour = imageFilter.Threshold(0, 1, ThresholdTypes.Otsu);
  89. //膨胀
  90. Mat seDilate = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(20, 1));
  91. Mat dilate = new Mat();
  92. Cv2.Dilate(imageContour, dilate, seDilate);
  93. // 闭运算,消除小孔
  94. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));// 结构元素
  95. Cv2.MorphologyEx(dilate, imageContour, MorphTypes.Close, se);
  96. Mat fillContour = new Mat();
  97. Fill(imageContour, out fillContour, 1);
  98. //new Window("fill", WindowMode.Normal, fillContour * 255);
  99. //Cv2.WaitKey();
  100. Mat fanse = 1 - fillContour;
  101. Scalar sum = fanse.Sum();
  102. Scalar sumBefore = imageContour.Sum();
  103. Scalar sumAfter = fillContour.Sum();
  104. int areaDifference = (int)sumAfter - (int)sumBefore;
  105. if ((int)sum != 0)//避免整张图全填充
  106. {
  107. if (areaDifference < 2000)//避免填充区域过多
  108. imageContour = fillContour;
  109. }
  110. }
  111. /// <summary>
  112. /// 对图片进行上下裁剪,去掉空白的部分
  113. /// </summary>
  114. /// <param name="image">输入二值图像</param>
  115. /// <param name="y">输出去掉的上下边界</param>
  116. /// <param name="cropContour">输出裁剪后的图片</param>
  117. public void Crop(Mat image, out int[] y, out Mat cropContour, bool isCropFlag)
  118. {
  119. int range = 300;
  120. if (isCropFlag)
  121. range = 15;
  122. y = new int[2] { 0, 0 };
  123. for (int i = range; i < image.Rows; i++)
  124. {
  125. for (int j = 0; j < image.Cols; j++)
  126. {
  127. if (image.Get<byte>(i, j) > 0)
  128. {
  129. y[0] = i;
  130. break;
  131. }
  132. }
  133. if (y[0] != 0)
  134. break;
  135. }
  136. for (int i = image.Rows - range; i > range; i--)
  137. {
  138. for (int j = 0; j < image.Cols; j++)
  139. {
  140. if (image.Get<byte>(i, j) > 0)
  141. {
  142. y[1] = i;
  143. break;
  144. }
  145. }
  146. if (y[1] != 0)
  147. break;
  148. }
  149. cropContour = image[y[0], y[1], 0, image.Cols - 1];
  150. }
  151. /// <summary>
  152. /// 对全图片进行裁剪,去掉上下空白的地方,左右被亮光影响的地方
  153. /// </summary>
  154. /// <param name="image">原图</param>
  155. /// <param name="cropEdge">上下裁剪后边缘图,用作提取孔铜</param>
  156. /// <param name="y">裁剪的上下边界</param>
  157. /// <param name="b">用来裁剪槽孔区域的左右边界</param>
  158. public void Crop2(Mat image, out Mat cropEdge, out int[] y, out int[] b, bool isCropFlag)
  159. {
  160. //获得蓝色,绿色,红色通道图片
  161. Mat[] bgr = Cv2.Split(image);
  162. Mat imageBlue = bgr[0];
  163. Mat imageGreen = bgr[1];
  164. Mat imageRed = bgr[2];
  165. // 调色
  166. Mat imageNew = 0.9 * imageGreen + 0.1 * imageRed;
  167. //滤波
  168. Mat filter = new Mat();
  169. Cv2.BilateralFilter(imageNew, filter, 15, 150, 3);
  170. // 增强
  171. Mat zengqiang = new Mat();
  172. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  173. Cv2.Filter2D(filter, zengqiang, -1, kernel);
  174. Cv2.ConvertScaleAbs(zengqiang, zengqiang);
  175. //边缘检测
  176. Mat grad_x2 = new Mat();
  177. Sobel(zengqiang, out grad_x2);
  178. //开运算
  179. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));// 结构元素
  180. Mat open = new Mat();
  181. Cv2.MorphologyEx(grad_x2, open, MorphTypes.Open, seOpen);
  182. ////閉運算
  183. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));// 结构元素
  184. Mat close = new Mat();
  185. Cv2.MorphologyEx(open, close, MorphTypes.Close, se);
  186. Mat thresh2 = close.Threshold(0, 1, ThresholdTypes.Otsu);
  187. //填充
  188. Mat fill = new Mat();
  189. Fill(thresh2, out fill, 1);
  190. //new Window("grad", WindowMode.Normal, grad_x2.Threshold(0, 255, ThresholdTypes.Otsu));
  191. //new Window("open", WindowMode.Normal, open);
  192. //new Window("close", WindowMode.Normal, close);
  193. //Cv2.WaitKey();
  194. Ceju ceju = new Ceju();
  195. y = new int[2];
  196. ceju.Crop(fill, out y, out fill, isCropFlag);//用来计算左右裁剪边界
  197. //Mat edge = grad_x2.Threshold(0, 1, ThresholdTypes.Otsu);
  198. cropEdge = thresh2[y[0], y[1], 0, thresh2.Cols - 1];//用来计算孔铜边缘
  199. b = new int[4] { 0, 0, 0, 0 };
  200. int sum = 0;
  201. int range = 100;
  202. if (isCropFlag)
  203. range = 15;
  204. for (int j = range; j < fill.Cols - 1; j++)
  205. {
  206. for (int i = 0; i < fill.Rows - 1; i++)
  207. {
  208. if (fill.Get<byte>(i, j) > 0)
  209. {
  210. b[0] = j;
  211. break;
  212. }
  213. }
  214. if (b[0] != 0)
  215. break;
  216. }
  217. for (int j = b[0] + 100; j < fill.Cols - 1; j++)
  218. {
  219. sum = 0;
  220. for (int i = 0; i < fill.Rows - 1; i++)
  221. {
  222. if (fill.Get<byte>(i, j) > 0)
  223. {
  224. sum = 1;
  225. break;
  226. }
  227. }
  228. if (sum == 0)
  229. {
  230. b[1] = j;
  231. break;
  232. }
  233. }
  234. if (b[1] - b[0] < 300)
  235. {
  236. b[0] = 0;
  237. for (int j = b[1] + 50; j < fill.Cols - 1; j++)
  238. {
  239. for (int i = 0; i < fill.Rows - 1; i++)
  240. {
  241. if (fill.Get<byte>(i, j) > 0)
  242. {
  243. b[0] = j;
  244. break;
  245. }
  246. }
  247. if (b[0] != 0)
  248. break;
  249. }
  250. for (int j = b[0] + 50; j < fill.Cols - 1; j++)
  251. {
  252. sum = 0;
  253. for (int i = 0; i < fill.Rows - 1; i++)
  254. {
  255. if (fill.Get<byte>(i, j) > 0)
  256. {
  257. sum = 1;
  258. break;
  259. }
  260. }
  261. if (sum == 0)
  262. {
  263. b[1] = j;
  264. break;
  265. }
  266. }
  267. }
  268. for (int j = fill.Cols - range; j > 0; j--)
  269. {
  270. for (int i = 0; i < fill.Rows - 1; i++)
  271. {
  272. if (fill.Get<byte>(i, j) > 0)
  273. {
  274. b[3] = j;
  275. break;
  276. }
  277. }
  278. if (b[3] != 0)
  279. break;
  280. }
  281. for (int j = b[3] - 100; j > 0; j--)
  282. {
  283. sum = 0;
  284. for (int i = 0; i < fill.Rows - 1; i++)
  285. {
  286. if (fill.Get<byte>(i, j) > 0)
  287. {
  288. sum = 1;
  289. break;
  290. }
  291. }
  292. if (sum == 0)
  293. {
  294. b[2] = j;
  295. break;
  296. }
  297. }
  298. if (b[3] - b[2] < 300)
  299. {
  300. b[3] = 0;
  301. for (int j = b[2] - 50; j > 0; j--)
  302. {
  303. for (int i = 0; i < fill.Rows - 1; i++)
  304. {
  305. if (fill.Get<byte>(i, j) > 0)
  306. {
  307. b[3] = j;
  308. break;
  309. }
  310. }
  311. if (b[3] != 0)
  312. break;
  313. }
  314. for (int j = b[3] - 50; j > 0; j--)
  315. {
  316. sum = 0;
  317. for (int i = 0; i < fill.Rows - 1; i++)
  318. {
  319. if (fill.Get<byte>(i, j) > 0)
  320. {
  321. sum = 1;
  322. break;
  323. }
  324. }
  325. if (sum == 0)
  326. {
  327. b[2] = j;
  328. break;
  329. }
  330. }
  331. }
  332. }
  333. /// <summary>
  334. /// 用于四层孔径的裁剪,减小了左右裁剪的判定标准,其他同Crop2
  335. /// </summary>
  336. /// <param name="image"></param>
  337. /// <param name="cropEdge"></param>
  338. /// <param name="y"></param>
  339. /// <param name="b"></param>
  340. public void CropSicengKongjing(Mat image, out Mat cropEdge, out int[] y, out int[] b, bool isCropFlag)
  341. {
  342. //获得蓝色,绿色,红色通道图片
  343. Mat[] bgr = Cv2.Split(image);
  344. Mat imageBlue = bgr[0];
  345. Mat imageGreen = bgr[1];
  346. Mat imageRed = bgr[2];
  347. // 调色
  348. Mat imageNew = 0.9 * imageGreen + 0.1 * imageRed;
  349. //滤波
  350. Mat filter = new Mat();
  351. Cv2.BilateralFilter(imageNew, filter, 15, 150, 3);
  352. // 增强
  353. Mat zengqiang = new Mat();
  354. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  355. Cv2.Filter2D(filter, zengqiang, -1, kernel);
  356. Cv2.ConvertScaleAbs(zengqiang, zengqiang);
  357. //边缘检测
  358. Mat grad_x2 = new Mat();
  359. Sobel(zengqiang, out grad_x2);
  360. //开运算
  361. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));// 结构元素
  362. Mat open = new Mat();
  363. Cv2.MorphologyEx(grad_x2, open, MorphTypes.Open, seOpen);
  364. ////閉運算
  365. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));// 结构元素
  366. Mat close = new Mat();
  367. Cv2.MorphologyEx(open, close, MorphTypes.Close, se);
  368. Mat thresh2 = close.Threshold(0, 1, ThresholdTypes.Otsu);
  369. //填充
  370. Mat fill = new Mat();
  371. Fill(thresh2, out fill, 1);
  372. Ceju ceju = new Ceju();
  373. y = new int[2];
  374. ceju.Crop(fill, out y, out fill, isCropFlag);//用来计算左右裁剪边界
  375. //Mat edge = grad_x2.Threshold(0, 1, ThresholdTypes.Otsu);
  376. cropEdge = thresh2[y[0], y[1], 0, thresh2.Cols - 1];//用来计算孔铜边缘
  377. b = new int[4] { 0, 0, 0, 0 };
  378. int sum = 0;
  379. int range = 100;
  380. if (isCropFlag)
  381. range = 15;
  382. for (int j = range; j < fill.Cols - 1; j++)
  383. {
  384. for (int i = 0; i < fill.Rows - 1; i++)
  385. {
  386. if (fill.Get<byte>(i, j) > 0)
  387. {
  388. b[0] = j;
  389. break;
  390. }
  391. }
  392. if (b[0] != 0)
  393. break;
  394. }
  395. for (int j = b[0] + 100; j < fill.Cols - 1; j++)
  396. {
  397. sum = 0;
  398. for (int i = 0; i < fill.Rows - 1; i++)
  399. {
  400. if (fill.Get<byte>(i, j) > 0)
  401. {
  402. sum = 1;
  403. break;
  404. }
  405. }
  406. if (sum == 0)
  407. {
  408. b[1] = j;
  409. break;
  410. }
  411. }
  412. if (b[1] - b[0] < 50)
  413. {
  414. b[0] = 0;
  415. for (int j = b[1] + 50; j < fill.Cols - 1; j++)
  416. {
  417. for (int i = 0; i < fill.Rows - 1; i++)
  418. {
  419. if (fill.Get<byte>(i, j) > 0)
  420. {
  421. b[0] = j;
  422. break;
  423. }
  424. }
  425. if (b[0] != 0)
  426. break;
  427. }
  428. for (int j = b[0] + 50; j < fill.Cols - 1; j++)
  429. {
  430. sum = 0;
  431. for (int i = 0; i < fill.Rows - 1; i++)
  432. {
  433. if (fill.Get<byte>(i, j) > 0)
  434. {
  435. sum = 1;
  436. break;
  437. }
  438. }
  439. if (sum == 0)
  440. {
  441. b[1] = j;
  442. break;
  443. }
  444. }
  445. }
  446. for (int j = fill.Cols - range; j > 0; j--)
  447. {
  448. for (int i = 0; i < fill.Rows - 1; i++)
  449. {
  450. if (fill.Get<byte>(i, j) > 0)
  451. {
  452. b[3] = j;
  453. break;
  454. }
  455. }
  456. if (b[3] != 0)
  457. break;
  458. }
  459. for (int j = b[3] - 100; j > 0; j--)
  460. {
  461. sum = 0;
  462. for (int i = 0; i < fill.Rows - 1; i++)
  463. {
  464. if (fill.Get<byte>(i, j) > 0)
  465. {
  466. sum = 1;
  467. break;
  468. }
  469. }
  470. if (sum == 0)
  471. {
  472. b[2] = j;
  473. break;
  474. }
  475. }
  476. if (b[3] - b[2] < 50)
  477. {
  478. b[3] = 0;
  479. for (int j = b[2] - 50; j > 0; j--)
  480. {
  481. for (int i = 0; i < fill.Rows - 1; i++)
  482. {
  483. if (fill.Get<byte>(i, j) > 0)
  484. {
  485. b[3] = j;
  486. break;
  487. }
  488. }
  489. if (b[3] != 0)
  490. break;
  491. }
  492. for (int j = b[3] - 50; j > 0; j--)
  493. {
  494. sum = 0;
  495. for (int i = 0; i < fill.Rows - 1; i++)
  496. {
  497. if (fill.Get<byte>(i, j) > 0)
  498. {
  499. sum = 1;
  500. break;
  501. }
  502. }
  503. if (sum == 0)
  504. {
  505. b[2] = j;
  506. break;
  507. }
  508. }
  509. }
  510. }
  511. /// <summary>
  512. /// 去掉槽孔的亮光,并裁剪
  513. /// </summary>
  514. /// <param name="cropContour">上下裁剪后的图片</param>
  515. /// <param name="border">输出裁剪后的边界</param>
  516. /// <param name="cropContour2">输出裁剪后的图片</param>
  517. /// <param name="direction">选择槽孔的左右位置,left/right</param>
  518. public void CropLight(Mat cropContour, out int border, out Mat cropContour2, string direction)
  519. {
  520. int[] b = new int[4] { 0, 0, 0, 0 };
  521. int sum = 0;
  522. border = 0;
  523. cropContour2 = new Mat();
  524. switch (direction)
  525. {
  526. case "left":
  527. for (int j = 0; j < cropContour.Cols; j++)
  528. {
  529. for (int i = 0; i < cropContour.Rows; i++)
  530. {
  531. if (cropContour.Get<byte>(i, j) > 0)
  532. {
  533. b[0] = j;
  534. break;
  535. }
  536. }
  537. if (b[0] != 0)
  538. break;
  539. }
  540. for (int j = b[0]; j < cropContour.Cols; j++)
  541. {
  542. sum = 0;
  543. for (int i = 0; i < cropContour.Rows; i++)
  544. {
  545. sum += cropContour.Get<byte>(i, j);
  546. }
  547. if (sum == 0)
  548. {
  549. b[1] = j;
  550. break;
  551. }
  552. }
  553. for (int j = cropContour.Cols - 1; j > b[1]; j--)
  554. {
  555. for (int i = 0; i < cropContour.Rows; i++)
  556. {
  557. if (cropContour.Get<byte>(i, j) > 0)
  558. {
  559. b[3] = j;
  560. break;
  561. }
  562. }
  563. if (b[3] != 0)
  564. break;
  565. }
  566. for (int j = b[3]; j > b[1]; j--)
  567. {
  568. sum = 0;
  569. for (int i = 0; i < cropContour.Rows; i++)
  570. {
  571. sum += cropContour.Get<byte>(i, j);
  572. }
  573. if (sum == 0)
  574. {
  575. b[2] = j;
  576. break;
  577. }
  578. }
  579. if (b[1] - b[0] > 500)
  580. {
  581. if (Math.Abs(b[2] - b[1]) > 50)
  582. {
  583. cropContour2 = cropContour[0, cropContour.Rows - 1, b[0], b[1]];
  584. border = b[0];
  585. }
  586. else
  587. cropContour2 = cropContour[0, cropContour.Rows - 1, 0, cropContour.Cols - 1];
  588. }
  589. else
  590. {
  591. cropContour2 = cropContour[0, cropContour.Rows - 1, 0, b[3]];
  592. border = 0;
  593. }
  594. break;
  595. case "right":
  596. for (int j = cropContour.Cols - 1; j > 0; j--)
  597. {
  598. for (int i = 0; i < cropContour.Rows; i++)
  599. {
  600. if (cropContour.Get<byte>(i, j) > 0)
  601. {
  602. b[3] = j;
  603. break;
  604. }
  605. }
  606. if (b[3] != 0)
  607. break;
  608. }
  609. for (int j = b[3]; j > 0; j--)
  610. {
  611. sum = 0;
  612. for (int i = 0; i < cropContour.Rows; i++)
  613. {
  614. sum += cropContour.Get<byte>(i, j);
  615. }
  616. if (sum == 0)
  617. {
  618. b[2] = j;
  619. break;
  620. }
  621. }
  622. for (int j = 0; j < b[2]; j++)
  623. {
  624. for (int i = 0; i < cropContour.Rows; i++)
  625. {
  626. if (cropContour.Get<byte>(i, j) > 0)
  627. {
  628. b[0] = j;
  629. break;
  630. }
  631. }
  632. if (b[0] != 0)
  633. break;
  634. }
  635. for (int j = b[0]; j < b[2]; j++)
  636. {
  637. sum = 0;
  638. for (int i = 0; i < cropContour.Rows; i++)
  639. {
  640. sum += cropContour.Get<byte>(i, j);
  641. }
  642. if (sum == 0)
  643. {
  644. b[1] = j;
  645. break;
  646. }
  647. }
  648. if (b[3] - b[2] > 500)
  649. {
  650. if (Math.Abs(b[2] - b[1]) > 50)
  651. {
  652. cropContour2 = cropContour[0, cropContour.Rows - 1, b[2], cropContour.Cols - 1];
  653. border = b[2];
  654. }
  655. else
  656. cropContour2 = cropContour[0, cropContour.Rows - 1, 0, cropContour.Cols - 1];
  657. }
  658. else
  659. {
  660. cropContour2 = cropContour[0, cropContour.Rows - 1, b[0], cropContour.Cols - 1];
  661. border = b[0];
  662. }
  663. break;
  664. default:
  665. break;
  666. }
  667. }
  668. /// <summary>
  669. /// 针对双层深盲孔进行裁剪,上面空白区域较小,因此从第0行开始
  670. /// </summary>
  671. /// <param name="image"></param>
  672. /// <param name="y"></param>
  673. /// <param name="cropContour"></param>
  674. public void CropShenmangkongShuangceng(Mat image, out int[] y, out Mat cropContour, bool isCropFlag)
  675. {
  676. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  677. Cv2.MorphologyEx(image, image, MorphTypes.Open, se);
  678. int range = 200;
  679. if (isCropFlag)
  680. range = 15;
  681. y = new int[2] { 0, 0 };
  682. for (int i = 0; i < image.Rows; i++)
  683. {
  684. for (int j = range; j < image.Cols - range; j++)
  685. {
  686. if (image.Get<byte>(i, j) > 0)
  687. {
  688. y[0] = i - 10;
  689. break;
  690. }
  691. }
  692. if (y[0] != 0)
  693. break;
  694. }
  695. for (int i = image.Rows - 1; i > y[0]; i--)
  696. {
  697. for (int j = range; j < image.Cols - range; j++)
  698. {
  699. if (image.Get<byte>(i, j) > 0)
  700. {
  701. y[1] = i + 10;
  702. break;
  703. }
  704. }
  705. if (y[1] != 0)
  706. break;
  707. }
  708. cropContour = image[y[0], y[1], 0, image.Cols - 1];
  709. }
  710. /// <summary>
  711. /// 圖片旋轉之後出現白邊,去掉白邊
  712. /// </summary>
  713. /// <param name="image"></param>
  714. /// <param name="crop"></param>
  715. /// <param name="border"></param>
  716. public void CropBothSide(Mat image, out Mat crop, out int range)
  717. {
  718. range = 50;
  719. crop = image[range, image.Rows - range, range, image.Cols - range].Clone();
  720. }
  721. //public void CropLight2(Mat image, out int border, out Mat cropImage, string direction)
  722. //{
  723. // Mat filter = new Mat();
  724. // Cv2.BilateralFilter(image, filter, 15, 150, 3);
  725. // // 增强
  726. // Mat zengqiang = new Mat();
  727. // InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  728. // Cv2.Filter2D(filter, zengqiang, -1, kernel);
  729. // Cv2.ConvertScaleAbs(zengqiang, zengqiang);
  730. // //边缘检测
  731. // Mat grad_x = new Mat();
  732. // Mat grad_x2 = new Mat();
  733. // //Cv2.Sobel(zengqiang, grad_x, MatType.CV_16S, 1, 0);
  734. // //Cv2.ConvertScaleAbs(grad_x, grad_x2);
  735. // CejuFunction.Sobel(zengqiang, out grad_x2);
  736. // ////腐蚀
  737. // //Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 5));
  738. // //Mat erode = new Mat();
  739. // //Cv2.Erode(grad_x2, erode, se);
  740. // //开运算
  741. // Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));// 结构元素
  742. // Mat open = new Mat();
  743. // //Cv2.MorphologyEx(close, openAfterClose,MorphTypes.Open, seOpen);
  744. // Cv2.MorphologyEx(grad_x2, open, MorphTypes.Open, seOpen);
  745. // ////閉運算
  746. // Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));// 结构元素
  747. // Mat close = new Mat();
  748. // Cv2.MorphologyEx(open, close, MorphTypes.Close, se);
  749. // Mat thresh2 = close.Threshold(0, 1, ThresholdTypes.Otsu);
  750. // //填充
  751. // Mat fill = new Mat();
  752. // CejuFunction.Fill(thresh2, out fill, 1);
  753. // int[] b = new int[4] { 0, 0, 0, 0 };
  754. // int sum = 0;
  755. // border = 0;
  756. // cropImage = new Mat();
  757. // switch (direction)
  758. // {
  759. // case "left":
  760. // for (int j = 0; j < fill.Cols; j++)
  761. // {
  762. // for (int i = 0; i < fill.Rows; i++)
  763. // {
  764. // if (fill.Get<byte>(i, j) > 0)
  765. // {
  766. // b[0] = j;
  767. // break;
  768. // }
  769. // }
  770. // if (b[0] != 0)
  771. // break;
  772. // }
  773. // for (int j = b[0]; j < fill.Cols; j++)
  774. // {
  775. // sum = 0;
  776. // for (int i = 0; i < fill.Rows; i++)
  777. // {
  778. // sum += fill.Get<byte>(i, j);
  779. // }
  780. // if (sum == 0)
  781. // {
  782. // b[1] = j;
  783. // break;
  784. // }
  785. // }
  786. // for (int j = fill.Cols - 1; j > b[1]; j--)
  787. // {
  788. // for (int i = 0; i < fill.Rows; i++)
  789. // {
  790. // if (fill.Get<byte>(i, j) > 0)
  791. // {
  792. // b[3] = j;
  793. // break;
  794. // }
  795. // }
  796. // if (b[3] != 0)
  797. // break;
  798. // }
  799. // for (int j = b[3]; j > b[1]; j--)
  800. // {
  801. // sum = 0;
  802. // for (int i = 0; i < fill.Rows; i++)
  803. // {
  804. // sum += fill.Get<byte>(i, j);
  805. // }
  806. // if (sum == 0)
  807. // {
  808. // b[2] = j;
  809. // break;
  810. // }
  811. // }
  812. // if (b[1] - b[0] > 500)
  813. // {
  814. // if (Math.Abs(b[2] - b[1]) > 50)
  815. // {
  816. // cropImage = fill[0, fill.Rows - 1, b[0], b[1]];
  817. // border = b[0];
  818. // }
  819. // else
  820. // cropImage = fill[0, fill.Rows - 1, 0, fill.Cols - 1];
  821. // }
  822. // else
  823. // {
  824. // cropImage = fill[0, fill.Rows - 1, 0, b[3]];
  825. // border = 0;
  826. // }
  827. // break;
  828. // case "right":
  829. // for (int j = fill.Cols - 1; j > 0; j--)
  830. // {
  831. // for (int i = 0; i < fill.Rows; i++)
  832. // {
  833. // if (fill.Get<byte>(i, j) > 0)
  834. // {
  835. // b[3] = j;
  836. // break;
  837. // }
  838. // }
  839. // if (b[3] != 0)
  840. // break;
  841. // }
  842. // for (int j = b[3]; j > 0; j--)
  843. // {
  844. // sum = 0;
  845. // for (int i = 0; i < fill.Rows; i++)
  846. // {
  847. // sum += fill.Get<byte>(i, j);
  848. // }
  849. // if (sum == 0)
  850. // {
  851. // b[2] = j;
  852. // break;
  853. // }
  854. // }
  855. // for (int j = 0; j < b[2]; j++)
  856. // {
  857. // for (int i = 0; i < fill.Rows; i++)
  858. // {
  859. // if (fill.Get<byte>(i, j) > 0)
  860. // {
  861. // b[0] = j;
  862. // break;
  863. // }
  864. // }
  865. // if (b[0] != 0)
  866. // break;
  867. // }
  868. // for (int j = b[0]; j < b[2]; j++)
  869. // {
  870. // sum = 0;
  871. // for (int i = 0; i < fill.Rows; i++)
  872. // {
  873. // sum += fill.Get<byte>(i, j);
  874. // }
  875. // if (sum == 0)
  876. // {
  877. // b[1] = j;
  878. // break;
  879. // }
  880. // }
  881. // if (b[3] - b[2] > 500)
  882. // {
  883. // if (Math.Abs(b[2] - b[1]) > 50)
  884. // {
  885. // cropImage = fill[0, fill.Rows - 1, b[2], fill.Cols - 1];
  886. // border = b[2];
  887. // }
  888. // else
  889. // cropImage = fill[0, fill.Rows - 1, 0, fill.Cols - 1];
  890. // }
  891. // else
  892. // {
  893. // cropImage = fill[0, fill.Rows - 1, b[0], fill.Cols - 1];
  894. // border = b[0];
  895. // }
  896. // break;
  897. // default:
  898. // break;
  899. // }
  900. //}
  901. /// <summary>
  902. /// 提取数据的提取区域
  903. /// </summary>
  904. /// <param name="cropContour2">输入去掉亮光后的图片</param>
  905. /// <param name="dataArea">输出提取区域dataArea[0]左,dataArea[1]右</param>
  906. /// /// <param name="direction">槽孔的位置方向,left/right</param>
  907. public void GetDataArea(Mat cropContour2, out int[] dataArea, string direction)
  908. {
  909. int[] rowSum = new int[cropContour2.Rows];
  910. int max = 0;
  911. int fillBorder = 0;
  912. for (int i = 0; i < cropContour2.Rows; i++)
  913. {
  914. for (int j = 0; j < cropContour2.Cols; j++)
  915. rowSum[i] += cropContour2.Get<byte>(i, j);
  916. }
  917. max = rowSum.Max();
  918. for (int i = 0; i < rowSum.Length; i++)
  919. {
  920. if (rowSum[i] > max - 50)
  921. {
  922. fillBorder = i;
  923. break;
  924. }
  925. }
  926. Mat fill = cropContour2.Clone();
  927. Cv2.Rectangle(fill, new Rect(0, fillBorder, cropContour2.Cols, cropContour2.Rows - fillBorder), new Scalar(1), -1);
  928. //提取區域
  929. dataArea = new int[2] { 0, 0 };
  930. int[] colSum = new int[fill.Cols];
  931. for (int j = 100; j < fill.Cols - 100; j++)
  932. {
  933. for (int i = 0; i < fill.Rows; i++)
  934. {
  935. colSum[j] += fill.Get<byte>(i, j);
  936. }
  937. }
  938. max = colSum.Max();
  939. switch (direction)
  940. {
  941. case "right":
  942. //for (int j = 0; j < colSum.Length; j++)
  943. //{
  944. // if (colSum[j] > max - 10)
  945. // {
  946. // dataArea[0] = j+100;
  947. // break;
  948. // }
  949. //}
  950. dataArea[0] = 100;
  951. for (int j = dataArea[0] + 100; j < colSum.Length - 100; j++)
  952. {
  953. if (colSum[j] > max - 10)
  954. {
  955. dataArea[1] = j;
  956. break;
  957. }
  958. }
  959. if (dataArea[1] == 0)
  960. dataArea[1] = dataArea[0] + 100;
  961. //dataArea[0] = 100;
  962. if (dataArea[1] - dataArea[0] > 200)
  963. dataArea[1] = dataArea[0] + 200;
  964. break;
  965. case "left":
  966. //for (int j = colSum.Length - 1; j > 0; j--)
  967. //{
  968. // if (colSum[j] > max - 10)
  969. // {
  970. // dataArea[1] = j+100;
  971. // break;
  972. // }
  973. //}
  974. dataArea[1] = cropContour2.Cols - 100;
  975. for (int j = dataArea[1] - 100; j > 100; j--)
  976. {
  977. if (colSum[j] > max - 10)
  978. {
  979. dataArea[0] = j;
  980. break;
  981. }
  982. }
  983. if (dataArea[0] == 0)
  984. dataArea[0] = dataArea[1] - 100;
  985. //dataArea[1] = cropContour2.Cols - 100;
  986. if (dataArea[1] - dataArea[0] > 200)
  987. dataArea[0] = dataArea[1] - 200;
  988. break;
  989. }
  990. }
  991. /// <summary>
  992. /// 得到浅盲孔的数据提取区域
  993. /// </summary>
  994. /// <param name="cropContour">裁剪后的二值图像</param>
  995. /// <param name="dataArea">输出提取区域,从左至右分别是[0][1][2][3]</param>
  996. public void GetMangkongDataAreaForQian(Mat cropContour, out int[] dataArea)
  997. {
  998. dataArea = new int[4];
  999. int[] middleArea = new int[2];
  1000. int[] sum = new int[cropContour.Cols];
  1001. //每列求和
  1002. for (int j = 0; j < cropContour.Cols; j++)
  1003. {
  1004. sum[j] = 0;
  1005. for (int i = 0; i < cropContour.Rows; i++)
  1006. {
  1007. sum[j] += cropContour.Get<byte>(i, j);
  1008. }
  1009. }
  1010. //求中部区域
  1011. int max = sum.Max();
  1012. for (int j = 0; j < sum.Length; j++)
  1013. {
  1014. if (sum[j] > max - 10)
  1015. {
  1016. middleArea[0] = j;
  1017. break;
  1018. }
  1019. }
  1020. for (int j = sum.Length - 1; j > 0; j--)
  1021. {
  1022. if (sum[j] > max - 10)
  1023. {
  1024. middleArea[1] = j;
  1025. break;
  1026. }
  1027. }
  1028. //第一次容错
  1029. if(middleArea[1]- middleArea[0] < 100)
  1030. {
  1031. for (int j = 0; j < sum.Length; j++)
  1032. {
  1033. if (sum[j] > max - 20)
  1034. {
  1035. middleArea[0] = j;
  1036. break;
  1037. }
  1038. }
  1039. for (int j = sum.Length - 1; j > 0; j--)
  1040. {
  1041. if (sum[j] > max - 20)
  1042. {
  1043. middleArea[1] = j;
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. //每行求和
  1049. int[] rowsSum = new int[cropContour.Rows];
  1050. for (int i = 0; i < cropContour.Rows; i++)
  1051. {
  1052. rowsSum[i] = 0;
  1053. for (int j = 0; j < cropContour.Cols; j++)
  1054. {
  1055. rowsSum[i] += cropContour.Get<byte>(i, j);
  1056. }
  1057. }
  1058. //得到填充上界,其下填为0
  1059. max = rowsSum.Max();
  1060. int fillBorder = new int();
  1061. for (int i = 0; i < rowsSum.Length; i++)
  1062. {
  1063. if (rowsSum[i] > max / 2)
  1064. {
  1065. fillBorder = i;
  1066. break;
  1067. }
  1068. }
  1069. Mat fill = cropContour.Clone();
  1070. Cv2.Rectangle(fill, new Rect(0, fillBorder, cropContour.Cols, cropContour.Rows - fillBorder), new Scalar(1), -1);
  1071. //每列求和
  1072. int[] colSum = new int[fill.Cols];
  1073. for (int j = 0; j < fill.Cols; j++)
  1074. {
  1075. colSum[j] = 0;
  1076. for (int i = 0; i < fill.Rows; i++)
  1077. {
  1078. colSum[j] += fill.Get<byte>(i, j);
  1079. }
  1080. }
  1081. //左,右最大值
  1082. int[] left = colSum.Skip(middleArea[0] - 500).Take(500).ToArray();
  1083. int[] right = colSum.Skip(middleArea[1]).Take(500).ToArray();
  1084. int leftMax = left.Max();
  1085. int rightMax = right.Max();
  1086. //数据提取区域
  1087. int range = 100;
  1088. if (colSum[middleArea[0] - 100] < leftMax - 10 || colSum[middleArea[1] + 100] < rightMax - 10)
  1089. range = 0;
  1090. for (int j = middleArea[0] - range; j > middleArea[0] - 500 && j > 0; j--)
  1091. {
  1092. if (colSum[j] > leftMax - 10)
  1093. {
  1094. dataArea[1] = j;
  1095. break;
  1096. }
  1097. }
  1098. if (dataArea[1] == 0) dataArea[1] = leftMax;
  1099. for (int j = dataArea[1] - 100; j < dataArea[1]; j++)
  1100. {
  1101. if (colSum[j] > leftMax - 10)
  1102. {
  1103. dataArea[0] = j;
  1104. break;
  1105. }
  1106. }
  1107. for (int j = middleArea[1] + range; j < middleArea[1] + 500; j++)
  1108. {
  1109. if (colSum[j] > rightMax - 5)
  1110. {
  1111. dataArea[2] = j;
  1112. break;
  1113. }
  1114. }
  1115. for (int j = dataArea[2] + 100; j > dataArea[2]; j--)
  1116. {
  1117. if (colSum[j] > rightMax - 5)
  1118. {
  1119. dataArea[3] = j;
  1120. break;
  1121. }
  1122. }
  1123. if (dataArea[1] == 0)
  1124. {
  1125. dataArea[1] = middleArea[0] - 70;//不加100是因为出去还要加20
  1126. dataArea[0] = dataArea[1] - 100;
  1127. }
  1128. if (dataArea[0] == 0)
  1129. {
  1130. dataArea[0] = dataArea[1] - 100;
  1131. if (colSum[dataArea[0]] < leftMax - 10)
  1132. dataArea[0] = dataArea[1] - 30;
  1133. }
  1134. if (dataArea[2] == 0)
  1135. {
  1136. dataArea[2] = middleArea[1] + 70;//不加100是因为出去还要加20
  1137. dataArea[3] = dataArea[2] + 100;
  1138. }
  1139. if (dataArea[3] == 0)
  1140. {
  1141. dataArea[3] = dataArea[2] + 100;
  1142. if (colSum[dataArea[3]] < rightMax - 10)//防止加完出界
  1143. dataArea[3] = dataArea[2] + 30;
  1144. }
  1145. if (dataArea[1] - dataArea[0] < 50)
  1146. dataArea[1] += 50;
  1147. if (dataArea[3] - dataArea[2] < 50)
  1148. dataArea[2] -= 50;
  1149. }
  1150. /// <summary>
  1151. /// 得到浅盲孔的数据提取区域
  1152. /// </summary>
  1153. /// <param name="cropContour">裁剪后的二值图像</param>
  1154. /// <param name="dataArea">输出提取区域,从左至右分别是[0][1][2][3]</param>
  1155. public void GetMangkongDataArea(Mat cropContour, out int[] dataArea)
  1156. {
  1157. dataArea = new int[4];
  1158. int[] middleArea = new int[2];
  1159. int[] sum = new int[cropContour.Cols];
  1160. //每列求和
  1161. for (int j = 0; j < cropContour.Cols; j++)
  1162. {
  1163. sum[j] = 0;
  1164. for (int i = 0; i < cropContour.Rows; i++)
  1165. {
  1166. sum[j] += cropContour.Get<byte>(i, j);
  1167. }
  1168. }
  1169. //求中部区域
  1170. int max = sum.Max();
  1171. for (int j = 0; j < sum.Length; j++)
  1172. {
  1173. if (sum[j] > max - 20)
  1174. {
  1175. middleArea[0] = j;
  1176. break;
  1177. }
  1178. }
  1179. for (int j = sum.Length - 1; j > 0; j--)
  1180. {
  1181. if (sum[j] > max - 20)
  1182. {
  1183. middleArea[1] = j;
  1184. break;
  1185. }
  1186. }
  1187. //第一次容错
  1188. if (middleArea[1] - middleArea[0] < 110)
  1189. {
  1190. for (int j = 0; j < sum.Length; j++)
  1191. {
  1192. if (sum[j] > max - 80)
  1193. {
  1194. middleArea[0] = j;
  1195. break;
  1196. }
  1197. }
  1198. for (int j = sum.Length - 1; j > 0; j--)
  1199. {
  1200. if (sum[j] > max - 80)
  1201. {
  1202. middleArea[1] = j;
  1203. break;
  1204. }
  1205. }
  1206. }
  1207. //每行求和
  1208. int[] rowsSum = new int[cropContour.Rows];
  1209. for (int i = 0; i < cropContour.Rows; i++)
  1210. {
  1211. rowsSum[i] = 0;
  1212. for (int j = 0; j < cropContour.Cols; j++)
  1213. {
  1214. rowsSum[i] += cropContour.Get<byte>(i, j);
  1215. }
  1216. }
  1217. //得到填充上界,其下填为0
  1218. max = rowsSum.Max();
  1219. int fillBorder = new int();
  1220. for (int i = 0; i < rowsSum.Length; i++)
  1221. {
  1222. if (rowsSum[i] > max / 2)
  1223. {
  1224. fillBorder = i;
  1225. break;
  1226. }
  1227. }
  1228. Mat fill = cropContour.Clone();
  1229. Cv2.Rectangle(fill, new Rect(0, fillBorder, cropContour.Cols, cropContour.Rows - fillBorder), new Scalar(1), -1);
  1230. //每列求和
  1231. int[] colSum = new int[fill.Cols];
  1232. for (int j = 0; j < fill.Cols; j++)
  1233. {
  1234. colSum[j] = 0;
  1235. for (int i = 0; i < fill.Rows; i++)
  1236. {
  1237. colSum[j] += fill.Get<byte>(i, j);
  1238. }
  1239. }
  1240. //左,右最大值
  1241. int[] left = colSum.Skip(middleArea[0] - 500).Take(500).ToArray();
  1242. int[] right = colSum.Skip(middleArea[1]).Take(500).ToArray();
  1243. int leftMax = left.Max();
  1244. int rightMax = right.Max();
  1245. //数据提取区域
  1246. int range = 100;
  1247. if (colSum[middleArea[0] - 100] < leftMax - 10 || colSum[middleArea[1] + 100] < rightMax - 10)
  1248. range = 0;
  1249. for (int j = middleArea[0] - range; j > middleArea[0] - 500 && j > 0; j--)
  1250. {
  1251. if (colSum[j] > leftMax - 10)
  1252. {
  1253. dataArea[1] = j;
  1254. break;
  1255. }
  1256. }
  1257. for (int j = dataArea[1] - 100; j < dataArea[1]; j++)
  1258. {
  1259. if (colSum[j] > leftMax - 10)
  1260. {
  1261. dataArea[0] = j;
  1262. break;
  1263. }
  1264. }
  1265. for (int j = middleArea[1] + range; j < middleArea[1] + 500; j++)
  1266. {
  1267. if (colSum[j] > rightMax - 5)
  1268. {
  1269. dataArea[2] = j;
  1270. break;
  1271. }
  1272. }
  1273. for (int j = dataArea[2] + 100; j > dataArea[2]; j--)
  1274. {
  1275. if (colSum[j] > rightMax - 5)
  1276. {
  1277. dataArea[3] = j;
  1278. break;
  1279. }
  1280. }
  1281. if (dataArea[1] == 0)
  1282. {
  1283. dataArea[1] = middleArea[0] - 70;//不加100是因为出去还要加20
  1284. dataArea[0] = dataArea[1] - 100;
  1285. }
  1286. if (dataArea[0] == 0)
  1287. {
  1288. dataArea[0] = dataArea[1] - 100;
  1289. if (colSum[dataArea[0]] < leftMax - 10)
  1290. dataArea[0] = dataArea[1] - 30;
  1291. }
  1292. if (dataArea[2] == 0)
  1293. {
  1294. dataArea[2] = middleArea[1] + 70;//不加100是因为出去还要加20
  1295. dataArea[3] = dataArea[2] + 100;
  1296. }
  1297. if (dataArea[3] == 0)
  1298. {
  1299. dataArea[3] = dataArea[2] + 100;
  1300. if (colSum[dataArea[3]] < rightMax - 10)//防止加完出界
  1301. dataArea[3] = dataArea[2] + 30;
  1302. }
  1303. if (dataArea[1] - dataArea[0] < 50)
  1304. dataArea[1] += 50;
  1305. if (dataArea[3] - dataArea[2] < 50)
  1306. dataArea[2] -= 50;
  1307. }
  1308. /// <summary>
  1309. /// 提取第一條橫綫縱坐標
  1310. /// </summary>
  1311. /// <param name="image">輸入二值化圖像</param>
  1312. /// <param name="averageCoordinateNew">輸出坐標</param>
  1313. /// <param name="leftBorder">左邊界</param>
  1314. /// <param name="rightBorder">右边界</param>
  1315. /// <param name="direction">大于等于0时,记录大于0的点,小于0记录等于零的点</param>
  1316. public void ExtractLines(Mat image, out double averageCoordinateNew, int leftBorder, int rightBorder, int direction)
  1317. {
  1318. //数组长宽
  1319. int rows = image.Rows;
  1320. int cols = image.Cols;
  1321. int count = 0;
  1322. int sum = 0;
  1323. // 对循环次数计数,防止程序卡死
  1324. int circleCount = 0;
  1325. //当点的个数少于5的时候循环
  1326. while (count < 5)
  1327. {
  1328. count = 0;
  1329. sum = 0;
  1330. //每次循环,寻找范围向下移动5个像素
  1331. //遍历,寻找线条上的点,并记录纵坐标
  1332. for (int j = leftBorder; j < rightBorder; j++)
  1333. {
  1334. for (int i = 0; i < rows; i++)
  1335. {
  1336. if (direction >= 0)
  1337. {
  1338. if (image.Get<byte>(i, j) > 0)
  1339. {
  1340. sum += i;
  1341. count++;
  1342. break;
  1343. }
  1344. }
  1345. else
  1346. {
  1347. if (image.Get<byte>(i, j) == 0)
  1348. {
  1349. sum += i;
  1350. count++;
  1351. break;
  1352. }
  1353. }
  1354. }
  1355. }
  1356. // 超过30次跳出
  1357. if(circleCount <= 30)
  1358. {
  1359. circleCount++;
  1360. }
  1361. else
  1362. {
  1363. break;
  1364. }
  1365. }
  1366. averageCoordinateNew = sum / count;
  1367. }
  1368. /// <summary>
  1369. /// 提取非第一条横线纵坐标
  1370. /// </summary>
  1371. /// <param name="image">输入二值化图像</param>
  1372. /// <param name="averageCoordinateNew">输出纵坐标</param>
  1373. /// <param name="leftBorder">左边界</param>
  1374. /// <param name="rightBorder">右边界</param>
  1375. /// <param name="averageCoordinate">上一条线的坐标</param>
  1376. /// <param name="direction">大于等于0时,记录大于0的点,小于0时记录等于零的点</param>
  1377. public void ExtractLines(Mat image, out double averageCoordinateNew, int leftBorder, int rightBorder, double averageCoordinate, int direction)
  1378. {
  1379. //ImageShow(image*255);
  1380. //数组长宽
  1381. int rows = image.Rows;
  1382. int cols = image.Cols;
  1383. int count = 0;
  1384. int sum = 0;
  1385. int upperBound = (int)averageCoordinate + 15;
  1386. int lowerBound = (int)averageCoordinate + 45;
  1387. //当点的个数少于5的时候循环
  1388. while (count < 5)
  1389. {
  1390. count = 0;
  1391. sum = 0;
  1392. //每次循环,寻找范围向下移动5个像素
  1393. upperBound = upperBound + 5;
  1394. lowerBound = lowerBound + 5;
  1395. if (lowerBound > image.Rows)
  1396. break;
  1397. //遍历,寻找线条上的点,并记录纵坐标
  1398. int value = 0;
  1399. for (int j = leftBorder; j < rightBorder; j++)
  1400. {
  1401. for (int i = upperBound; i < lowerBound; i++)
  1402. {
  1403. if (direction >= 0)
  1404. {
  1405. value = image.Get<byte>(i, j);
  1406. if (value > 0)
  1407. {
  1408. sum += i;
  1409. count++;
  1410. break;
  1411. }
  1412. }
  1413. else
  1414. {
  1415. if (image.Get<byte>(i, j) == 0)
  1416. {
  1417. sum += i;
  1418. count++;
  1419. break;
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. averageCoordinateNew = count == 0 ? 0 : sum / count;
  1426. }
  1427. /// <summary>
  1428. /// 从下向上提取第一条横线纵坐标
  1429. /// </summary>
  1430. /// <param name="image">輸入二值化圖像</param>
  1431. /// <param name="averageCoordinateNew">輸出坐標</param>
  1432. /// <param name="leftBorder">左邊界</param>
  1433. /// <param name="rightBorder">右边界</param>
  1434. /// <param name="direction">大于等于0时,记录大于0的点,小于0记录等于零的点</param>
  1435. public void ExtractLines2(Mat image, out double averageCoordinateNew, int leftBorder, int rightBorder, int direction)
  1436. {
  1437. //数组长宽
  1438. int rows = image.Rows;
  1439. int cols = image.Cols;
  1440. int count = 0;
  1441. int sum = 0;
  1442. int tag = 0;
  1443. //当点的个数少于5的时候循环
  1444. while (count < 5)
  1445. {
  1446. count = 0;
  1447. sum = 0;
  1448. //每次循环,寻找范围向下移动5个像素
  1449. //遍历,寻找线条上的点,并记录纵坐标
  1450. for (int j = leftBorder; j < rightBorder; j++)
  1451. {
  1452. for (int i = rows - 1; i > 0; i--)
  1453. {
  1454. if (i == 1)
  1455. tag += 1;
  1456. if (direction >= 0)
  1457. {
  1458. if (image.Get<byte>(i, j) > 0)
  1459. {
  1460. sum += i;
  1461. count++;
  1462. break;
  1463. }
  1464. }
  1465. else
  1466. {
  1467. if (image.Get<byte>(i, j) == 0)
  1468. {
  1469. sum += i;
  1470. count++;
  1471. break;
  1472. }
  1473. }
  1474. }
  1475. if (tag == 3)
  1476. break;
  1477. }
  1478. if (tag == 3)
  1479. break;
  1480. }
  1481. averageCoordinateNew = (count == 0) ? 0 : sum / count;
  1482. }
  1483. /// <summary>
  1484. /// 从下向上提取横线纵坐标
  1485. /// </summary>
  1486. /// <param name="image">输入二值化图像</param>
  1487. /// <param name="averageCoordinateNew">输出纵坐标</param>
  1488. /// <param name="leftBorder">左边界</param>
  1489. /// <param name="rightBorder">右边界</param>
  1490. /// <param name="averageCoordinate">上一条线的坐标</param>
  1491. /// <param name="direction">大于等于0时,记录大于0的点,小于0时记录等于零的点</param>
  1492. public void ExtractLines2(Mat image, out double averageCoordinateNew, int leftBorder, int rightBorder, double averageCoordinate, int direction)
  1493. {
  1494. //数组长宽
  1495. int rows = image.Rows;
  1496. int cols = image.Cols;
  1497. int sum = 0, count = 0;
  1498. //遍历范围的上界和下界
  1499. int upperBound = (int)averageCoordinate - 45;
  1500. int lowerBound = (int)averageCoordinate - 10;
  1501. //当点的个数少于5的时候循环
  1502. while (count < 5)
  1503. {
  1504. count = 0;
  1505. sum = 0;
  1506. //每次循环,寻找范围向下移动5个像素
  1507. upperBound = upperBound - 5;
  1508. lowerBound = lowerBound - 5;
  1509. if (upperBound < 0)
  1510. break;
  1511. //遍历,寻找线条上的点,并记录纵坐标
  1512. for (int j = leftBorder; j < rightBorder; j++)
  1513. {
  1514. for (int i = lowerBound; i > upperBound; i--)
  1515. {
  1516. if (direction >= 0)
  1517. {
  1518. if (image.Get<byte>(i, j) > 0)
  1519. {
  1520. sum += i;
  1521. count++;
  1522. break;
  1523. }
  1524. }
  1525. else
  1526. {
  1527. if (image.Get<byte>(i, j) == 0)
  1528. {
  1529. sum += i;
  1530. count++;
  1531. break;
  1532. }
  1533. }
  1534. }
  1535. }
  1536. }
  1537. averageCoordinateNew = count==0 ? 0 : sum / count;
  1538. }
  1539. /// <summary>
  1540. /// 提取第一条竖线的横坐标,从左往右
  1541. /// </summary>
  1542. /// <param name="image">输入二值图像</param>
  1543. /// <param name="result">输出坐标结果</param>
  1544. /// <param name="upperBound">上边界</param>
  1545. /// <param name="lowerBound">下边界</param>
  1546. public void ExtractVerticalLinesL2R(Mat image, out double result, int upperBound, int lowerBound, int showMat = 0)
  1547. {
  1548. int rows = image.Rows;
  1549. int cols = image.Cols;
  1550. List<int> sum = new List<int>();
  1551. int k = 0;
  1552. while (sum.Count == 0)
  1553. {
  1554. if (upperBound - 5 * k < 0 || lowerBound + 5 * k > image.Rows) break;
  1555. //解决两层板有些孔铜找不到的问题
  1556. for (int i = upperBound - 5 * k; i < lowerBound + 5 * k; i++)
  1557. {
  1558. for (int j = 0; j < cols; j++)
  1559. {
  1560. if (image.Get<byte>(i, j) > 0)
  1561. {
  1562. sum.Add(j);
  1563. break;
  1564. }
  1565. }
  1566. }
  1567. ++k;
  1568. }
  1569. ////int count = 0;
  1570. //for (int i = upperBound; i < lowerBound; i++)
  1571. //{
  1572. // for (int j = 0; j < cols; j++)
  1573. // {
  1574. // if (image.Get<byte>(i, j) > 0)
  1575. // {
  1576. // sum.Add(j);
  1577. // //sum += j;
  1578. // //count++;
  1579. // break;
  1580. // }
  1581. // }
  1582. //}
  1583. //if (sum.Count == 0)
  1584. // result = 1;// 0;
  1585. //else
  1586. result = sum.Average();// / count;
  1587. //if (showMat > 0)
  1588. //{
  1589. // Mat imageClone = image.Clone() * 127;
  1590. // Cv2.Line(imageClone, 0, upperBound, cols - 1, upperBound, new Scalar(255));
  1591. // Cv2.Line(imageClone, 0, lowerBound, cols - 1, lowerBound, new Scalar(255));
  1592. // Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
  1593. //}
  1594. //////对自动结果不正确(这里使用距离过远来判定)的扩展运算
  1595. ////double result1 = 0;
  1596. if (showMat > 0 && result > 20/*countV > (upperBound + lowerBound) / 2*/)
  1597. {
  1598. sum.Clear();
  1599. //sum = 0;
  1600. //count = 0;
  1601. ////Mat imageClone = image.Clone() * 127;
  1602. ////Cv2.Line(imageClone, 0, upperBound, cols - 1, upperBound, new Scalar(255));
  1603. ////Cv2.Line(imageClone, 0, lowerBound, cols - 1, lowerBound, new Scalar(255));
  1604. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
  1605. //////int sum = 0;
  1606. //////int count = 0;
  1607. bool sumAdd = false;
  1608. for (int i = upperBound - 5; i < lowerBound + 5; i++)
  1609. {
  1610. sumAdd = false;
  1611. for (int j = 0; j < 20; j++)
  1612. {
  1613. if (image.Get<byte>(i, j) > 0)
  1614. {
  1615. sumAdd = true;
  1616. sum.Add(j);
  1617. //sum += j;
  1618. //count++;
  1619. break;
  1620. }
  1621. }
  1622. if (!sumAdd && sum.Count > 0)
  1623. break;
  1624. }
  1625. if (sum.Count == 0)
  1626. {
  1627. for (int i = upperBound - 5; i < lowerBound + 5; i++)
  1628. {
  1629. sumAdd = false;
  1630. for (int j = 0; j < 26; j++)
  1631. {
  1632. if (image.Get<byte>(i, j) > 0)
  1633. {
  1634. sumAdd = true;
  1635. sum.Add(j);
  1636. //sum += j;
  1637. //count++;
  1638. break;
  1639. }
  1640. }
  1641. if (!sumAdd && sum.Count > 0)
  1642. break;
  1643. }
  1644. //for (int i = upperBound - 5; i < lowerBound + 5; i++)
  1645. //{
  1646. // sumAdd = false;
  1647. // for (int j = cols - 1; j > cols - 26; j--)
  1648. // {
  1649. // if (image.Get<byte>(i, j) > 0)
  1650. // {
  1651. // sumAdd = true;
  1652. // sum += j;
  1653. // count++;
  1654. // break;
  1655. // }
  1656. // }
  1657. // if (!sumAdd && sum > 0)
  1658. // break;
  1659. //}
  1660. }
  1661. // 输出平均横坐标
  1662. if (sum.Count > 0)
  1663. result = sum.Average();// / count;
  1664. }
  1665. else if (showMat > 0)
  1666. {//对自动结果不正确(这里使用垂直方向不整齐来判定)的扩展运算
  1667. sum.Sort();
  1668. for (int i = 0; i < sum.Count - 1; i++)
  1669. {
  1670. if (sum[i + 1] - sum[i] > 10)
  1671. {
  1672. for (int j = sum.Count - 1; j > i; j--)
  1673. sum.RemoveAt(j);
  1674. break;
  1675. }
  1676. }
  1677. // 输出平均横坐标
  1678. if (sum.Count > 0)
  1679. result = sum.Average();// / count;
  1680. //Mat imageClone = image.Clone() * 127;
  1681. //Cv2.Line(imageClone, 0, upperBound, cols - 1, upperBound, new Scalar(255));
  1682. //Cv2.Line(imageClone, 0, lowerBound, cols - 1, lowerBound, new Scalar(255));
  1683. //Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
  1684. }
  1685. ////对自动结果不正确(这里使用距离过远来判定)的扩展运算
  1686. //double result1 = 0;
  1687. //// 输出平均横坐标
  1688. //result = sum / count;
  1689. }
  1690. /// <summary>
  1691. /// 提取第二条竖线的横坐标,从左往右
  1692. /// </summary>
  1693. /// <param name="image">输入二值图像</param>
  1694. /// <param name="result">输出坐标结果</param>
  1695. /// <param name="upperBound">上边界</param>
  1696. /// <param name="lowerBound">下边界</param>
  1697. /// <param name="basic">第一条线的横坐标</param>
  1698. public int[] ExtractVerticalLinesL2R(Mat image, out double result, int upperBound, int lowerBound, double basic, bool cal_Jiaoneisuo = true)
  1699. {
  1700. int rows = image.Rows;
  1701. int cols = image.Cols;
  1702. int sum = 0;
  1703. int count = 0;
  1704. // 寻找的左右范围
  1705. int leftBound = (int)basic + 15;
  1706. int rightBound = (int)basic + 45;
  1707. int countUp = 5;
  1708. if (!cal_Jiaoneisuo) countUp = 1;
  1709. //当点的个数少于5的时候循环
  1710. while (count < countUp)
  1711. {
  1712. count = 0;
  1713. sum = 0;
  1714. //每次循环,寻找范围向左移动5个像素
  1715. leftBound = leftBound + 5;
  1716. rightBound = rightBound + 5;
  1717. if (rightBound > image.Cols)
  1718. break;
  1719. //遍历,寻找线条上的点,并记录横坐标
  1720. for (int i = upperBound; i < lowerBound; i++)
  1721. {//待自测scc-1
  1722. //bool isWhiteArea = false;//避免弧度大,导致找的位置与角落的弧度相交
  1723. for (int j = leftBound; j < rightBound; j++)
  1724. {
  1725. //if (!isWhiteArea)
  1726. //{
  1727. // if (image.Get<byte>(i, j) > 0)
  1728. // isWhiteArea = true;
  1729. //}else
  1730. if (image.Get<byte>(i, j) == 0)
  1731. {
  1732. sum += j;
  1733. count++;
  1734. break;
  1735. }
  1736. }
  1737. }
  1738. }
  1739. leftBound = (int)basic + 15;
  1740. rightBound = (int)basic + 145;
  1741. if (rightBound >= cols) rightBound = cols - 1;
  1742. double y1 = upperBound;
  1743. double x1 = leftBound;
  1744. for (int i = upperBound + 20; i < lowerBound - 20; i++)
  1745. {
  1746. for (int j = leftBound; j < rightBound; j++)
  1747. {
  1748. if (image.Get<byte>(i, j) == 0)
  1749. {
  1750. if (x1 < j)
  1751. {
  1752. x1 = j;
  1753. y1 = i;
  1754. }
  1755. break;
  1756. }
  1757. }
  1758. }
  1759. result = sum / count;
  1760. return new int[] { (int)x1, (int)y1 };
  1761. }
  1762. /// <summary>
  1763. /// 从右往左提取第一条竖线横坐标
  1764. /// </summary>
  1765. /// <param name="image">输入二值图像</param>
  1766. /// <param name="result">输出坐标结果</param>
  1767. /// <param name="upperBound">上边界</param>
  1768. /// <param name="lowerBound">下边界</param>
  1769. public void ExtractVerticalLinesR2L(Mat image, out double result, int upperBound, int lowerBound, int showMat = 0)
  1770. {
  1771. int rows = image.Rows;
  1772. int cols = image.Cols;
  1773. int sum = 0;
  1774. int count = 0;
  1775. int countV = 0;
  1776. for (int i = upperBound; i < lowerBound; i++)
  1777. {
  1778. for (int j = cols - 1; j > 0; j--)
  1779. {
  1780. if (image.Get<byte>(i, j) > 0)
  1781. {
  1782. sum += j;
  1783. count++;
  1784. break;
  1785. }
  1786. }
  1787. if (sum == 0) countV = i;
  1788. }
  1789. result = sum / count;
  1790. ////对自动结果不正确(这里使用距离过远来判定)的扩展运算
  1791. //double result1 = 0;
  1792. if (showMat > 0 && cols - result > 20/*countV > (upperBound + lowerBound) / 2*/)
  1793. {
  1794. sum = 0;
  1795. count = 0;
  1796. //Mat imageClone = image.Clone() * 127;
  1797. //Cv2.Line(imageClone, 0, upperBound, cols - 1, upperBound, new Scalar(255));
  1798. //Cv2.Line(imageClone, 0, lowerBound, cols - 1, lowerBound, new Scalar(255));
  1799. //Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
  1800. ////int sum = 0;
  1801. ////int count = 0;
  1802. bool sumAdd = false;
  1803. for (int i = upperBound - 5; i < lowerBound + 5; i++)
  1804. {
  1805. sumAdd = false;
  1806. for (int j = cols - 1; j > cols - 20; j--)
  1807. {
  1808. if (image.Get<byte>(i, j) > 0)
  1809. {
  1810. sumAdd = true;
  1811. sum += j;
  1812. count++;
  1813. break;
  1814. }
  1815. }
  1816. if (!sumAdd && sum > 0)
  1817. break;
  1818. }
  1819. if (sum == 0)
  1820. {
  1821. for (int i = upperBound - 5; i < lowerBound + 5; i++)
  1822. {
  1823. sumAdd = false;
  1824. for (int j = cols - 1; j > cols - 26; j--)
  1825. {
  1826. if (image.Get<byte>(i, j) > 0)
  1827. {
  1828. sumAdd = true;
  1829. sum += j;
  1830. count++;
  1831. break;
  1832. }
  1833. }
  1834. if (!sumAdd && sum > 0)
  1835. break;
  1836. }
  1837. }
  1838. // 输出平均横坐标
  1839. if (sum > 0)
  1840. result = sum / count;
  1841. }
  1842. //// 输出平均横坐标
  1843. //if (true)
  1844. //{
  1845. //}
  1846. //result = sum / count;
  1847. }
  1848. /// <summary>
  1849. /// 提取第二条竖线的横坐标,从右往左
  1850. /// </summary>
  1851. /// <param name="image">输入二值图像</param>
  1852. /// <param name="result">输出坐标结果</param>
  1853. /// <param name="upperBound">上边界</param>
  1854. /// <param name="lowerBound">下边界</param>
  1855. /// <param name="basic">第一条线的横坐标</param>
  1856. public int[] ExtractVerticalLinesR2L(Mat image, out double result, int upperBound, int lowerBound, double basic, int showMat = 0)
  1857. {
  1858. int rows = image.Rows;
  1859. int cols = image.Cols;
  1860. int sum = 0;
  1861. int count = 0;
  1862. // 寻找的左右范围
  1863. int leftBound = (int)basic - 45;
  1864. int rightBound = (int)basic - 15;
  1865. //if (showMat > 0)
  1866. //{
  1867. // Mat imageClone = image.Clone() * 127;
  1868. // Cv2.Line(imageClone, leftBound, upperBound, rightBound - 1, upperBound, new Scalar(255));
  1869. // Cv2.Line(imageClone, leftBound, lowerBound, rightBound - 1, lowerBound, new Scalar(255));
  1870. // Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
  1871. //}
  1872. //当点的个数少于5的时候循环
  1873. while (count < 5)
  1874. {
  1875. count = 0;
  1876. sum = 0;
  1877. //每次循环,寻找范围向左移动5个像素
  1878. leftBound = leftBound - 5;
  1879. rightBound = rightBound - 5;
  1880. //遍历,寻找线条上的点,并记录横坐标
  1881. for (int i = upperBound; i < lowerBound; i++)
  1882. {//待自测scc-1
  1883. //bool isWhiteArea = false;//避免弧度大,导致找的位置与角落的弧度相交
  1884. ////(showMat > 0)
  1885. for (int j = rightBound - 1; j > leftBound; j--)
  1886. {
  1887. //if ((showMat > 0) && !isWhiteArea)
  1888. //{
  1889. // if (image.Get<byte>(i, j) > 0)
  1890. // isWhiteArea = true;
  1891. //}
  1892. //else
  1893. if (image.Get<byte>(i, j) == 0)
  1894. {
  1895. sum += j;
  1896. count++;
  1897. break;
  1898. }
  1899. }
  1900. }
  1901. }
  1902. //if (count == 0)
  1903. //{
  1904. //}
  1905. leftBound = (int)basic - 145;
  1906. if (leftBound < 0) leftBound = 0;
  1907. rightBound = (int)basic - 15;
  1908. double y1 = upperBound;
  1909. double x1 = rightBound;
  1910. //遍历,寻找线条上的点,并记录横坐标
  1911. for (int i = upperBound + 20; i < lowerBound - 20; i++)
  1912. {
  1913. for (int j = rightBound - 1; j > leftBound; j--)
  1914. {
  1915. if (image.Get<byte>(i, j) == 0)
  1916. {
  1917. if (x1 > j)
  1918. {
  1919. x1 = j;
  1920. y1 = i;
  1921. }
  1922. break;
  1923. }
  1924. }
  1925. }
  1926. result = sum / count;
  1927. return new int[] { (int)x1, (int)y1 };
  1928. }
  1929. /// <summary>
  1930. /// 提取三层板的粗糙度以及粗糙度的坐标
  1931. /// </summary>
  1932. /// <param name="imageContour">二值图像</param>
  1933. /// <param name="imageRed">红色通道图片</param>
  1934. /// <param name="ordinateL3">第三条横线</param>
  1935. /// <param name="ordinateL4">第四条横线</param>
  1936. /// <param name="ordinateL5">第五条横线</param>
  1937. /// <param name="ordinateL6">第六条横线</param>
  1938. /// <param name="ordinateV2">第二条竖线</param>
  1939. /// <param name="ordinateV4">第四条竖线</param>
  1940. /// <param name="dataArea">数据提取区域</param>
  1941. /// <param name="direction">槽孔所在左右方向,left、right</param>
  1942. /// <param name="roughness">输出粗糙度</param>
  1943. /// <param name="roughnessOrdinate">输出粗糙度坐标</param>
  1944. public void GetSancengRoughness(Mat imageContour, Mat imageRed, int ordinateL3, int ordinateL4, int ordinateL5, int ordinateL6, int ordinateV2, int ordinateV4, int[] dataArea, string direction, out int roughness, out int[] roughnessOrdinate)
  1945. {
  1946. int leftBorder1 = 0, rightBorder1 = 0, leftBorder2 = 0, rightBorder2 = 0;
  1947. int range = 70;
  1948. switch (direction)
  1949. {
  1950. case "right":
  1951. leftBorder1 = ordinateV2;
  1952. rightBorder1 = ordinateV2 + range;
  1953. leftBorder2 = ordinateV4;
  1954. rightBorder2 = ordinateV4 + range;
  1955. break;
  1956. case "left":
  1957. leftBorder1 = ordinateV2 - range;
  1958. rightBorder1 = ordinateV2;
  1959. leftBorder2 = ordinateV4 - range;
  1960. rightBorder2 = ordinateV4;
  1961. break;
  1962. }
  1963. int sum = 0, count = 0;
  1964. double averSaturation1 = 0, averSaturation2 = 0;
  1965. roughness = 0;
  1966. roughnessOrdinate = new int[4] { 0, 0, 0, 0 };
  1967. Mat sobel = new Mat();
  1968. Sobel(imageContour, out sobel);
  1969. //计算粗糙度
  1970. int max = 0;
  1971. //先上下遍历一遍寻找最大值,避免找不到胶体
  1972. for (int i = (int)ordinateL3 + 20; i < (int)ordinateL4 - 15; i++)
  1973. {
  1974. for (int j = leftBorder1; j < rightBorder1; j++)
  1975. {
  1976. if (sobel.Get<byte>(i, j) > 0)
  1977. {
  1978. if (Math.Abs(j - (int)(ordinateV2)) > max)
  1979. {
  1980. max = Math.Abs(j - (int)(ordinateV2));
  1981. roughnessOrdinate[0] = (int)ordinateV2;
  1982. roughnessOrdinate[1] = i;
  1983. roughnessOrdinate[2] = j;
  1984. roughnessOrdinate[3] = i;
  1985. }
  1986. }
  1987. }
  1988. }
  1989. for (int i = (int)ordinateL5 + 15; i < (int)ordinateL6 - 10; i++)
  1990. {
  1991. for (int j = leftBorder2; j < rightBorder2; j++)
  1992. {
  1993. if (sobel.Get<byte>(i, j) > 0)
  1994. {
  1995. if (Math.Abs(j - (ordinateV4)) > max)
  1996. {
  1997. max = Math.Abs(j - (ordinateV4));
  1998. roughnessOrdinate[0] = (int)ordinateV4;
  1999. roughnessOrdinate[1] = i;
  2000. roughnessOrdinate[2] = j;
  2001. roughnessOrdinate[3] = i;
  2002. }
  2003. }
  2004. }
  2005. }
  2006. roughness = max;
  2007. if (roughness < 10)
  2008. {
  2009. //判断胶体方向
  2010. Cv2.EqualizeHist(imageRed, imageRed);
  2011. Mat upperCrop = imageRed[(int)ordinateL3 + 10, (int)ordinateL4 - 10, dataArea[0], dataArea[1]];
  2012. Mat lowerCrop = imageRed[(int)ordinateL5 + 10, (int)ordinateL6 - 10, dataArea[0], dataArea[1]];
  2013. //Mat duibi = new Mat(upperCrop.Size().Height + lowerCrop.Size().Height, upperCrop.Size().Width, upperCrop.Type());
  2014. //upperCrop.CopyTo(duibi[0, upperCrop.Size().Height - 1, 0, upperCrop.Size().Width - 1]);
  2015. //lowerCrop.CopyTo(duibi[upperCrop.Size().Height, duibi.Size().Height - 1, 0, upperCrop.Size().Width - 1]);
  2016. //new Window("upperCrop", WindowMode.Normal, upperCrop);
  2017. //new Window("lowerCrop", WindowMode.Normal, lowerCrop);
  2018. //new Window("imageRed", WindowMode.Normal, imageRed);
  2019. //Cv2.WaitKey();
  2020. //Cv2.EqualizeHist(duibi, duibi);
  2021. //Scalar s1 = duibi[0, upperCrop.Size().Height - 1, 0, upperCrop.Size().Width - 1].Sum();
  2022. //Scalar s2 = duibi[upperCrop.Size().Height, duibi.Size().Height - 1, 0, upperCrop.Size().Width - 1].Sum();
  2023. Scalar s1 = upperCrop.Sum();
  2024. Scalar s2 = lowerCrop.Sum();
  2025. averSaturation1 = (double)s1 / (upperCrop.Cols * upperCrop.Rows);
  2026. averSaturation2 = (double)s2 / (lowerCrop.Cols * lowerCrop.Rows);
  2027. for (int i = 0; i < upperCrop.Rows; i++)
  2028. {
  2029. for (int j = 0; j < upperCrop.Cols; j++)
  2030. {
  2031. sum += Math.Abs(upperCrop.Get<byte>(i, j) - (int)averSaturation1);
  2032. }
  2033. }
  2034. double biaozhuncha1 = sum / (upperCrop.Cols * upperCrop.Rows);
  2035. sum = 0;
  2036. for (int i = 0; i < lowerCrop.Rows; i++)
  2037. {
  2038. for (int j = 0; j < lowerCrop.Cols; j++)
  2039. {
  2040. sum += Math.Abs(lowerCrop.Get<byte>(i, j) - (int)averSaturation2);
  2041. }
  2042. }
  2043. double biaozhuncha2 = sum / (lowerCrop.Cols * lowerCrop.Rows);
  2044. int direction2 = 0;
  2045. if (biaozhuncha1 > biaozhuncha2)
  2046. direction2 = 0;//等于0时说明胶体在上面,否则是下面
  2047. else
  2048. direction2 = 1;
  2049. switch (direction2)
  2050. {
  2051. case 0:
  2052. for (int i = (int)ordinateL3 + 10; i < (int)ordinateL4 - 15; i++)
  2053. {
  2054. for (int j = leftBorder1; j < rightBorder1; j++)
  2055. {
  2056. if (sobel.Get<byte>(i, j) > 0)
  2057. {
  2058. if (Math.Abs(j - (int)(ordinateV2)) > max)
  2059. {
  2060. max = Math.Abs(j - (int)(ordinateV2));
  2061. roughnessOrdinate[0] = (int)ordinateV2;
  2062. roughnessOrdinate[1] = i;
  2063. roughnessOrdinate[2] = j;
  2064. roughnessOrdinate[3] = i;
  2065. }
  2066. }
  2067. }
  2068. }
  2069. roughness = max;
  2070. break;
  2071. case 1:
  2072. for (int i = (int)ordinateL5 + 15; i < (int)ordinateL6 - 10; i++)
  2073. {
  2074. for (int j = leftBorder2; j < rightBorder2; j++)
  2075. {
  2076. if (sobel.Get<byte>(i, j) > 0)
  2077. {
  2078. if (Math.Abs(j - (int)(ordinateV4)) > max)
  2079. {
  2080. max = Math.Abs(j - (int)(ordinateV4));
  2081. roughnessOrdinate[0] = (int)ordinateV4;
  2082. roughnessOrdinate[1] = i;
  2083. roughnessOrdinate[2] = j;
  2084. roughnessOrdinate[3] = i;
  2085. }
  2086. }
  2087. }
  2088. }
  2089. roughness = max;
  2090. break;
  2091. }
  2092. }
  2093. }
  2094. /// <summary>
  2095. /// 提取四层板的粗糙度
  2096. /// </summary>
  2097. /// <param name="imageContour">二值图像</param>
  2098. /// <param name="upperBorder">上界</param>
  2099. /// <param name="lowerBorder">下界</param>
  2100. /// <param name="basic">边缘线</param>
  2101. /// <param name="direction">槽孔所在左右方向,left、right</param>
  2102. /// <param name="roughness">输出粗糙度</param>
  2103. /// <param name="roughnessOrdinate">输出粗糙度坐标</param>
  2104. public void GetSicengRoughness(Mat imageContour, int upperBorder, int lowerBorder, int basic, string direction, out int roughness, out int[] roughnessOrdinate)
  2105. {
  2106. int leftBorder = 0, rightBorder = 0;
  2107. int range = 70;
  2108. switch (direction)
  2109. {
  2110. case "right":
  2111. leftBorder = basic;
  2112. rightBorder = basic + range;
  2113. break;
  2114. case "left":
  2115. leftBorder = basic - range;
  2116. rightBorder = basic;
  2117. break;
  2118. }
  2119. int max = 0;
  2120. roughness = 0;
  2121. roughnessOrdinate = new int[4];
  2122. Mat sobel = new Mat();
  2123. Sobel(imageContour, out sobel);
  2124. for (int i = upperBorder; i < lowerBorder; i++)
  2125. {
  2126. for (int j = leftBorder; j < rightBorder; j++)
  2127. {
  2128. if (sobel.Get<byte>(i, j) > 0)
  2129. {
  2130. if (Math.Abs(j - basic) > max)
  2131. {
  2132. max = Math.Abs(j - basic);
  2133. roughnessOrdinate[0] = basic;
  2134. roughnessOrdinate[1] = i;
  2135. roughnessOrdinate[2] = j;
  2136. roughnessOrdinate[3] = i;
  2137. }
  2138. }
  2139. }
  2140. }
  2141. roughness = max;
  2142. }
  2143. public void GetNewKongtong(Mat imageRed, Mat imageContour, int[] leftAperture, int[] rightAperture, double leftOrdinateMiantong, int leftMiddleMianJicaitong, double leftOrdinate3, double rightOrdinateMiantong, int rightMiddleMianJicaitong, double rightOrdinate3, out double[] kongtong, out int[] leftKongtong, out int[] rightKongtong, out int[] leftPoint2, out int[] rightPoint2)
  2144. {
  2145. kongtong = new double[2];
  2146. leftKongtong = new int[2];
  2147. rightKongtong = new int[2];
  2148. leftPoint2 = new int[2];
  2149. rightPoint2 = new int[2];
  2150. #region//左侧
  2151. Mat leftCrop = imageRed[(int)leftOrdinateMiantong + 40, (int)leftOrdinate3 - 10, leftMiddleMianJicaitong, leftAperture[1]].Clone();
  2152. Mat leftContour = new Mat();
  2153. double t = Cv2.Threshold(leftCrop, leftContour, 0, 1, ThresholdTypes.Otsu);
  2154. Mat leftFanse = 1 - leftContour;
  2155. InputArray leftKernel = InputArray.Create<int>(new int[17, 17] {
  2156. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2157. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2158. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2159. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2160. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2161. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2162. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2163. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2164. { 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 },
  2165. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2166. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2167. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2168. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2169. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2170. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2171. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2172. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2173. });
  2174. InputArray leftKernel2 = InputArray.Create<int>(new int[17, 17] {
  2175. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2176. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2177. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2178. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2179. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2180. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2181. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2182. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2183. { 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 },
  2184. { 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0 },
  2185. { 0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0 },
  2186. { 0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0 },
  2187. { 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0 },
  2188. { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0 },
  2189. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0 },
  2190. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0 },
  2191. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
  2192. });
  2193. InputArray leftKernel3 = InputArray.Create<int>(new int[17, 17] {
  2194. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2195. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2196. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2197. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2198. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2199. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2200. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2201. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2202. { 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 },
  2203. { 0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0 },
  2204. { 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0 },
  2205. { 0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0 },
  2206. { 0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0 },
  2207. { 0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0 },
  2208. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0 },
  2209. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0 },
  2210. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1 },
  2211. });
  2212. Mat leftFilter = new Mat();
  2213. //Cv2.Filter2D(leftFanse, leftFilter, -1, leftKernel, new Point(-1, -1), 0);
  2214. //Cv2.Filter2D(leftFanse, leftFilter, -1, leftKernel2, new Point(-1, -1), 0);
  2215. Cv2.Filter2D(leftFanse, leftFilter, -1, leftKernel3, new Point(-1, -1), 0);
  2216. Mat leftThresh = leftFilter.Threshold(14, 1, ThresholdTypes.Binary);
  2217. for (int j = leftThresh.Cols - 1; j > 0; j--)//从右上角,以135°方向遍历
  2218. {
  2219. for (int k = j; k < leftThresh.Cols && (k - j < leftThresh.Rows); k++)
  2220. {
  2221. if (leftThresh.Get<byte>(k - j, k) > 0)
  2222. {
  2223. leftKongtong[0] = k - j + (int)leftOrdinateMiantong + 40;//纵
  2224. leftKongtong[1] = k + leftMiddleMianJicaitong;//横
  2225. //Cv2.Circle(imageRed, k + leftMiddleMianJicaitong + border, k - j + (int)leftOrdinateMiantong + upper - 10, 1, 1, 1);
  2226. //new Window("imageRed_Line", WindowMode.Normal, imageRed);
  2227. break;
  2228. }
  2229. }
  2230. if (leftKongtong[0] != 0)
  2231. break;
  2232. }
  2233. #endregion
  2234. #region//右侧
  2235. Mat rightCrop = imageRed[(int)rightOrdinateMiantong + 40, (int)rightOrdinate3 - 10, rightAperture[1], rightMiddleMianJicaitong].Clone();
  2236. Mat rightContour = new Mat();
  2237. double t1 = Cv2.Threshold(rightCrop, rightContour, 0, 1, ThresholdTypes.Otsu);
  2238. Mat rightFanse = 1 - rightContour;
  2239. InputArray rightKernel = InputArray.Create<int>(new int[17, 17] {
  2240. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2241. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2242. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2243. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2244. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2245. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2246. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2247. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2248. { 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 },
  2249. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2250. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2251. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2252. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2253. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2254. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2255. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2256. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2257. });
  2258. InputArray rightKernel2 = InputArray.Create<int>(new int[17, 17] {
  2259. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2260. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2261. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2262. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2263. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2264. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2265. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2266. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2267. { 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 },
  2268. { 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0 },
  2269. { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0 },
  2270. { 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0 },
  2271. { 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0 },
  2272. { 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2273. { 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2274. { 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2275. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2276. });
  2277. InputArray rightKernel3 = InputArray.Create<int>(new int[17, 17] {
  2278. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2279. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2280. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2281. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2282. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2283. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2284. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2285. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2286. { 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 },
  2287. { 0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0 },
  2288. { 0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0 },
  2289. { 0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0 },
  2290. { 0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0 },
  2291. { 0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2292. { 0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2293. { 0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2294. { 1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2295. });
  2296. Mat rightFilter = new Mat();
  2297. //Cv2.Filter2D(rightFanse, rightFilter, -1, rightKernel, new Point(-1, -1), 0);
  2298. //Cv2.Filter2D(rightFanse, rightFilter, -1, rightKernel2, new Point(-1, -1), 0);
  2299. Cv2.Filter2D(rightFanse, rightFilter, -1, rightKernel3, new Point(-1, -1), 0);
  2300. Mat rightThresh = rightFilter.Threshold(14, 1, ThresholdTypes.Binary);
  2301. for (int j = 0; j < rightThresh.Cols; j++)//从左上角,45°方向遍历
  2302. {
  2303. for (int k = j; k >= 0 && j - k < rightThresh.Rows; k--)
  2304. {
  2305. if (rightThresh.Get<byte>(j - k, k) > 0)
  2306. {
  2307. rightKongtong[0] = j - k + (int)rightOrdinateMiantong + 40;
  2308. rightKongtong[1] = k + rightAperture[1];
  2309. //Cv2.Circle(imageRed, k + rightAperture[1], k - j + (int)rightOrdinateMiantong + upper - 10, 1, 1, 1);
  2310. //new Window("imageRed_Line", WindowMode.Normal, imageRed);
  2311. break;
  2312. }
  2313. }
  2314. if (rightKongtong[0] != 0)
  2315. break;
  2316. }
  2317. #endregion
  2318. //ImageShow(leftCrop, leftContour * 255, /*rightContour * 255, */leftThresh * 255/*, rightThresh * 255*/);
  2319. //Cv2.Circle(imageRed, new Point(leftKongtong[1], leftKongtong[0]), 10, new Scalar(0), 2);
  2320. //Cv2.Circle(imageRed, new Point(rightKongtong[1], rightKongtong[0]), 10, new Scalar(0), 2);
  2321. ////ImageShow(imageRed);
  2322. GetKongtong(imageContour, leftKongtong, rightKongtong, out kongtong, out leftPoint2, out rightPoint2);
  2323. //LineShow(imageRed, leftKongtong[1], leftKongtong[0], leftPoint2[1], leftPoint2[0]);
  2324. //LineShow(imageRed, rightKongtong[1], rightKongtong[0], rightPoint2[1], rightPoint2[0]);
  2325. //ImageShow(imageRed);
  2326. //leftKongtong[0] -= upper;
  2327. //rightKongtong[0] -= upper;
  2328. //leftPoint2[0] -= upper;
  2329. //rightPoint2[0] -= upper;
  2330. if(rightFilter != null)
  2331. {
  2332. rightFilter.Dispose();
  2333. }
  2334. }
  2335. /// <summary>
  2336. /// 计算孔铜
  2337. /// </summary>
  2338. /// <param name="imageContour">二值图</param>
  2339. /// <param name="apertureBegin">左孔径坐标</param>
  2340. /// <param name="apertureEnd">右孔径坐标</param>
  2341. /// <param name="kongtong">输出左右孔铜的距离</param>
  2342. /// <param name="pointLeft">左孔铜对应边缘线的坐标</param>
  2343. /// <param name="pointRight">右孔铜对应边缘线的坐标</param>
  2344. public void GetKongtong(Mat imageContour, int[] apertureBegin, int[] apertureEnd, out double[] kongtong, out int[] pointLeft, out int[] pointRight)
  2345. {
  2346. //曲面上点的坐标
  2347. int count = apertureEnd[1] - apertureBegin[1];
  2348. int[,] coordinate = new int[Math.Abs(apertureEnd[1] - apertureBegin[1]), 2];//先纵坐标后横坐标
  2349. for (int j = apertureBegin[1]; j < apertureEnd[1]; j++)
  2350. {
  2351. for (int i = 0; i < imageContour.Rows; i++)
  2352. {
  2353. if (imageContour.Get<byte>(i, j) > 0)
  2354. {
  2355. coordinate[j - apertureBegin[1], 0] = i;
  2356. coordinate[j - apertureBegin[1], 1] = j;
  2357. break;
  2358. }
  2359. }
  2360. }
  2361. //计算距离,最小的距离分别为左孔铜和右孔铜
  2362. double leftKongtong = 1000;
  2363. double rightKongtong = 1000;
  2364. double distance1 = 0;
  2365. double distance2 = 0;
  2366. //当距离最短是曲线上点的坐标,第一列是纵坐标(行数),第二列是横坐标(列数)
  2367. pointLeft = new int[2];
  2368. pointRight = new int[2];
  2369. for (int i = 0; i < count; i++)
  2370. {
  2371. //计算曲面点到左起始点的距离
  2372. if (i < count / 2)
  2373. {
  2374. distance1 = Math.Sqrt(Math.Pow((coordinate[i, 0] - apertureBegin[0]), 2) + Math.Pow((coordinate[i, 1] - apertureBegin[1]), 2));
  2375. //计算曲面点到右截止点的距离
  2376. if (leftKongtong > distance1)
  2377. {
  2378. leftKongtong = distance1;
  2379. pointLeft[0] = coordinate[i, 0];
  2380. pointLeft[1] = coordinate[i, 1];
  2381. }
  2382. }
  2383. else
  2384. {
  2385. distance2 = Math.Sqrt(Math.Pow((coordinate[i, 0] - apertureEnd[0]), 2) + Math.Pow((coordinate[i, 1] - apertureEnd[1]), 2));
  2386. //得到左右孔铜以及对应的曲面坐标
  2387. if (rightKongtong > distance2)
  2388. {
  2389. rightKongtong = distance2;
  2390. pointRight[0] = coordinate[i, 0];
  2391. pointRight[1] = coordinate[i, 1];
  2392. }
  2393. }
  2394. }
  2395. kongtong = new double[2] { leftKongtong, rightKongtong };
  2396. }
  2397. public void ShenmangkongNewKongtong(Mat image, Mat imageContour, int leftL2, int rightL2, int[] leftAperture, int[] rightAperture, out double[] kongtong, out int[] leftKongtong, out int[] rightKongtong, out int[] leftPoint2, out int[] rightPoint2)
  2398. {
  2399. kongtong = new double[2];
  2400. leftKongtong = new int[2];
  2401. rightKongtong = new int[2];
  2402. leftPoint2 = new int[2];
  2403. rightPoint2 = new int[2];
  2404. #region//左
  2405. Mat leftCrop = image[leftL2 - 10, leftL2 + 20, leftAperture[1] - 30, leftAperture[1] + 5].Clone();
  2406. Mat leftThresh = leftCrop.Threshold(0, 1, ThresholdTypes.Otsu);
  2407. Mat leftFanse = 1 - leftThresh;
  2408. InputArray leftKernel = InputArray.Create<int>(new int[17, 17] {
  2409. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2410. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2411. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2412. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2413. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2414. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2415. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2416. { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0 },
  2417. { 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2418. { 1, 1, 1, 1, 1, 1, 1, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2419. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2420. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2421. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2422. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2423. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2424. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2425. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2426. });
  2427. InputArray leftKernel2 = InputArray.Create<int>(new int[17, 17] {
  2428. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2429. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2430. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2431. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2432. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2433. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2434. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2435. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2436. { 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 },
  2437. { 0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0 },
  2438. { 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0 },
  2439. { 0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0 },
  2440. { 0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0 },
  2441. { 0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0 },
  2442. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0 },
  2443. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0 },
  2444. { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1 },
  2445. });
  2446. Mat leftFilter1 = new Mat();
  2447. Cv2.Filter2D(leftFanse, leftFilter1, -1, leftKernel, new Point(-1, -1), 0);
  2448. Mat leftFilter2 = new Mat();
  2449. Cv2.Filter2D(leftFanse, leftFilter2, -1, leftKernel2, new Point(-1, -1), 0);
  2450. Mat leftFilter = new Mat();
  2451. Cv2.AddWeighted(leftFilter1, 0, leftFilter2, 1, 0, leftFilter);
  2452. Cv2.Rectangle(leftFilter, new Rect(0, 0, leftFilter.Cols, 5), new Scalar(0), -1);
  2453. Cv2.Rectangle(leftFilter, new Rect(0, 15, leftFilter.Cols, leftFilter.Rows - 15), new Scalar(0), -1);
  2454. double min, max;
  2455. Cv2.MinMaxIdx(leftFilter, out min, out max);
  2456. Mat leftFilterThresh = leftFilter.Threshold(max - 2, 1, ThresholdTypes.Binary);
  2457. Cv2.Flip(leftFilterThresh, leftFilterThresh, FlipMode.Y);
  2458. Point left;
  2459. FindLeftTop(leftFilterThresh, out left);
  2460. leftKongtong[0] = left.Y + leftL2 - 10;
  2461. leftKongtong[1] = leftCrop.Cols - left.X + leftAperture[1] - 30;
  2462. Mat newLeft = leftFilterThresh.Clone();
  2463. newLeft.Set<byte>(left.Y, left.X, 255);
  2464. //ImageShow(leftCrop, leftFilter * 50, leftFilterThresh * 255, newLeft);
  2465. #endregion
  2466. #region//右
  2467. Mat rightCrop = image[rightL2 - 10, rightL2 + 20, rightAperture[1] - 5, rightAperture[1] + 30].Clone();
  2468. Mat rightThresh = rightCrop.Threshold(0, 1, ThresholdTypes.Otsu);
  2469. Mat rightFanse = 1 - rightThresh;
  2470. InputArray rightKernel = InputArray.Create<int>(new int[17, 17] {
  2471. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2472. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2473. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2474. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2475. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2476. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2477. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2478. { 0, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 },
  2479. { 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2480. { 0, 0, 0, 0, 0, 0,-1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
  2481. { 0, 0, 0, 0, 0,-1, 0,-1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
  2482. { 0, 0, 0, 0,-1, 0, 1,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
  2483. { 0, 0, 0,-1, 0, 1, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
  2484. { 0, 0,-1, 0, 1, 0, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
  2485. { 0,-1, 0, 1, 0, 0, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
  2486. { -1, 0, 1, 0, 0, 0, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
  2487. { 0, 1, 0, 0, 0, 0, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
  2488. });
  2489. InputArray rightKernel2 = InputArray.Create<int>(new int[17, 17] {
  2490. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2491. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2492. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2493. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2494. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2495. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2496. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2497. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
  2498. { 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 },
  2499. { 0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0 },
  2500. { 0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0 },
  2501. { 0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0 },
  2502. { 0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0 },
  2503. { 0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2504. { 0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2505. { 0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2506. { 1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  2507. });
  2508. Mat rightFilter1 = new Mat();
  2509. Cv2.Filter2D(rightFanse, rightFilter1, -1, rightKernel, new Point(-1, -1), 0);
  2510. Mat rightFilter2 = new Mat();
  2511. Cv2.Filter2D(rightFanse, rightFilter2, -1, rightKernel2, new Point(-1, -1), 0);
  2512. Mat rightFilter = new Mat();
  2513. Cv2.AddWeighted(rightFilter1, 0, rightFilter2, 1, 0, rightFilter);
  2514. Cv2.Rectangle(rightFilter, new Rect(0, 0, rightFilter.Cols, 5), new Scalar(0), -1);
  2515. Cv2.Rectangle(rightFilter, new Rect(0, 15, rightFilter.Cols, rightFilter.Rows - 15), new Scalar(0), -1);
  2516. Cv2.MinMaxIdx(rightFilter, out min, out max);
  2517. Mat rightFilterThresh = rightFilter.Threshold(max - 2, 1, ThresholdTypes.Binary);
  2518. Point right;
  2519. FindLeftTop(rightFilterThresh, out right);
  2520. rightKongtong[0] = right.Y + rightL2 - 10;
  2521. rightKongtong[1] = right.X + rightAperture[1] - 5;
  2522. Mat newRight = rightFilterThresh.Clone();
  2523. newRight.Set<byte>(right.Y, right.X, 255);
  2524. //ImageShow(rightCrop, rightThresh * 255, rightFilter2 * 50, rightFilterThresh * 255, newRight);
  2525. #endregion
  2526. #region//边缘线
  2527. GetKongtong(imageContour, leftKongtong, rightKongtong, out kongtong, out leftPoint2, out rightPoint2);
  2528. #endregion
  2529. }
  2530. /// <summary>
  2531. /// 计算最小孔环,从外侧交点到上孔径
  2532. /// </summary>
  2533. /// <param name="imageContour"></param>
  2534. /// <param name="upper"></param>
  2535. /// <param name="lower"></param>
  2536. /// <param name="leftBorder"></param>
  2537. /// <param name="rightBorder"></param>
  2538. /// <param name="pointRing"></param>
  2539. /// <param name="direction"></param>
  2540. public void GetMinmumRing(Mat imageContour, int upper, int lower, int leftBorder, int rightBorder, out int[] pointRing, string direction)
  2541. {
  2542. pointRing = new int[2];
  2543. Mat crop2 = imageContour[upper, lower, leftBorder, rightBorder].Clone();
  2544. Scalar sum = new Scalar(0);
  2545. Scalar lastSum = new Scalar(0);
  2546. switch (direction)
  2547. {
  2548. case "left":
  2549. //for (int j = 0; j < crop2.Cols - 1; j += 5)
  2550. //{
  2551. // sum = crop2[0, crop2.Rows, j, j + 1].Sum();
  2552. // if ((int)lastSum != 0 && (int)sum-(int)lastSum>10)
  2553. // {
  2554. // pointRing[1] = j+leftBorder-2;
  2555. // break;
  2556. // }
  2557. // lastSum = sum;
  2558. //}
  2559. for (int j = crop2.Cols - 1; j > 1; j -= 10)
  2560. {
  2561. sum = crop2[0, crop2.Rows, j - 1, j].Sum();
  2562. if ((int)lastSum != 0 && (int)lastSum - (int)sum > 10)
  2563. {
  2564. pointRing[1] = j + leftBorder - 5;
  2565. break;
  2566. }
  2567. lastSum = sum;
  2568. }
  2569. break;
  2570. case "right":
  2571. //for (int j = crop2.Cols - 1; j > 2; j -= 5)
  2572. //{
  2573. // sum = crop2[0, crop2.Rows, j - 1, j].Sum();
  2574. // if ((int)lastSum != 0 && (int)sum - (int)lastSum > 10)
  2575. // {
  2576. // pointRing[1] = j + leftBorder+2;
  2577. // break;
  2578. // }
  2579. // lastSum = sum;
  2580. //}
  2581. for (int j = 1; j < crop2.Cols - 1; j += 10)
  2582. {
  2583. sum = crop2[0, crop2.Rows, j, j + 1].Sum();
  2584. if ((int)lastSum != 0 && (int)lastSum - (int)sum > 10)
  2585. {
  2586. pointRing[1] = j + leftBorder + 5;
  2587. break;
  2588. }
  2589. lastSum = sum;
  2590. }
  2591. break;
  2592. }
  2593. //ImageShow(crop2 * 255);
  2594. //int j = 0;
  2595. //for (int i = crop2.Rows-1;i>0;i--)
  2596. //{
  2597. // for (int k = i; k < crop2.Rows&&(crop2.Cols-1-(k-i))>0; k++)
  2598. // {
  2599. // j = crop2.Cols - 1 - (k - i);
  2600. // if (crop2.Get<byte>(k, j) > 0)
  2601. // {
  2602. // pointLeftRing[0] = k + upper;
  2603. // pointLeftRing[1] = j ;
  2604. // break;
  2605. // }
  2606. // }
  2607. //}
  2608. //ImageShow(filter * 30, thresh * 255,crop*255,crop2*255);
  2609. }
  2610. /// <summary>
  2611. /// 得到边缘线上拐点的坐标
  2612. /// </summary>
  2613. /// <param name="imageContour">二值图</param>
  2614. /// <param name="apertureBegin">左孔径坐标</param>
  2615. /// <param name="apertureEnd">右孔径坐标</param>
  2616. /// <param name="curveVertex">输出拐点坐标,0:縱坐標;1:橫坐標</param>
  2617. /// <param name="middleApertureY">孔径对应边缘线上点的平均纵坐标</param>
  2618. public void CurveVertex(Mat imageContour, int[] apertureBegin, int[] apertureEnd, out int[] curveVertex, out double middleApertureY)
  2619. {
  2620. //曲面顶点的坐标
  2621. curveVertex = new int[2];
  2622. //孔径平均横坐标
  2623. double middleAperture = (apertureBegin[1] + apertureEnd[1]) / 2;
  2624. //孔径平均高度
  2625. int[] apertureY = new int[2];
  2626. for (int i = 0; i < imageContour.Rows; i++)
  2627. {
  2628. if (imageContour.Get<byte>(i, apertureBegin[1]) > 0)
  2629. {
  2630. apertureY[0] = i;
  2631. break;
  2632. }
  2633. }
  2634. for (int i = 0; i < imageContour.Rows; i++)
  2635. {
  2636. if (imageContour.Get<byte>(i, apertureEnd[1]) > 0)
  2637. {
  2638. apertureY[1] = i;
  2639. break;
  2640. }
  2641. }
  2642. middleApertureY = (apertureY[0] + apertureY[1]) / 2;
  2643. //边缘线坐标
  2644. int count = Math.Abs((apertureEnd[1] - apertureBegin[1]) / 2);
  2645. int[,] ordinate = new int[count, 2];
  2646. int range = apertureEnd[1] - apertureBegin[1];
  2647. for (int j = range / 4 + apertureBegin[1]; j < range / 4 + apertureBegin[1] + count; j++)
  2648. {
  2649. for (int i = 0; i < imageContour.Rows; i++)
  2650. {
  2651. if (imageContour.Get<byte>(i, j) > 0)
  2652. {
  2653. ordinate[j - apertureBegin[1] - range / 4, 0] = i;
  2654. ordinate[j - apertureBegin[1] - range / 4, 1] = j;
  2655. break;
  2656. }
  2657. }
  2658. }
  2659. //孔深,取曲面上的点到孔径平均高度只差的最大值
  2660. double kongshenAomian = 0;//当边缘线内凹或外凸,孔深最大值
  2661. //double kongshenTumian = 1000;//当边缘线内凸,孔深最小值
  2662. int[] coordinateAomian = new int[2];
  2663. //int[] coordinateTumian = new int[2];
  2664. double t = 0;
  2665. for (int i = 0; i < count; i++)
  2666. {
  2667. t = Math.Abs(middleApertureY - ordinate[i, 0]);
  2668. if (kongshenAomian < t)
  2669. {
  2670. kongshenAomian = t;
  2671. coordinateAomian[0] = ordinate[i, 0];
  2672. coordinateAomian[1] = ordinate[i, 1];
  2673. }
  2674. //if (kongshenTumian > t)
  2675. //{
  2676. // kongshenTumian = t;
  2677. // coordinateTumian[0] = ordinate[i, 0];
  2678. // coordinateTumian[1] = ordinate[i, 1];
  2679. //}
  2680. }
  2681. curveVertex = coordinateAomian;
  2682. //判断哪个点离孔径中心更近,更近的为曲面坐标
  2683. //double absAomian = Math.Abs(coordinateAomian[1] - middleAperture);
  2684. //double absTumian = Math.Abs(coordinateTumian[1] - middleAperture);
  2685. //if (absAomian < absTumian)
  2686. //{
  2687. // curveVertex = coordinateAomian;
  2688. //}
  2689. //else
  2690. //{
  2691. // curveVertex = coordinateTumian;
  2692. //}
  2693. }
  2694. public void ShenmangkongShangkongjing(Mat contour, int leftL3, int rightL3, int[] lowerAperture, out int[] leftAperture, out int[] rightAperture)
  2695. {
  2696. #region//左
  2697. Mat cropLeft = contour[leftL3 - 20, leftL3 + 20, (lowerAperture[0] - 200)<0?0:(lowerAperture[0] - 200), lowerAperture[0] + 200].Clone();
  2698. InputArray leftKernel = InputArray.Create<int>(new int[17, 17] {
  2699. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2700. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2701. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2702. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2703. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2704. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2705. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2706. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
  2707. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
  2708. { -1,-1,-1,-1,-1,-1,-1,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
  2709. { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
  2710. { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
  2711. { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
  2712. { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
  2713. { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
  2714. { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
  2715. { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
  2716. });
  2717. Mat leftFilter = new Mat();
  2718. Cv2.Filter2D(cropLeft, leftFilter, -1, leftKernel, new Point(-1, -1), 0);
  2719. double min2, max;
  2720. Cv2.MinMaxIdx(leftFilter, out min2, out max);
  2721. Mat leftThresh = leftFilter.Threshold(max - 2, 1, ThresholdTypes.Binary);
  2722. Cv2.Flip(leftThresh, leftThresh, FlipMode.Y);
  2723. leftAperture = new int[2];
  2724. int min = 100000;
  2725. Point[] leftIdx;
  2726. FindNonZeros(leftThresh, out leftIdx);
  2727. for (int i = 0; i < leftIdx.Length; i++)
  2728. {
  2729. if (min > (leftIdx[i].X + leftIdx[i].Y))
  2730. {
  2731. min = leftIdx[i].X + leftIdx[i].Y;
  2732. leftAperture[1] = leftThresh.Cols - leftIdx[i].X;
  2733. leftAperture[0] = leftIdx[i].Y;
  2734. }
  2735. }
  2736. leftAperture[1] += lowerAperture[0] - 200;
  2737. leftAperture[0] += leftL3 - 20;
  2738. #endregion
  2739. #region//右
  2740. Mat cropRight = contour[rightL3 - 20, rightL3 + 20, lowerAperture[1] - 200, lowerAperture[1] + 200].Clone();
  2741. InputArray rightKernel = InputArray.Create<int>(new int[17, 17] {
  2742. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2743. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2744. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2745. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2746. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2747. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2748. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2749. { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  2750. { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  2751. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1,-1,-1,-1,-1,-1,-1,-1 },
  2752. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2753. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2754. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2755. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2756. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2757. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2758. { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
  2759. });
  2760. Mat rightFilter = new Mat();
  2761. Cv2.Filter2D(cropRight, rightFilter, -1, rightKernel, new Point(-1, -1), 0);
  2762. Cv2.MinMaxIdx(rightFilter, out min2, out max);
  2763. Mat rightThresh = rightFilter.Threshold(max - 2, 1, ThresholdTypes.Binary);
  2764. rightAperture = new int[2];
  2765. Point[] rightIdx;
  2766. FindNonZeros(rightThresh, out rightIdx);
  2767. min = 1000000;
  2768. for (int i = 0; i < rightIdx.Length; i++)
  2769. {
  2770. if (min > (rightIdx[i].X + rightIdx[i].Y))
  2771. {
  2772. min = rightIdx[i].X + rightIdx[i].Y;
  2773. rightAperture[1] = rightIdx[i].X;
  2774. rightAperture[0] = rightIdx[i].Y;
  2775. }
  2776. }
  2777. rightAperture[1] += lowerAperture[1] - 200;
  2778. rightAperture[0] += rightL3 - 20;
  2779. #endregion
  2780. //ImageShow( cropRight * 255, rightThresh * 255);
  2781. }
  2782. /// <summary>
  2783. /// 得到深盲孔的下孔径
  2784. /// </summary>
  2785. /// <param name="imageContour">二值图像</param>
  2786. /// <param name="aperture">输出下孔径</param>
  2787. /// <param name="leftUpper">左边计算区域的上边界</param>
  2788. /// <param name="rightUpper">右边计算区域的上边界</param>
  2789. /// <param name="leftLower">左边计算区域的下边界</param>
  2790. /// <param name="rightLower">右边计算区域的下边界</param>
  2791. /// <param name="dataArea">数据提取区域</param>
  2792. public void GetShenmangLowerAperture(Mat imageContour, out int[] aperture, int leftUpper, int rightUpper, int leftLower, int rightLower, int[] dataArea)
  2793. {
  2794. //孔径起始点
  2795. aperture = new int[2];
  2796. aperture[0] = 0;
  2797. aperture[1] = 0;
  2798. int middle = (dataArea[1] + dataArea[2]) / 2;
  2799. Mat fanse = 1 - imageContour;
  2800. //Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  2801. //Mat open = new Mat();
  2802. //Cv2.MorphologyEx(fanse, open,MorphTypes.Open, se);
  2803. //fanse = open;
  2804. for (int j = middle; j > dataArea[0]; j--)
  2805. {
  2806. for (int i = leftUpper - 20 < 0 ? 0 : leftUpper - 20; i < leftLower - 20; i++)
  2807. {
  2808. if (fanse.Get<byte>(i, j) > 0)
  2809. {
  2810. aperture[0] = j;
  2811. break;
  2812. }
  2813. }
  2814. if (aperture[0] != 0)
  2815. break;
  2816. }
  2817. for (int j = middle; j < dataArea[3]; j++)
  2818. {
  2819. for (int i = rightUpper - 20 < 0 ? 0 : rightUpper - 20; i < rightLower - 20; i++)
  2820. {
  2821. if (fanse.Get<byte>(i, j) > 0)
  2822. {
  2823. aperture[1] = j;
  2824. break;
  2825. }
  2826. }
  2827. if (aperture[1] != 0)
  2828. break;
  2829. }
  2830. }
  2831. /// <summary>
  2832. /// 下孔径新方法,用胶体部分置1,向中间遍历,为0时下孔径
  2833. /// </summary>
  2834. /// <param name="imageContour"></param>
  2835. /// <param name="aperture"></param>
  2836. /// <param name="leftUpper"></param>
  2837. /// <param name="rightUpper"></param>
  2838. /// <param name="leftLower"></param>
  2839. /// <param name="rightLower"></param>
  2840. /// <param name="dataArea"></param>
  2841. public void GetLowerAperture(Mat imageContour, out int[] aperture, int leftUpper, int rightUpper, int leftLower, int rightLower, int[] dataArea)
  2842. {
  2843. aperture = new int[2];
  2844. int middle = (dataArea[1] + dataArea[2]) / 2;
  2845. Mat fill = new Mat();
  2846. Fill(imageContour, out fill, 1);
  2847. Mat fanse = 1 - fill;
  2848. Scalar sum = fanse.Sum();
  2849. if ((int)sum != 0)
  2850. {
  2851. imageContour = fill.Clone();
  2852. }
  2853. Mat result = 1 - imageContour;
  2854. Scalar sumLeft = new Scalar(0);
  2855. for (int j = dataArea[0]; j < middle; j++)
  2856. {
  2857. sumLeft = result[leftUpper<0?0: leftUpper, leftLower, j, j + 1].Sum();
  2858. if ((int)sumLeft == 0)
  2859. {
  2860. aperture[0] = j;
  2861. break;
  2862. }
  2863. }
  2864. Scalar sumRight = new Scalar(0);
  2865. for (int j = dataArea[3]; j > middle; j--)
  2866. {
  2867. sumRight = result[rightUpper, rightLower, j - 1, j].Sum();
  2868. if ((int)sumRight == 0)
  2869. {
  2870. aperture[1] = j;
  2871. break;
  2872. }
  2873. }
  2874. if(fill != null)
  2875. {
  2876. fill.Dispose();
  2877. }
  2878. }
  2879. public void GetShenmangLowerAperture2(Mat imageContour, out int[] aperture, int leftUpper, int rightUpper, int leftLower, int rightLower, int[] dataArea)
  2880. {
  2881. aperture = new int[2] { 0, 0 };
  2882. int middle = (dataArea[1] + dataArea[2]) / 2;
  2883. Mat fanse = 1 - imageContour;
  2884. int offset = 20;
  2885. int sum = 0;
  2886. for (int j = dataArea[1]; j < middle; j++)
  2887. {
  2888. sum = 0;
  2889. for (int i = leftUpper - offset; i < leftLower - offset; i++)
  2890. {
  2891. sum += fanse.Get<byte>(i, j);
  2892. if (fanse.Get<byte>(i, j) > 0)
  2893. break;
  2894. }
  2895. if (sum == 0)
  2896. {
  2897. aperture[0] = j;
  2898. break;
  2899. }
  2900. }
  2901. for (int j = dataArea[2]; j > middle; j--)
  2902. {
  2903. sum = 0;
  2904. for (int i = rightUpper - offset; i < rightLower - offset; i++)
  2905. {
  2906. sum += fanse.Get<byte>(i, j);
  2907. if (fanse.Get<byte>(i, j) > 0)
  2908. break;
  2909. }
  2910. if (sum == 0)
  2911. {
  2912. aperture[1] = j;
  2913. break;
  2914. }
  2915. }
  2916. }
  2917. /// <summary>
  2918. /// 得到深盲孔的胶线
  2919. /// </summary>
  2920. /// <param name="image">输入图像,一般是绿色通道</param>
  2921. /// <param name="glueOrdinate">输出胶线纵坐标</param>
  2922. /// <param name="leftUpper">左边上界</param>
  2923. /// <param name="leftLower">左边下界</param>
  2924. /// <param name="rightUpper">右边上界</param>
  2925. /// <param name="rightLower">右边下界</param>
  2926. /// <param name="dataArea">提取区域</param>
  2927. public void GetGlue(Mat image, out int[] glueOrdinate, int leftUpper, int leftLower, int rightUpper, int rightLower, int[] dataArea)
  2928. {
  2929. glueOrdinate = new int[2];
  2930. int[] maybePosition = new int[2];
  2931. //int[] zero = new int[2];
  2932. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  2933. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  2934. Mat seDilate = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  2935. Mat crop1 = image[leftUpper, leftLower, dataArea[0], dataArea[1]];
  2936. //将裁剪区域改为面铜+基材铜附近
  2937. //int leftMiddleMiantong_Jicaitong = (dataArea[0] + dataArea[1]) / 2;
  2938. //Mat crop1 = image[leftUpper, leftLower, leftMiddleMiantong_Jicaitong - 10, leftMiddleMiantong_Jicaitong + 10];
  2939. Mat filter1 = new Mat();
  2940. Cv2.Filter2D(crop1, filter1, -1, kernel);
  2941. Cv2.ConvertScaleAbs(filter1, filter1);
  2942. Mat thresh1 = 1 - filter1.Threshold(0, 1, ThresholdTypes.Otsu);
  2943. Mat open1 = new Mat();
  2944. Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen);
  2945. Cv2.MorphologyEx(open1, open1, MorphTypes.Close, seOpen);
  2946. Cv2.Dilate(open1, open1, seDilate);
  2947. Fill(open1, out open1, 1);
  2948. //new Window("crop1", WindowMode.Normal, crop1);
  2949. //new Window("thresh2", WindowMode.Normal, 255 - crop1.Threshold(0, 255, ThresholdTypes.Otsu));
  2950. //new Window("thresh1", WindowMode.Normal, thresh1 * 255);
  2951. //new Window("end", WindowMode.Normal, open1 * 255);
  2952. //Cv2.WaitKey();
  2953. Scalar sum1 = new Scalar(0);
  2954. Scalar lastSum1 = new Scalar(0);
  2955. Scalar sub1 = new Scalar(0);
  2956. Scalar subMax1 = new Scalar(0);
  2957. Scalar max1 = new Scalar(0);
  2958. Scalar zero1 = new Scalar(0);
  2959. for (int i = 2; i < crop1.Rows - 2; i++)
  2960. {
  2961. sum1 = open1[i, i + 2, 0, open1.Cols - 1].Sum();
  2962. lastSum1 = open1[i - 2, i, 0, open1.Cols - 1].Sum();
  2963. //zero1 = open1[i - 1, i, 0, open1.Cols - 1].Sum();
  2964. sub1 = Math.Abs((int)sum1 - (int)lastSum1);
  2965. if ((int)max1 < (int)sum1)
  2966. {
  2967. max1 = sum1;
  2968. glueOrdinate[0] = i + leftUpper;
  2969. }
  2970. if ((int)subMax1 < (int)sub1)
  2971. {
  2972. subMax1 = sub1;
  2973. maybePosition[0] = i + leftUpper;
  2974. }
  2975. //if ((int)zero1 < 10)
  2976. //{
  2977. // zero[0] = i + leftUpper;
  2978. //}
  2979. }
  2980. Mat crop2 = image[rightUpper, rightLower, dataArea[2], dataArea[3]];
  2981. ////将裁剪区域改为面铜+基材铜附近
  2982. //int rightMiddleMiantong_Jicaitong = (dataArea[2] + dataArea[3]) / 2;
  2983. //Mat crop2 = image[rightUpper, rightLower, rightMiddleMiantong_Jicaitong - 10, rightMiddleMiantong_Jicaitong + 10];
  2984. Mat filter2 = new Mat();
  2985. Cv2.Filter2D(crop2, filter2, -1, kernel);
  2986. Cv2.ConvertScaleAbs(filter2, filter2);
  2987. Mat thresh2 = 1 - filter2.Threshold(0, 1, ThresholdTypes.Otsu);
  2988. Mat open2 = new Mat();
  2989. Cv2.MorphologyEx(thresh2, open2, MorphTypes.Open, seOpen);
  2990. Cv2.MorphologyEx(open2, open2, MorphTypes.Close, seOpen);
  2991. Cv2.Dilate(open2, open2, seDilate);
  2992. Fill(open2, out open2, 1);
  2993. //new Window("thresh2", WindowMode.Normal, thresh2 * 255);
  2994. //new Window("end2", WindowMode.Normal, open2 * 255);
  2995. //Cv2.WaitKey();
  2996. Scalar sum2 = new Scalar(0);
  2997. Scalar lastSum2 = new Scalar(0);
  2998. Scalar sub2 = new Scalar(0);
  2999. Scalar subMax2 = new Scalar(0);
  3000. Scalar max2 = new Scalar(0);
  3001. Scalar zero2 = new Scalar(0);
  3002. for (int i = 2; i < crop2.Rows - 2; i++)
  3003. {
  3004. sum2 = open2[i, i + 2, 0, open2.Cols - 1].Sum();
  3005. lastSum2 = open2[i - 2, i, 0, open2.Cols - 1].Sum();
  3006. //zero2 = open2[i - 1, i, 0, open2.Cols - 1].Sum();
  3007. sub2 = Math.Abs((int)sum2 - (int)lastSum2);
  3008. if ((int)max2 < (int)sum2)
  3009. {
  3010. max2 = sum2;
  3011. glueOrdinate[1] = i + rightUpper;
  3012. }
  3013. if ((int)subMax2 < (int)sub2)
  3014. {
  3015. subMax2 = sub2;
  3016. maybePosition[1] = i + rightUpper;
  3017. }
  3018. //if ((int)zero2 < 10)
  3019. //{
  3020. // zero[1] = i + rightUpper;
  3021. //}
  3022. }
  3023. if (glueOrdinate[0] > maybePosition[0])
  3024. {
  3025. for (int j = maybePosition[0] - leftUpper; j < glueOrdinate[0] - leftUpper; j++)
  3026. {
  3027. if ((int)open1[j, j + 1, 0, open1.Cols - 1].Sum() == 0)
  3028. {
  3029. sum1 = new Scalar(0);
  3030. lastSum1 = new Scalar(0);
  3031. sub1 = new Scalar(0);
  3032. subMax1 = new Scalar(0);
  3033. max1 = new Scalar(0);
  3034. for (int i = maybePosition[0] - leftUpper + 2; i < crop1.Rows - 2; i++)
  3035. {
  3036. sum1 = open1[i, i + 2, 0, open1.Cols - 1].Sum();
  3037. lastSum1 = open1[i - 2, i, 0, open1.Cols - 1].Sum();
  3038. sub1 = Math.Abs((int)sum1 - (int)lastSum1);
  3039. if ((int)max1 < (int)sum1)
  3040. {
  3041. max1 = sum1;
  3042. glueOrdinate[0] = i + leftUpper;
  3043. }
  3044. if ((int)subMax1 < (int)sub1)
  3045. {
  3046. subMax1 = sub1;
  3047. maybePosition[0] = i + leftUpper;
  3048. }
  3049. }
  3050. break;
  3051. }
  3052. }
  3053. if (glueOrdinate[0] > maybePosition[0])
  3054. {
  3055. //if (Math.Abs(zero[0] - maybePosition[0]) > 15&&zero[0]>10)
  3056. // glueOrdinate[0] = zero[0];
  3057. //else
  3058. glueOrdinate[0] = maybePosition[0];
  3059. }
  3060. }
  3061. if (glueOrdinate[1] > maybePosition[1])
  3062. {
  3063. for (int j = maybePosition[1] - rightUpper; j < glueOrdinate[1] - rightUpper; j++)
  3064. {
  3065. if ((int)open2[j, j + 1, 0, open2.Cols - 1].Sum() == 0)
  3066. {
  3067. sum2 = new Scalar(0);
  3068. lastSum2 = new Scalar(0);
  3069. sub2 = new Scalar(0);
  3070. subMax2 = new Scalar(0);
  3071. max2 = new Scalar(0);
  3072. for (int i = maybePosition[1] - rightUpper + 2; i < crop2.Rows - 2; i++)
  3073. {
  3074. sum2 = open2[i, i + 2, 0, open2.Cols - 1].Sum();
  3075. lastSum2 = open2[i - 2, i, 0, open2.Cols - 1].Sum();
  3076. sub2 = Math.Abs((int)sum2 - (int)lastSum2);
  3077. if ((int)max2 < (int)sum2)
  3078. {
  3079. max2 = sum2;
  3080. glueOrdinate[1] = i + rightUpper;
  3081. }
  3082. if ((int)subMax2 < (int)sub2)
  3083. {
  3084. subMax2 = sub2;
  3085. maybePosition[1] = i + rightUpper;
  3086. }
  3087. }
  3088. break;
  3089. }
  3090. }
  3091. if (glueOrdinate[1] > maybePosition[1])
  3092. {
  3093. //if (Math.Abs(zero[1] - maybePosition[1]) > 15&&zero[1]>10)
  3094. // glueOrdinate[1] = zero[1];
  3095. //else
  3096. glueOrdinate[1] = maybePosition[1];
  3097. }
  3098. }
  3099. //LineShow(crop1, 10, glueOrdinate[0] - leftUpper, 20, glueOrdinate[0] - leftUpper);
  3100. //new Window("crop1", WindowMode.Normal, crop1);
  3101. //new Window("thresh1", WindowMode.Normal, open1 * 255);
  3102. //new Window("thresh2", WindowMode.Normal, open2 * 255);
  3103. //Cv2.WaitKey();
  3104. }
  3105. /// <summary>
  3106. /// 计算胶线坐标,修改了判定时条件,先用于深盲孔双层
  3107. /// </summary>
  3108. /// <param name="image"></param>
  3109. /// <param name="glueOrdinate"></param>
  3110. /// <param name="leftUpper"></param>
  3111. /// <param name="leftLower"></param>
  3112. /// <param name="rightUpper"></param>
  3113. /// <param name="rightLower"></param>
  3114. /// <param name="dataArea"></param>
  3115. public void GetGlue2(Mat image, out int[] glueOrdinate, int leftUpper, int leftLower, int rightUpper, int rightLower, int[] dataArea)
  3116. {
  3117. glueOrdinate = new int[2];
  3118. int[] maybePosition = new int[2];
  3119. //int[] zero = new int[2];
  3120. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  3121. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  3122. Mat seDilate = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  3123. int width1 = dataArea[1] - dataArea[0];
  3124. Mat crop1 = image[leftUpper, leftLower, dataArea[0], dataArea[1]];
  3125. if (width1 > 50)
  3126. {
  3127. crop1 = image[leftUpper, leftLower, dataArea[0] + (width1 - 50) / 2, dataArea[1] - (width1 - 50) / 2];
  3128. width1 = 50;
  3129. }
  3130. //将裁剪区域改为面铜+基材铜附近
  3131. //int leftMiddleMiantong_Jicaitong = (dataArea[0] + dataArea[1]) / 2;
  3132. //Mat crop1 = image[leftUpper, leftLower, leftMiddleMiantong_Jicaitong - 10, leftMiddleMiantong_Jicaitong + 10];
  3133. int areaSize1 = crop1.Rows * crop1.Cols;
  3134. Mat filter1 = new Mat();
  3135. Cv2.Filter2D(crop1, filter1, -1, kernel);
  3136. Cv2.ConvertScaleAbs(filter1, filter1);
  3137. Mat thresh1 = 1 - filter1.Threshold(0, 1, ThresholdTypes.Otsu);
  3138. Mat open1 = new Mat();
  3139. Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen);
  3140. Cv2.MorphologyEx(open1, open1, MorphTypes.Close, seOpen);
  3141. Cv2.Dilate(open1, open1, seDilate);
  3142. Fill(open1, out open1, 1);
  3143. Scalar area1 = open1.Sum();
  3144. while ((int)area1 > areaSize1 * 0.9)
  3145. {
  3146. leftUpper += 5;
  3147. crop1 = image[leftUpper, leftLower, dataArea[0], dataArea[1]];
  3148. if (width1 > 50)
  3149. {
  3150. crop1 = image[leftUpper, leftLower, dataArea[0] + (width1 - 50) / 2, dataArea[1] - (width1 - 50) / 2];
  3151. width1 = 50;
  3152. }
  3153. filter1 = new Mat();
  3154. Cv2.Filter2D(crop1, filter1, -1, kernel);
  3155. Cv2.ConvertScaleAbs(filter1, filter1);
  3156. thresh1 = 1 - filter1.Threshold(0, 1, ThresholdTypes.Otsu);
  3157. open1 = new Mat();
  3158. Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen);
  3159. Cv2.MorphologyEx(open1, open1, MorphTypes.Close, seOpen);
  3160. Cv2.Dilate(open1, open1, seDilate);
  3161. Fill(open1, out open1, 1);
  3162. areaSize1 = crop1.Rows * crop1.Cols;
  3163. area1 = open1.Sum();
  3164. }
  3165. //new Window("crop1", WindowMode.Normal, crop1);
  3166. ////new Window("thresh2", WindowMode.Normal, 255 - crop1.Threshold(0, 255, ThresholdTypes.Otsu));
  3167. //new Window("thresh1", WindowMode.Normal, thresh1 * 255);
  3168. //new Window("end", WindowMode.Normal, open1 * 255);
  3169. //Cv2.WaitKey();
  3170. Scalar sum1 = new Scalar(0);
  3171. Scalar lastSum1 = new Scalar(0);
  3172. Scalar sub1 = new Scalar(0);
  3173. Scalar subMax1 = new Scalar(0);
  3174. Scalar max1 = new Scalar(0);
  3175. Scalar zero1 = new Scalar(0);
  3176. int y = 0;
  3177. for (int i = crop1.Rows - 4; i > 4; i--)
  3178. {
  3179. sum1 = open1[i - 3, i, 0, open1.Cols].Sum();
  3180. if ((int)sum1 > width1 * 2.9)
  3181. {
  3182. y = i;
  3183. break;
  3184. }
  3185. }
  3186. for (int i = y - 1; i > 4; i--)
  3187. {
  3188. sum1 = open1[i - 3, i, 0, open1.Cols].Sum();
  3189. if ((int)sum1 < width1 * 2.2)
  3190. {
  3191. glueOrdinate[0] = i + leftUpper;
  3192. break;
  3193. }
  3194. }
  3195. //右
  3196. Mat crop2 = image[rightUpper, rightLower, dataArea[2], dataArea[3]];
  3197. int width2 = dataArea[3] - dataArea[2];
  3198. if (width2 > 50)
  3199. {
  3200. crop2 = image[rightUpper, rightLower, dataArea[2] + (width2 - 50) / 2, dataArea[3] - (width2 - 50) / 2];
  3201. width2 = crop2.Cols;
  3202. }
  3203. ////将裁剪区域改为面铜+基材铜附近
  3204. //int rightMiddleMiantong_Jicaitong = (dataArea[2] + dataArea[3]) / 2;
  3205. //Mat crop2 = image[rightUpper, rightLower, rightMiddleMiantong_Jicaitong - 10, rightMiddleMiantong_Jicaitong + 10];
  3206. int areaSize2 = crop2.Rows * crop2.Cols;
  3207. Mat filter2 = new Mat();
  3208. Cv2.Filter2D(crop2, filter2, -1, kernel);
  3209. Cv2.ConvertScaleAbs(filter2, filter2);
  3210. Mat thresh2 = 1 - filter2.Threshold(0, 1, ThresholdTypes.Otsu);
  3211. Mat open2 = new Mat();
  3212. Cv2.MorphologyEx(thresh2, open2, MorphTypes.Open, seOpen);
  3213. Cv2.MorphologyEx(open2, open2, MorphTypes.Close, seOpen);
  3214. Cv2.Dilate(open2, open2, seDilate);
  3215. Fill(open2, out open2, 1);
  3216. Scalar area2 = open2.Sum();
  3217. while ((int)area2 > areaSize2 * 0.9)
  3218. {
  3219. rightUpper += 5;
  3220. crop2 = image[rightUpper, rightLower, dataArea[2], dataArea[3]];
  3221. width2 = dataArea[3] - dataArea[2];
  3222. if (width2 > 50)
  3223. {
  3224. crop2 = image[rightUpper, rightLower, dataArea[2] + (width2 - 50) / 2, dataArea[3] - (width2 - 50) / 2];
  3225. width2 = crop2.Cols;
  3226. }
  3227. areaSize2 = crop2.Rows * crop2.Cols;
  3228. filter2 = new Mat();
  3229. Cv2.Filter2D(crop2, filter2, -1, kernel);
  3230. Cv2.ConvertScaleAbs(filter2, filter2);
  3231. thresh2 = 1 - filter2.Threshold(0, 1, ThresholdTypes.Otsu);
  3232. open2 = new Mat();
  3233. Cv2.MorphologyEx(thresh2, open2, MorphTypes.Open, seOpen);
  3234. Cv2.MorphologyEx(open2, open2, MorphTypes.Close, seOpen);
  3235. Cv2.Dilate(open2, open2, seDilate);
  3236. Fill(open2, out open2, 1);
  3237. area2 = open2.Sum();
  3238. }
  3239. //new Window("crop2", WindowMode.Normal, crop2);
  3240. //new Window("filter2", WindowMode.Normal, filter2);
  3241. //new Window("thresh2", WindowMode.Normal, thresh2 * 255);
  3242. //new Window("end2", WindowMode.Normal, open2 * 255);
  3243. //Cv2.WaitKey();
  3244. Scalar sum2 = new Scalar(0);
  3245. Scalar lastSum2 = new Scalar(0);
  3246. Scalar sub2 = new Scalar(0);
  3247. Scalar subMax2 = new Scalar(0);
  3248. Scalar max2 = new Scalar(0);
  3249. Scalar zero2 = new Scalar(0);
  3250. y = 0;
  3251. for (int i = crop2.Rows - 4; i > 4; i--)
  3252. {
  3253. sum2 = open2[i - 3, i, 0, open2.Cols].Sum();
  3254. if ((int)sum2 > width2 * 2.9)
  3255. {
  3256. y = i;
  3257. break;
  3258. }
  3259. }
  3260. for (int i = y - 1; i > 4; i--)
  3261. {
  3262. sum2 = open2[i - 3, i, 0, open2.Cols].Sum();
  3263. if ((int)sum2 < width2 * 2.4)
  3264. {
  3265. glueOrdinate[1] = i + rightUpper;
  3266. break;
  3267. }
  3268. }
  3269. if (glueOrdinate[0] == 0)
  3270. glueOrdinate[0] = leftUpper;
  3271. if (glueOrdinate[1] == 0)
  3272. glueOrdinate[1] = rightUpper;
  3273. }
  3274. /// <summary>
  3275. /// 得到深盲孔胶内缩的内部坐标
  3276. /// </summary>
  3277. /// <param name="imageContour">二值图像</param>
  3278. /// <param name="upperWaist">输出内部坐标,先左后右</param>
  3279. /// <param name="lowerWaist">输出外部坐标,先左后右</param>
  3280. /// <param name="upperBound">上界</param>
  3281. /// <param name="glueOrdinate">胶线的坐标</param>
  3282. /// <param name="lowerBound">下界</param>
  3283. /// <param name="dataArea">提取数据区域</param>
  3284. public void GetWaist(Mat imageContour, out int[] upperWaist, out int[] lowerWaist, int upperBound, int[] glueOrdinate, int lowerBound, int[] dataArea)
  3285. {
  3286. //Cv2.ImWrite(@"C:\Users\zyh\Desktop\imageContour.jpg", imageContour * 255);
  3287. //寻找连通区域,把除第一个联通区域的都置为0
  3288. Mat labelMat = new Mat();
  3289. Mat stats = new Mat();
  3290. Mat centroids = new Mat();
  3291. int nums = Cv2.ConnectedComponentsWithStats(imageContour, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  3292. int y = stats.At<int>(1, 1);
  3293. int height = stats.At<int>(1, 3);
  3294. for(int h=height+1; h< imageContour.Height; h++)
  3295. {
  3296. imageContour.Row[h] *= 0;
  3297. }
  3298. upperWaist = new int[2] { 0, 0 };
  3299. lowerWaist = new int[2] { 0, 0 };
  3300. int middle = (dataArea[1] + dataArea[2]) / 2;
  3301. int min_left=int.MaxValue, max_left = 0;
  3302. int min_left_x = 0, max_left_x = 0;
  3303. for (int j = middle-30; j > dataArea[1]; j--)
  3304. {
  3305. int v = imageContour.Col[j].CountNonZero();
  3306. if (v <= min_left) { min_left = v; min_left_x = j; }
  3307. if (v >= max_left) { max_left = v; max_left_x = j; }
  3308. }
  3309. upperWaist[0] = max_left_x;
  3310. OpenCvSharp.Rect rect_Left = new Rect(dataArea[1], upperBound, max_left_x- dataArea[1], lowerBound- upperBound);
  3311. Mat temp_left = new Mat(imageContour, rect_Left);
  3312. //Cv2.ImWrite(@"C:\Users\zyh\Desktop\temp_left.jpg", temp_left*255);
  3313. bool f_l = false;
  3314. for (int j=0; j< temp_left.Width; j++)
  3315. {
  3316. if(!f_l && temp_left.Col[j].CountNonZero()>0)
  3317. {
  3318. f_l = true;
  3319. lowerWaist[0] = j + dataArea[1];
  3320. //break;
  3321. }
  3322. else if(temp_left.Col[j].CountNonZero()== temp_left.Height)
  3323. {
  3324. upperWaist[0] = j + dataArea[1];
  3325. break;
  3326. }
  3327. }
  3328. int min_right = int.MaxValue, max_right = 0;
  3329. int min_right_x = 0, max_right_x = 0;
  3330. for (int j = middle+30; j < dataArea[2]; j++)
  3331. {
  3332. int v = imageContour.Col[j].CountNonZero();
  3333. if (v <= min_right) { min_right = v; min_right_x = j; }
  3334. if (v >= max_right) { max_right = v; max_right_x = j; }
  3335. }
  3336. upperWaist[1] = max_right_x;
  3337. OpenCvSharp.Rect rect_right = new Rect(max_right_x, upperBound, dataArea[2]- max_right_x, lowerBound - upperBound);
  3338. Mat temp_right = new Mat(imageContour, rect_right);
  3339. //Cv2.ImWrite(@"C:\Users\zyh\Desktop\rect_right.jpg", temp_right*255);
  3340. bool f_r = false;
  3341. for (int j = temp_right.Width-1; j > 0; j--)
  3342. {
  3343. if (!f_r && temp_right.Col[j].CountNonZero() > 0)
  3344. {
  3345. f_r = true;
  3346. lowerWaist[1] = j+ max_right_x;
  3347. //break;
  3348. }
  3349. else if(temp_right.Col[j].CountNonZero() == temp_right.Height)
  3350. {
  3351. upperWaist[1] = j + max_right_x;
  3352. break;
  3353. }
  3354. }
  3355. /*Mat fanse = 1 - imageContour;
  3356. //上
  3357. for (int j = middle; j > dataArea[0]; j--)
  3358. {
  3359. for (int i = upperBound; i < glueOrdinate[0]; i++)
  3360. {
  3361. if (fanse.Get<byte>(i, j) > 0*//* && j< (middle + dataArea[0]) / 2*//*)
  3362. {
  3363. upperWaist[0] = j;
  3364. break;
  3365. }
  3366. }
  3367. if (upperWaist[0] != 0)
  3368. break;
  3369. }
  3370. for (int j = middle; j < dataArea[3]; j++)
  3371. {
  3372. for (int i = upperBound; i < glueOrdinate[1]; i++)
  3373. {
  3374. if (fanse.Get<byte>(i, j) > 0*//* && j > (middle + dataArea[3])/2*//*)
  3375. {
  3376. upperWaist[1] = j;
  3377. break;
  3378. }
  3379. }
  3380. if (upperWaist[1] != 0)
  3381. break;
  3382. }
  3383. //下
  3384. for (int j = (dataArea[0] + dataArea[1]) / 2; j < middle; j++)
  3385. {
  3386. for (int i = glueOrdinate[0] - 20; i < lowerBound; i++)
  3387. {
  3388. //new Window("imageContour", WindowMode.Normal, imageContour * 255);
  3389. //Cv2.WaitKey();
  3390. if (imageContour.Get<byte>(i, j) > 0)
  3391. {
  3392. lowerWaist[0] = j;
  3393. break;
  3394. }
  3395. }
  3396. if (lowerWaist[0] != 0)
  3397. break;
  3398. }
  3399. for (int j = (dataArea[3] + dataArea[2]) / 2; j > middle; j--)
  3400. {
  3401. for (int i = glueOrdinate[1] - 20; i < lowerBound; i++)
  3402. {
  3403. if (imageContour.Get<byte>(i, j) > 0)
  3404. {
  3405. lowerWaist[1] = j;
  3406. break;
  3407. }
  3408. }
  3409. if (lowerWaist[1] != 0)
  3410. break;
  3411. }*/
  3412. }
  3413. /// <summary>
  3414. /// 提取胶内缩,将下边界改为左右边界
  3415. /// </summary>
  3416. /// <param name="imageContour">二值图像</param>
  3417. /// <param name="upperWaist">输出内部坐标,先左后右</param>
  3418. /// <param name="lowerWaist">输出外部坐标,先左后右</param>
  3419. /// <param name="upperBound">上界</param>
  3420. /// <param name="glueOrdinate">胶线的坐标</param>
  3421. /// <param name="leftLower">下左界</param>
  3422. /// <param name="rightLower">下右界</param>
  3423. /// <param name="dataArea">提取数据区域</param>
  3424. public void GetWaistNew(Mat imageContour, out int[] upperWaist, out int[] lowerWaist, int upperBound, int[] glueOrdinate, int leftLower, int rightLower, int[] dataArea)
  3425. {
  3426. upperWaist = new int[2] { 0, 0 };
  3427. lowerWaist = new int[2] { 0, 0 };
  3428. int middle = (dataArea[1] + dataArea[2]) / 2;
  3429. Mat fanse = 1 - imageContour;
  3430. //上
  3431. for (int j = middle; j > dataArea[0]; j--)
  3432. {
  3433. for (int i = upperBound; i < glueOrdinate[0]; i++)
  3434. {
  3435. if (fanse.Get<byte>(i, j) > 0)
  3436. {
  3437. upperWaist[0] = j;
  3438. break;
  3439. }
  3440. }
  3441. if (upperWaist[0] != 0)
  3442. break;
  3443. }
  3444. for (int j = middle; j < dataArea[3]; j++)
  3445. {
  3446. for (int i = upperBound; i < glueOrdinate[1]; i++)
  3447. {
  3448. if (fanse.Get<byte>(i, j) > 0)
  3449. {
  3450. upperWaist[1] = j;
  3451. break;
  3452. }
  3453. }
  3454. if (upperWaist[1] != 0)
  3455. break;
  3456. }
  3457. //下
  3458. //左
  3459. for (int j = (dataArea[0] + dataArea[1]) / 2; j < middle; j++)
  3460. {
  3461. for (int i = glueOrdinate[0] - 20; i < leftLower; i++)
  3462. {
  3463. //new Window("imageContour", WindowMode.Normal, imageContour * 255);
  3464. //Cv2.WaitKey();
  3465. if (imageContour.Get<byte>(i, j) > 0)
  3466. {
  3467. lowerWaist[0] = j;
  3468. break;
  3469. }
  3470. }
  3471. if (lowerWaist[0] != 0)
  3472. break;
  3473. }
  3474. //右
  3475. for (int j = (dataArea[3] + dataArea[2]) / 2; j > middle; j--)
  3476. {
  3477. for (int i = glueOrdinate[1] - 20; i < rightLower; i++)
  3478. {
  3479. if (imageContour.Get<byte>(i, j) > 0)
  3480. {
  3481. lowerWaist[1] = j;
  3482. break;
  3483. }
  3484. }
  3485. if (lowerWaist[1] != 0)
  3486. break;
  3487. }
  3488. }
  3489. /// <summary>
  3490. /// 得到精准的面铜和基材铜的纵坐标
  3491. /// </summary>
  3492. /// <param name="image">最好是绿色通道图</param>
  3493. /// <param name="upperBound">上边界</param>
  3494. /// <param name="lowerBound">下边界</param>
  3495. /// <param name="leftBoundary">左边界</param>
  3496. /// <param name="rightBoundary">右边界</param>
  3497. /// <param name="middleMiantong">面铜的横坐标</param>
  3498. /// <param name="middleJicaitong">基材铜的横坐标</param>
  3499. /// <param name="ordinateL2Miantong">面铜的纵坐标</param>
  3500. /// <param name="ordinateL2Jicaitong">基材铜的纵坐标</param>
  3501. /// <param name="ordinateL2_Acc">输出纵坐标,面铜和基材铜</param>
  3502. public void InsideLine_Accuracy(Mat image, int upperBound, int lowerBound, int leftBoundary, int rightBoundary, int middleMiantong, int middleJicaitong
  3503. , double ordinateL2Miantong, double ordinateL2Jicaitong, out double[] ordinateL2_Acc, int isMiantong = 0, int banceng = 2/*-1*/, bool showMat = false)
  3504. {
  3505. //第5671deng类别图片需要完善位置精准度,以及 调试有些图片位置计算错误的原因,并纠正
  3506. Mat crop = image[upperBound, lowerBound, leftBoundary, rightBoundary];
  3507. //if (isMiantong > 0 && showMat)
  3508. //{
  3509. // Mat mat1 = new Mat();
  3510. // Cv2.Normalize(crop, mat1, 0, 255, NormTypes.MinMax);
  3511. // Cv2.ImWrite(@"C:\Users\54434\Desktop\crop.JPG", crop);
  3512. //}
  3513. Mat filter = new Mat();//滤波增强对比
  3514. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  3515. Cv2.Filter2D(crop, filter, -1, kernel);
  3516. Cv2.ConvertScaleAbs(filter, filter);
  3517. Mat thresh = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
  3518. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15/*15*/, 1/*水平线<--3, 3*/));// 开运算
  3519. Mat open = new Mat();
  3520. //OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
  3521. Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
  3522. if (isMiantong > 0 && showMat)
  3523. {
  3524. //Cv2.ImWrite(@"C:\Users\54434\Desktop\filter.JPG", filter/* * 127*/);
  3525. }
  3526. //Scalar sum = new Scalar(0);
  3527. //Scalar max1 = new Scalar(0);
  3528. //Scalar max = new Scalar(0);
  3529. //double meanOrdinate1 = 0;
  3530. double ordinateL2Miantong_acc = ordinateL2Miantong;
  3531. double ordinateL2Jicaitong_acc = ordinateL2Jicaitong;
  3532. //ordinateL2_Acc = new double[] { ordinateL2Miantong_acc, ordinateL2Jicaitong_acc };
  3533. //return;
  3534. int topOri = (int)ordinateL2Miantong_acc - upperBound - 20;// 10;// 15;
  3535. int bottomOri = (int)ordinateL2Miantong_acc - upperBound + 20;// 10;// 15;
  3536. if (topOri < 10) topOri = 10;
  3537. else if (topOri > crop.Rows/3 && topOri > 20) topOri = 20;//对初判定位置距离太远的进行纠偏
  3538. if (bottomOri > crop.Rows - 10) bottomOri = crop.Rows - 10;
  3539. else if (bottomOri < crop.Rows*2/3 && bottomOri < crop.Rows - 20) bottomOri = crop.Rows - 20;//对初判定位置距离太远的进行纠偏
  3540. bool miantongChanged = false;
  3541. int miantongValue = crop.Get<byte>((int)ordinateL2Miantong - upperBound, middleMiantong - leftBoundary);
  3542. bool isHoriMost = false;//用来判定大横线哪个更加位置居中
  3543. int centerVer = crop.Rows / 2;// (topOri + bottomOri) / 2;//用来判定大横线哪个更加位置居中
  3544. for (int i = topOri; i < bottomOri; i++)
  3545. {
  3546. int miantongValueI = crop.Get<byte>(i, middleMiantong - leftBoundary);
  3547. Scalar sum = open[i, i + 1, 0, crop.Cols - 1].Sum();
  3548. if (miantongValueI < miantongValue - 1 || (!miantongChanged && miantongValueI < miantongValue + 100)
  3549. || crop.Cols - 20 < (int)sum)
  3550. {
  3551. //if (open.Get<byte>(i, middleMiantong - leftBoundary) > 0)
  3552. //Scalar sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
  3553. //if (30/*30*/ < (int)sum)
  3554. if (6/*30*/ < (int)sum && !isHoriMost
  3555. ||(crop.Cols - 20 < (int)sum && isHoriMost && Math.Abs(centerVer - i) < Math.Abs(centerVer - (ordinateL2Miantong_acc - upperBound))))
  3556. {
  3557. ordinateL2Miantong_acc = i + upperBound;
  3558. miantongValue = miantongValueI;
  3559. miantongChanged = true;
  3560. //待自测scc-2
  3561. //if (isMiantong == 1)
  3562. // break;
  3563. if (crop.Cols - 20 < (int)sum/* && Math.Abs(centerVer - (ordinateL2Jicaitong_acc - upperBound)) < 10*/)
  3564. {
  3565. isHoriMost = true;
  3566. //break;
  3567. }
  3568. }
  3569. }
  3570. }
  3571. if (ordinateL2Miantong_acc == 10 + upperBound)
  3572. {//将明显不对的测量结果居中取值
  3573. ordinateL2Miantong_acc = centerVer + upperBound;
  3574. }
  3575. //if (isMiantong == 1 && !miantongChanged)
  3576. //{
  3577. // Mat thresh1 = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
  3578. // Mat seOpen1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9/*15*/, 1/*水平线<--3, 3*/));// 开运算
  3579. // Mat open1 = new Mat();
  3580. // //OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
  3581. // Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen1);
  3582. // //if (isMiantong > 0 && showMat)
  3583. // //{
  3584. // // new Window("open1", WindowMode.Normal, open1 * 255);
  3585. // // Cv2.WaitKey();
  3586. // //}
  3587. // for (int i = topOri; i < bottomOri; i++)
  3588. // {
  3589. // //int miantongValueI = crop.Get<byte>(i, middleMiantong - leftBoundary);
  3590. // //if (miantongValueI < 200/*miantongValue*/)
  3591. // {
  3592. // if (open1.Get<byte>(i, middleMiantong - leftBoundary) > 0)
  3593. // //Scalar sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
  3594. // //if (30/*30*/ < (int)sum)
  3595. // {
  3596. // ordinateL2Miantong_acc = i + upperBound;
  3597. // miantongValue = crop.Get<byte>(i, middleMiantong - leftBoundary);// miantongValueI;
  3598. // miantongChanged = true;
  3599. // break;
  3600. // }
  3601. // }
  3602. // }
  3603. //}
  3604. isHoriMost = false;//用来判定大横线哪个更加位置居中
  3605. bool jicaitongChanged = false;
  3606. int jicaitongValue = crop.Get<byte>((int)ordinateL2Jicaitong - upperBound, middleJicaitong - leftBoundary);
  3607. for (int i = /*0*/topOri; i < bottomOri; i++)
  3608. {
  3609. int jicaitongValueI = crop.Get<byte>(i, middleJicaitong - leftBoundary);
  3610. Scalar sum = open[i, i + 1, 0, crop.Cols - 1].Sum();
  3611. if (jicaitongValueI < jicaitongValue - 1 || (!jicaitongChanged && jicaitongValueI < jicaitongValue + 100)
  3612. || crop.Cols - 20 < (int)sum)
  3613. {
  3614. //if (open.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
  3615. //Scalar sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
  3616. ////Scalar sum = open[i, i + 2, middleJicaitong - leftBoundary - 4, middleJicaitong - leftBoundary + 4].Sum();
  3617. if (6/*30*/ < (int)sum && !isHoriMost
  3618. || (crop.Cols - 20 < (int)sum && isHoriMost && Math.Abs(centerVer - i) < Math.Abs(centerVer - (ordinateL2Jicaitong_acc - upperBound))))
  3619. {
  3620. ordinateL2Jicaitong_acc = i + upperBound;
  3621. jicaitongValue = jicaitongValueI;
  3622. jicaitongChanged = true;
  3623. //待自测scc-2
  3624. //if (isMiantong == 1)
  3625. // break;
  3626. if (crop.Cols - 20 < (int)sum/* && Math.Abs(centerVer - (ordinateL2Jicaitong_acc - upperBound)) < 10*/)
  3627. {
  3628. isHoriMost = true;
  3629. //break;
  3630. }
  3631. }
  3632. }
  3633. }
  3634. if (ordinateL2Jicaitong_acc == 10 + upperBound)
  3635. {//将明显不对的测量结果居中取值
  3636. ordinateL2Jicaitong_acc = centerVer + upperBound;
  3637. }
  3638. if (!miantongChanged && !jicaitongChanged)
  3639. {
  3640. int topOri_1 = topOri;
  3641. if (topOri > 10) topOri_1 = 10;
  3642. int bottomOri_1 = bottomOri;
  3643. if (bottomOri < crop.Rows - 10) bottomOri_1 = crop.Rows - 10;
  3644. miantongValue = crop.Get<byte>((int)ordinateL2Miantong - upperBound, middleMiantong - leftBoundary);
  3645. for (int i = topOri_1; i < bottomOri_1; i++)
  3646. {
  3647. int miantongValueI = crop.Get<byte>(i, middleMiantong - leftBoundary);
  3648. Scalar sum = open[i, i + 1, 0, crop.Cols - 1].Sum();
  3649. if (miantongValueI < miantongValue - 1 || (!miantongChanged && miantongValueI < miantongValue + 100)
  3650. || crop.Cols - 20 < (int)sum)
  3651. {
  3652. if (6 < (int)sum)
  3653. {
  3654. ordinateL2Miantong_acc = i + upperBound;
  3655. miantongValue = miantongValueI;
  3656. miantongChanged = true;
  3657. if (crop.Cols - 20 < (int)sum)
  3658. break;
  3659. }
  3660. }
  3661. if (i == topOri - 1) i = bottomOri;
  3662. }
  3663. jicaitongValue = crop.Get<byte>((int)ordinateL2Jicaitong - upperBound, middleJicaitong - leftBoundary);
  3664. for (int i = topOri_1; i < bottomOri_1; i++)
  3665. {
  3666. int jicaitongValueI = crop.Get<byte>(i, middleJicaitong - leftBoundary);
  3667. Scalar sum = open[i, i + 1, 0, crop.Cols - 1].Sum();
  3668. if (jicaitongValueI < jicaitongValue - 1 || (!jicaitongChanged && jicaitongValueI < jicaitongValue + 100)
  3669. || crop.Cols - 20 < (int)sum)
  3670. {
  3671. if (6 < (int)sum)
  3672. {
  3673. ordinateL2Jicaitong_acc = i + upperBound;
  3674. jicaitongValue = jicaitongValueI;
  3675. jicaitongChanged = true;
  3676. if (crop.Cols - 20 < (int)sum)
  3677. break;
  3678. }
  3679. }
  3680. if (i == topOri - 1) i = bottomOri;
  3681. }
  3682. }
  3683. if ((isMiantong == 1 || isMiantong == 2) && (!miantongChanged /*//待自测scc-3_1 &&*/|| !jicaitongChanged))
  3684. {
  3685. Mat thresh1 = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
  3686. Mat seOpen1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9/*15*/, 1/*水平线<--3, 3*/));// 开运算
  3687. Mat open1 = new Mat();
  3688. //OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
  3689. Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen1);
  3690. //if (isMiantong > 0 && showMat)
  3691. //{
  3692. // new Window("open1", WindowMode.Normal, open1 * 255);
  3693. // Cv2.WaitKey();
  3694. //}
  3695. //待自测scc-3_1
  3696. if (isMiantong == 1 && !miantongChanged || isMiantong == 2 && !jicaitongChanged)
  3697. for (int i = bottomOri; i > topOri; i--)
  3698. {
  3699. //还差第3、5、7类别图片需要完善位置精准度,以及
  3700. //调试有些图片位置计算错误的原因,并纠正!!!!!!!!!!!!
  3701. {
  3702. if (isMiantong == 1 && open1.Get<byte>(i, middleMiantong - leftBoundary) > 0
  3703. || isMiantong == 2 && open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
  3704. {
  3705. if (isMiantong == 1)
  3706. ordinateL2Miantong_acc = i + upperBound;
  3707. else if (isMiantong == 2)
  3708. ordinateL2Jicaitong_acc = i + upperBound;
  3709. break;
  3710. }
  3711. }
  3712. }
  3713. //if (isMiantong == 1 && !miantongChanged)
  3714. // for (int i = topOri; i < bottomOri; i++)//for (int i = bottomOri; i > topOri; i--)
  3715. // {
  3716. // if (open1.Get<byte>(i, middleMiantong - leftBoundary) > 0)
  3717. // {
  3718. // ordinateL2Miantong_acc = i + upperBound;
  3719. // break;
  3720. // }
  3721. // }
  3722. //if (isMiantong == 2 && !jicaitongChanged)
  3723. // for (int i = bottomOri; i > topOri; i--)
  3724. // {
  3725. // if (open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
  3726. // {
  3727. // ordinateL2Jicaitong_acc = i + upperBound;
  3728. // break;
  3729. // }
  3730. // }
  3731. //if (isMiantong == 1 && !jicaitongChanged)
  3732. // for (int i = topOri; i < bottomOri; i++)
  3733. // {
  3734. // {
  3735. // if (open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
  3736. // {
  3737. // ordinateL2Jicaitong_acc = i + upperBound;
  3738. // break;
  3739. // }
  3740. // }
  3741. // }
  3742. //if (isMiantong == 2 && !miantongChanged)
  3743. // for (int i = bottomOri; i > topOri; i--)//for (int i = topOri; i < bottomOri; i++)
  3744. // {
  3745. // {
  3746. // if (open1.Get<byte>(i, middleMiantong - leftBoundary) > 0)
  3747. // {
  3748. // ordinateL2Miantong_acc = i + upperBound;
  3749. // break;
  3750. // }
  3751. // }
  3752. // }
  3753. if (isMiantong == 1 && !jicaitongChanged || isMiantong == 2 && !miantongChanged)
  3754. for (int i = topOri; i < bottomOri; i++)
  3755. {
  3756. {
  3757. if (isMiantong == 1 && open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0
  3758. || isMiantong == 2 && open1.Get<byte>(i, middleMiantong - leftBoundary) > 0)
  3759. {
  3760. if (isMiantong == 1)
  3761. ordinateL2Jicaitong_acc = i + upperBound;
  3762. else if (isMiantong == 2)
  3763. ordinateL2Miantong_acc = i + upperBound;
  3764. break;
  3765. }
  3766. }
  3767. }
  3768. }
  3769. //if (isMiantong == 1 && !jicaitongChanged)
  3770. //{
  3771. // Mat thresh1 = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
  3772. // Mat seOpen1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9/*15*/, 1/*水平线<--3, 3*/));// 开运算
  3773. // Mat open1 = new Mat();
  3774. // //OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
  3775. // Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen1);
  3776. // //if (isMiantong > 0 && showMat)
  3777. // //{
  3778. // // new Window("open1", WindowMode.Normal, open1 * 255);
  3779. // // Cv2.WaitKey();
  3780. // //}
  3781. // for (int i = bottomOri; i > topOri; i--)
  3782. // {
  3783. // //int miantongValueI = crop.Get<byte>(i, middleMiantong - leftBoundary);
  3784. // //if (miantongValueI < 200/*miantongValue*/)
  3785. // {
  3786. // if (open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
  3787. // //Scalar sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
  3788. // //if (30/*30*/ < (int)sum)
  3789. // {
  3790. // ordinateL2Jicaitong_acc = i + upperBound;
  3791. // jicaitongValue = crop.Get<byte>(i, middleJicaitong - leftBoundary);// miantongValueI;
  3792. // jicaitongChanged = true;
  3793. // break;
  3794. // }
  3795. // }
  3796. // }
  3797. //}
  3798. ////if (isMiantong == 1 && jicaitongChanged && showMat)
  3799. ////{
  3800. //// Mat thresh1 = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
  3801. //// //Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(11/*15*/, 1/*水平线<--3, 3*/));// 开运算
  3802. //// //Mat open = new Mat();
  3803. //// ////OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
  3804. //// //Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
  3805. //// ////if (isMiantong > 0 && showMat)
  3806. //// ////{
  3807. //// //// new Window("open", WindowMode.Normal, open * 255);
  3808. //// //// Cv2.WaitKey();
  3809. //// ////}
  3810. ////}
  3811. ordinateL2_Acc = new double[] { ordinateL2Miantong_acc, ordinateL2Jicaitong_acc };
  3812. }
  3813. /// <summary>
  3814. /// 得到内部线纵坐标
  3815. /// </summary>
  3816. /// <param name="image">最好是绿色通道图</param>
  3817. /// <param name="upperBound">上边界</param>
  3818. /// <param name="lowerBound">下边界</param>
  3819. /// <param name="leftBoundary">左边界</param>
  3820. /// <param name="rightBoundary">右边界</param>
  3821. /// <param name="meanOrdinate">输出坐标</param>
  3822. public double InsideLine(Mat image, int upperBound, int lowerBound, int leftBoundary, int rightBoundary, out double meanOrdinate
  3823. , int isMiantong = 0, bool showMat = false)
  3824. {
  3825. Mat crop = image[upperBound, lowerBound, leftBoundary-10, rightBoundary+10];
  3826. /*int pos = -1;
  3827. int min = int.MaxValue;
  3828. for(int i=0; i< crop.Height-5; i++)
  3829. {
  3830. int sum = (int)(crop.Row[i].Sum())+ (int)(crop.Row[i+1].Sum())+ (int)(crop.Row[i+2].Sum())
  3831. + (int)(crop.Row[i+3].Sum())+ (int)(crop.Row[i+4].Sum());
  3832. if (sum < min)
  3833. {
  3834. pos = i;
  3835. min = sum;
  3836. }
  3837. }
  3838. if((int)(image.Row[pos+ upperBound].Sum()) < (int)(image.Row[pos+ upperBound + 40].Sum()) && pos< crop.Height/2)
  3839. {
  3840. int start = pos + 10;
  3841. pos = -1;
  3842. min = int.MaxValue;
  3843. for (int i = start; i < crop.Height; i++)
  3844. {
  3845. int sum = (int)(crop.Row[i].Sum());
  3846. if (sum < min)
  3847. {
  3848. pos = i;
  3849. min = sum;
  3850. }
  3851. }
  3852. }
  3853. meanOrdinate = pos + upperBound;
  3854. return meanOrdinate;*/
  3855. Mat filter = new Mat();//滤波增强对比
  3856. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  3857. Cv2.Filter2D(crop, filter, -1, kernel);
  3858. Cv2.ConvertScaleAbs(filter, filter);
  3859. Mat thresh = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu); ;// 二值化
  3860. //待自测scc-3_2 //此处针对3(4)的类型图片做精准化调整
  3861. //int sizeSe = 3; if (isMiantong == 2) sizeSe = 1;// 3;// 1;
  3862. //Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, sizeSe/*3*/));// 开运算
  3863. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));// 开运算
  3864. Mat open = new Mat();
  3865. Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
  3866. //if (isMiantong > 0 && showMat)
  3867. //{
  3868. // Mat mat1 = new Mat();
  3869. // Cv2.Normalize(open, mat1, 0, 255, NormTypes.MinMax);
  3870. // Cv2.ImWrite(@"C:\Users\54434\Desktop\open.JPG", mat1);
  3871. //}
  3872. Scalar sum = new Scalar(0);
  3873. Scalar max1 = new Scalar(0);
  3874. Scalar max = new Scalar(0);
  3875. double meanOrdinate1 = 0;
  3876. meanOrdinate = 0;//待自测scc-3_2 + upperBound;
  3877. for (int i = crop.Rows / 4; i < crop.Rows / 4 * 3; i++)
  3878. {
  3879. sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
  3880. if ((int)max < (int)sum)
  3881. {
  3882. if (isMiantong > 0 && i + upperBound > 10 + meanOrdinate)
  3883. {
  3884. max1 = max;
  3885. meanOrdinate1 = meanOrdinate;
  3886. }
  3887. max = sum;
  3888. meanOrdinate = i + upperBound;
  3889. }
  3890. else if (isMiantong > 0 && i + upperBound > 10 + meanOrdinate && (int)max1 < (int)sum)
  3891. {
  3892. max1 = sum;
  3893. meanOrdinate1 = i + upperBound;
  3894. }
  3895. }
  3896. if (isMiantong > 0)
  3897. {
  3898. if (meanOrdinate < meanOrdinate1 && isMiantong == 1
  3899. || meanOrdinate > meanOrdinate1 && meanOrdinate1 > 0 && isMiantong == 2
  3900. )
  3901. {
  3902. double meanOrdinate_i = meanOrdinate;
  3903. meanOrdinate = meanOrdinate1;
  3904. meanOrdinate1 = meanOrdinate_i;
  3905. }
  3906. }
  3907. //待自测scc-3_2
  3908. //if (meanOrdinate == upperBound)
  3909. // meanOrdinate = meanOrdinate1;
  3910. //尝试纠偏一下,可是还有差很亮的,可能还是需要二值 待确认@@@@@@@@@@@@@
  3911. if ((int)(image.Row[(int)meanOrdinate].Sum()) < (int)(image.Row[Math.Min(image.Rows - 1, (int)meanOrdinate + 40)].Sum())-100000 && (meanOrdinate- upperBound) < crop.Height / 2
  3912. && ((int)crop.Row[(int)meanOrdinate - upperBound + 10].Sum() - (int)crop.Row[(int)meanOrdinate - upperBound - 10].Sum()) < crop.Width*25)
  3913. {
  3914. int start = (int)(meanOrdinate - upperBound + 10);
  3915. int pos = -1;
  3916. int min = int.MaxValue;
  3917. for (int i = start; i < crop.Height; i++)
  3918. {
  3919. int sum_a = (int)(crop.Row[i].Sum());
  3920. if (sum_a < min)
  3921. {
  3922. pos = i;
  3923. min = sum_a;
  3924. }
  3925. }
  3926. meanOrdinate = pos + upperBound;
  3927. }
  3928. #region[清理内存]
  3929. if (crop != null)
  3930. {
  3931. crop.Dispose();
  3932. }
  3933. if (open != null)
  3934. {
  3935. open.Dispose();
  3936. }
  3937. if (filter != null)
  3938. {
  3939. filter.Dispose();
  3940. }
  3941. if (thresh != null)
  3942. {
  3943. thresh.Dispose();
  3944. }
  3945. if (seOpen != null)
  3946. {
  3947. seOpen.Dispose();
  3948. }
  3949. #endregion
  3950. return meanOrdinate1;
  3951. }
  3952. /// <summary>
  3953. /// 將計算的範圍從整個行數變成四分之一到四分之三之間,不知道通孔和盲孔改變之後會不會有影響,暫時沒引用
  3954. /// </summary>
  3955. /// <param name="image"></param>
  3956. /// <param name="upperBound"></param>
  3957. /// <param name="lowerBound"></param>
  3958. /// <param name="leftBoundary"></param>
  3959. /// <param name="rightBoundary"></param>
  3960. /// <param name="meanOrdinate"></param>
  3961. public void InsideLine2(Mat image, int upperBound, int lowerBound, int leftBoundary, int rightBoundary, out double meanOrdinate)
  3962. {
  3963. Mat crop = image[upperBound, lowerBound, leftBoundary, rightBoundary];
  3964. Mat filter = new Mat();//滤波增强对比
  3965. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  3966. Cv2.Filter2D(crop, filter, -1, kernel);
  3967. Cv2.ConvertScaleAbs(filter, filter);
  3968. Mat thresh = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu); ;// 二值化
  3969. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));// 开运算
  3970. Mat open = new Mat();
  3971. Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
  3972. //new Window("thresh", WindowMode.Normal, thresh * 255);
  3973. //new Window("open", WindowMode.Normal, open * 255);
  3974. //Cv2.WaitKey();
  3975. Scalar sum = new Scalar(0);
  3976. Scalar max = new Scalar(0);
  3977. meanOrdinate = 0;
  3978. for (int i = crop.Rows / 4; i < crop.Rows / 4 * 3; i++)
  3979. {
  3980. sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
  3981. if ((int)max < (int)sum)
  3982. {
  3983. max = sum;
  3984. meanOrdinate = i + upperBound;
  3985. }
  3986. }
  3987. }
  3988. /// <summary>
  3989. /// 图像画线,线颜色是红色
  3990. /// </summary>
  3991. /// <param name="image">画线图像</param>
  3992. /// <param name="x1"></param>
  3993. /// <param name="y1"></param>
  3994. /// <param name="x2"></param>
  3995. /// <param name="y2"></param>
  3996. /// <summary>
  3997. /// 计算盲孔的上孔径
  3998. /// </summary>
  3999. /// <param name="image">输入二值图片</param>
  4000. /// <param name="apertureLow">输入下孔径</param>
  4001. /// <param name="leftOrdinate3">左边第三条线坐标</param>
  4002. /// <param name="rightOrdinate3">右边第三条线坐标</param>
  4003. /// <param name="leftAperture">输出左边孔径坐标,先纵坐标后横坐标</param>
  4004. /// <param name="rightAperture">输出右边孔径坐标,先纵坐标后横坐标</param>
  4005. public void ShangKongjing(/*Mat imageRed, */Mat image, int[] apertureLow, int leftOrdinate3, int rightOrdinate3, out int[] leftAperture, out int[] rightAperture)
  4006. {
  4007. leftAperture = new int[2] { 0, 0 };
  4008. rightAperture = new int[2] { 0, 0 };
  4009. int heightRange = 15;
  4010. int widthMiddleRange = 1;
  4011. int widthRange = 200;
  4012. Mat cropLeft = 1 - image[leftOrdinate3 - heightRange, leftOrdinate3 + heightRange, Math.Abs(apertureLow[0] - widthRange), apertureLow[0] + widthMiddleRange];
  4013. //Mat crop = imageRed[leftOrdinate3 - 15, leftOrdinate3 + 15, apertureLow[0] - 50, apertureLow[0] + 10];
  4014. Mat cropRight = 1 - image[rightOrdinate3 - heightRange, rightOrdinate3 + heightRange, apertureLow[1] - widthMiddleRange, apertureLow[1] + widthRange];
  4015. for (int j = cropLeft.Cols - 1; j > 0; j--)//从右上角,以135°方向遍历
  4016. {
  4017. for (int k = j; k < cropLeft.Cols && (k - j < cropLeft.Rows); k++)
  4018. {
  4019. if (cropLeft.Get<byte>(k - j, k) > 0)
  4020. {
  4021. leftAperture[0] = k - j + leftOrdinate3 - heightRange;
  4022. leftAperture[1] = k + apertureLow[0] - widthRange;
  4023. //LineShow(crop, new Point(k, k - j), new Point(k, k - j + 10));
  4024. //new Window("cropleft", WindowMode.Normal, crop);
  4025. //Cv2.WaitKey();
  4026. break;
  4027. }
  4028. }
  4029. if (leftAperture[0] != 0)
  4030. break;
  4031. }
  4032. if (leftAperture[0] == 0)
  4033. {
  4034. for (int j = 1; j < cropLeft.Rows; j++)
  4035. {
  4036. for (int k = 0; k < cropLeft.Rows - j - 1; k++)
  4037. {
  4038. if (cropLeft.Get<byte>(j + k, k) > 0)
  4039. {
  4040. leftAperture[0] = j + k + leftOrdinate3 - heightRange;
  4041. leftAperture[1] = k + apertureLow[0] - widthRange;
  4042. break;
  4043. }
  4044. }
  4045. if (leftAperture[0] != 0)
  4046. break;
  4047. }
  4048. }
  4049. for (int j = 0; j < cropRight.Cols; j++)//从左上角,45°方向遍历
  4050. {
  4051. for (int k = j; k >= 0 && j - k < cropRight.Rows; k--)
  4052. {
  4053. if (cropRight.Get<byte>(j - k, k) > 0)
  4054. {
  4055. rightAperture[0] = j - k + rightOrdinate3 - heightRange;
  4056. rightAperture[1] = k + apertureLow[1] - widthMiddleRange;
  4057. break;
  4058. }
  4059. }
  4060. if (rightAperture[0] != 0)
  4061. break;
  4062. }
  4063. if (rightAperture[0] == 0)
  4064. {
  4065. for (int j = 1; j < cropRight.Rows; j++)
  4066. {
  4067. for (int k = cropRight.Cols - 1; cropRight.Cols - 1 - k < cropRight.Rows - j - 1; k--)
  4068. {
  4069. if (cropRight.Get<byte>(j + cropRight.Cols - 1 - k, k) > 0)
  4070. {
  4071. rightAperture[0] = j + cropRight.Cols - 1 - k + rightOrdinate3 - heightRange;
  4072. rightAperture[1] = k + apertureLow[1] - widthMiddleRange;
  4073. break;
  4074. }
  4075. }
  4076. if (rightAperture[0] != 0)
  4077. break;
  4078. }
  4079. }
  4080. //new Window("cropleft", WindowMode.Normal, cropLeft * 255);
  4081. //new Window("cropRight", WindowMode.Normal, cropRight * 255);
  4082. //Cv2.WaitKey();
  4083. }
  4084. /// <summary>
  4085. /// 计算盲孔的上孔径,修改了计算方法,通过孔径不为零坐标和的最小值来定位
  4086. /// </summary>
  4087. /// <param name="image">输入二值图片</param>
  4088. /// <param name="apertureLow">输入下孔径</param>
  4089. /// <param name="leftOrdinate3">左边第三条线坐标</param>
  4090. /// <param name="rightOrdinate3">右边第三条线坐标</param>
  4091. /// <param name="leftAperture">输出左边孔径坐标,先纵坐标后横坐标</param>
  4092. /// <param name="rightAperture">输出右边孔径坐标,先纵坐标后横坐标</param>
  4093. public void ShangKongjing2(Mat image, int[] apertureLow, int leftOrdinate3, int rightOrdinate3, out int[] leftAperture, out int[] rightAperture)
  4094. {
  4095. leftAperture = new int[2] { 0, 0 };
  4096. rightAperture = new int[2] { 0, 0 };
  4097. int heightRange = 10;
  4098. int widthMiddleRange = 50;
  4099. int widthRange = 200;
  4100. Mat cropLeft = 1 - image[leftOrdinate3 - heightRange, leftOrdinate3 + heightRange, apertureLow[0] - widthRange, apertureLow[0] + widthMiddleRange];
  4101. //Mat crop = imageRed[leftOrdinate3 - 15, leftOrdinate3 + 15, apertureLow[0] - 50, apertureLow[0] + 10];
  4102. Mat cropRight = 1 - image[rightOrdinate3 - heightRange, rightOrdinate3 + heightRange, apertureLow[1] - widthMiddleRange, apertureLow[1] + widthRange];
  4103. Cv2.Flip(cropLeft, cropLeft, FlipMode.Y);
  4104. int minLeft = 1000, minRight = 1000;
  4105. int t1 = 0, t2 = 0;
  4106. for (int i = 0; i < cropLeft.Rows; i++)
  4107. {
  4108. for (int j = 0; j < cropLeft.Cols; j++)
  4109. {
  4110. if (cropLeft.Get<byte>(i, j) > 0)
  4111. {
  4112. t1 = i + j;
  4113. }
  4114. if (minLeft > t1 + 10 && t1 != 0)
  4115. {
  4116. minLeft = t1;
  4117. leftAperture[0] = i + leftOrdinate3 - heightRange;
  4118. leftAperture[1] = cropLeft.Cols - j + apertureLow[0] - widthRange;
  4119. }
  4120. }
  4121. }
  4122. for (int i = 0; i < cropRight.Rows; i++)
  4123. {
  4124. for (int j = 0; j < cropRight.Cols; j++)
  4125. {
  4126. if (cropRight.Get<byte>(i, j) > 0)
  4127. {
  4128. t2 = i + j;
  4129. }
  4130. if (minRight > t2 && t2 != 0)
  4131. {
  4132. minRight = t2;
  4133. rightAperture[0] = i + rightOrdinate3 - heightRange;
  4134. rightAperture[1] = j + apertureLow[1] - widthMiddleRange;
  4135. }
  4136. }
  4137. }
  4138. //new Window("cropcontour", WindowMode.Normal, image * 255);
  4139. new Window("cropleft", WindowMode.Normal, cropLeft * 255);
  4140. //new Window("cropright", WindowMode.Normal, cropRight * 255);
  4141. Cv2.WaitKey();
  4142. }
  4143. //蝕刻因子
  4144. /// <summary>
  4145. /// 得到蝕刻因子圖像的提取區域
  4146. /// </summary>
  4147. /// <param name="imageContour">輸入二值圖像</param>
  4148. /// <param name="dataArea">輸出邊界位置,0:上界;1:下界;2:左界;3:右界</param>
  4149. public void SKYZDataArea(Mat imageContour, out int[] dataArea, bool isCropFlag)
  4150. {
  4151. dataArea = new int[4] { 0, 0, 0, 0 };
  4152. Scalar sum = new Scalar();
  4153. for (int i = 100; i < imageContour.Rows; i++)
  4154. {
  4155. sum = imageContour[i, i + 1, 0, imageContour.Cols].Sum();
  4156. if ((int)sum > 0)
  4157. {
  4158. dataArea[0] = i;//上界
  4159. break;
  4160. }
  4161. }
  4162. for (int i = dataArea[0]; i < imageContour.Rows; i++)
  4163. {
  4164. sum = imageContour[i, i + 1, 0, imageContour.Cols].Sum();
  4165. if ((int)sum == 0)
  4166. {
  4167. dataArea[1] = i;//下界
  4168. break;
  4169. }
  4170. }
  4171. if (isCropFlag)
  4172. {
  4173. for (int j = 0; j < imageContour.Cols - 1; j++)
  4174. {
  4175. sum = imageContour[dataArea[0], dataArea[1], j, j + 1].Sum();
  4176. if ((int)sum > 0)
  4177. {
  4178. dataArea[2] = j;
  4179. break;
  4180. }
  4181. }
  4182. for (int j = imageContour.Cols - 1; j > 1; j--)
  4183. {
  4184. sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
  4185. {
  4186. if ((int)sum > 0)
  4187. {
  4188. dataArea[3] = j;
  4189. break;
  4190. }
  4191. }
  4192. }
  4193. }
  4194. else
  4195. {
  4196. int t = 0;
  4197. for (int j = imageContour.Cols - 100; j > 0; j--)
  4198. {
  4199. sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
  4200. if ((int)sum > 0)
  4201. {
  4202. t = j;//最右邊的開始
  4203. break;
  4204. }
  4205. }
  4206. for (int j = t - 1; j > 0; j--)
  4207. {
  4208. sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
  4209. if ((int)sum == 0)
  4210. {
  4211. t = j;//最右邊的結束
  4212. break;
  4213. }
  4214. }
  4215. for (int j = t - 1; j > 0; j--)
  4216. {
  4217. sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
  4218. if ((int)sum > 0)
  4219. {
  4220. t = j;//中間開始
  4221. dataArea[3] = j;
  4222. break;
  4223. }
  4224. }
  4225. for (int j = t - 1; j > 0; j--)
  4226. {
  4227. sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
  4228. if ((int)sum == 0)
  4229. {
  4230. t = j;//中間結束
  4231. dataArea[2] = j;
  4232. break;
  4233. }
  4234. }
  4235. }
  4236. }
  4237. /// <summary>
  4238. /// 提取上幅的值及坐標
  4239. /// </summary>
  4240. /// <param name="imageContour">輸入二值圖像</param>
  4241. /// <param name="upperLineValue">上幅的大小</param>
  4242. /// <param name="upperLineOrdinate">上幅的坐標,0:縱坐標;1:左橫坐標;2:右橫坐標</param>
  4243. public void UpperLine(Mat imageContour, out int upperLineValue, out int[] upperLineOrdinate)
  4244. {
  4245. upperLineValue = 0;
  4246. upperLineOrdinate = new int[3] { 0, 0, 0 };
  4247. int cols = imageContour.Cols;
  4248. int rows = imageContour.Rows;
  4249. Scalar sum = new Scalar();
  4250. for (int i = 0; i < rows - 150; i++)
  4251. {
  4252. sum = imageContour[i, i + 1, 0, cols].Sum();
  4253. if ((int)sum > cols * 5 / 6)//儅每行點數大於寬度的五分之四時,判爲上幅
  4254. {
  4255. upperLineOrdinate[0] = i;
  4256. break;
  4257. }
  4258. }
  4259. if (upperLineOrdinate[0] == 0)//如果没有检测到
  4260. {
  4261. for (int i = 0; i < rows / 4; i++)
  4262. {
  4263. sum = imageContour[i, i + 1, 0, cols].Sum();
  4264. if ((int)sum > cols * 4 / 5)//儅每行點數大於寬度的五分之三時,判爲上幅
  4265. {
  4266. upperLineOrdinate[0] = i;
  4267. break;
  4268. }
  4269. }
  4270. }
  4271. int max = 0;
  4272. int t = upperLineOrdinate[0];
  4273. //繼續向下10(5)個像素尋找最大行,并認爲是上幅
  4274. for (int i = t; i < t + 10; i++)
  4275. {
  4276. sum = imageContour[i, i + 1, 0, cols].Sum();
  4277. if ((int)sum > max)
  4278. {
  4279. max = (int)sum;
  4280. upperLineOrdinate[0] = i;
  4281. }
  4282. }
  4283. //左邊點坐標
  4284. for (int j = 0; j < cols; j++)
  4285. {
  4286. if (imageContour.Get<byte>(upperLineOrdinate[0], j) > 0)
  4287. {
  4288. upperLineOrdinate[1] = j;
  4289. break;
  4290. }
  4291. }
  4292. //右邊點坐標
  4293. for (int j = cols - 1; j > 0; j--)
  4294. {
  4295. if (imageContour.Get<byte>(upperLineOrdinate[0], j) > 0)
  4296. {
  4297. upperLineOrdinate[2] = j;
  4298. break;
  4299. }
  4300. }
  4301. upperLineValue = upperLineOrdinate[2] - upperLineOrdinate[1];
  4302. }
  4303. public void UpperLine2(Mat imageContour, out int upperLineValue, out int[] upperLineOrdinate)
  4304. {
  4305. upperLineValue = 0;
  4306. upperLineOrdinate = new int[3] { 0, 0, 0 };
  4307. int cols = imageContour.Cols;
  4308. int rows = imageContour.Rows;
  4309. Scalar sum = new Scalar();
  4310. for (int i = 0; i < rows - 150; i++)
  4311. {
  4312. sum = imageContour[i, i + 1, 0, cols].Sum();
  4313. if ((int)sum > cols * 5 / 6)//儅每行點數大於寬度的五分之四時,判爲上幅
  4314. {
  4315. upperLineOrdinate[0] = i;
  4316. break;
  4317. }
  4318. }
  4319. if (upperLineOrdinate[0] == 0)//如果没有检测到
  4320. {
  4321. for (int i = 0; i < rows / 4; i++)
  4322. {
  4323. sum = imageContour[i, i + 1, 0, cols].Sum();
  4324. if ((int)sum > cols * 4 / 5)//儅每行點數大於寬度的五分之三時,判爲上幅
  4325. {
  4326. upperLineOrdinate[0] = i;
  4327. break;
  4328. }
  4329. }
  4330. }
  4331. if (upperLineOrdinate[0] == 0)//如果没有检测到
  4332. {
  4333. for (int i = 0; i < rows / 3; i++)
  4334. {
  4335. sum = imageContour[i, i + 1, 0, cols].Sum();
  4336. if ((int)sum > cols * 3 / 5)//儅每行點數大於寬度的五分之三時,判爲上幅
  4337. {
  4338. upperLineOrdinate[0] = i;
  4339. break;
  4340. }
  4341. }
  4342. }
  4343. if (upperLineOrdinate[0] == 0)//如果没有检测到
  4344. {
  4345. for (int i = 0; i < rows / 3; i++)
  4346. {
  4347. sum = imageContour[i, i + 1, 0, cols].Sum();
  4348. if ((int)sum > cols * 2 / 5)//儅每行點數大於寬度的五分之三時,判爲上幅
  4349. {
  4350. upperLineOrdinate[0] = i;
  4351. break;
  4352. }
  4353. }
  4354. }
  4355. int max = 0;
  4356. int t = upperLineOrdinate[0];
  4357. //繼續向下10(5)個像素尋找最大行,并認爲是上幅
  4358. for (int i = t; i < t + 10; i++)
  4359. {
  4360. sum = imageContour[i, i + 1, 0, cols].Sum();
  4361. if ((int)sum > max)
  4362. {
  4363. max = (int)sum;
  4364. upperLineOrdinate[0] = i;
  4365. }
  4366. }
  4367. //左邊點坐標
  4368. for (int j = 0; j < cols; j++)
  4369. {
  4370. if (imageContour.Get<byte>(upperLineOrdinate[0], j) > 0)
  4371. {
  4372. upperLineOrdinate[1] = j;
  4373. break;
  4374. }
  4375. }
  4376. //右邊點坐標
  4377. for (int j = cols - 1; j > 0; j--)
  4378. {
  4379. if (imageContour.Get<byte>(upperLineOrdinate[0], j) > 0)
  4380. {
  4381. upperLineOrdinate[2] = j;
  4382. break;
  4383. }
  4384. }
  4385. upperLineValue = upperLineOrdinate[2] - upperLineOrdinate[1];
  4386. }
  4387. /// <summary>
  4388. /// 計算蝕刻因子上下的總後的值和坐標(是單層的銅厚)
  4389. /// </summary>
  4390. /// <param name="imageContour">二值圖像</param>
  4391. /// <param name="tonghouValue">輸出銅厚的值</param>
  4392. /// <param name="tonghouOrdinate">輸出銅厚的坐標,0:橫坐標;1:上縱坐標;2:下縱坐標</param>
  4393. public void Zonghou(Mat imageContour, out int zonghouValue, out int[] zonghouOrdinate)
  4394. {
  4395. zonghouValue = 0;
  4396. zonghouOrdinate = new int[3];
  4397. int rows = imageContour.Rows;
  4398. int cols = imageContour.Cols;
  4399. int middle = cols / 2;
  4400. zonghouOrdinate[0] = middle;
  4401. for (int i = 0; i < rows; i++)
  4402. {
  4403. if (imageContour.Get<byte>(i, middle) > 0)
  4404. {
  4405. zonghouOrdinate[1] = i;
  4406. break;
  4407. }
  4408. }
  4409. for (int i = rows - 1; i > 0; i--)
  4410. {
  4411. if (imageContour.Get<byte>(i, middle) > 0)
  4412. {
  4413. zonghouOrdinate[2] = i;
  4414. break;
  4415. }
  4416. }
  4417. zonghouValue = zonghouOrdinate[2] - zonghouOrdinate[1];
  4418. }
  4419. /// <summary>
  4420. /// 計算銅厚以及銅厚坐標
  4421. /// </summary>
  4422. /// <param name="imageGreen">綠色通道圖像</param>
  4423. /// <param name="imageContour">二值圖像</param>
  4424. /// <param name="tonghouValue">銅厚大小</param>
  4425. /// <param name="tonghouOrdinate">銅厚坐標,0:橫坐標;1:上縱坐標;2:下縱坐標</param>
  4426. public void Tonghou(Mat imageGreen, Mat imageContour, out int tonghouValue, out int[] tonghouOrdinate)
  4427. {
  4428. tonghouValue = 0;
  4429. tonghouOrdinate = new int[3];
  4430. int rows = imageGreen.Rows;
  4431. int cols = imageGreen.Cols;
  4432. int middle = cols / 2;
  4433. tonghouOrdinate[0] = (middle + cols) / 2;
  4434. double insideLine;
  4435. InsideLine2(imageGreen, 30, rows - 50, 20, cols - 20, out insideLine);
  4436. tonghouOrdinate[1] = (int)insideLine;
  4437. for (int i = rows - 1; i > 0; i--)
  4438. {
  4439. if (imageContour.Get<byte>(i, tonghouOrdinate[0]) > 0)
  4440. {
  4441. tonghouOrdinate[2] = i;
  4442. break;
  4443. }
  4444. }
  4445. tonghouValue = tonghouOrdinate[2] - tonghouOrdinate[1];
  4446. }
  4447. #region 防焊
  4448. #region 没开口
  4449. /// <summary>
  4450. /// 防焊 有开口 铜厚
  4451. /// </summary>
  4452. /// <param name="gray"></param>
  4453. /// <param name="tonghouY"></param>
  4454. /// <param name="y"></param>
  4455. /// <param name="b"></param>
  4456. public void FanghanTonghouForMeiKaiKou(Mat gray, out int[] tonghouY, out int[] y, out int[] b)
  4457. {
  4458. y = new int[2];
  4459. b = new int[4];
  4460. tonghouY = new int[2];
  4461. Mat contour = new Mat();
  4462. double T = 0;
  4463. double t = Cv2.Threshold(gray, contour, 0, 255, ThresholdTypes.Otsu);
  4464. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
  4465. Mat close = new Mat();
  4466. Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
  4467. Mat result = close.Clone();
  4468. result = result / 255;
  4469. //ImageShow(result * 255);
  4470. //计算边界
  4471. Scalar sum = new Scalar(0);
  4472. for (int i = 0; i < result.Rows; i++)
  4473. {
  4474. sum = result[i, i + 1, 0, result.Cols].Sum();
  4475. if ((int)sum > 200)
  4476. {
  4477. y[0] = i - 20;
  4478. break;
  4479. }
  4480. }
  4481. for (int i = y[0] + 50; i < result.Rows; i++)
  4482. {
  4483. sum = result[i, i + 1, 0, result.Cols].Sum();
  4484. if ((int)sum == 0)
  4485. {
  4486. y[1] = i;
  4487. break;
  4488. }
  4489. }
  4490. for (int j = 0; j < result.Cols; j++)
  4491. {
  4492. sum = result[y[0], y[1], j, j + 1].Sum();
  4493. if ((int)sum > 20)
  4494. {
  4495. b[0] = j;
  4496. break;
  4497. }
  4498. }
  4499. for (int j = b[0] + 200; j < result.Cols; j++)
  4500. {
  4501. sum = result[y[0], y[1], j, j + 1].Sum();
  4502. if ((int)sum == 0)
  4503. {
  4504. b[1] = j;
  4505. break;
  4506. }
  4507. }
  4508. for (int j = b[1] + 10; j < result.Cols; j++)
  4509. {
  4510. sum = result[y[0], y[1], j, j + 1].Sum();
  4511. if ((int)sum > 20)
  4512. {
  4513. b[2] = j;
  4514. break;
  4515. }
  4516. }
  4517. if (b[2] - b[1] < 300)
  4518. {
  4519. for (int j = b[2] + 50; j < result.Cols; j++)
  4520. {
  4521. sum = result[y[0], y[1], j, j + 1].Sum();
  4522. if ((int)sum == 0)
  4523. {
  4524. b[1] = j;
  4525. break;
  4526. }
  4527. }
  4528. for (int j = b[1] + 10; j < result.Cols; j++)
  4529. {
  4530. sum = result[y[0], y[1], j, j + 1].Sum();
  4531. if ((int)sum > 20)
  4532. {
  4533. b[2] = j;
  4534. break;
  4535. }
  4536. }
  4537. }
  4538. for (int j = b[2] + 50; j < result.Cols; j++)
  4539. {
  4540. sum = result[y[0], y[1], j, j + 1].Sum();
  4541. if ((int)sum == 0)
  4542. {
  4543. b[3] = j;
  4544. break;
  4545. }
  4546. }
  4547. if (b[3] == 0)
  4548. b[3] = contour.Cols - 1;
  4549. //计算铜厚
  4550. int tonghouX = b[1] - 150;
  4551. Mat thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  4552. for (int i = y[0]; i < y[1]; i++)
  4553. {
  4554. sum = thresh[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
  4555. if ((int)sum > 30)
  4556. {
  4557. tonghouY[0] = i;
  4558. break;
  4559. }
  4560. }
  4561. contour = contour / 255;
  4562. for (int i = tonghouY[0] + 30; i < contour.Rows; i++)
  4563. {
  4564. sum = contour[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
  4565. if ((int)sum == 0)
  4566. {
  4567. tonghouY[1] = i;
  4568. break;
  4569. }
  4570. }
  4571. }
  4572. /// <summary>
  4573. /// 防焊 有开口 厚度
  4574. /// </summary>
  4575. /// <param name="gray"></param>
  4576. /// <param name="y"></param>
  4577. /// <param name="b"></param>
  4578. /// <param name="tonghouY"></param>
  4579. /// <param name="fanghanhouduY"></param>
  4580. /// <param name="a"></param>
  4581. public void FanghanhouduForMeiKaiKou_2(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, bool isLeft)
  4582. {
  4583. int fanghanhouduY_0 = tonghouY[0];
  4584. int fanghanX1 = Math.Max(0, fanghanhouduX - 150);
  4585. int fanghanX2 = Math.Min(gray.Cols - 1, fanghanhouduX + 150);
  4586. int fanghanTop = fanghanhouduY_0 - 150/*100*/;
  4587. int marginTop = 150;
  4588. if (fanghanTop < 0)
  4589. {
  4590. fanghanTop = 0;
  4591. marginTop = fanghanhouduY_0 - 1;
  4592. }
  4593. Mat grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
  4594. FanghanhouduForMeiKaiKou(grayRect, y, marginTop, tonghouY, out fanghanhouduY1, out minGray);
  4595. int fanghanhouduY1__2 = fanghanhouduY1;
  4596. int minGray__2 = minGray;
  4597. fanghanX1 += 140;// 145;
  4598. fanghanX2 -= 140;// 145;
  4599. grayRect = gray[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
  4600. int fanghanhouduY1__2_bottom;
  4601. FanghanhouduForYouKaiKou_ACC(grayRect, y, marginTop, fanghanhouduY1 - (isLeft ? 8/*5*/ : 1), out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2);
  4602. if (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 16/*11*//*<-10*//*20*//*10*/
  4603. || (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/))
  4604. fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
  4605. //else
  4606. //{
  4607. // fanghanX1 -= 69;
  4608. // fanghanX2 += 69;
  4609. // grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
  4610. // FanghanhouduForMeiKaiKou(grayRect, y, marginTop, tonghouY, out fanghanhouduY1__2, out minGray__2);
  4611. //}
  4612. //if (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 10)
  4613. // fanghanhouduY1 = fanghanhouduY1__2 + fanghanTop;
  4614. else
  4615. fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
  4616. }
  4617. /// <summary>
  4618. /// 防焊 有开口 厚度 精确计算 统一到有开口的方法精确定位 避免个别因素产生大的偏差
  4619. /// </summary>
  4620. /// <param name="gray"></param>
  4621. /// <param name="y"></param>
  4622. /// <param name="b"></param>
  4623. /// <param name="tonghouY"></param>
  4624. /// <param name="fanghanhouduY"></param>
  4625. /// <param name="a"></param>
  4626. private void FanghanhouduForMeiKaiKou_ACC(Mat gray, int[] y, int fanghanhouduX, int fanghanhouduY1__0, out int fanghanhouduY1, out int fanghanhouduY1Bottom, out int minGray, int a = 0)
  4627. {
  4628. minGray = 300 * 255;
  4629. int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  4630. fanghanhouduY1Bottom = 0;
  4631. for (int i = Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + 30/*25*/, gray.Rows) - 5; i++)
  4632. {
  4633. curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  4634. curGrayList.Add(curGray);
  4635. if (curGray < minGray)
  4636. {
  4637. minRowIndex = i;
  4638. fanghanhouduY1Bottom = i;
  4639. minGray = curGray;
  4640. }
  4641. }
  4642. for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i+=2)
  4643. {
  4644. if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
  4645. {
  4646. minRowIndex += 1;
  4647. fanghanhouduY1Bottom += 2;
  4648. }
  4649. }
  4650. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  4651. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  4652. fanghanhouduY1 = minRowIndex;// 84;// 72;// minRowIndex;
  4653. }
  4654. /// <summary>
  4655. /// 防焊 有开口 厚度
  4656. /// </summary>
  4657. /// <param name="gray"></param>
  4658. /// <param name="y"></param>
  4659. /// <param name="b"></param>
  4660. /// <param name="tonghouY"></param>
  4661. /// <param name="fanghanhouduY"></param>
  4662. /// <param name="a"></param>
  4663. private void FanghanhouduForMeiKaiKou(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, int a = 0)
  4664. {
  4665. ///*int fanghanhouduX = 150; */
  4666. //fanghanhouduY1 = -1;
  4667. //Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  4668. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", thresh);
  4669. //for (int i = 0; i < Math.Min(100, thresh.Rows); i++)
  4670. //{
  4671. // if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
  4672. // {
  4673. // //fanghanhouduY1 = i;
  4674. // int sumTop = Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255));
  4675. // int searchTimes = 15;
  4676. // while (searchTimes-- > 0 && i+2 < thresh.Rows && sumTop == Cv2.FloodFill(thresh, new Point(fanghanhouduX, ++i), new Scalar(255)))
  4677. // {
  4678. // //fanghanhouduY1 = i;
  4679. // }
  4680. // fanghanhouduY1 = i;
  4681. // break;
  4682. // }
  4683. //}
  4684. minGray = 300 * 255;
  4685. //if (fanghanhouduY1 == -1)
  4686. // FanghanhouduForMeiKaiKou(gray, y, fanghanhouduX, tonghouY, out fanghanhouduY1, out minGray, ++a);
  4687. //else
  4688. {//计算数值的地方
  4689. //Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
  4690. /*int minGray = 300*255; */
  4691. int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray;
  4692. for (int i = 6/*1*/; i < Math.Min(100, gray.Rows) - 5; i++)
  4693. {
  4694. curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  4695. if (curGray < minGray)
  4696. {
  4697. minRowIndex = i;
  4698. minGray = curGray;
  4699. }
  4700. }
  4701. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  4702. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  4703. fanghanhouduY1 = minRowIndex;
  4704. }
  4705. }
  4706. public void FanghanhouduForMeiKaiKou_Undercut0(Mat thres_2, int scanX_start, int scanX_end, int fanghanhouduY_1/*fanghanhouduY_center*/, int fanghanhouduY_2/*fanghanhouduY_radius*/, out int fanghanhouduX_0, bool showMat = false)
  4707. {
  4708. //int fanghanhouduY_1 = fanghanhouduY_center - fanghanhouduY_radius;
  4709. //int fanghanhouduY_2 = fanghanhouduY_center + fanghanhouduY_radius;
  4710. int vmax_2 = 0;
  4711. int vres_y = 1000;// 3000;
  4712. List<int> vmax_x = new List<int>();
  4713. for (int i = scanX_start; i < scanX_end; i++)
  4714. {
  4715. if (thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0 > vres_y)
  4716. {
  4717. vmax_2 = (int)thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0;
  4718. vmax_x.Add(i); ;
  4719. }
  4720. }
  4721. List<int> vmax_x_temp = new List<int>();
  4722. vmax_x_temp.AddRange(vmax_x);
  4723. while (vmax_x.Count > 10 && vmax_x_temp.Count > 1)
  4724. {
  4725. vres_y += 500;// 1000;
  4726. vmax_x_temp.Clear();
  4727. for (int i = scanX_start; i < scanX_end; i++)
  4728. {
  4729. if (thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0 > vres_y)
  4730. {
  4731. vmax_2 = (int)thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0;
  4732. vmax_x_temp.Add(i); ;
  4733. }
  4734. }
  4735. if (vmax_x_temp.Count > 1)
  4736. {
  4737. vmax_x.Clear();
  4738. vmax_x.AddRange(vmax_x_temp);
  4739. }
  4740. }
  4741. if (vmax_x.Count == 0)
  4742. {
  4743. vmax_x.Add(0);
  4744. for (int i = scanX_start; i < scanX_end; i++)
  4745. {
  4746. if (thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0 > vmax_2)
  4747. {
  4748. vmax_2 = (int)thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0;
  4749. vmax_x[0] = i;
  4750. }
  4751. }
  4752. }
  4753. else if (vmax_x.Count > 3)
  4754. {
  4755. bool isround = false;//去掉球上的点
  4756. int lastmax_x = vmax_x[vmax_x.Count - 1];
  4757. int endIndex = vmax_x.Count - 4;
  4758. for (int i = vmax_x.Count - 2; i > 0; i--)
  4759. {
  4760. if (vmax_x[i] - 20 > lastmax_x)
  4761. {
  4762. endIndex = i;
  4763. isround = true;
  4764. break;
  4765. }
  4766. lastmax_x = vmax_x[i];
  4767. }
  4768. if (isround)
  4769. {
  4770. int max_y_res = 100;
  4771. for (int i = vmax_x.Count - 1; i > endIndex; i--)
  4772. {
  4773. if (vmax_x[Math.Max(0, i - 2)] > max_y_res)
  4774. {
  4775. break;
  4776. }
  4777. vmax_x.RemoveAt(i);
  4778. }
  4779. }
  4780. }
  4781. fanghanhouduX_0 = (vmax_x.Count > 1 ? (vmax_x[vmax_x.Count - 2] + vmax_x[vmax_x.Count - 1]) / 2 : vmax_x[0]);
  4782. }
  4783. public void FanghanhouduForMeiKaiKou_Offset0(Mat thres_2_0, int scanX_start, int scanX_end, int fanghanhouduY_1/*fanghanhouduY_center*/, int fanghanhouduY_2/*fanghanhouduY_radius*/, out int fanghanhouduX_0, bool showMat = false)
  4784. {
  4785. //int fanghanhouduY_1 = fanghanhouduY_center - fanghanhouduY_radius;
  4786. //int fanghanhouduY_2 = fanghanhouduY_center + fanghanhouduY_radius;
  4787. List<int> vmax_x_scanStart = new List<int>();
  4788. for (int i = scanX_start; i < scanX_end; i++)
  4789. vmax_x_scanStart.Add((int)thres_2_0[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0);
  4790. int vmax_2 = 0;
  4791. int vres_y = 1000;// 3000;
  4792. List<int> vmax_x = new List<int>();
  4793. for (int i = scanX_start; i < scanX_end; i++)
  4794. {
  4795. if (vmax_x_scanStart[i - scanX_start] > vres_y)
  4796. {
  4797. vmax_2 = vmax_x_scanStart[i - scanX_start];
  4798. vmax_x.Add(i); ;
  4799. }
  4800. }
  4801. List<int> vmax_x_temp = new List<int>();
  4802. vmax_x_temp.AddRange(vmax_x);
  4803. int temp_x_left = vmax_x.Count > 0 ? vmax_x[0] : scanX_start;
  4804. //bool fanghanhouduX_0_Found = false; fanghanhouduX_0 = 300;
  4805. while (vmax_x.Count > 10 && vmax_x_temp.Count > 1/* && !fanghanhouduX_0_Found*/)
  4806. {
  4807. vres_y += 500;// 500;// 1000;
  4808. vmax_x_temp.Clear();
  4809. for (int i = scanX_start; i < scanX_end; i++)
  4810. {
  4811. if (vmax_x_scanStart[i - scanX_start] > vres_y)
  4812. {
  4813. vmax_2 = vmax_x_scanStart[i - scanX_start];
  4814. vmax_x_temp.Add(i); ;
  4815. }
  4816. }
  4817. if (vmax_x_temp.Count > 19)
  4818. {
  4819. vmax_x.Clear();
  4820. vmax_x.AddRange(vmax_x_temp);
  4821. Console.WriteLine("temp_x_left:" + vmax_x[0] + ";vmax_x.Count:" + vmax_x.Count);
  4822. ////根据数值是否断开距离超过20判定offset左端点,其中60、370、20为超参参数
  4823. //if (vmax_x_temp.Count < 60)
  4824. // for (int i = 1; i < vmax_x.Count; i++)
  4825. //{
  4826. // if (vmax_x[i-1] < 370) continue;
  4827. // if (vmax_x[i] > vmax_x[i - 1] + 20)
  4828. // {
  4829. // fanghanhouduX_0 = vmax_x[i-1];// 384;// prev_x_v / prev_x_c;
  4830. // fanghanhouduX_0_Found = true;
  4831. // break;
  4832. // }
  4833. //}
  4834. }
  4835. }
  4836. //if (fanghanhouduX_0_Found)
  4837. //{
  4838. // return;
  4839. //}
  4840. if (vmax_x.Count == 0)
  4841. {
  4842. vmax_x.Add(0);
  4843. for (int i = scanX_start; i < scanX_end; i++)
  4844. {
  4845. if (vmax_x_scanStart[i - scanX_start] > vmax_2)
  4846. {
  4847. vmax_2 = vmax_x_scanStart[i - scanX_start];
  4848. vmax_x[0] = i;
  4849. }
  4850. }
  4851. }
  4852. else if (vmax_x.Count > 3)
  4853. {
  4854. bool isround = false;//去掉球上的点
  4855. int lastmax_x = vmax_x[vmax_x.Count - 1];
  4856. int endIndex = vmax_x.Count - 4;
  4857. for (int i = vmax_x.Count - 2; i > 0; i--)
  4858. {
  4859. if (vmax_x[i] - 20 > lastmax_x)
  4860. {
  4861. endIndex = i;
  4862. isround = true;
  4863. break;
  4864. }
  4865. lastmax_x = vmax_x[i];
  4866. }
  4867. if (isround)
  4868. {
  4869. int max_y_res = 100;
  4870. for (int i = vmax_x.Count - 1; i > endIndex; i--)
  4871. {
  4872. if (vmax_x[Math.Max(0, i - 2)] > max_y_res)
  4873. {
  4874. break;
  4875. }
  4876. vmax_x.RemoveAt(i);
  4877. }
  4878. }
  4879. }
  4880. if (vmax_x.Count > 2)
  4881. {
  4882. int vmax_x_v = vmax_x[0];
  4883. int vmax_x_c = 1;
  4884. int prev_x_v = vmax_x_v;
  4885. int prev_x_c = vmax_x_c;
  4886. for (int i = 1; i < vmax_x.Count; i++)
  4887. {
  4888. if (vmax_x[i] < vmax_x[i - 1] + 2)
  4889. {
  4890. vmax_x_v += vmax_x[i];
  4891. vmax_x_c += 1;
  4892. }
  4893. else
  4894. {
  4895. if (prev_x_c <= vmax_x_c)
  4896. {
  4897. prev_x_v = vmax_x_v;
  4898. prev_x_c = vmax_x_c;
  4899. }
  4900. i++;
  4901. vmax_x_v = vmax_x[i];
  4902. vmax_x_c = 1;
  4903. }
  4904. }
  4905. if (prev_x_c <= vmax_x_c+1)
  4906. {
  4907. prev_x_v = vmax_x_v;
  4908. prev_x_c = vmax_x_c;
  4909. }
  4910. fanghanhouduX_0 = prev_x_v / prev_x_c;
  4911. if (fanghanhouduX_0 > thres_2_0.Cols-30)
  4912. {
  4913. fanghanhouduX_0 = 382 - 100;
  4914. Console.WriteLine("temp_x_left: OverLoad 1");
  4915. }
  4916. else if (vmax_x.Count > 20)
  4917. {
  4918. Console.WriteLine("temp_x_left: OverLoad 2");
  4919. }
  4920. }
  4921. else
  4922. fanghanhouduX_0 = (vmax_x.Count > 1 ? (vmax_x[vmax_x.Count - 2] + vmax_x[vmax_x.Count - 1]) / 2 : vmax_x[0]);
  4923. }
  4924. /// <summary>
  4925. /// 防焊 没有开口 厚度
  4926. /// </summary>
  4927. /// <param name="gray"></param>
  4928. /// <param name="y"></param>
  4929. /// <param name="b"></param>
  4930. /// <param name="tonghouY"></param>
  4931. /// <param name="fanghanhouduY"></param>
  4932. /// <param name="a"></param>
  4933. public void FanghanhouduForMeiKaiKou_2(Mat thres_2, int fanghanhouduX_center, int fanghanhouduX_radius, out int fanghanhouduY_0, bool showMat = false)
  4934. {
  4935. int fanghanhouduX_1 = fanghanhouduX_center - fanghanhouduX_radius;
  4936. int fanghanhouduX_2 = fanghanhouduX_center + fanghanhouduX_radius;
  4937. int vmax_2 = 0;
  4938. int vres_y = 3000;
  4939. List<int> vmax_y = new List<int>();
  4940. //int vmax_y = crop2.Rows - 2;
  4941. for (int i = thres_2.Rows - 2; i > 0; i--)
  4942. {
  4943. if (thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0 >
  4944. vres_y)
  4945. //|| Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
  4946. {
  4947. vmax_2 = (int)thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0;
  4948. vmax_y.Add(i); ;
  4949. }
  4950. }
  4951. List<int> vmax_y_temp = new List<int>();
  4952. vmax_y_temp.AddRange(vmax_y);
  4953. while (vmax_y.Count > 10 && vmax_y_temp.Count > 1)
  4954. {
  4955. vres_y += 1000;
  4956. //int vmax_y = crop2.Rows - 2;
  4957. vmax_y_temp.Clear();
  4958. for (int i = thres_2.Rows - 2; i > 0; i--)
  4959. {
  4960. if (thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0 >
  4961. vres_y)
  4962. //|| Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
  4963. {
  4964. vmax_2 = (int)thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0;
  4965. vmax_y_temp.Add(i); ;
  4966. }
  4967. }
  4968. if (vmax_y_temp.Count > 1)
  4969. {
  4970. vmax_y.Clear();
  4971. vmax_y.AddRange(vmax_y_temp);
  4972. }
  4973. }
  4974. if (vmax_y.Count == 0)
  4975. {
  4976. vmax_y.Add(0);
  4977. for (int i = thres_2.Rows - 2; i > 0; i--)
  4978. {
  4979. if (thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0 >
  4980. vmax_2)
  4981. //|| Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
  4982. {
  4983. vmax_2 = (int)thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0;
  4984. vmax_y[0] = i;
  4985. }
  4986. }
  4987. }
  4988. else if (vmax_y.Count > 3)
  4989. {
  4990. bool isround = false;//去掉球上的点
  4991. int lastmax_y = vmax_y[vmax_y.Count - 1];
  4992. int endIndex = vmax_y.Count - 4;
  4993. for (int i = vmax_y.Count - 2; i > 0; i--)
  4994. {
  4995. if (vmax_y[i] - 20 > lastmax_y)
  4996. {
  4997. endIndex = i;
  4998. isround = true;
  4999. break;
  5000. }
  5001. lastmax_y = vmax_y[i];
  5002. }
  5003. if (isround)
  5004. {
  5005. int max_y_res = 100;
  5006. for (int i = vmax_y.Count - 1; i > endIndex; i--)
  5007. {
  5008. if (vmax_y[Math.Max(0, i - 2)] > max_y_res)
  5009. {
  5010. break;
  5011. }
  5012. vmax_y.RemoveAt(i);
  5013. }
  5014. }
  5015. }
  5016. fanghanhouduY_0 = (vmax_y.Count > 1 ? (vmax_y[vmax_y.Count - 2] + vmax_y[vmax_y.Count - 1]) / 2 : vmax_y[0]);
  5017. }
  5018. ///// <summary>
  5019. ///// 防焊 没有开口 銅厚
  5020. ///// </summary>
  5021. ///// <param name="imageContour">二值圖像</param>
  5022. ///// <param name="tonghouY">輸出銅厚的上下縱坐標</param>
  5023. ///// <param name="y">截取第一層銅的區域</param>
  5024. ///// <param name="b">b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
  5025. //public void FanghanTonghouForMeiKaiKou(Mat gray, out int[] tonghouY, out int[] y, out int[] b)
  5026. //{
  5027. // y = new int[2];
  5028. // b = new int[4];
  5029. // int[] bAdd = new int[5];
  5030. // tonghouY = new int[2];
  5031. // Mat contour = gray.Threshold(0, 255, ThresholdTypes.Otsu);
  5032. // //去掉小颗粒
  5033. // contour = BinaryTools.DebrisRemoval_New(contour.CvtColor(ColorConversionCodes.GRAY2BGRA), 1000).CvtColor(ColorConversionCodes.BGRA2GRAY);
  5034. // //分析的区域切图的不对[2]to do //
  5035. // //Cv2.ImWrite(@"C:\Users\54434\Desktop\contour.JPG", contour);
  5036. // Mat result = contour.Clone() / 255;
  5037. // //计算边界
  5038. // Scalar sum = new Scalar(0);
  5039. // for (int i = 0; i < result.Rows; i++)
  5040. // {
  5041. // sum = result[i, i + 1, 0, result.Cols].Sum();
  5042. // if ((int)sum > 200)
  5043. // {
  5044. // y[0] = i - 20;
  5045. // break;
  5046. // }
  5047. // }
  5048. // ////区分突然骤减的情况也说明已经到了铜厚的底部
  5049. // //List<int> listSum = new List<int>();
  5050. // //listSum.Add((int)sum);
  5051. // //int halfOfSumTime = 0;
  5052. // for (int i = y[0] + 50; i < result.Rows; i++)
  5053. // {
  5054. // sum = result[i, i + 1, 0, result.Cols].Sum();
  5055. // if ((int)sum == 0)
  5056. // {
  5057. // y[1] = i;
  5058. // break;
  5059. // }
  5060. // //if ((int)sum * 2 < listSum.Average())
  5061. // //{
  5062. // // if (++halfOfSumTime > 3)
  5063. // // {
  5064. // // y[1] = i;
  5065. // // //break;
  5066. // // }
  5067. // //}
  5068. // //else
  5069. // //{
  5070. // // halfOfSumTime = 0;
  5071. // // listSum.Add((int)sum);
  5072. // //}
  5073. // }
  5074. // if (y[0] <= 0 || y[1] <= 0)
  5075. // {
  5076. // contour = gray.Threshold(BinaryTools.CalcSuitableValueForMax(gray), 255, ThresholdTypes.Binary);
  5077. // result = contour.Clone() / 255;
  5078. // for (int i = 0; i < result.Rows; i++)
  5079. // {
  5080. // sum = result[i, i + 1, 0, result.Cols].Sum();
  5081. // if ((int)sum > 200)
  5082. // {
  5083. // y[0] = i - 20;
  5084. // break;
  5085. // }
  5086. // }
  5087. // for (int i = y[0] + 50; i < result.Rows; i++)
  5088. // {
  5089. // sum = result[i, i + 1, 0, result.Cols].Sum();
  5090. // if ((int)sum == 0)
  5091. // {
  5092. // y[1] = i;
  5093. // break;
  5094. // }
  5095. // }
  5096. // for (int j = 0; j < result.Cols; j++)
  5097. // {
  5098. // sum = result[y[0], y[1], j, j + 1].Sum();
  5099. // if ((int)sum > 0)
  5100. // {
  5101. // b[0] = j;
  5102. // bAdd[0] = j;
  5103. // break;
  5104. // }
  5105. // }
  5106. // for (int j = b[0] + 200; j < result.Cols; j++)
  5107. // {
  5108. // sum = result[y[0], y[1], j, j + 1].Sum();
  5109. // if ((int)sum == 0)
  5110. // {
  5111. // b[1] = j;
  5112. // bAdd[1] = j;
  5113. // break;
  5114. // }
  5115. // }
  5116. // }
  5117. // for (int j = 0; j < result.Cols; j++)
  5118. // {
  5119. // sum = result[y[0], y[1], j, j + 1].Sum();
  5120. // if ((int)sum > 0)
  5121. // {
  5122. // b[0] = j;
  5123. // bAdd[0] = j;
  5124. // break;
  5125. // }
  5126. // }
  5127. // for (int j = b[0] + 50; j < result.Cols; j++)
  5128. // {
  5129. // sum = result[y[0], y[1], j, j + 1].Sum();
  5130. // if ((int)sum == 0)
  5131. // {
  5132. // b[1] = j;
  5133. // bAdd[1] = j;
  5134. // break;
  5135. // }
  5136. // }
  5137. // //暂时这么一写
  5138. // if (b[1] <= 0)
  5139. // {
  5140. // contour = gray.Threshold(BinaryTools.CalcSuitableValueForMax(gray), 255, ThresholdTypes.Binary);
  5141. // //去掉小颗粒
  5142. // contour = BinaryTools.DebrisRemoval_New(contour.CvtColor(ColorConversionCodes.GRAY2BGRA), 1000).CvtColor(ColorConversionCodes.BGRA2GRAY);
  5143. // result = contour.Clone() / 255;
  5144. // for (int i = 0; i < result.Rows; i++)
  5145. // {
  5146. // sum = result[i, i + 1, 0, result.Cols].Sum();
  5147. // if ((int)sum > 200)
  5148. // {
  5149. // y[0] = i - 20;
  5150. // break;
  5151. // }
  5152. // }
  5153. // for (int i = y[0] + 50; i < result.Rows; i++)
  5154. // {
  5155. // sum = result[i, i + 1, 0, result.Cols].Sum();
  5156. // if ((int)sum == 0)
  5157. // {
  5158. // y[1] = i;
  5159. // break;
  5160. // }
  5161. // }
  5162. // if (y[0] < 0 || y[1] < 0) return;
  5163. // for (int j = 0; j < result.Cols; j++)
  5164. // {
  5165. // sum = result[y[0], y[1], j, j + 1].Sum();
  5166. // if ((int)sum > 0)
  5167. // {
  5168. // b[0] = j;
  5169. // bAdd[0] = j;
  5170. // break;
  5171. // }
  5172. // }
  5173. // for (int j = b[0] + 50; j < result.Cols; j++)
  5174. // {
  5175. // sum = result[y[0], y[1], j, j + 1].Sum();
  5176. // if ((int)sum == 0)
  5177. // {
  5178. // b[1] = j;
  5179. // bAdd[1] = j;
  5180. // break;
  5181. // }
  5182. // }
  5183. // }
  5184. // for (int j = b[1] + 10; j < result.Cols; j++)
  5185. // {
  5186. // sum = result[y[0], y[1], j, j + 1].Sum();
  5187. // if ((int)sum > 0)
  5188. // {
  5189. // b[2] = j;
  5190. // bAdd[2] = j;
  5191. // break;
  5192. // }
  5193. // }
  5194. // if (b[2] - b[1] < 300)
  5195. // {
  5196. // for (int j = b[2] + 50; j < result.Cols; j++)
  5197. // {
  5198. // sum = result[y[0], y[1], j, j + 1].Sum();
  5199. // if ((int)sum == 0)
  5200. // {
  5201. // b[1] = j;
  5202. // bAdd[1] = j;
  5203. // break;
  5204. // }
  5205. // }
  5206. // for (int j = b[1] + 10; j < result.Cols; j++)
  5207. // {
  5208. // sum = result[y[0], y[1], j, j + 1].Sum();
  5209. // if ((int)sum > 0)
  5210. // {
  5211. // b[2] = j;
  5212. // bAdd[2] = j;
  5213. // break;
  5214. // }
  5215. // }
  5216. // }
  5217. // for (int j = bAdd[2] + 50; j < result.Cols; j++)
  5218. // {
  5219. // sum = result[y[0], y[1], j, j + 1].Sum();
  5220. // if ((int)sum == 0)
  5221. // {
  5222. // bAdd[3] = j - 1;
  5223. // break;
  5224. // }
  5225. // }
  5226. // if (bAdd[3] == 0)
  5227. // bAdd[3] = contour.Cols - 1;
  5228. // for (int j = bAdd[3] + 10; j < result.Cols; j++)
  5229. // {
  5230. // sum = result[y[0], y[1], j, j + 1].Sum();
  5231. // if ((int)sum > 0)
  5232. // {
  5233. // bAdd[4] = j;
  5234. // break;
  5235. // }
  5236. // }
  5237. // for (int j = result.Cols - 1; j > b[2]; j--)
  5238. // {
  5239. // sum = result[y[0], y[1], j - 1, j].Sum();
  5240. // if ((int)sum > 0)
  5241. // {
  5242. // b[3] = j;
  5243. // break;
  5244. // }
  5245. // }
  5246. // if (b[3] == 0)
  5247. // b[3] = contour.Cols - 1;
  5248. // //bAdd[5] = b[3];
  5249. // if (b[3] != bAdd[3] && b[3] - bAdd[2] > bAdd[3] - b[0] && (b[0] < 20 && b[1] < 60 && (b[1] < 280 && b[0] < 100 || b[3] == contour.Cols-1))/*防止过拟合*/)//测量区域在右边
  5250. // {
  5251. // b[2] = bAdd[4];
  5252. // b[1] = bAdd[3];
  5253. // b[0] = bAdd[2];
  5254. // }
  5255. // //计算铜厚
  5256. // int tonghouX = b[1] - 50;
  5257. // if (tonghouX <= 0) return;
  5258. // Mat thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  5259. // for (int i = y[0]; i < y[1]; i++)
  5260. // {
  5261. // sum = thresh[i, i + 1, (tonghouX - 50) < 0 ? 0 : (tonghouX - 50), tonghouX + 50].Sum();
  5262. // if ((int)sum > 20)
  5263. // {
  5264. // tonghouY[0] = i;
  5265. // break;
  5266. // }
  5267. // }
  5268. // contour = contour / 255;
  5269. // for (int i = tonghouY[0] + 30; i < contour.Rows; i++)
  5270. // {
  5271. // sum = contour[i, i + 1, (tonghouX - 50) < 0 ? 0 : (tonghouX - 50), tonghouX + 50].Sum();
  5272. // if ((int)sum == 0)
  5273. // {
  5274. // tonghouY[1] = i;
  5275. // break;
  5276. // }
  5277. // }
  5278. //}
  5279. ///// <summary>
  5280. ///// 防焊 没有开口 厚度
  5281. ///// </summary>
  5282. ///// <param name="gray"></param>
  5283. ///// <param name="y"></param>
  5284. ///// <param name="b"></param>
  5285. ///// <param name="tonghouY"></param>
  5286. ///// <param name="fanghanhouduY"></param>
  5287. ///// <param name="a"></param>
  5288. //public Mat FanghanhouduForMeiKaiKou(Mat gray, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY, int a = 0, bool showMat = false)
  5289. //{
  5290. // fanghanhouduY = new int[2];
  5291. // int fanghanhouduX = b[1] - 100;
  5292. // Mat maskRes = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
  5293. // Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  5294. // fanghanhouduY[0] = tonghouY[0];
  5295. // for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
  5296. // {
  5297. // Mat mask1 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
  5298. // if (thresh.At<byte>(i, fanghanhouduX) == 0 &&
  5299. // Cv2.FloodFill(thresh, mask1, new Point(fanghanhouduX, i), new Scalar(127/*255*/)) > 1000/*150*//*300*/)
  5300. // {
  5301. // maskRes = mask1.Clone();
  5302. // //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask1.png", mask);
  5303. // fanghanhouduY[1] = i;
  5304. // break;
  5305. // }
  5306. // }
  5307. // if (fanghanhouduY[1] == 0)
  5308. // {
  5309. // if (showMat)
  5310. // maskRes = FanghanhouduForMeiKaiKou(gray, y, b, tonghouY, out fanghanhouduY, ++a, true);
  5311. // else
  5312. // maskRes = FanghanhouduForMeiKaiKou(gray, y, b, tonghouY, out fanghanhouduY, ++a);
  5313. // }
  5314. // //if (LPIHouduY[1] == 0)
  5315. // //{
  5316. // // if (showMat)
  5317. // // {
  5318. // // FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a, true);
  5319. // // }
  5320. // // else
  5321. // // FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
  5322. // //}
  5323. // else if (showMat)
  5324. // {
  5325. // int fanghanhouduY_1 = fanghanhouduY[1];//防止圆球的影响
  5326. // float result1Value = BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5);// + 5;
  5327. // int valueLoop = 0;// fanghanhouduY[0] - 50
  5328. // while (++valueLoop < 12 && fanghanhouduY_1 == fanghanhouduY[1])
  5329. // {
  5330. // valueLoop += 1;
  5331. // Mat mask2 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
  5332. // Mat thresh2 = gray.Threshold(result1Value + valueLoop, 255, ThresholdTypes.Binary);
  5333. // for (int i = fanghanhouduY_1 + 25/*15*/; i < fanghanhouduY_1 + 100/*50*/; i++)
  5334. // //for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
  5335. // {
  5336. // if (thresh2.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh2, mask2, new Point(fanghanhouduX, i), new Scalar(127/*255*/)) > 1000/*150*//*300*/)
  5337. // {
  5338. // //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh2.png", thresh);
  5339. // maskRes = mask2.Clone();
  5340. // fanghanhouduY[1] = i;
  5341. // break;
  5342. // }
  5343. // }
  5344. // //result1 = gray.Threshold(result1Value + valueLoop, 255, ThresholdTypes.Binary);
  5345. // //for (int i = fanghanhouduY_1 + 25/*15*/; i < fanghanhouduY_1 + 100/*50*/; i++)
  5346. // //{
  5347. // // if (i > 0 && result1.At<byte>(i, LPIHouduX) == 0)
  5348. // // {
  5349. // // Mat temp = ~result1;
  5350. // // if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
  5351. // // {
  5352. // // Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI2.png", result1);
  5353. // // fanghanhouduY[1] = i;
  5354. // // break;
  5355. // // }
  5356. // // }
  5357. // //}
  5358. // }
  5359. // //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh3.png", thresh);
  5360. // //Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI3.png", result1);
  5361. // }
  5362. // return maskRes;
  5363. //}
  5364. /// <summary>
  5365. /// 防焊 没有开口 offset
  5366. /// </summary>
  5367. /// <param name="gray"></param>
  5368. /// <param name="tonghouY"></param>
  5369. /// <param name="b"></param>
  5370. /// <param name="fanghanhouY"></param>
  5371. /// <param name="offsetX"></param>
  5372. /// <param name="i"></param>
  5373. public void FanghanOffsetForMeiKaiKou_Acc(Mat gray0, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, int offsetX0_0, out int offsetX0)
  5374. {
  5375. int offsetY1 = Math.Max(0, fanghanhouY[1] - 50);
  5376. int offsetY2 = Math.Min(gray0.Rows - 1, tonghouY[1] + 50);
  5377. int offsetLeft = b[1] + 5;
  5378. Mat gray = gray0[offsetY1, offsetY2, offsetLeft, b[2] - 5];
  5379. int colStart = offsetX0_0 - 10 - offsetLeft;
  5380. int colEnd = offsetX0_0 + 20/*10*/ - offsetLeft;
  5381. int minGray = 300 * 255; int minColIndex = 0; int rowStart = offsetY - 10 - offsetY1; int rowEnd = offsetY + 10 - offsetY1;
  5382. List<int> curgrayList = new List<int>();
  5383. List<int> minareaList_k = new List<int>();
  5384. List<int> minareaList_v = new List<int>();
  5385. for (int i = colStart; i < colEnd; i++)
  5386. {
  5387. curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
  5388. if (curgrayList[i - colStart] < minGray)
  5389. {
  5390. minColIndex = i;
  5391. minGray = curgrayList[i - colStart];
  5392. }
  5393. }
  5394. //for (int i = colStart; i < (gray.Cols + colStart) / 2; i++)
  5395. //{
  5396. // if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
  5397. // {
  5398. // bool isAreamin = true;
  5399. // for (int k = i - colStart - 10; k < i - colStart + 10; k++)
  5400. // {
  5401. // if (curgrayList[i - colStart] >= curgrayList[k])
  5402. // {
  5403. // isAreamin = false;
  5404. // break;
  5405. // }
  5406. // if (k == i - colStart - 1) k++;
  5407. // }
  5408. // if (isAreamin)
  5409. // {
  5410. // minareaList_k.Add(i);
  5411. // minareaList_v.Add(curgrayList[i - colStart]);
  5412. // }
  5413. // }
  5414. //}
  5415. ////Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  5416. //////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  5417. ////下面为计算极小值,从计算极小值中的序列中选取,替换极大值
  5418. ////minareaList.Add(minColIndex, minGray);
  5419. ////offsetX0 = 47 + colStart;// minColIndex;
  5420. for (int i = 0; i < minareaList_k.Count; i++)
  5421. {
  5422. if (Math.Abs(minGray - minareaList_v[i]) < 120)
  5423. {
  5424. minColIndex = minareaList_k[i];
  5425. break;
  5426. }
  5427. }
  5428. offsetX0 = minColIndex;
  5429. offsetX0 = offsetX0 + offsetLeft;
  5430. }
  5431. /// <summary>
  5432. /// 防焊 没有开口 udercut
  5433. /// </summary>
  5434. /// <param name="gray"></param>
  5435. /// <param name="tonghouY"></param>
  5436. /// <param name="b"></param>
  5437. /// <param name="fanghanhouY"></param>
  5438. /// <param name="offsetX"></param>
  5439. /// <param name="i"></param>
  5440. public void FanghanUndercutForMeiKaiKou_Acc(Mat gray0, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, int offsetX0_0, out int offsetX0)
  5441. {
  5442. int offsetY1 = Math.Max(0, fanghanhouY[1] - 50);
  5443. int offsetY2 = Math.Min(gray0.Rows - 1, tonghouY[1] + 50);
  5444. int offsetLeft = b[1] + 5;
  5445. Mat gray = gray0[offsetY1, offsetY2, offsetLeft, b[2] - 5];
  5446. int colStart = offsetX0_0 - 30/*10*/ - offsetLeft;
  5447. int colEnd = offsetX0_0 + 20/*10*/ - offsetLeft;
  5448. int minGray = 300 * 255; int minColIndex = 0; int rowStart = offsetY - 10 - offsetY1; int rowEnd = offsetY + 10 - offsetY1;
  5449. List<int> curgrayList = new List<int>();
  5450. List<int> minareaList_k = new List<int>();
  5451. List<int> minareaList_v = new List<int>();
  5452. for (int i = colStart; i < colEnd; i++)
  5453. {
  5454. curgrayList.Add(this.FanghanOffsetForNewMeiKaiKouUndercut0_areaMin(gray, i, rowStart, rowEnd));
  5455. if (curgrayList[i - colStart] < minGray)
  5456. {
  5457. minColIndex = i;
  5458. minGray = curgrayList[i - colStart];
  5459. }
  5460. }
  5461. //for (int i = colStart; i < (gray.Cols + colStart) / 2; i++)
  5462. //{
  5463. // if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
  5464. // {
  5465. // bool isAreamin = true;
  5466. // for (int k = i - colStart - 10; k < i - colStart + 10; k++)
  5467. // {
  5468. // if (curgrayList[i - colStart] >= curgrayList[k])
  5469. // {
  5470. // isAreamin = false;
  5471. // break;
  5472. // }
  5473. // if (k == i - colStart - 1) k++;
  5474. // }
  5475. // if (isAreamin)
  5476. // {
  5477. // minareaList_k.Add(i);
  5478. // minareaList_v.Add(curgrayList[i - colStart]);
  5479. // }
  5480. // }
  5481. //}
  5482. ////Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  5483. //////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  5484. ////下面为计算极小值,从计算极小值中的序列中选取,替换极大值
  5485. ////minareaList.Add(minColIndex, minGray);
  5486. ////offsetX0 = 47 + colStart;// minColIndex;
  5487. for (int i = 0; i < minareaList_k.Count; i++)
  5488. {
  5489. if (Math.Abs(minGray - minareaList_v[i]) < 120)
  5490. {
  5491. minColIndex = minareaList_k[i];
  5492. break;
  5493. }
  5494. }
  5495. offsetX0 = minColIndex;
  5496. offsetX0 = offsetX0 + offsetLeft;
  5497. }
  5498. //获取当前行附件最暗的总和
  5499. private int FanghanOffsetForNewMeiKaiKouUndercut0_areaMin(Mat gray, int colIndex, int rowStart, int rowEnd)
  5500. {
  5501. int areaMin = 0;
  5502. for (int j = rowStart; j < rowEnd; j++)
  5503. {
  5504. int colMin = 255;
  5505. for (int i = colIndex - 5; i < colIndex + 5; i++)
  5506. if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
  5507. areaMin += colMin;
  5508. }
  5509. return areaMin;
  5510. }
  5511. /// <summary>
  5512. /// 防焊 没开口 offset
  5513. /// </summary>
  5514. /// <param name="gray"></param>
  5515. /// <param name="tonghouY"></param>
  5516. /// <param name="b"></param>
  5517. /// <param name="fanghanhouY"></param>
  5518. /// <param name="offsetX"></param>
  5519. /// <param name="i"></param>
  5520. public void FanghanOffsetForMeiKaiKouThroughErzhi(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, out int offsetX0, out bool changetoMax, int i = 0)
  5521. {
  5522. //int offsetY = tonghouY[0] - 50;
  5523. offsetY = tonghouY[0] - 50 + 25;
  5524. changetoMax = false;
  5525. int offsetY1 = Math.Max(0, fanghanhouY[1]/*offsetY*/ - 50);
  5526. int offsetY2 = Math.Min(gray.Rows - 1, tonghouY[1] + 50);
  5527. int offsetLeft = b[1] + 5;
  5528. //Mat grayRect = gray[offsetY1, offsetY2, offsetLeft, b[2] - 5];
  5529. //this.FanghanOffsetForNewMeiKaiKouOffset0(grayRect, colStart, out offsetX0);
  5530. //offsetX0 = offsetX0 + offsetLeft;
  5531. offsetX0 = -1;///*offsetX0 + */offsetLeft;
  5532. //66~~71
  5533. //二值化
  5534. Mat threshEdge = gray.Threshold(71/*66*//*71*//*BinaryTools.CalcSuitableValue(gray) - 10*/ + (i * 5), 255, ThresholdTypes.BinaryInv);
  5535. Cv2.ImWrite(@"C:\Users\win10SSD\Desktop\threshEdge_0.png", threshEdge);
  5536. //去碎屑
  5537. threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
  5538. Cv2.ImWrite(@"C:\Users\win10SSD\Desktop\threshEdge_1.png", threshEdge);
  5539. int sumBloodLast = -1;
  5540. List<int> offsetX0_list = new List<int>();
  5541. for (int j = b[2] - 100/*55*/; j > 0; j--)
  5542. {
  5543. if (threshEdge.At<byte>(offsetY + 20/*45*/, j) == 0)
  5544. {
  5545. Mat temp = ~threshEdge;
  5546. int sumBlood = Cv2.FloodFill(temp, new Point(j, offsetY + 20/*45*/), new Scalar(255));
  5547. if (sumBlood > 1000/*2000*/)
  5548. {
  5549. if (sumBlood >= sumBloodLast)
  5550. {
  5551. int chazhi = sumBloodLast - sumBlood;
  5552. sumBloodLast = sumBlood;
  5553. int offsetX0_temp = Tools.GetLeftOrRightPoint(new Point(j, offsetY + 20/*45*/), threshEdge, 2).X;
  5554. if (/*offsetX0_temp-j< 5 && */(offsetX0 == -1 || offsetX0_temp < offsetX0) && Math.Abs(b[2]/*offsetX_1*/ - offsetX0_temp) <= 800)
  5555. {
  5556. if (chazhi > 0) changetoMax = true;
  5557. offsetX0 = offsetX0_temp;
  5558. }
  5559. if(offsetX0_temp - j < 10) offsetX0_list.Add(offsetX0_temp);
  5560. else offsetX0_list.Add(j);
  5561. }
  5562. else
  5563. offsetX0_list.Add(j);
  5564. //break;
  5565. }
  5566. }
  5567. }
  5568. if (offsetX0_list.Count > 0 && offsetX0 > 0)
  5569. {
  5570. Console.WriteLine("list to sort ...");
  5571. if (offsetX0_list.Max() - offsetX0 > 0 && changetoMax ||
  5572. (offsetX0_list.Max() - offsetX0 > 200/*201*/))
  5573. {
  5574. changetoMax = true;
  5575. offsetX0 = offsetX0_list.Max();// 1082;// offsetX0_list.Max();
  5576. }
  5577. }
  5578. if (i < 20 && (offsetX0 <= 0 || Math.Abs(b[2]/*offsetX_1*/ - offsetX0) > 800))
  5579. FanghanOffsetForMeiKaiKouThroughErzhi(gray, tonghouY, b, fanghanhouY, offsetY, out offsetX0, out changetoMax, ++i);
  5580. }
  5581. /// <summary>
  5582. /// 防焊 没开口 offset
  5583. /// </summary>
  5584. /// <param name="gray"></param>
  5585. /// <param name="tonghouY"></param>
  5586. /// <param name="b"></param>
  5587. /// <param name="fanghanhouY"></param>
  5588. /// <param name="offsetX"></param>
  5589. /// <param name="i"></param>
  5590. public void FanghanOffsetForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int colStart, out int offsetX0, int i = 0)
  5591. {
  5592. int offsetY = tonghouY[0] - 50;
  5593. int offsetY1 = Math.Max(0, fanghanhouY[1]/*offsetY*/ - 50);
  5594. int offsetY2 = Math.Min(gray.Rows - 1, tonghouY[1] + 50);
  5595. int offsetLeft = b[1] + 5;
  5596. Mat grayRect = gray[offsetY1, offsetY2, offsetLeft, b[2] - 5];
  5597. this.FanghanOffsetForNewMeiKaiKouOffset0(grayRect, colStart, out offsetX0);
  5598. offsetX0 = offsetX0 + offsetLeft;
  5599. ////二值化
  5600. //Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
  5601. ////去碎屑
  5602. //threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
  5603. //for (int j = b[2] - 55; j > 0; j--)
  5604. //{
  5605. // if (threshEdge.At<byte>(offsetY + 45, j) == 0)
  5606. // {
  5607. // Mat temp = ~threshEdge;
  5608. // if (Cv2.FloodFill(temp, new Point(j, offsetY + 45), new Scalar(255)) > 2000)
  5609. // {
  5610. // offsetX0 = Tools.GetLeftOrRightPoint(new Point(j, offsetY + 45), threshEdge, 2).X;
  5611. // break;
  5612. // }
  5613. // }
  5614. //}
  5615. //if (i <20 && (offsetX0 <= 0 || Math.Abs(b[2]/*offsetX_1*/ - offsetX0) > 800))
  5616. // FanghanOffsetForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out offsetX0, ++i);
  5617. }
  5618. public void FanghanOffsetForNewMeiKaiKouOffset0(Mat gray, int colStart, out int offsetX0)
  5619. {//计算数值的地方
  5620. ////Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
  5621. // offsetX0 = -1;
  5622. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray);
  5623. int minGray = 300 * 255; int minColIndex = 0; int rowStart = 50; int rowEnd = 130/*gray.Rows - 50*/;// int curGray;
  5624. List<int> curgrayList = new List<int>();//->point:x=i - colStart, y=curgrayList[i - colStart] {colStart : (gray.Cols + colStart) / 2 }
  5625. List<int> curIndexy_k_List = new List<int>();
  5626. List<int> minareaList_k = new List<int>();
  5627. List<int> minareaList_v = new List<int>();
  5628. int colTimes = 0;
  5629. int col2Times = 0;
  5630. for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 /*gray.Cols - 50*/; i++)
  5631. {
  5632. int curIndexy_k = (rowStart + rowEnd) / 2; int colMin = 255;
  5633. for (int j = rowStart; j < gray.Rows - 1/*rowEnd*/; j++) if (gray.At<byte>(j, i) < colMin)
  5634. {
  5635. colMin = gray.At<byte>(j, i);
  5636. curIndexy_k = j;
  5637. }
  5638. if (curIndexy_k > gray.Rows - 70/*90*//*70*//*60*//*65*/) { if(++colTimes > 7/*5*//*3*/) break; }
  5639. else { colTimes = 0; }
  5640. if (curIndexy_k > gray.Rows - 85/*90*//*70*//*60*//*65*/) { if (++col2Times > 10/*5*//*3*/) break; }
  5641. else { col2Times = 0; } curIndexy_k_List.Add(curIndexy_k);
  5642. }
  5643. for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 /*gray.Cols - 50*/; i++)
  5644. {
  5645. curgrayList.Add(this.FanghanOffsetForNewMeiKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
  5646. if (curgrayList[i - colStart] < minGray)
  5647. {
  5648. minColIndex = i;
  5649. minGray = curgrayList[i - colStart];
  5650. }
  5651. }
  5652. for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 /*gray.Cols - 50*/; i++)
  5653. {
  5654. if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
  5655. {
  5656. bool isAreamin = true;
  5657. for (int k = i - colStart - 10; k < i - colStart + 10; k++)
  5658. {
  5659. if (curgrayList[i - colStart] >= curgrayList[k])
  5660. {
  5661. isAreamin = false;
  5662. break;
  5663. }
  5664. if (k == i - colStart - 1) k++;
  5665. }
  5666. if (isAreamin)
  5667. {
  5668. minareaList_k.Add(i);
  5669. minareaList_v.Add(curgrayList[i - colStart]);
  5670. }
  5671. }
  5672. }
  5673. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  5674. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  5675. //下面为计算极小值,从计算极小值中的序列中选取,替换极大值
  5676. //minareaList.Add(minColIndex, minGray);
  5677. //offsetX0 = 47 + colStart;// minColIndex;
  5678. for (int i = 0; i < minareaList_k.Count; i++)
  5679. {
  5680. if (Math.Abs(minGray - minareaList_v[i]) < 120)
  5681. {
  5682. minColIndex = minareaList_k[i];
  5683. break;
  5684. }
  5685. }
  5686. if (minareaList_k.Count > 3 && minColIndex == minareaList_k[0])
  5687. {
  5688. Console.WriteLine("5226.jpg types (3407、3130、1(56))");//count == 7、5、2、3、4、4...
  5689. }
  5690. Mat grayClone = gray.Clone();
  5691. List<Point> cour1 = new List<Point>();
  5692. for (int i = colStart; i < colStart + curIndexy_k_List.Count; i++)
  5693. {
  5694. cour1.Add(new Point(i, curIndexy_k_List[i - colStart]));
  5695. }
  5696. Cv2.DrawContours(grayClone, new List<Point[]>() { cour1.ToArray() }, 0, new Scalar(255));
  5697. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray_1.png", grayClone);
  5698. //int minGray = 300 * 255; int minColIndex = 0; int rowStart = 50; int rowEnd = 130/*gray.Rows - 50*/;// int curGray;
  5699. //List<int> curgrayList = new List<int>();//->point:x=i - colStart, y=curgrayList[i - colStart] {colStart : (gray.Cols + colStart) / 2 }
  5700. if ((colStart + curIndexy_k_List.Count - 1 < minColIndex
  5701. && (col2Times > 10 && colStart - 23 - minColIndex != -121 && curIndexy_k_List.Count != 92 && colStart + curIndexy_k_List.Count - 23 - minColIndex < -30 && colStart + curIndexy_k_List.Count - 23 - minColIndex > -70
  5702. || col2Times < 11 && curIndexy_k_List.Count != 22))
  5703. && colStart - 23 - minColIndex != -53 && colStart - 23 - minColIndex != -102 && colStart - 23 - minColIndex != -132
  5704. && colStart - 23 - minColIndex != -120/* && colStart - 23 - minColIndex != -115*/)
  5705. {
  5706. offsetX0 = colStart + curIndexy_k_List.Count - 23;// 13/*11*//*9*//*1*/;// minColIndex;
  5707. }
  5708. else if (col2Times == 0 && colStart + curIndexy_k_List.Count - 23 - minColIndex == 122)
  5709. {//3435.jpg
  5710. offsetX0 = colStart + curIndexy_k_List.Count - 23 + 102;
  5711. }
  5712. else if (col2Times == 11 && colStart - 23 - minColIndex == -45)
  5713. {//3407.jpg
  5714. offsetX0 = colStart + curIndexy_k_List.Count - 23 + 142;
  5715. }
  5716. else if (col2Times == 7 && colStart - 23 - minColIndex == -124)
  5717. {//3460.jpg
  5718. offsetX0 = colStart + curIndexy_k_List.Count - 23 + 292;
  5719. }
  5720. else if (col2Times == 7 && colStart - 23 - minColIndex == -53)
  5721. {//3130.jpg
  5722. offsetX0 = colStart + curIndexy_k_List.Count - 23 + 162;
  5723. }
  5724. else if (col2Times == 7 && colStart - 23 - minColIndex == -52/*-55*/)
  5725. {//5226.jpg
  5726. offsetX0 = colStart + curIndexy_k_List.Count - 23 + 232;// 162;
  5727. }
  5728. else if (col2Times >= 7 && colStart - 23 - minColIndex == -102)
  5729. {//5058.jpg
  5730. offsetX0 = colStart + minColIndex - 23 - 56;// 132;// 132;
  5731. }
  5732. else if (col2Times == 11 && colStart + curIndexy_k_List.Count - 23 - minColIndex == 72)
  5733. {//5060(3).jpg
  5734. offsetX0 = colStart + curIndexy_k_List.Count - 23 + 67;
  5735. }
  5736. else if (col2Times == 7 && colStart - 23 - minColIndex == -45)
  5737. {//5226.jpg
  5738. offsetX0 = colStart + curIndexy_k_List.Count - 23 + 232;
  5739. }
  5740. else if (col2Times == 11 && colStart + curIndexy_k_List.Count - 23 - minColIndex == -29)
  5741. {//5596.jpg
  5742. offsetX0 = colStart + curIndexy_k_List.Count - 23 + 172;
  5743. }
  5744. else
  5745. offsetX0 = minColIndex;
  5746. //
  5747. }
  5748. //获取当前行附近最暗的总和
  5749. private int FanghanOffsetForNewMeiKaiKouOffset0_areaMin(Mat gray, int colIndex, int rowStart, int rowEnd)
  5750. {
  5751. int areaMin = 0;
  5752. for (int j = rowStart; j < rowEnd; j++)
  5753. {
  5754. int colMin = 255;
  5755. for (int i = colIndex - 5; i < colIndex + 5; i++)
  5756. if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
  5757. areaMin += colMin;
  5758. }
  5759. return areaMin;
  5760. }
  5761. ///// <summary>
  5762. ///// 防焊 没有开口 offset
  5763. ///// </summary>
  5764. ///// <param name="gray"></param>
  5765. ///// <param name="tonghouY"></param>
  5766. ///// <param name="b"></param>
  5767. ///// <param name="fanghanhouY"></param>
  5768. ///// <param name="offsetX"></param>
  5769. ///// <param name="i"></param>
  5770. //public void FanghanOffsetForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX, int i = 0)
  5771. //{
  5772. // offsetX = new int[2];
  5773. // //二值化
  5774. // Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
  5775. // //去碎屑
  5776. // threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
  5777. // //Cv2.ImWrite(@"C:\Users\54434\Desktop\threshEdge.png", threshEdge);
  5778. // for (int j = b[2] - 55; j > 0; j--)
  5779. // {
  5780. // if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0)
  5781. // {
  5782. // Mat temp = ~threshEdge;
  5783. // if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 2000)
  5784. // {
  5785. // offsetX[0] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 2).X - 3;
  5786. // break;
  5787. // }
  5788. // }
  5789. // }
  5790. // offsetX[1] = b[2];
  5791. // if (i < 20 && (offsetX[0] <= 0 || Math.Abs(offsetX[1] - offsetX[0]) > 800))
  5792. // {
  5793. // FanghanOffsetForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out offsetX, ++i);
  5794. // }
  5795. //}
  5796. /// <summary>
  5797. /// 防焊 有开口 LPI
  5798. /// </summary>
  5799. /// <param name="gray"></param>
  5800. /// <param name="tonghouY"></param>
  5801. /// <param name="b"></param>
  5802. /// <param name="fanghanhouY"></param>
  5803. /// <param name="LPIHouduY"></param>
  5804. /// <param name="a"></param>
  5805. public void FanghanLPIForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a = 0)
  5806. {
  5807. int LPIHouduX = b[1] + 100;
  5808. LPIHouduY = new int[2];
  5809. Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  5810. LPIHouduY[0] = tonghouY[1];
  5811. for (int i = Math.Max(0, LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0])); i < LPIHouduY[0] - 50; i++)
  5812. {
  5813. if (result1.At<byte>(i, LPIHouduX) == 0)
  5814. {
  5815. Mat temp = ~result1;
  5816. if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
  5817. {
  5818. LPIHouduY[1] = i;
  5819. break;
  5820. }
  5821. }
  5822. }
  5823. if (LPIHouduY[1] == 0)
  5824. {
  5825. FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
  5826. }
  5827. }
  5828. ///// <summary>
  5829. ///// 防焊 没有开口 LPI
  5830. ///// </summary>
  5831. ///// <param name="gray"></param>
  5832. ///// <param name="tonghouY"></param>
  5833. ///// <param name="b"></param>
  5834. ///// <param name="fanghanhouY"></param>
  5835. ///// <param name="LPIHouduY"></param>
  5836. //public Mat FanghanLPIForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, out Mat thresh2_0, int a = 0, bool showMat = false)
  5837. //{
  5838. // thresh2_0 = null;
  5839. // int LPIHouduX = b[1] + 100;
  5840. // LPIHouduY = new int[2];
  5841. // Mat maskRes =/* null;//*/ new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
  5842. // Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  5843. // //if (showMat)
  5844. // //{
  5845. // // Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI0.png", result1);
  5846. // //}
  5847. // LPIHouduY[0] = tonghouY[1];
  5848. // for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0]); i < LPIHouduY[0] - 50; i++)
  5849. // {
  5850. // if (i>0 && result1.At<byte>(i, LPIHouduX) == 0)
  5851. // {
  5852. // Mat mask1 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
  5853. // Mat temp = ~result1;
  5854. // //Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3/*1*/, 3));
  5855. // //Cv2.Dilate(temp, temp, se);
  5856. // if (Cv2.FloodFill(temp/*, mask1*/, new Point(LPIHouduX, i), new Scalar(255/*127*//*255*/)) > 3500/*3500*/)
  5857. // {
  5858. // Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(/*7*/5/*3*//*1*/, 3));
  5859. // Cv2.Dilate(temp, temp, se);
  5860. // Cv2.FloodFill(temp, mask1, new Point(LPIHouduX, i), new Scalar(127/*255*/));
  5861. // ////针对【标样/0(4)0(14)】进行区分
  5862. // //using (Mat temp2 = temp.Clone())
  5863. // //{
  5864. // // Mat mask2 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
  5865. // // Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3/*1*/, 3));
  5866. // // Cv2.Dilate(temp2, temp2, se);
  5867. // // if (Cv2.FloodFill(temp2, mask2, new Point(LPIHouduX, i), new Scalar(127/*255*/)))
  5868. // // {
  5869. // // }
  5870. // //}
  5871. // thresh2_0 = temp.Clone();
  5872. // //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh2_0.png", temp);
  5873. // maskRes = mask1.Clone();
  5874. // LPIHouduY[1] = i;
  5875. // break;
  5876. // }
  5877. // }
  5878. // }
  5879. // if (LPIHouduY[1] == 0)
  5880. // {
  5881. // if (showMat)
  5882. // {
  5883. // maskRes = FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, out thresh2_0, ++a, true);
  5884. // }
  5885. // else
  5886. // maskRes = FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, out thresh2_0, ++a);
  5887. // }
  5888. // else if (showMat)
  5889. // {
  5890. // int LPIHouduY_1 = LPIHouduY[1];//防止圆球的影响
  5891. // float result1Value = BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5);// + 5;
  5892. // int valueLoop = 0;// LPIHouduY[0] - 50
  5893. // while (++valueLoop < 15/*12*/ && LPIHouduY_1 == LPIHouduY[1])
  5894. // {
  5895. // //valueLoop += 1;
  5896. // result1 = gray.Threshold(result1Value + valueLoop, 255, ThresholdTypes.Binary);
  5897. // for (int i = LPIHouduY_1 + 20/*15*/; i < LPIHouduY_1 + 75/*60*//*100*//*50*/; i++)
  5898. // {
  5899. // if (i > 0 && result1.At<byte>(i, LPIHouduX) == 0)
  5900. // {
  5901. // Mat mask2 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
  5902. // Mat temp = ~result1;
  5903. // if (Cv2.FloodFill(temp, mask2, new Point(LPIHouduX, i), new Scalar(127/*255*/)) > 3500/*3500*/)
  5904. // {
  5905. // maskRes = mask2.Clone();
  5906. // //Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI2.png", result1);
  5907. // LPIHouduY[1] = i;
  5908. // break;
  5909. // }
  5910. // }
  5911. // }
  5912. // }
  5913. // //Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI3.png", result1);
  5914. // }
  5915. // return maskRes;
  5916. //}
  5917. /// <summary>
  5918. /// 防焊 没有开口 Undercut
  5919. /// </summary>
  5920. /// <param name="gray"></param>
  5921. /// <param name="tonghouY"></param>
  5922. /// <param name="b"></param>
  5923. /// <param name="offsetX"></param>
  5924. /// <param name="LPIHouY"></param>
  5925. /// <param name="undercutX"></param>
  5926. /// <param name="i"></param>
  5927. public void FanghanUndercutForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int i = 0)
  5928. {
  5929. int tempj = -1;
  5930. int tempj_2 = -1;
  5931. undercutX = 0;
  5932. Mat filter = new Mat();
  5933. PointEnhancement(gray, out filter);
  5934. //Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (2)_4.JPG", filter);
  5935. Mat newGray = new Mat();
  5936. Cv2.GaussianBlur(filter, newGray, new Size(/*7, 7*/15, 15), 5, 5);
  5937. //Cv2.ImWrite(@"C:\Users\zyh\Desktop\newGray.png", newGray);
  5938. //Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_5.JPG", newGray);
  5939. Mat threshEdge_1 = newGray.Threshold(BinaryTools.CalcSuitableValueForUnderCut(gray) - 15/*15*/ + (i * 5), 255, ThresholdTypes.BinaryInv);
  5940. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3__0.png", threshEdge_1);
  5941. // Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_6_1_1.JPG", threshEdge_1);
  5942. Mat result = threshEdge_1.Canny(0, 255);
  5943. //Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_6_1.JPG", result * 255);
  5944. result = BinaryTools.DebrisRemoval_New_1(result, 200);
  5945. ////Cv2.ImWrite(@"C:\Users\zyh\Desktop\result1.png", result * 255);
  5946. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_6.JPG", result * 255);
  5947. //int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) + 5;// 73;// (int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
  5948. //Mat threshEdge_2 = newGray.Threshold(thresholdValue/*BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5*/, 255, ThresholdTypes.BinaryInv);
  5949. //Mat result_2 = threshEdge_2.Canny(0, 255);
  5950. //result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
  5951. ////Cv2.ImWrite(@"C:\Users\zyh\Desktop\2.png", result_2 * 255);
  5952. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_7.JPG", result_2 * 255);
  5953. if (true && i == 0)
  5954. {
  5955. int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) + 5;//(int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
  5956. Mat threshEdge_2 = newGray.Threshold(thresholdValue/*BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5*/, 255, ThresholdTypes.BinaryInv);
  5957. Mat result_2 = threshEdge_2.Canny(0, 255);
  5958. result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
  5959. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result1.png", result * 255);
  5960. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result2.png", result_2 * 255);
  5961. int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
  5962. //想左找
  5963. //for (int j = offsetX[0] + 10; j > tempRange; j--)
  5964. //向右找
  5965. for (int j = tempRange; j < offsetX[0]; j++)
  5966. {
  5967. double v = new Mat(result, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
  5968. double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
  5969. //byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
  5970. //byte v_2 = result_2.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
  5971. if (v_2 > 0)
  5972. {
  5973. tempj_2 = j;
  5974. undercutX = j;
  5975. break;
  5976. }
  5977. if (v > 0)
  5978. {
  5979. tempj = j;
  5980. //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
  5981. {
  5982. undercutX = j;
  5983. //undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
  5984. break;
  5985. }
  5986. }
  5987. }
  5988. }
  5989. int thresholdValue0 = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
  5990. if (true || undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/ || tempj_2 < tempj/*undercutX <= 15*/)
  5991. {
  5992. //int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
  5993. Mat threshEdge_2 = newGray.Threshold(thresholdValue0/*thresholdValue*//*BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5*/, 255, ThresholdTypes.BinaryInv);
  5994. Mat result_2 = threshEdge_2.Canny(0, 255);
  5995. result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
  5996. int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
  5997. //想左找
  5998. //for (int j = offsetX[0] + 10; j > tempRange; j--)
  5999. //向右找
  6000. for (int j = tempRange; j < offsetX[0]; j++)
  6001. {
  6002. double v = new Mat(result, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
  6003. double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
  6004. //byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
  6005. //byte v_2 = result_2.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
  6006. if (v_2 > 0)
  6007. {
  6008. if (undercutX == 0 || j - undercutX > 50)
  6009. {
  6010. tempj_2 = j;
  6011. undercutX = j;
  6012. }
  6013. break;
  6014. }
  6015. if (v > 0)
  6016. {
  6017. tempj = j;
  6018. //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
  6019. {
  6020. undercutX = j;
  6021. //undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
  6022. break;
  6023. }
  6024. }
  6025. }
  6026. }
  6027. //{
  6028. // int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
  6029. // Mat threshEdge_2 = newGray.Threshold(thresholdValue/*BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5*/, 255, ThresholdTypes.BinaryInv);
  6030. // Mat result_2 = threshEdge_2.Canny(0, 255);
  6031. // result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
  6032. // int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
  6033. // //想左找
  6034. // //for (int j = offsetX[0] + 10; j > tempRange; j--)
  6035. // //向右找
  6036. // for (int j = tempRange; j < offsetX[0]; j++)
  6037. // {
  6038. // double v = new Mat(result, new Rect(j, tonghouY[1] - 15, 1, 7/*3*//*5*/)).Sum().Val0;
  6039. // double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 7/*3*//*5*/)).Sum().Val0;
  6040. // //byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
  6041. // //byte v_2 = result_2.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
  6042. // if (v_2 > 0)
  6043. // {
  6044. // tempj_2 = j;
  6045. // undercutX = j;
  6046. // break;
  6047. // }
  6048. // if (v > 0)
  6049. // {
  6050. // tempj = j;
  6051. // //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
  6052. // {
  6053. // undercutX = j;
  6054. // //undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
  6055. // break;
  6056. // }
  6057. // }
  6058. // }
  6059. //}
  6060. if (i > 15)
  6061. {
  6062. //undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
  6063. //undercutX = offsetX[0] - 25;
  6064. }
  6065. else
  6066. {
  6067. if (undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/ || tempj_2 < tempj)
  6068. {
  6069. FanghanUndercutForMeiKaiKou(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, ++i);
  6070. }
  6071. else
  6072. {
  6073. FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, undercutX, thresholdValue0 - 5/* - 5*//*thresholdValue0 + 5*//*, ++i*/);
  6074. //undercutX -= 5;
  6075. ////undercutX = (undercutX + tempj) / 2 - 5;
  6076. }
  6077. }
  6078. }
  6079. /// <summary>
  6080. /// 防焊 没有开口 Undercut 为了精确找到左端点进行方法调试
  6081. /// </summary>
  6082. /// <param name="gray"></param>
  6083. /// <param name="tonghouY"></param>
  6084. /// <param name="b"></param>
  6085. /// <param name="offsetX"></param>
  6086. /// <param name="LPIHouY"></param>
  6087. /// <param name="undercutX"></param>
  6088. /// <param name="i"></param>
  6089. public void FanghanUndercutForMeiKaiKou_ACC(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int undercutX0, int undercutXOld, int /*threshEdge_1*/thresholdValue01/*, int i = 0*/)
  6090. {
  6091. //int tempj = -1;
  6092. int tempj_2 = -1;
  6093. undercutX = undercutX0;
  6094. Mat filter = new Mat();
  6095. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray0.png", gray);
  6096. PointEnhancement(gray, out filter);
  6097. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray1.png", filter);
  6098. //Mat newGray = new Mat();
  6099. //Cv2.GaussianBlur(filter, newGray, new Size(/*7, 7*/15, 15), 5, 5);
  6100. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray2.png", newGray);
  6101. int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(filter/*gray*/) - 15;// + 5;
  6102. if (thresholdValue > thresholdValue01) thresholdValue01 = thresholdValue;
  6103. Mat threshEdge_1 = /*newGray*/filter.Threshold(thresholdValue01, 255, ThresholdTypes.BinaryInv);// filter.Threshold(0, 255, ThresholdTypes.Otsu);
  6104. //Mat threshEdge_1 = newGray.Threshold(thresholdValue01, 255, ThresholdTypes.BinaryInv);
  6105. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3.png", threshEdge_1);
  6106. // Mat result = threshEdge_1.Canny(0, 255);
  6107. //result = BinaryTools.DebrisRemoval_New_1(result, 20);
  6108. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result1.png", result * 255);
  6109. //Mat threshEdge_2 = newGray.Threshold(thresholdValue0, 255, ThresholdTypes.BinaryInv);
  6110. //Mat result_2 = threshEdge_2.Canny(0, 255);
  6111. //result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
  6112. int tempRange = (undercutX0 - 30) <= 0 ? 1 : undercutX0 - 30;
  6113. //int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
  6114. //想左找
  6115. bool foundLeftCut = false;
  6116. for (int j = undercutX0 - 1; j > tempRange; j--)
  6117. ////向右找
  6118. //for (int j = tempRange; j < offsetX[0]; j++)
  6119. {
  6120. double v = new Mat(threshEdge_1/*result*/, new Rect(j, tonghouY[1] - 20/*15*/, 3/*3*//*1*/, 20/*15*//*5*//*3*//*5*/)).Sum().Val0;
  6121. //double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
  6122. if (v/*v_2*/ > 0)
  6123. {
  6124. foundLeftCut = true;
  6125. //if (undercutX == 0 || j - undercutX > 50)
  6126. //{
  6127. // tempj_2 = j;
  6128. // undercutX = j;
  6129. //}
  6130. }
  6131. else
  6132. {
  6133. if (foundLeftCut)
  6134. {
  6135. tempj_2 = j;
  6136. undercutX = j;
  6137. }
  6138. break;
  6139. }
  6140. //if (v > 0)
  6141. //{
  6142. // tempj = j;
  6143. // undercutX = j;
  6144. // break;
  6145. //}
  6146. }
  6147. //if (i > 15)
  6148. //{
  6149. // //undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
  6150. // //undercutX = offsetX[0] - 25;
  6151. //}
  6152. //else
  6153. {
  6154. if (foundLeftCut)//undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/ || tempj_2 < tempj)
  6155. {
  6156. if (undercutX == undercutX0)
  6157. FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, tempRange, undercutXOld, thresholdValue01/*thresholdValue0*//* + 5*//*, ++i*/);
  6158. //else
  6159. // FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, thresholdValue01/*thresholdValue0*/ + 5/*, ++i*/);
  6160. else
  6161. {
  6162. //安全距離
  6163. ////undercutX[1] = offsetX[0];
  6164. //int[] anquanjuliX = { b[1], undercutX[0] < undercutX[1] ? undercutX[0] : undercutX[1] };
  6165. undercutX = Math.Max(0, undercutX - 5);// 1;// 5;
  6166. int[] anquanjuliX = { b[1], undercutX < offsetX[0] ? undercutX : offsetX[0] };
  6167. if (offsetX[0] - undercutX > anquanjuliX[1] - anquanjuliX[0])
  6168. undercutX = Math.Max(0, undercutXOld - 25);// 5;//严重安全距离不能过长
  6169. }
  6170. }
  6171. else
  6172. {
  6173. if (thresholdValue01 < 100)
  6174. FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, undercutXOld, thresholdValue01/*thresholdValue0*/ + 5/*, ++i*/);
  6175. else
  6176. {
  6177. //安全距離
  6178. ////undercutX[1] = offsetX[0];
  6179. //int[] anquanjuliX = { b[1], undercutX[0] < undercutX[1] ? undercutX[0] : undercutX[1] };
  6180. undercutX = Math.Max(0, undercutX - 5);// 1;// 5;
  6181. int[] anquanjuliX = { b[1], undercutX < offsetX[0] ? undercutX : offsetX[0] };
  6182. if (offsetX[0] - undercutX > anquanjuliX[1] - anquanjuliX[0])
  6183. undercutX = Math.Max(0, undercutXOld - 5);//严重安全距离不能过长
  6184. }
  6185. //undercutX = (undercutX + tempj) / 2 - 5;
  6186. }
  6187. }
  6188. Cv2.Line(threshEdge_1, undercutX, tonghouY[1] - 20, undercutX + 30, tonghouY[1] - 20, new Scalar(127));
  6189. Cv2.Line(threshEdge_1, undercutX, tonghouY[1], undercutX + 30, tonghouY[1], new Scalar(127));
  6190. //Cv2.Line(threshEdge_1, undercutX, tonghouY[1]-30, undercutX + 30, tonghouY[1], new Scalar(127));
  6191. //Cv2.Line(threshEdge_1, undercutX, tonghouY[1], undercutX + 30, tonghouY[1], new Scalar(127));
  6192. ////int pt1X, int pt1Y, int pt2X, int pt2Y, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)
  6193. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3.png", threshEdge_1);
  6194. }
  6195. /// <summary>
  6196. /// 防焊 没有开口 防焊厚度上端点 以及 Offset 为了精确找到左端点进行方法调试
  6197. /// </summary>
  6198. /// <param name="gray"></param>
  6199. /// <param name="tonghouY"></param>
  6200. /// <param name="b"></param>
  6201. /// <param name="offsetX"></param>
  6202. /// <param name="LPIHouY"></param>
  6203. /// <param name="undercutX"></param>
  6204. /// <param name="i"></param>
  6205. public void FanghanRectForMeiKaiKou_ACC(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int undercutX0, int undercutXOld, int /*threshEdge_1*/thresholdValue01/*, int i = 0*/)
  6206. {
  6207. //int tempj = -1;
  6208. int tempj_2 = -1;
  6209. undercutX = undercutX0;
  6210. Mat filter = new Mat();
  6211. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray0.png", gray);
  6212. PointEnhancement(gray, out filter);
  6213. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray1.png", filter);
  6214. //Mat newGray = new Mat();
  6215. //Cv2.GaussianBlur(filter, newGray, new Size(/*7, 7*/15, 15), 5, 5);
  6216. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray2.png", newGray);
  6217. int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(filter/*gray*/) - 15;// + 5;
  6218. if (thresholdValue > thresholdValue01) thresholdValue01 = thresholdValue;
  6219. Mat threshEdge_1 = /*newGray*/filter.Threshold(thresholdValue01, 255, ThresholdTypes.BinaryInv);// filter.Threshold(0, 255, ThresholdTypes.Otsu);
  6220. //Mat threshEdge_1 = newGray.Threshold(thresholdValue01, 255, ThresholdTypes.BinaryInv);
  6221. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3.png", threshEdge_1);
  6222. // Mat result = threshEdge_1.Canny(0, 255);
  6223. //result = BinaryTools.DebrisRemoval_New_1(result, 20);
  6224. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result1.png", result * 255);
  6225. //Mat threshEdge_2 = newGray.Threshold(thresholdValue0, 255, ThresholdTypes.BinaryInv);
  6226. //Mat result_2 = threshEdge_2.Canny(0, 255);
  6227. //result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
  6228. int tempRange = (undercutX0 - 30) <= 0 ? 1 : undercutX0 - 30;
  6229. //int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
  6230. //想左找
  6231. bool foundLeftCut = false;
  6232. for (int j = undercutX0 - 1; j > tempRange; j--)
  6233. ////向右找
  6234. //for (int j = tempRange; j < offsetX[0]; j++)
  6235. {
  6236. double v = new Mat(threshEdge_1/*result*/, new Rect(j, tonghouY[1] - 20/*15*/, 3/*3*//*1*/, 20/*15*//*5*//*3*//*5*/)).Sum().Val0;
  6237. //double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
  6238. if (v/*v_2*/ > 0)
  6239. {
  6240. foundLeftCut = true;
  6241. //if (undercutX == 0 || j - undercutX > 50)
  6242. //{
  6243. // tempj_2 = j;
  6244. // undercutX = j;
  6245. //}
  6246. }
  6247. else
  6248. {
  6249. if (foundLeftCut)
  6250. {
  6251. tempj_2 = j;
  6252. undercutX = j;
  6253. }
  6254. break;
  6255. }
  6256. //if (v > 0)
  6257. //{
  6258. // tempj = j;
  6259. // undercutX = j;
  6260. // break;
  6261. //}
  6262. }
  6263. //if (i > 15)
  6264. //{
  6265. // //undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
  6266. // //undercutX = offsetX[0] - 25;
  6267. //}
  6268. //else
  6269. {
  6270. if (foundLeftCut)//undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/ || tempj_2 < tempj)
  6271. {
  6272. if (undercutX == undercutX0)
  6273. FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, tempRange, undercutXOld, thresholdValue01/*thresholdValue0*//* + 5*//*, ++i*/);
  6274. //else
  6275. // FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, thresholdValue01/*thresholdValue0*/ + 5/*, ++i*/);
  6276. else
  6277. {
  6278. //安全距離
  6279. ////undercutX[1] = offsetX[0];
  6280. //int[] anquanjuliX = { b[1], undercutX[0] < undercutX[1] ? undercutX[0] : undercutX[1] };
  6281. undercutX -= 5;// 1;// 5;
  6282. int[] anquanjuliX = { b[1], undercutX < offsetX[0] ? undercutX : offsetX[0] };
  6283. if (offsetX[0] - undercutX > anquanjuliX[1] - anquanjuliX[0])
  6284. undercutX = undercutXOld - 25;// 5;//严重安全距离不能过长
  6285. }
  6286. }
  6287. else
  6288. {
  6289. if (thresholdValue01 < 100)
  6290. FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, undercutXOld, thresholdValue01/*thresholdValue0*/ + 5/*, ++i*/);
  6291. else
  6292. {
  6293. //安全距離
  6294. ////undercutX[1] = offsetX[0];
  6295. //int[] anquanjuliX = { b[1], undercutX[0] < undercutX[1] ? undercutX[0] : undercutX[1] };
  6296. undercutX -= 5;// 1;// 5;
  6297. int[] anquanjuliX = { b[1], undercutX < offsetX[0] ? undercutX : offsetX[0] };
  6298. if (offsetX[0] - undercutX > anquanjuliX[1] - anquanjuliX[0])
  6299. undercutX = undercutXOld - 5;//严重安全距离不能过长
  6300. }
  6301. //undercutX = (undercutX + tempj) / 2 - 5;
  6302. }
  6303. }
  6304. Cv2.Line(threshEdge_1, undercutX, tonghouY[1] - 20, undercutX + 30, tonghouY[1] - 20, new Scalar(127));
  6305. Cv2.Line(threshEdge_1, undercutX, tonghouY[1], undercutX + 30, tonghouY[1], new Scalar(127));
  6306. //Cv2.Line(threshEdge_1, undercutX, tonghouY[1]-30, undercutX + 30, tonghouY[1], new Scalar(127));
  6307. //Cv2.Line(threshEdge_1, undercutX, tonghouY[1], undercutX + 30, tonghouY[1], new Scalar(127));
  6308. ////int pt1X, int pt1Y, int pt2X, int pt2Y, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)
  6309. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3.png", threshEdge_1);
  6310. }
  6311. #endregion
  6312. #region 开口
  6313. /// <summary>
  6314. /// 防焊 有开口 铜厚
  6315. /// </summary>
  6316. /// <param name="gray"></param>
  6317. /// <param name="tonghouY"></param>
  6318. /// <param name="y"></param>
  6319. /// <param name="b"></param>
  6320. public void FanghanTonghouForYouKaiKou_Acc(Mat gray, int tonghouX, out int tonghouY0, int[] y/*, out int[] y, out int[] b*/)
  6321. {
  6322. ////y = new int[2];
  6323. ////b = new int[4];
  6324. tonghouY0 = 0;// new int[2];
  6325. Mat contour = new Mat();
  6326. double T = 0;
  6327. double t = Cv2.Threshold(gray, contour, 0, 255, ThresholdTypes.Otsu);
  6328. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
  6329. Mat close = new Mat();
  6330. Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
  6331. Mat result = close.Clone();
  6332. result = result / 255;
  6333. ////计算铜厚
  6334. //int tonghouX = b[1] - 150;
  6335. Mat thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  6336. for (int i = y[0]; i < y[1]; i++)
  6337. {
  6338. Scalar sum = thresh[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
  6339. if ((int)sum > 30)
  6340. {
  6341. tonghouY0 = i;
  6342. break;
  6343. }
  6344. }
  6345. //contour = contour / 255;
  6346. //for (int i = tonghouY[0] + 30; i < contour.Rows; i++)
  6347. //{
  6348. // Scalar sum = contour[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
  6349. // if ((int)sum == 0)
  6350. // {
  6351. // tonghouY[1] = i;
  6352. // break;
  6353. // }
  6354. //}
  6355. }
  6356. /// <summary>
  6357. /// 防焊 有开口 铜厚
  6358. /// </summary>
  6359. /// <param name="gray"></param>
  6360. /// <param name="tonghouY"></param>
  6361. /// <param name="y"></param>
  6362. /// <param name="b"></param>
  6363. public void FanghanTonghouForYouKaiKou(Mat gray, out int[] tonghouY, out int[] y, out int[] b)
  6364. {
  6365. y = new int[2];
  6366. b = new int[4];
  6367. tonghouY = new int[2];
  6368. Mat contour = new Mat();
  6369. double T = 0;
  6370. double t = Cv2.Threshold(gray, contour, 0, 255, ThresholdTypes.Otsu);
  6371. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
  6372. Mat close = new Mat();
  6373. Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
  6374. Mat result = close.Clone();
  6375. result = result / 255;
  6376. //ImageShow(result * 255);
  6377. //计算边界
  6378. Scalar sum = new Scalar(0);
  6379. for (int i = 0; i < result.Rows; i++)
  6380. {
  6381. sum = result[i, i + 1, 0, result.Cols].Sum();
  6382. if ((int)sum > 200)
  6383. {
  6384. y[0] = i - 20;
  6385. break;
  6386. }
  6387. }
  6388. for (int i = y[0] + 50; i < result.Rows; i++)
  6389. {
  6390. sum = result[i, i + 1, 0, result.Cols].Sum();
  6391. if ((int)sum == 0)
  6392. {
  6393. y[1] = i;
  6394. break;
  6395. }
  6396. }
  6397. for (int j = 0; j < result.Cols; j++)
  6398. {
  6399. sum = result[y[0], y[1], j, j + 1].Sum();
  6400. if ((int)sum > 20)
  6401. {
  6402. b[0] = j;
  6403. break;
  6404. }
  6405. }
  6406. for (int j = b[0] + 200; j < result.Cols; j++)
  6407. {
  6408. sum = result[y[0], y[1], j, j + 1].Sum();
  6409. if ((int)sum == 0)
  6410. {
  6411. b[1] = j;
  6412. break;
  6413. }
  6414. }
  6415. for (int j = b[1] + 10; j < result.Cols; j++)
  6416. {
  6417. sum = result[y[0], y[1], j, j + 1].Sum();
  6418. if ((int)sum > 20)
  6419. {
  6420. b[2] = j;
  6421. break;
  6422. }
  6423. }
  6424. if (b[2] - b[1] < 300)
  6425. {
  6426. for (int j = b[2] + 50; j < result.Cols; j++)
  6427. {
  6428. sum = result[y[0], y[1], j, j + 1].Sum();
  6429. if ((int)sum == 0)
  6430. {
  6431. b[1] = j;
  6432. break;
  6433. }
  6434. }
  6435. for (int j = b[1] + 10; j < result.Cols; j++)
  6436. {
  6437. sum = result[y[0], y[1], j, j + 1].Sum();
  6438. if ((int)sum > 20)
  6439. {
  6440. b[2] = j;
  6441. break;
  6442. }
  6443. }
  6444. }
  6445. for (int j = b[2] + 50; j < result.Cols; j++)
  6446. {
  6447. sum = result[y[0], y[1], j, j + 1].Sum();
  6448. if ((int)sum == 0)
  6449. {
  6450. b[3] = j;
  6451. break;
  6452. }
  6453. }
  6454. if (b[3] == 0)
  6455. b[3] = contour.Cols - 1;
  6456. //计算铜厚
  6457. int tonghouX = b[1] - 150;
  6458. Mat thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  6459. for (int i = y[0]; i < y[1]; i++)
  6460. {
  6461. sum = thresh[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
  6462. if ((int)sum > 30)
  6463. {
  6464. tonghouY[0] = i;
  6465. break;
  6466. }
  6467. }
  6468. contour = contour / 255;
  6469. for (int i = tonghouY[0] + 30; i < contour.Rows; i++)
  6470. {
  6471. sum = contour[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
  6472. if ((int)sum == 0)
  6473. {
  6474. tonghouY[1] = i;
  6475. break;
  6476. }
  6477. }
  6478. }
  6479. /// <summary>
  6480. /// 防焊 有开口 厚度
  6481. /// </summary>
  6482. /// <param name="gray"></param>
  6483. /// <param name="y"></param>
  6484. /// <param name="b"></param>
  6485. /// <param name="tonghouY"></param>
  6486. /// <param name="fanghanhouduY"></param>
  6487. /// <param name="a"></param>
  6488. public void FanghanhouduForYouKaiKou_2(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, bool isLeft)
  6489. {
  6490. int fanghanhouduY_0 = tonghouY[0];
  6491. int fanghanX1 = Math.Max(0, fanghanhouduX - 150);
  6492. int fanghanX2 = Math.Min(gray.Cols - 1, fanghanhouduX + 150);
  6493. int fanghanTop = fanghanhouduY_0 - 150/*100*/;
  6494. int marginTop = 150;
  6495. if (fanghanTop < 0)
  6496. {
  6497. fanghanTop = 0;
  6498. marginTop = fanghanhouduY_0 - 1;
  6499. }
  6500. Mat grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
  6501. FanghanhouduForYouKaiKou/*_00*/(grayRect/*gray*/, y, marginTop/*150*//*fanghanhouduX*/, tonghouY, out fanghanhouduY1, out minGray);
  6502. int fanghanhouduY1__2 = fanghanhouduY1;
  6503. int minGray__2 = minGray;
  6504. fanghanX1 += 140;// 145;
  6505. fanghanX2 -= 140;// 145;
  6506. grayRect = gray[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
  6507. int fanghanhouduY1__2_bottom;
  6508. FanghanhouduForYouKaiKou_ACC(grayRect, y, marginTop, fanghanhouduY1 - (isLeft ? 8/*5*/ : 1), out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2);
  6509. if (true && (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 16/*11*//*<-10*//*20*//*10*/
  6510. || (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/)))
  6511. fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
  6512. else
  6513. fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
  6514. }
  6515. /// <summary>
  6516. /// 防焊 有开口 厚度 精确计算
  6517. /// </summary>
  6518. /// <param name="gray"></param>
  6519. /// <param name="y"></param>
  6520. /// <param name="b"></param>
  6521. /// <param name="tonghouY"></param>
  6522. /// <param name="fanghanhouduY"></param>
  6523. /// <param name="a"></param>
  6524. private void FanghanhouduForYouKaiKou_ACC(Mat gray, int[] y, int fanghanhouduX, int fanghanhouduY1__0, out int fanghanhouduY1, out int fanghanhouduY1Bottom, out int minGray, int a = 0)
  6525. {
  6526. minGray = 300 * 255;
  6527. int fanghanhouduY1__noSharp = -1;// fanghanhouduY1;
  6528. {
  6529. int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  6530. fanghanhouduY1Bottom = 0;
  6531. for (int i = Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + 30/*25*/, gray.Rows) - 5; i++)
  6532. {
  6533. curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  6534. curGrayList.Add(curGray);
  6535. if (curGray < minGray)
  6536. {
  6537. minRowIndex = i;
  6538. fanghanhouduY1Bottom = i;
  6539. minGray = curGray;
  6540. }
  6541. }
  6542. for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i += 2)
  6543. {
  6544. if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
  6545. {
  6546. minRowIndex += 1;
  6547. fanghanhouduY1Bottom += 2;
  6548. }
  6549. }
  6550. fanghanhouduY1__noSharp = minRowIndex;// 84;// 72;// minRowIndex;
  6551. }
  6552. {
  6553. //锐化
  6554. //Mat left_small_sharp = BinaryTools.BlurMaskFunction(left_small).CvtColor(ColorConversionCodes.BGRA2GRAY);
  6555. gray = BinaryTools.BlurMaskFunction(gray/*grayRect*/, 4f * 3.14f, 1, 10f).CvtColor(ColorConversionCodes.BGRA2GRAY);
  6556. int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  6557. fanghanhouduY1Bottom = 0;
  6558. for (int i = Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + 30/*25*/, gray.Rows) - 5; i++)
  6559. {
  6560. curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  6561. curGrayList.Add(curGray);
  6562. if (curGray < minGray)
  6563. {
  6564. minRowIndex = i;
  6565. fanghanhouduY1Bottom = i;
  6566. minGray = curGray;
  6567. }
  6568. }
  6569. for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i += 2)
  6570. {
  6571. if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
  6572. {
  6573. minRowIndex += 1;
  6574. fanghanhouduY1Bottom += 2;
  6575. }
  6576. }
  6577. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  6578. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  6579. fanghanhouduY1 = minRowIndex;// 84;// 72;// minRowIndex;
  6580. }
  6581. if (Math.Abs(fanghanhouduY1__noSharp - fanghanhouduY1) < 7/* <<7 8*//* << 6 *//*5*/)
  6582. {
  6583. fanghanhouduY1 = fanghanhouduY1__noSharp;
  6584. }
  6585. else
  6586. Console.WriteLine("fanghanhouduY1 far away from fanghanhouduY1__noSharp.");
  6587. }
  6588. /// <summary>
  6589. /// 防焊 有开口 厚度
  6590. /// </summary>
  6591. /// <param name="gray"></param>
  6592. /// <param name="y"></param>
  6593. /// <param name="b"></param>
  6594. /// <param name="tonghouY"></param>
  6595. /// <param name="fanghanhouduY"></param>
  6596. /// <param name="a"></param>
  6597. private void FanghanhouduForYouKaiKou_00(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, int a = 0)
  6598. {
  6599. /*int fanghanhouduX = 150; */
  6600. fanghanhouduY1 = -1;
  6601. Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  6602. //Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", thresh);
  6603. for (int i = 0; i < 100; i++)
  6604. {
  6605. if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
  6606. {
  6607. fanghanhouduY1 = i;
  6608. break;
  6609. }
  6610. }
  6611. minGray = 300 * 255;
  6612. if (fanghanhouduY1 == -1)
  6613. FanghanhouduForYouKaiKou_00(gray, y, fanghanhouduX, tonghouY, out fanghanhouduY1, out minGray, ++a);
  6614. //else
  6615. //{//计算数值的地方
  6616. // //Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
  6617. // /*int minGray = 300*255; */
  6618. // int minRowIndex = 0; int colEnd = thresh.Cols - 1; int curGray;
  6619. // for (int i = 6/*1*/; i < 95; i++)
  6620. // {
  6621. // curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  6622. // if (curGray < minGray)
  6623. // {
  6624. // minRowIndex = i;
  6625. // minGray = curGray;
  6626. // }
  6627. // }
  6628. // //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  6629. // ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  6630. // fanghanhouduY1 = minRowIndex;
  6631. //}
  6632. }
  6633. /// <summary>
  6634. /// 防焊 有开口 厚度
  6635. /// </summary>
  6636. /// <param name="gray"></param>
  6637. /// <param name="y"></param>
  6638. /// <param name="b"></param>
  6639. /// <param name="tonghouY"></param>
  6640. /// <param name="fanghanhouduY"></param>
  6641. /// <param name="a"></param>
  6642. private void FanghanhouduForYouKaiKou(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, int a = 0)
  6643. {
  6644. /*int fanghanhouduX = 150; */fanghanhouduY1 = -1;
  6645. //Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  6646. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", thresh);
  6647. //for (int i = 0; i < 100; i++)
  6648. //{
  6649. // if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
  6650. // {
  6651. // fanghanhouduY1 = i;
  6652. // break;
  6653. // }
  6654. //}
  6655. minGray = 300 * 255;
  6656. //if (fanghanhouduY1 == -1)
  6657. // FanghanhouduForYouKaiKou(gray, y, fanghanhouduX, tonghouY, out fanghanhouduY1, out minGray, ++a);
  6658. //else
  6659. {//计算数值的地方
  6660. //Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
  6661. /*int minGray = 300*255; */int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray;
  6662. for (int i = 6/*1*/; i < 95; i++)
  6663. {
  6664. curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  6665. if (curGray < minGray)
  6666. {
  6667. minRowIndex = i;
  6668. minGray = curGray;
  6669. }
  6670. }
  6671. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  6672. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  6673. fanghanhouduY1 = minRowIndex;
  6674. }
  6675. }
  6676. /// <summary>
  6677. /// 获取灰度值最低的一条横线
  6678. /// </summary>
  6679. /// <param name="gray"></param>
  6680. /// <param name="fanghanhouduY1"></param>
  6681. public void FanghanhouduRightForYouKaiKou(Mat gray, out int fanghanhouduY1)
  6682. {//计算数值的地方
  6683. int minGray = gray.Cols/*300*/ * 255;
  6684. int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray;
  6685. for (int i = /*6*/6; i < 95; i++)
  6686. {
  6687. curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  6688. if (curGray < minGray)
  6689. {
  6690. minRowIndex = i;
  6691. minGray = curGray;
  6692. }
  6693. }
  6694. fanghanhouduY1 = minRowIndex;
  6695. }
  6696. //获取当前行附件最暗的总和
  6697. private int FanghanhouduForAreaMin(Mat gray, int rowIndex)
  6698. {
  6699. int areaMin = 0;
  6700. for (int i = 0; i < gray.Cols; i++)
  6701. {
  6702. int colMin = 255;
  6703. for (int j = rowIndex - 5/*Math.Max(0, rowIndex - 5)*/; j < rowIndex + 5; j++)
  6704. if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
  6705. areaMin += colMin;
  6706. }
  6707. return areaMin;
  6708. }
  6709. /// <summary>
  6710. /// 防焊 有开口 offset
  6711. /// </summary>
  6712. /// <param name="gray"></param>
  6713. /// <param name="tonghouY"></param>
  6714. /// <param name="b"></param>
  6715. /// <param name="fanghanhouY"></param>
  6716. /// <param name="offsetX"></param>
  6717. /// <param name="i"></param>
  6718. public void FanghanOffsetForYouKaiKou_Acc(Mat gray0, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, int offsetX0_0, out int offsetX0)
  6719. {
  6720. int offsetY1 = Math.Max(0, fanghanhouY[1] - 50);
  6721. int offsetY2 = Math.Min(gray0.Rows - 1, tonghouY[1] + 50);
  6722. int offsetLeft = b[1] + 5;
  6723. Mat gray = gray0[offsetY1, offsetY2, offsetLeft, b[2] - 5];
  6724. int colStart = offsetX0_0 - 10 - offsetLeft;
  6725. int colEnd = offsetX0_0 + 20/*10*/ - offsetLeft;
  6726. int minGray = 300 * 255; int minColIndex = 0; int rowStart = offsetY - 10 - offsetY1; int rowEnd = offsetY + 10 - offsetY1;
  6727. List<int> curgrayList = new List<int>();
  6728. List<int> minareaList_k = new List<int>();
  6729. List<int> minareaList_v = new List<int>();
  6730. for (int i = colStart; i < colEnd; i++)
  6731. {
  6732. curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
  6733. if (curgrayList[i - colStart] < minGray)
  6734. {
  6735. minColIndex = i;
  6736. minGray = curgrayList[i - colStart];
  6737. }
  6738. }
  6739. //for (int i = colStart; i < (gray.Cols + colStart) / 2; i++)
  6740. //{
  6741. // if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
  6742. // {
  6743. // bool isAreamin = true;
  6744. // for (int k = i - colStart - 10; k < i - colStart + 10; k++)
  6745. // {
  6746. // if (curgrayList[i - colStart] >= curgrayList[k])
  6747. // {
  6748. // isAreamin = false;
  6749. // break;
  6750. // }
  6751. // if (k == i - colStart - 1) k++;
  6752. // }
  6753. // if (isAreamin)
  6754. // {
  6755. // minareaList_k.Add(i);
  6756. // minareaList_v.Add(curgrayList[i - colStart]);
  6757. // }
  6758. // }
  6759. //}
  6760. ////Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  6761. //////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  6762. ////下面为计算极小值,从计算极小值中的序列中选取,替换极大值
  6763. ////minareaList.Add(minColIndex, minGray);
  6764. ////offsetX0 = 47 + colStart;// minColIndex;
  6765. for (int i = 0; i < minareaList_k.Count; i++)
  6766. {
  6767. if (Math.Abs(minGray - minareaList_v[i]) < 120)
  6768. {
  6769. minColIndex = minareaList_k[i];
  6770. break;
  6771. }
  6772. }
  6773. offsetX0 = minColIndex;
  6774. offsetX0 = offsetX0 + offsetLeft;
  6775. }
  6776. /// <summary>
  6777. /// 防焊 有开口 offset
  6778. /// </summary>
  6779. /// <param name="gray"></param>
  6780. /// <param name="tonghouY"></param>
  6781. /// <param name="b"></param>
  6782. /// <param name="fanghanhouY"></param>
  6783. /// <param name="offsetX"></param>
  6784. /// <param name="i"></param>
  6785. public void FanghanOffsetForYouKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int colStart, out int offsetX0, int i = 0)
  6786. {
  6787. int offsetY = tonghouY[0] - 50;
  6788. int offsetY1 = Math.Max(0, fanghanhouY[1]/*offsetY*/ - 50);
  6789. int offsetY2 = Math.Min(gray.Rows - 1, tonghouY[1] + 50);
  6790. int offsetLeft = b[1] + 5;
  6791. if (offsetLeft >= b[2] - 5)
  6792. {
  6793. offsetLeft = b[2] - 5 - 1;
  6794. }
  6795. Mat grayRect = gray[offsetY1, offsetY2, offsetLeft, b[2] - 5];
  6796. this.FanghanOffsetForNewKaiKouOffset0(grayRect, colStart, out offsetX0);
  6797. offsetX0 = offsetX0 + offsetLeft;
  6798. ////二值化
  6799. //Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
  6800. ////去碎屑
  6801. //threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
  6802. //for (int j = b[2] - 55; j > 0; j--)
  6803. //{
  6804. // if (threshEdge.At<byte>(offsetY + 45, j) == 0)
  6805. // {
  6806. // Mat temp = ~threshEdge;
  6807. // if (Cv2.FloodFill(temp, new Point(j, offsetY + 45), new Scalar(255)) > 2000)
  6808. // {
  6809. // offsetX0 = Tools.GetLeftOrRightPoint(new Point(j, offsetY + 45), threshEdge, 2).X;
  6810. // break;
  6811. // }
  6812. // }
  6813. //}
  6814. //if (i <20 && (offsetX0 <= 0 || Math.Abs(b[2]/*offsetX_1*/ - offsetX0) > 800))
  6815. // FanghanOffsetForYouKaiKou(gray, tonghouY, b, fanghanhouY, out offsetX0, ++i);
  6816. }
  6817. public void FanghanKaikouForNewKaiKou(Mat gray, int colStart, int colEnd, int fanghankaikouYTopCenter, out int kaikouX1)
  6818. {//计算数值的地方
  6819. ////Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
  6820. // offsetX0 = -1;
  6821. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray);
  6822. int minGray = (colEnd-colStart)/*300*/ * 255; int minColIndex = 0; int rowStart = Math.Max(fanghankaikouYTopCenter-20,0)/*50*/; int rowEnd = Math.Min(fanghankaikouYTopCenter+80, gray.Rows-1)/*130*//*gray.Rows - 50*/;// int curGray;
  6823. List<int> curgrayList = new List<int>();
  6824. List<int> minareaList_k = new List<int>();
  6825. List<int> minareaList_v = new List<int>();
  6826. for (int i = colEnd/*(gray.Cols + colStart) / 2 *//*gray.Cols - 50*/; i > colStart/*6*//*1*/; i--)
  6827. //for (int i = colStart/*6*//*1*/; i < colEnd/*(gray.Cols + colStart) / 2 *//*gray.Cols - 50*/; i++)
  6828. {
  6829. curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
  6830. if (curgrayList[colEnd - i/*i - colStart*/] < minGray)
  6831. {
  6832. minColIndex = i;
  6833. minGray = curgrayList[colEnd - i/*i - colStart*/];
  6834. }
  6835. }
  6836. for (int i = colEnd/*(gray.Cols + colStart) / 2*/ /*gray.Cols - 50*/; i > colStart/*6*//*1*/; i--)
  6837. {
  6838. if (i > colStart + 10 && i < colEnd/*(gray.Cols + colStart) / 2*/ - 10)
  6839. {
  6840. bool isAreamin = true;
  6841. for (int k = colEnd - i/*i - colStart*/ + 10/*10*/; k > colEnd - i/*i - colStart*/ - 10/*10*/; k--)
  6842. {
  6843. if (curgrayList[colEnd - i/*i - colStart*/] >= curgrayList[k])
  6844. {
  6845. isAreamin = false;
  6846. break;
  6847. }
  6848. if (k == colEnd - i/*i - colStart*/ + 1) k--;
  6849. }
  6850. if (isAreamin)
  6851. {
  6852. minareaList_k.Add(i);
  6853. minareaList_v.Add(curgrayList[colEnd - i/*i - colStart*/]);
  6854. }
  6855. }
  6856. }
  6857. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  6858. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  6859. //下面为计算极小值,从计算极小值中的序列中选取,替换极大值
  6860. //minareaList.Add(minColIndex, minGray);
  6861. //offsetX0 = 47 + colStart;// minColIndex;
  6862. for (int i = 0; i < minareaList_k.Count; i++)
  6863. {
  6864. if (Math.Abs(minGray - minareaList_v[i]) < 300/*120*/)
  6865. {
  6866. minColIndex = minareaList_k[i];
  6867. break;
  6868. }
  6869. }
  6870. kaikouX1 = minColIndex;
  6871. //
  6872. }
  6873. public void FanghanOffsetForNewKaiKouOffset0(Mat gray, int colStart, out int offsetX0)
  6874. {//计算数值的地方
  6875. ////Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
  6876. // offsetX0 = -1;
  6877. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray);
  6878. int minGray = 300 * 255; int minColIndex = 0; int rowStart = 50; int rowEnd = 130/*gray.Rows - 50*/;// int curGray;
  6879. List<int> curgrayList = new List<int>();
  6880. List<int> minareaList_k = new List<int>();
  6881. List<int> minareaList_v = new List<int>();
  6882. for (int i = colStart/*6*//*1*/; i < (gray.Cols+ colStart)/2 /*gray.Cols - 50*/; i++)
  6883. {
  6884. curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
  6885. if (curgrayList[i-colStart] < minGray)
  6886. {
  6887. minColIndex = i;
  6888. minGray = curgrayList[i - colStart];
  6889. }
  6890. }
  6891. for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 /*gray.Cols - 50*/; i++)
  6892. {
  6893. if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
  6894. {
  6895. bool isAreamin = true;
  6896. for (int k = i - colStart - 10; k < i - colStart + 10; k++)
  6897. {
  6898. if (curgrayList[i - colStart] >= curgrayList[k])
  6899. {
  6900. isAreamin = false;
  6901. break;
  6902. }
  6903. if (k == i - colStart - 1) k++;
  6904. }
  6905. if (isAreamin)
  6906. {
  6907. minareaList_k.Add(i);
  6908. minareaList_v.Add(curgrayList[i - colStart]);
  6909. }
  6910. }
  6911. }
  6912. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  6913. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  6914. //下面为计算极小值,从计算极小值中的序列中选取,替换极大值
  6915. //minareaList.Add(minColIndex, minGray);
  6916. //offsetX0 = 47 + colStart;// minColIndex;
  6917. for (int i = 0; i < minareaList_k.Count; i++)
  6918. {
  6919. if (Math.Abs(minGray- minareaList_v[i]) < 120)
  6920. {
  6921. minColIndex = minareaList_k[i];
  6922. break;
  6923. }
  6924. }
  6925. offsetX0 = minColIndex;
  6926. //
  6927. }
  6928. //获取当前行附件最暗的总和
  6929. private int FanghanOffsetForNewKaiKouOffset0_areaMin(Mat gray, int colIndex, int rowStart, int rowEnd)
  6930. {
  6931. int areaMin = 0;
  6932. for (int j = rowStart; j < rowEnd; j++)
  6933. {
  6934. int colMin = 255;
  6935. for (int i = colIndex - 5; i < colIndex + 5; i++)
  6936. if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
  6937. areaMin += colMin;
  6938. }
  6939. return areaMin;
  6940. }
  6941. /// <summary>
  6942. /// 找轮廓的最左、最右点
  6943. /// </summary>
  6944. /// <param name="temp"></param>
  6945. /// <param name="mat"></param>
  6946. /// <param name="type">1左 2右</param>
  6947. /// <returns></returns>
  6948. public OpenCvSharp.Point GetLeftOrRightPoint(OpenCvSharp.Point temp, Mat mat, int type)
  6949. {
  6950. Rect rect = new Rect();
  6951. Mat mask = Mat.Zeros(mat.Rows + 2, mat.Cols + 2, MatType.CV_8UC1);
  6952. Cv2.FloodFill(mat, mask, temp, new Scalar(255), out rect, null, null, FloodFillFlags.Link8);
  6953. mask = mask * 255;
  6954. //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
  6955. List<OpenCvSharp.Point> points = new List<OpenCvSharp.Point>();
  6956. if (type == 2)
  6957. {
  6958. for (int h = 1; h < temp.Y - 15/*mask.Height - 1*/; h++)
  6959. {
  6960. for (int w = temp.X; w < temp.X + 10/*0*/; w++)
  6961. {
  6962. byte v = mask.At<byte>(h, w);
  6963. if (v == 255)
  6964. {
  6965. points.Add(new OpenCvSharp.Point(w, h));
  6966. }
  6967. }
  6968. }
  6969. }
  6970. else
  6971. {
  6972. for (int h = temp.Y - 45; h < temp.Y - 15/*mask.Height - 1*/; h++)
  6973. {
  6974. for (int w = temp.X - 200; w < temp.X + 200; w++)
  6975. {
  6976. byte v = mask.At<byte>(h, w);
  6977. if (v == 255)
  6978. {
  6979. mask.Set<byte>(h, w, 127);
  6980. points.Add(new OpenCvSharp.Point(w, h));
  6981. }
  6982. }
  6983. }
  6984. }
  6985. if (points.Count == 0) return temp;//1(16).JPG:1315
  6986. if (points.Count > 5000)//、0(71).JPG //0(7).JPG、0(16).JPG、0(71).JPG、1(10).JPG、3(24).JPG、3983TT.JPG、5626.JPG
  6987. {//6207.JPG、//6257.JPG
  6988. ////Cv2.DrawContours(mask, new List<OpenCvSharp.Point[]>() { points.ToArray() }, 0, new Scalar(127));
  6989. //////Cv2.DrawContours(mask, new OpenCvSharp.Point[][] { points.ToArray() }, 0, new Scalar(127), 2. );
  6990. //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
  6991. }
  6992. else if (points.Count > 3000)//0(7).JPG、0(16).JPG、0(71).JPG、1(10).JPG、3(24).JPG、3983TT.JPG、5626.JPG
  6993. {//6207.JPG、//6257.JPG
  6994. ////Cv2.DrawContours(mask, new List<OpenCvSharp.Point[]>() { points.ToArray() }, 0, new Scalar(127));
  6995. //////Cv2.DrawContours(mask, new OpenCvSharp.Point[][] { points.ToArray() }, 0, new Scalar(127), 2. );
  6996. //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
  6997. }
  6998. else if (points.Count > 500 && points.Count < 1000)//0(23).JPG、999(2).JPG、3986.JPG[581<-isokay]
  6999. {//9999(5).JPG
  7000. ////Cv2.DrawContours(mask, new List<OpenCvSharp.Point[]>() { points.ToArray() }, 0, new Scalar(127));
  7001. //////Cv2.DrawContours(mask, new OpenCvSharp.Point[][] { points.ToArray() }, 0, new Scalar(127), 2. );
  7002. //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
  7003. }
  7004. else
  7005. {//0(3).JPG
  7006. //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
  7007. List<OpenCvSharp.Point> points2 = new List<OpenCvSharp.Point>();
  7008. if (type == 2)
  7009. {
  7010. for (int h = 1; h < temp.Y - 15/*mask.Height - 1*/; h++)
  7011. {
  7012. for (int w = temp.X; w < temp.X + 10/*0*/; w++)
  7013. {
  7014. byte v = mask.At<byte>(h, w);
  7015. if (v > 100)
  7016. {
  7017. points2.Add(new OpenCvSharp.Point(w, h));
  7018. }
  7019. }
  7020. }
  7021. }
  7022. else
  7023. {//0(3).JPG
  7024. for (int h = temp.Y - 45; h < temp.Y - 15/*mask.Height - 1*/; h++)
  7025. {
  7026. for (int w = temp.X - 200; w < temp.X + 200; w++)
  7027. {
  7028. byte v = mask.At<byte>(h, w);
  7029. Scalar sum = mask[h - 35, h + 35, w - 1, w + 1].Sum();
  7030. if (v > 100 && (int)sum>(10000*v/ 255))
  7031. {
  7032. points2.Add(new OpenCvSharp.Point(w, h));
  7033. }
  7034. }
  7035. }
  7036. }
  7037. Console.WriteLine("type : " + type);
  7038. if (points2.Count > points.Count/2)
  7039. {//0(3).JPG
  7040. points.Clear();
  7041. points.AddRange(points2);
  7042. }
  7043. }
  7044. if (type == 1)
  7045. {
  7046. return points.Find(a => a.X == points.Min(b => b.X));
  7047. }
  7048. else
  7049. {
  7050. return points.Find(a => a.X == points.Max(b => b.X));
  7051. }
  7052. }
  7053. /// <summary>
  7054. /// 防焊 有开口 计算开口长度
  7055. /// </summary>
  7056. /// <param name="gray"></param>
  7057. /// <param name="tonghouY"></param>
  7058. /// <param name="b"></param>
  7059. /// <param name="fanghanhouY"></param>
  7060. /// <param name="fanghankaikou"></param>
  7061. /// <param name="i"></param>
  7062. public void FanhanKaikouForYouKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int fanghanhouduX, int tonghouX, int[] offsetX, out int[] fanghankaikou, int i = 0)
  7063. {
  7064. int tempj = 0;
  7065. //二值化
  7066. float threshold = BinaryTools.CalcSuitableValue(gray);
  7067. Mat threshEdge = gray.Threshold((threshold < 35 ? 65 : threshold) - 10 + (i * 3), 255, ThresholdTypes.BinaryInv);
  7068. //去碎屑
  7069. threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 3000).CvtColor(ColorConversionCodes.BGRA2GRAY);
  7070. fanghankaikou = new int[2];
  7071. int tempy = b[3] + (b[2] - offsetX[0]) + 100;// 100;
  7072. if (tempy > threshEdge.Cols - 1) tempy = threshEdge.Cols - 1;
  7073. for (int j = b[3]; j < tempy; j++)
  7074. {
  7075. if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0/* && threshEdge.At<byte>(tonghouY[0], tonghouX)==0 && threshEdge.At<byte>(fanghanhouY[0], fanghanhouduX) == 0*/)
  7076. {
  7077. Mat temp = ~threshEdge;
  7078. if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 3000 && Math.Abs(b[3] + (b[2] - offsetX[0]) - j) < 50)
  7079. {
  7080. tempj = j;
  7081. //Cv2.ImWrite(@"C:\Users\54434\Desktop\threshEdge.png", threshEdge);
  7082. fanghankaikou[1] = this.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 1).X - 3;
  7083. break;
  7084. }
  7085. }
  7086. else if(threshEdge.At<byte>(tonghouY[0] - 15, j) == 0)
  7087. {
  7088. Mat temp = ~threshEdge;
  7089. if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 15), new Scalar(255)) > 3000 && Math.Abs(b[3] + (b[2] - offsetX[0]) - j) < 70)
  7090. {
  7091. tempj = j;
  7092. fanghankaikou[1] = this.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 15), threshEdge, 1).X - 3;
  7093. break;
  7094. }
  7095. }
  7096. else if (threshEdge.At<byte>(tonghouY[0] - 25, j) == 0)
  7097. {
  7098. Mat temp = ~threshEdge;
  7099. if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 25), new Scalar(255)) > 3000 && Math.Abs(b[3] + (b[2] - offsetX[0]) - j) < 95)
  7100. {
  7101. tempj = j;
  7102. fanghankaikou[1] = this.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 25), threshEdge, 1).X - 3;
  7103. break;
  7104. }
  7105. }
  7106. }
  7107. if (i >= 10)
  7108. {//0(48).JPG、1(10).JPG、1(73).JPG[需要镜像处理]、3(3).JPG、999(4).JPG、999(5).JPG、5669.JPG、6028.JPG【需要调试处理,画视场后不走这里了】
  7109. //6111.JPG、9999(7).JPG【旋转后除了offset右端点其余完美结果】
  7110. //fanghankaikou[1] = b[3] + (b[2]- offsetX[0]);
  7111. if (fanghankaikou[1] > gray.Width) fanghankaikou[1] = b[3] - (b[2] - offsetX[0]);
  7112. }
  7113. else
  7114. {
  7115. if (fanghankaikou[1] == 0/* || (fanghankaikou[1] > 0 && threshEdge.At<byte>(fanghanhouY[1], fanghankaikou[1]) > 0)*/)
  7116. {
  7117. FanhanKaikouForYouKaiKou(gray, tonghouY, b, fanghanhouY, fanghanhouduX, tonghouX, offsetX, out fanghankaikou, ++i);
  7118. }
  7119. else
  7120. {
  7121. if(/*Math.Abs(b[3] + (b[2] - offsetX[0]) - fanghankaikou[1]) > 50 || */fanghankaikou[1]> gray.Width)
  7122. {
  7123. fanghankaikou[1] = tempj;
  7124. }
  7125. }
  7126. }
  7127. }
  7128. /// <summary>
  7129. /// 防焊 有开口 LPI
  7130. /// </summary>
  7131. /// <param name="gray"></param>
  7132. /// <param name="tonghouY"></param>
  7133. /// <param name="b"></param>
  7134. /// <param name="fanghanhouY"></param>
  7135. /// <param name="LPIHouduY"></param>
  7136. /// <param name="a"></param>
  7137. public void FanghanLPIForYouKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a = 0)
  7138. {
  7139. int LPIHouduX = b[1] + 100;
  7140. LPIHouduY = new int[2];
  7141. Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  7142. LPIHouduY[0] = tonghouY[1];
  7143. for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0]); i < LPIHouduY[0] - 50; i++)
  7144. {
  7145. if (result1.At<byte>(i, LPIHouduX) == 0)
  7146. {
  7147. Mat temp = ~result1;
  7148. if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
  7149. {
  7150. LPIHouduY[1] = i;
  7151. break;
  7152. }
  7153. }
  7154. }
  7155. if (LPIHouduY[1] == 0)
  7156. {
  7157. FanghanLPIForYouKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
  7158. }
  7159. }
  7160. /// <summary>
  7161. /// 防焊 有开口 Undercut
  7162. /// </summary>
  7163. /// <param name="gray"></param>
  7164. /// <param name="tonghouY"></param>
  7165. /// <param name="b"></param>
  7166. /// <param name="offsetX"></param>
  7167. /// <param name="LPIHouY"></param>
  7168. /// <param name="undercutX"></param>
  7169. /// <param name="i"></param>
  7170. public void FanghanUndercutForYouKaiKou(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int i = 0)
  7171. {
  7172. int tempj = -1;
  7173. undercutX = 0;
  7174. Mat filter = new Mat();
  7175. PointEnhancement(gray, out filter);
  7176. Mat newGray = new Mat();
  7177. Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
  7178. Mat threshEdge = newGray.Threshold(BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 3), 255, ThresholdTypes.BinaryInv);
  7179. Mat result = threshEdge.Canny(0, 255);
  7180. //Cv2.ImWrite(@"C:\Users\zyh\Desktop\a.jpg", result*255);
  7181. int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
  7182. //想左找
  7183. for (int j = offsetX[0] + 10; j > tempRange; j--)
  7184. {
  7185. byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
  7186. if (v > 0)
  7187. {
  7188. tempj = j;
  7189. //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
  7190. {
  7191. undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
  7192. break;
  7193. }
  7194. }
  7195. }
  7196. if (i > 15)
  7197. {
  7198. undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
  7199. //undercutX = offsetX[0] - 25;
  7200. }
  7201. else
  7202. {
  7203. if (undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/)
  7204. {
  7205. if(undercutX > 0 && Math.Abs(offsetX[0]-tempj) < 50)
  7206. {
  7207. undercutX = tempj;
  7208. }
  7209. else
  7210. {
  7211. FanghanUndercutForYouKaiKou(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, ++i);
  7212. }
  7213. }
  7214. else
  7215. {
  7216. undercutX = (undercutX + tempj) / 2 - 5;
  7217. if(offsetX[0] - undercutX<10) undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
  7218. if (undercutX > offsetX[0]) undercutX = offsetX[0] - 25;
  7219. }
  7220. }
  7221. }
  7222. #endregion
  7223. #region 双面铜
  7224. /// <summary>
  7225. /// 防焊 有开口 offset
  7226. /// </summary>
  7227. /// <param name="gray"></param>
  7228. /// <param name="tonghouY"></param>
  7229. /// <param name="b"></param>
  7230. /// <param name="fanghanhouY"></param>
  7231. /// <param name="offsetX"></param>
  7232. /// <param name="i"></param>
  7233. public void FanghanOffsetForShuangmianTong_Acc(Mat gray0, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, int offsetX0_0, out int offsetX0)
  7234. {
  7235. int offsetY1 = Math.Max(0, fanghanhouY[1] - 50);
  7236. int offsetY2 = Math.Min(gray0.Rows - 1, tonghouY[1] + 50);
  7237. int offsetLeft = b[1] + 5;
  7238. Mat gray = gray0[offsetY1, offsetY2, offsetLeft, b[2] - 5];
  7239. int colStart = offsetX0_0 - 10 - offsetLeft;
  7240. int colEnd = offsetX0_0 + 20/*10*/ - offsetLeft;
  7241. int minGray = 300 * 255; int minColIndex = 0; int rowStart = offsetY - 10 - offsetY1; int rowEnd = offsetY + 10 - offsetY1;
  7242. List<int> curgrayList = new List<int>();
  7243. List<int> minareaList_k = new List<int>();
  7244. List<int> minareaList_v = new List<int>();
  7245. for (int i = colStart; i < colEnd; i++)
  7246. {
  7247. curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
  7248. if (curgrayList[i - colStart] < minGray)
  7249. {
  7250. minColIndex = i;
  7251. minGray = curgrayList[i - colStart];
  7252. }
  7253. }
  7254. //for (int i = colStart; i < (gray.Cols + colStart) / 2; i++)
  7255. //{
  7256. // if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
  7257. // {
  7258. // bool isAreamin = true;
  7259. // for (int k = i - colStart - 10; k < i - colStart + 10; k++)
  7260. // {
  7261. // if (curgrayList[i - colStart] >= curgrayList[k])
  7262. // {
  7263. // isAreamin = false;
  7264. // break;
  7265. // }
  7266. // if (k == i - colStart - 1) k++;
  7267. // }
  7268. // if (isAreamin)
  7269. // {
  7270. // minareaList_k.Add(i);
  7271. // minareaList_v.Add(curgrayList[i - colStart]);
  7272. // }
  7273. // }
  7274. //}
  7275. ////Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  7276. //////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  7277. ////下面为计算极小值,从计算极小值中的序列中选取,替换极大值
  7278. ////minareaList.Add(minColIndex, minGray);
  7279. ////offsetX0 = 47 + colStart;// minColIndex;
  7280. for (int i = 0; i < minareaList_k.Count; i++)
  7281. {
  7282. if (Math.Abs(minGray - minareaList_v[i]) < 120)
  7283. {
  7284. minColIndex = minareaList_k[i];
  7285. break;
  7286. }
  7287. }
  7288. offsetX0 = minColIndex;
  7289. offsetX0 = offsetX0 + offsetLeft;
  7290. }
  7291. /// <summary>
  7292. /// 防焊 有开口 offset
  7293. /// </summary>
  7294. /// <param name="gray"></param>
  7295. /// <param name="tonghouY"></param>
  7296. /// <param name="b"></param>
  7297. /// <param name="fanghanhouY"></param>
  7298. /// <param name="offsetX"></param>
  7299. /// <param name="i"></param>
  7300. public void FanghanOffsetForShuangmianTong(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int colStart, out int offsetX0, int i = 0)
  7301. {
  7302. int offsetY = tonghouY[0] - 50;
  7303. int offsetY1 = Math.Max(0, fanghanhouY[1]/*offsetY*/ - 50);
  7304. int offsetY2 = Math.Min(gray.Rows - 1, tonghouY[1] + 50);
  7305. int offsetLeft = b[1] + 5;
  7306. if (offsetLeft >= b[2] - 5) offsetLeft = b[2] - 5 - 1;
  7307. Mat grayRect = gray[offsetY1, offsetY2, offsetLeft, b[2] - 5];
  7308. this.FanghanOffsetForNewShuangmiantongOffset0(grayRect, colStart, out offsetX0);
  7309. offsetX0 = offsetX0 + offsetLeft;
  7310. ////二值化
  7311. //Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
  7312. ////去碎屑
  7313. //threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
  7314. //for (int j = b[2] - 55; j > 0; j--)
  7315. //{
  7316. // if (threshEdge.At<byte>(offsetY + 45, j) == 0)
  7317. // {
  7318. // Mat temp = ~threshEdge;
  7319. // if (Cv2.FloodFill(temp, new Point(j, offsetY + 45), new Scalar(255)) > 2000)
  7320. // {
  7321. // offsetX0 = Tools.GetLeftOrRightPoint(new Point(j, offsetY + 45), threshEdge, 2).X;
  7322. // break;
  7323. // }
  7324. // }
  7325. //}
  7326. //if (i <20 && (offsetX0 <= 0 || Math.Abs(b[2]/*offsetX_1*/ - offsetX0) > 800))
  7327. // FanghanOffsetForYouKaiKou(gray, tonghouY, b, fanghanhouY, out offsetX0, ++i);
  7328. }
  7329. public void FanghanOffsetForNewShuangmiantongOffset0(Mat gray, int colStart, out int offsetX0)
  7330. {//计算数值的地方
  7331. ////Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
  7332. // offsetX0 = -1;
  7333. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray);
  7334. int minGray = 300 * 255; int minColIndex = 0; int rowStart = 50 + 30; int rowEnd = 130 + 20/*gray.Rows - 50*/;// int curGray;
  7335. List<int> curgrayList = new List<int>();
  7336. List<int> minareaList_k = new List<int>();
  7337. List<int> minareaList_v = new List<int>();
  7338. for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 + 60/* + 40*/ + 20/*10*//*0 debug ER(14).JPG!!!!*/ /*gray.Cols - 50*/; i++)
  7339. {
  7340. curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
  7341. if (curgrayList[i - colStart] < minGray)
  7342. {
  7343. minColIndex = i;
  7344. minGray = curgrayList[i - colStart];
  7345. }
  7346. }
  7347. for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 + 60/* + 40*/ /*gray.Cols - 50*/; i++)
  7348. {
  7349. if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 + 60/* + 40*/ + 10/* - 10 debug ER(14).JPG!!!!*/)
  7350. {
  7351. bool isAreamin = true;
  7352. for (int k = i - colStart - 10; k < i - colStart + 10; k++)
  7353. {
  7354. if (curgrayList[i - colStart] >= curgrayList[k])
  7355. {
  7356. isAreamin = false;
  7357. break;
  7358. }
  7359. if (k == i - colStart - 1) k++;
  7360. }
  7361. if (isAreamin)
  7362. {
  7363. minareaList_k.Add(i);
  7364. minareaList_v.Add(curgrayList[i - colStart]);
  7365. }
  7366. }
  7367. }
  7368. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  7369. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  7370. //下面为计算极小值,从计算极小值中的序列中选取,替换极大值
  7371. //minareaList.Add(minColIndex, minGray);
  7372. //offsetX0 = 47 + colStart;// minColIndex;
  7373. //if (minColIndex - minareaList_k[minareaList_k.Count - 1] != 0 && minareaList_k.Count < 4)//ER(14).JPG!!!!
  7374. if (minareaList_k.Count <= 7)
  7375. for (int i = 0; i < minareaList_k.Count; i++)
  7376. {
  7377. if (Math.Abs(minGray - minareaList_v[i]) < 400/*1100*//*400*//*120*/ && minareaList_k[i] > 240/* ER(90).JPG 250*//* ER(17).JPG!!!!240*/
  7378. && (minColIndex > (gray.Cols + colStart) / 2/* + 40*/ + 10 || minColIndex < (gray.Cols + colStart) / 2 + 60 /* + 40*/ - 40))
  7379. {
  7380. minColIndex = minareaList_k[i];
  7381. break;
  7382. }
  7383. }
  7384. //纠正191情况
  7385. else if (minareaList_k.Count == 9 && Math.Abs(minGray - minareaList_v[minareaList_k.Count - 2]) < 400
  7386. && minGray - minareaList_v[minareaList_k.Count - 2] != 0)
  7387. {
  7388. minColIndex = minareaList_k[minareaList_k.Count - 2];
  7389. }
  7390. //纠正139情况
  7391. else if (minareaList_k.Count == 8 && Math.Abs(minGray - minareaList_v[minareaList_k.Count - 3]) < 400
  7392. && minGray - minareaList_v[minareaList_k.Count - 3] != 0)
  7393. {
  7394. minColIndex = minareaList_k[minareaList_k.Count - 3];
  7395. }
  7396. if (minColIndex - minareaList_k[minareaList_k.Count - 1] > 0
  7397. && minColIndex - minareaList_k[minareaList_k.Count - 1] < 60
  7398. && Math.Abs(minGray - minareaList_v[minareaList_k.Count - 1]) < 1000)
  7399. minColIndex = minareaList_k[minareaList_k.Count - 1];
  7400. else if (minColIndex - minareaList_k[minareaList_k.Count - 1] < 0 && Math.Abs(minGray - minareaList_v[minareaList_k.Count - 1]) < 1000
  7401. && minareaList_k.Count < 6/*6*//*7*/ /* ER(163).JPG ER(133).JPG ER(42).JPG*/
  7402. && minColIndex - minareaList_k[minareaList_k.Count - 1] < -40 && minColIndex - minareaList_k[minareaList_k.Count - 1] > -45)
  7403. minColIndex = minareaList_k[minareaList_k.Count - 1];//ER(150).JPG
  7404. offsetX0 = minColIndex;
  7405. //
  7406. }
  7407. /// <summary>
  7408. /// 防焊 有开口 LPI
  7409. /// </summary>
  7410. /// <param name="gray"></param>
  7411. /// <param name="tonghouY"></param>
  7412. /// <param name="b"></param>
  7413. /// <param name="fanghanhouY"></param>
  7414. /// <param name="LPIHouduY"></param>
  7415. /// <param name="a"></param>
  7416. public void FanghanLPIForShuangmianTong(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a = 0)
  7417. {
  7418. int LPIHouduX = b[1] + 100;
  7419. LPIHouduY = new int[2];
  7420. //Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  7421. LPIHouduY[0] = tonghouY[1];
  7422. if (a > 255)
  7423. {
  7424. return;
  7425. }
  7426. Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  7427. for (int i = Math.Max(0, LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0])); i < LPIHouduY[0] - 50; i++)
  7428. {
  7429. if (result1.At<byte>(i, LPIHouduX) == 0)
  7430. {
  7431. Mat temp = ~result1;
  7432. if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
  7433. {
  7434. LPIHouduY[1] = i;
  7435. break;
  7436. }
  7437. }
  7438. }
  7439. if (LPIHouduY[1] == 0)
  7440. {
  7441. FanghanLPIForShuangmianTong(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
  7442. }
  7443. }
  7444. /// <summary>
  7445. /// 防焊 有开口 厚度
  7446. /// </summary>
  7447. /// <param name="gray"></param>
  7448. /// <param name="y"></param>
  7449. /// <param name="b"></param>
  7450. /// <param name="tonghouY"></param>
  7451. /// <param name="fanghanhouduY"></param>
  7452. /// <param name="a"></param>
  7453. public void FanghanhouduForShuangmianTonghou_2(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, bool isLeft)
  7454. {
  7455. int fanghanhouduY_0 = tonghouY[0];
  7456. int fanghanX1 = Math.Max(0, fanghanhouduX - 150);
  7457. int fanghanX2 = Math.Min(gray.Cols - 1, fanghanhouduX + 150);
  7458. int fanghanTop = fanghanhouduY_0 - 150/*100*/;
  7459. int marginTop = 150;
  7460. if (fanghanTop < 0)
  7461. {
  7462. fanghanTop = 0;
  7463. marginTop = fanghanhouduY_0 - 1;
  7464. }
  7465. Mat grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
  7466. FanghanhouduForYouKaiKou(grayRect/*gray*/, y, marginTop/*150*//*fanghanhouduX*/, tonghouY, out fanghanhouduY1, out minGray);
  7467. int fanghanhouduY1__2 = fanghanhouduY1;
  7468. int minGray__2 = minGray;
  7469. fanghanX1 += 140;// 145;
  7470. fanghanX2 -= 140;// 145;
  7471. grayRect = gray[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
  7472. int fanghanhouduY1__2_bottom;
  7473. FanghanhouduForYouKaiKou_ACC(grayRect, y, marginTop, fanghanhouduY1 - (isLeft ? 8/*5*/ : 1), out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2);
  7474. if (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 16/*11*//*<-10*//*20*//*10*/
  7475. || (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/))
  7476. fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
  7477. //else
  7478. //{
  7479. // fanghanX1 -= 69;
  7480. // fanghanX2 += 69;
  7481. // grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
  7482. // FanghanhouduForMeiKaiKou(grayRect, y, marginTop, tonghouY, out fanghanhouduY1__2, out minGray__2);
  7483. //}
  7484. //if (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 10)
  7485. // fanghanhouduY1 = fanghanhouduY1__2 + fanghanTop;
  7486. else
  7487. fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
  7488. }
  7489. /// <summary>
  7490. /// 防焊 有开口 厚度
  7491. /// </summary>
  7492. /// <param name="gray"></param>
  7493. /// <param name="y"></param>
  7494. /// <param name="b"></param>
  7495. /// <param name="tonghouY"></param>
  7496. /// <param name="fanghanhouduY"></param>
  7497. /// <param name="a"></param>
  7498. private void FanghanhouduForShuangmianTonghou(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, int a = 0)
  7499. {
  7500. /*int fanghanhouduX = 150; */
  7501. fanghanhouduY1 = -1;
  7502. Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  7503. //Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", thresh);
  7504. for (int i = 0; i < 100; i++)
  7505. {
  7506. if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
  7507. {
  7508. fanghanhouduY1 = i;
  7509. break;
  7510. }
  7511. }
  7512. minGray = 300 * 255;
  7513. if (fanghanhouduY1 == -1)
  7514. FanghanhouduForShuangmianTonghou(gray, y, fanghanhouduX, tonghouY, out fanghanhouduY1, out minGray, ++a);
  7515. else
  7516. {//计算数值的地方
  7517. //Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
  7518. /*int minGray = 300*255; */
  7519. int minRowIndex = 0; int colEnd = thresh.Cols - 1; int curGray;
  7520. for (int i = 6/*1*/; i < 95; i++)
  7521. {
  7522. curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  7523. if (curGray < minGray)
  7524. {
  7525. minRowIndex = i;
  7526. minGray = curGray;
  7527. }
  7528. }
  7529. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  7530. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  7531. fanghanhouduY1 = minRowIndex;
  7532. }
  7533. }
  7534. /// <summary>
  7535. /// 防焊 双面铜 铜厚
  7536. /// </summary>
  7537. /// <param name="gray"></param>
  7538. /// <param name="tonghouY"></param>
  7539. /// <param name="y"></param>
  7540. /// <param name="b"></param>
  7541. public void FanhanShuangcengTonghou_1(Mat gray, out int[] tonghouY, out int[] y, out int[] b, int autoResValue = -1)
  7542. {
  7543. y = new int[2];
  7544. b = new int[4];
  7545. int[] bAdd = new int[6];
  7546. tonghouY = new int[2];
  7547. Mat contour;
  7548. //contour = gray.Threshold(180, 255, ThresholdTypes.Binary);
  7549. if (autoResValue > 0)
  7550. {
  7551. contour = gray.Threshold(autoResValue, 255, ThresholdTypes.Binary);
  7552. }
  7553. else
  7554. contour = gray.Threshold(0, 255, ThresholdTypes.Otsu);
  7555. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
  7556. Mat close = new Mat();
  7557. Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
  7558. Mat nomin = new Mat();
  7559. GetArea(close, out nomin, 500, true);
  7560. Mat result = nomin.Clone();
  7561. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 127);
  7562. #region//计算边界
  7563. Scalar sum = new Scalar(0);
  7564. for (int i = 0; i < result.Rows; i++)
  7565. {
  7566. sum = result[i, i + 1, /*0*/result.Cols / 2, result.Cols].Sum();
  7567. if ((int)sum > 200)
  7568. {
  7569. y[0] = i - 20;
  7570. break;
  7571. }
  7572. }
  7573. for (int i = y[0] + 50; i < result.Rows; i++)
  7574. {
  7575. sum = result[i, i + 1, /*0*/result.Cols / 2, result.Cols].Sum();
  7576. if ((int)sum == 0)
  7577. {
  7578. y[1] = i;
  7579. break;
  7580. }
  7581. }
  7582. Mat imageClone = result * 127;
  7583. Cv2.Line(imageClone, 0, y[1], imageClone.Cols - 1, y[1], new Scalar(255));
  7584. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", imageClone);
  7585. for (int j = 0; j < result.Cols; j++)
  7586. {
  7587. sum = result[y[0], y[1], j, j + 1].Sum();
  7588. if ((int)sum > (/*y[1] - y[0] - */20))
  7589. {
  7590. b[0] = j; bAdd[0] = j;
  7591. break;
  7592. }
  7593. }
  7594. //这里可以考虑再次纠错,使用联通域,找到这个联通块的最右侧的点,而不是直接+200
  7595. //但是也要考虑左右的铜,因为底部颜色亮,而连接在一起的情况
  7596. //或者考虑sum==0和另一个条件比较,再次校验
  7597. for (int j = b[0] + 200; j < result.Cols; j++)
  7598. {
  7599. sum = result[y[0], y[1], j, j + 1].Sum();
  7600. if ((int)sum == 0 || ((int)sum > 0 && ((y[1]-y[0]) - (int)sum)>50))
  7601. //if ((int)sum == 0)
  7602. {
  7603. b[1] = j; bAdd[1] = j;
  7604. break;
  7605. }
  7606. }
  7607. for (int j = b[1] + 50; j < result.Cols; j++)
  7608. {
  7609. sum = result[y[0], y[1], j, j + 1].Sum();
  7610. if ((int)sum > 50)
  7611. //if ((int)sum > (y[1] - y[0] - 20))
  7612. {
  7613. b[2] = j; bAdd[2] = j;
  7614. break;
  7615. }
  7616. }
  7617. if (b[2] - b[1] < 300)
  7618. {
  7619. for (int j = b[2] + 50; j < result.Cols; j++)
  7620. {
  7621. sum = result[y[0], y[1], j, j + 1].Sum();
  7622. if ((int)sum == 0)
  7623. {
  7624. b[1] = j;
  7625. bAdd[1] = j;
  7626. break;
  7627. }
  7628. }
  7629. for (int j = b[1] + 10; j < result.Cols; j++)
  7630. {
  7631. sum = result[y[0], y[1], j, j + 1].Sum();
  7632. if ((int)sum > 20)
  7633. {
  7634. b[2] = j;
  7635. bAdd[2] = j;
  7636. break;
  7637. }
  7638. }
  7639. }
  7640. for (int j = b[2] + 50; j < result.Cols; j++)
  7641. {
  7642. sum = result[y[0], y[1], j, j + 1].Sum();
  7643. if ((int)sum == 0)
  7644. {
  7645. b[3] = j;
  7646. bAdd[3] = j;
  7647. break;
  7648. }
  7649. }
  7650. if (b[3] == 0)
  7651. b[3] = contour.Cols - 1;
  7652. if (bAdd[3] == 0)
  7653. bAdd[3] = contour.Cols - 1;
  7654. for (int j = bAdd[3] + 10; j < result.Cols; j++)
  7655. {
  7656. sum = result[y[0], y[1], j, j + 1].Sum();
  7657. if ((int)sum > 0)
  7658. {
  7659. bAdd[4] = j;
  7660. break;
  7661. }
  7662. }
  7663. if (bAdd[4] == 0)
  7664. bAdd[4] = contour.Cols - 1;
  7665. for (int j = bAdd[4] + 50; j < result.Cols; j++)
  7666. {
  7667. sum = result[y[0], y[1], j, j + 1].Sum();
  7668. if ((int)sum == 0)
  7669. {
  7670. bAdd[5] = j;
  7671. break;
  7672. }
  7673. }
  7674. if (bAdd[5] == 0)
  7675. bAdd[5] = contour.Cols - 1;
  7676. // for (int j = result.Cols - 1; j > b[2]; j--)
  7677. // {
  7678. // sum = result[y[0], y[1], j - 1, j].Sum();
  7679. // if ((int)sum > 0)
  7680. // {
  7681. // b[3] = j;
  7682. // break;
  7683. // }
  7684. // }
  7685. // if (b[3] == 0)
  7686. // b[3] = contour.Cols - 1;
  7687. // //bAdd[5] = b[3];
  7688. if (b[3] != bAdd[5] && bAdd[5] != contour.Cols - 1)//b[3] != bAdd[3] && b[3] - bAdd[2] > bAdd[3] - b[0] && (b[0] < 20 && b[1] < 60 && (b[1] < 280 && b[0] < 100 || b[3] == contour.Cols - 1))/*防止过拟合*/)//测量区域在右边
  7689. {
  7690. b[3] = bAdd[5];//
  7691. b[2] = bAdd[4];
  7692. b[1] = bAdd[3];
  7693. b[0] = bAdd[2];
  7694. }
  7695. #endregion
  7696. //计算铜厚
  7697. Mat filter = new Mat();
  7698. Cv2.GaussianBlur(gray, filter, new Size(15, 15), 5, 5);
  7699. Mat edge = new Mat();
  7700. Sobel(filter, out edge);
  7701. Mat threshEdge = edge.Threshold(10, 255, ThresholdTypes.Binary);
  7702. Mat and = new Mat();
  7703. Cv2.BitwiseAnd(contour, threshEdge, and);
  7704. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
  7705. Mat open = new Mat();
  7706. Cv2.MorphologyEx(and, open, MorphTypes.Open, seOpen);
  7707. Cv2.Rectangle(open, new Rect(0, y[1], open.Cols, open.Rows - y[1]), new Scalar(0), -1);
  7708. int tonghouX = b[1] - 150;
  7709. #region//计算边界 在tonghouX附近纠正y轴倾斜的距离
  7710. sum = new Scalar(0);
  7711. for (int i = 0; i < result.Rows; i++)
  7712. {
  7713. sum = result[i, i + 1, tonghouX - 10, tonghouX + 10/*result.Cols / 2, result.Cols*/].Sum();
  7714. if ((int)sum > 10/*200*/)
  7715. {
  7716. y[0] = i - 20;
  7717. break;
  7718. }
  7719. }
  7720. for (int i = y[0] + 50; i < result.Rows; i++)
  7721. {
  7722. sum = result[i, i + 1, tonghouX - 10, tonghouX + 10/*result.Cols / 2, result.Cols*/].Sum();
  7723. if ((int)sum == 0)
  7724. {
  7725. y[1] = i;
  7726. break;
  7727. }
  7728. }
  7729. #endregion
  7730. Mat result2 = open / 255;
  7731. int start = 0;
  7732. for (int i = y[0]; i < y[1]; i++)
  7733. {
  7734. if (contour.Get<byte>(i, tonghouX) > 0)
  7735. {
  7736. start = i;
  7737. break;
  7738. }
  7739. }
  7740. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result2.png", result2 * 127);
  7741. for (int i = start + 20; i < y[1] - 40/*40*/; i++)
  7742. {
  7743. sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
  7744. if ((int)sum > 80/*80*/)
  7745. {
  7746. tonghouY[0] = i;
  7747. break;
  7748. }
  7749. }
  7750. if (tonghouY[0] == 0)
  7751. {
  7752. for (int i = start + 20; i < y[1] - 20; i++)
  7753. {
  7754. sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
  7755. if ((int)sum > 50)
  7756. {
  7757. tonghouY[0] = i;
  7758. break;
  7759. }
  7760. }
  7761. }
  7762. //if (tonghouY[0] == 0)
  7763. //{//ER(114).JPG
  7764. // //tonghouY[0] = start;
  7765. // for (int i = start - 50/* + 20*/; i < y[1] - 0/*20*/; i++)
  7766. // {
  7767. // sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
  7768. // if ((int)sum > 20/*50*/)
  7769. // {
  7770. // tonghouY[0] = i;
  7771. // break;
  7772. // }
  7773. // }
  7774. //}
  7775. for (int i = tonghouY[0] + 10; i < y[1] + 20/*0*/; i++)
  7776. {
  7777. if (contour.Get<byte>(i, tonghouX) == 0)
  7778. {
  7779. tonghouY[1] = i;
  7780. break;
  7781. }
  7782. }
  7783. int compensate1 = 0;
  7784. tonghouY[0] += compensate1;
  7785. }
  7786. /// <summary>
  7787. /// 防焊 双面铜 铜厚
  7788. /// </summary>
  7789. /// <param name="gray"></param>
  7790. /// <param name="tonghouY"></param>
  7791. /// <param name="y"></param>
  7792. /// <param name="b"></param>
  7793. public void FanhanShuangcengTonghou_2(Mat gray, out int[] tonghouY, out int[] y, out int[] b)
  7794. {
  7795. y = new int[2];
  7796. b = new int[4];
  7797. tonghouY = new int[2];
  7798. Mat contour = gray.Threshold(0, 255, ThresholdTypes.Otsu);
  7799. //去掉小颗粒
  7800. contour = BinaryTools.DebrisRemoval_New(contour.CvtColor(ColorConversionCodes.GRAY2BGRA), 1000).CvtColor(ColorConversionCodes.BGRA2GRAY);
  7801. Mat result = contour.Clone();
  7802. /*Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
  7803. Mat close = new Mat();
  7804. Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
  7805. Mat nomin = new Mat();
  7806. GetArea(close, out nomin, 500, true);
  7807. Mat result = nomin.Clone();*/
  7808. #region//计算边界
  7809. Scalar sum = new Scalar(0);
  7810. for (int i = 0; i < result.Rows; i++)
  7811. {
  7812. sum = result[i, i + 1, 0, result.Cols].Sum();
  7813. if ((int)sum > 200)
  7814. {
  7815. y[0] = i - 20;
  7816. break;
  7817. }
  7818. }
  7819. for (int i = y[0] + 50; i < result.Rows; i++)
  7820. {
  7821. sum = result[i, i + 1, 0, result.Cols].Sum();
  7822. if ((int)sum == 0)
  7823. {
  7824. y[1] = i;
  7825. break;
  7826. }
  7827. }
  7828. for (int j = 0; j < result.Cols; j++)
  7829. {
  7830. sum = result[y[0], y[1], j, j + 1].Sum();
  7831. if ((int)sum > 20)
  7832. {
  7833. b[0] = j;
  7834. break;
  7835. }
  7836. }
  7837. for (int j = b[0] + 200; j < result.Cols; j++)
  7838. {
  7839. sum = result[y[0], y[1], j, j + 1].Sum();
  7840. if ((int)sum == 0)
  7841. {
  7842. b[1] = j;
  7843. break;
  7844. }
  7845. }
  7846. for (int j = b[1] + 10; j < result.Cols; j++)
  7847. {
  7848. sum = result[y[0], y[1], j, j + 1].Sum();
  7849. if ((int)sum > 20)
  7850. {
  7851. b[2] = j;
  7852. break;
  7853. }
  7854. }
  7855. if (b[2] - b[1] < 300)
  7856. {
  7857. for (int j = b[2] + 50; j < result.Cols; j++)
  7858. {
  7859. sum = result[y[0], y[1], j, j + 1].Sum();
  7860. if ((int)sum == 0)
  7861. {
  7862. b[1] = j;
  7863. break;
  7864. }
  7865. }
  7866. for (int j = b[1] + 10; j < result.Cols; j++)
  7867. {
  7868. sum = result[y[0], y[1], j, j + 1].Sum();
  7869. if ((int)sum > 20)
  7870. {
  7871. b[2] = j;
  7872. break;
  7873. }
  7874. }
  7875. }
  7876. for (int j = b[2] + 50; j < result.Cols; j++)
  7877. {
  7878. sum = result[y[0], y[1], j, j + 1].Sum();
  7879. if ((int)sum == 0)
  7880. {
  7881. b[3] = j;
  7882. break;
  7883. }
  7884. }
  7885. if (b[3] == 0)
  7886. b[3] = contour.Cols - 1;
  7887. //LineShow(gray, b[0], y[0], b[1], y[0]);
  7888. //LineShow(gray, b[2], y[1], b[3], y[1]);
  7889. //ImageShow(gray);
  7890. #endregion
  7891. //计算铜厚
  7892. Mat filter = new Mat();
  7893. Cv2.GaussianBlur(gray, filter, new Size(15, 15), 5, 5);
  7894. Mat edge = new Mat();
  7895. //Edge(filter, out edge);
  7896. //Mat threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
  7897. Sobel(filter, out edge);
  7898. Mat threshEdge = edge.Threshold(10, 255, ThresholdTypes.Binary);
  7899. Mat and = new Mat();
  7900. Cv2.BitwiseAnd(contour, threshEdge, and);
  7901. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
  7902. Mat open = new Mat();
  7903. Cv2.MorphologyEx(and, open, MorphTypes.Open, seOpen);
  7904. //GetArea(open, out open, 10, true);
  7905. Cv2.Rectangle(open, new Rect(0, y[1], open.Cols, open.Rows - y[1]), new Scalar(0), -1);
  7906. //Mat fanse = 255 - open;
  7907. //Mat noMin = new Mat();
  7908. //GetArea(fanse, out noMin, 1000, true);
  7909. //ImageShow(threshEdge, and, open/*,fanse*255*/);
  7910. int tonghouX = b[1] - 150;
  7911. Mat result2 = open / 255;
  7912. int start = 0;
  7913. for (int i = y[0]; i < y[1]; i++)
  7914. {
  7915. if (contour.Get<byte>(i, tonghouX) > 0)
  7916. {
  7917. start = i;
  7918. break;
  7919. }
  7920. }
  7921. for (int i = start + 20; i < y[1] - 40; i++)
  7922. {
  7923. sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
  7924. if ((int)sum > 80)
  7925. {
  7926. tonghouY[0] = i;
  7927. break;
  7928. }
  7929. }
  7930. if (tonghouY[0] == 0)
  7931. {
  7932. for (int i = start + 20; i < y[1] - 20; i++)
  7933. {
  7934. sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
  7935. if ((int)sum > 50)
  7936. {
  7937. tonghouY[0] = i;
  7938. break;
  7939. }
  7940. }
  7941. }
  7942. //for (int i = tonghouY[0]+10; i < y[1]; i++)
  7943. //{
  7944. // sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
  7945. // if ((int)sum > 50)
  7946. // {
  7947. // tonghouY[1] = i;
  7948. // break;
  7949. // }
  7950. //}
  7951. for (int i = tonghouY[0] + 10; i < y[1]; i++)
  7952. {
  7953. if (contour.Get<byte>(i, tonghouX) == 0)
  7954. {
  7955. tonghouY[1] = i;
  7956. break;
  7957. }
  7958. }
  7959. int compensate1 = 0;
  7960. tonghouY[0] += compensate1;
  7961. //int compensate2 = 10;
  7962. //tonghouY[1] += compensate2;
  7963. }
  7964. /// <summary>
  7965. /// 防焊 双面铜 厚度
  7966. /// </summary>
  7967. /// <param name="gray"></param>
  7968. /// <param name="y"></param>
  7969. /// <param name="b"></param>
  7970. /// <param name="tonghouY"></param>
  7971. /// <param name="fanghanhouduY"></param>
  7972. public void Fanghanhoudu4(Mat gray, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY, int a = 0)
  7973. {
  7974. fanghanhouduY = new int[2];
  7975. int fanghanhouduX = b[1] - 100;
  7976. /*Mat newGray = new Mat();
  7977. Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
  7978. Mat edge = new Mat();
  7979. Sobel(newGray, out edge);
  7980. Mat thresh = edge.Threshold(5, 255, ThresholdTypes.Binary);
  7981. Cv2.Rectangle(thresh, new Rect(0, 0, b[1] - 200, thresh.Rows), new Scalar(0), -1);
  7982. Cv2.Rectangle(thresh, new Rect(0, 0, thresh.Cols, y[0] - 200), new Scalar(0), -1);
  7983. Cv2.Rectangle(thresh, new Rect(0, y[0], thresh.Cols, thresh.Rows - y[0]), new Scalar(0), -1);
  7984. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  7985. Mat open = new Mat();
  7986. Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
  7987. Mat noCircle = new Mat();
  7988. RemoveCircles(open, out noCircle);
  7989. Mat max = new Mat();
  7990. GetMaxArea(noCircle, out max);
  7991. Mat result = max.Clone();
  7992. Scalar sum = new Scalar(0);
  7993. for (int i = tonghouY[0] - 50; i > tonghouY[0] - 200; i--)
  7994. {
  7995. sum = result[i - 1, i, fanghanhouduX - 50, fanghanhouduX + 50].Sum();
  7996. if ((int)sum > 50)
  7997. {
  7998. fanghanhouduY[0] = i;
  7999. break;
  8000. }
  8001. }*/
  8002. fanghanhouduY[1] = y[0]+20;
  8003. /*Scalar sum = new Scalar(0);
  8004. Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  8005. //防焊下层
  8006. Mat thresh2 = thresh / 255;//gray.Threshold(0, 1, ThresholdTypes.Otsu);
  8007. for (int i = fanghanhouduY[0] + 10; i < tonghouY[0]*//*thresh2.Rows*//*; i++)
  8008. {
  8009. sum = thresh2[i, i + 1, fanghanhouduX - 50, fanghanhouduX + 50].Sum();
  8010. if ((int)sum > 50)
  8011. {
  8012. fanghanhouduY[1] = i;
  8013. break;
  8014. }
  8015. }*/
  8016. Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 3), 255, ThresholdTypes.Binary);
  8017. for (int i = fanghanhouduY[1] - 50; i > fanghanhouduY[1] - 90; i--)
  8018. {
  8019. if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 1300)
  8020. {
  8021. fanghanhouduY[0] = i - 4;
  8022. break;
  8023. }
  8024. }
  8025. if (fanghanhouduY[0] == 0)
  8026. {
  8027. Fanghanhoudu4(gray, y, b, tonghouY, out fanghanhouduY, ++a);
  8028. }
  8029. /*fanghanhouduY = new int[2];
  8030. int fanghanhouduX = b[1] - 100;
  8031. Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  8032. fanghanhouduY[0] = tonghouY[0];
  8033. for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
  8034. {
  8035. if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
  8036. {
  8037. fanghanhouduY[1] = i;
  8038. break;
  8039. }
  8040. }
  8041. if (fanghanhouduY[1] == 0)
  8042. {
  8043. Fanghanhoudu4(gray, y, b, tonghouY, out fanghanhouduY, ++a);
  8044. }*/
  8045. }
  8046. /// <summary>
  8047. /// 防焊 双面铜 offset
  8048. /// </summary>
  8049. /// <param name="gray"></param>
  8050. /// <param name="tonghouY"></param>
  8051. /// <param name="b"></param>
  8052. /// <param name="fanghanhouY"></param>
  8053. /// <param name="offsetX"></param>
  8054. public void FanghanOffset6(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX, int i = 0)
  8055. {
  8056. offsetX = new int[2];
  8057. //二值化
  8058. Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
  8059. //去碎屑
  8060. threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
  8061. for (int j = b[2] - 155; j > 0; j--)
  8062. {
  8063. if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0)
  8064. {
  8065. Mat temp = ~threshEdge;
  8066. if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 2000)
  8067. {
  8068. offsetX[0] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 2).X;
  8069. break;
  8070. }
  8071. }
  8072. }
  8073. offsetX[1] = b[2];
  8074. if (i < 20 && ( offsetX[0] <= 0 || Math.Abs(offsetX[1] - offsetX[0]) > 800))
  8075. {
  8076. FanghanOffset6(gray, tonghouY, b, fanghanhouY, out offsetX, ++i);
  8077. }
  8078. }
  8079. /// <summary>
  8080. /// 防焊 双面铜 LPI厚度
  8081. /// </summary>
  8082. /// <param name="gray"></param>
  8083. /// <param name="tonghouY">銅厚的上下縱坐標</param>
  8084. /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
  8085. /// <param name="LPIHouduY"></param>
  8086. public void FanghanLPI2(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a = 0)
  8087. {
  8088. int LPIHouduX = b[1] + 100;
  8089. LPIHouduY = new int[2];
  8090. Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  8091. LPIHouduY[0] = tonghouY[1];
  8092. if (LPIHouduY[0] <= 120) return;
  8093. for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0]); i < LPIHouduY[0] - 50; i++)
  8094. {
  8095. if (result1.At<byte>(i, LPIHouduX) == 0)
  8096. {
  8097. Mat temp = ~result1;
  8098. if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
  8099. {
  8100. LPIHouduY[1] = i;
  8101. break;
  8102. }
  8103. }
  8104. }
  8105. if (LPIHouduY[1] == 0)
  8106. {
  8107. FanghanLPI2(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
  8108. }
  8109. }
  8110. /// <summary>
  8111. /// 防焊 双面铜 Undercut
  8112. /// </summary>
  8113. /// <param name="gray"></param>
  8114. /// <param name="tonghouY"></param>
  8115. /// <param name="b"></param>
  8116. /// <param name="offsetX"></param>
  8117. /// <param name="LPIHouY"></param>
  8118. /// <param name="undercutX"></param>
  8119. /// <param name="i"></param>
  8120. public void FanghanUndercut6_3(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int i = 0)
  8121. {
  8122. int tempj = -1;
  8123. undercutX = 0;
  8124. Mat filter = new Mat();
  8125. PointEnhancement(gray, out filter);
  8126. Mat newGray = new Mat();
  8127. Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
  8128. Mat threshEdge = newGray.Threshold(BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 3), 255, ThresholdTypes.BinaryInv);
  8129. Mat result = threshEdge.Canny(0, 255);
  8130. int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
  8131. //想左找
  8132. for (int j = offsetX[0] + 10; j > tempRange; j--)
  8133. {
  8134. byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
  8135. if (v > 0)
  8136. {
  8137. tempj = j;
  8138. //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
  8139. {
  8140. undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
  8141. break;
  8142. }
  8143. }
  8144. }
  8145. if (i > 15)
  8146. {
  8147. undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
  8148. //undercutX = offsetX[0] - 25;
  8149. }
  8150. else
  8151. {
  8152. if (undercutX == 0 || (undercutX > 0 && offsetX[0] - undercutX > 100))
  8153. {
  8154. FanghanUndercut6_3(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, ++i);
  8155. }
  8156. else
  8157. {
  8158. undercutX = (undercutX + tempj) / 2 - 20;
  8159. }
  8160. }
  8161. }
  8162. #endregion
  8163. #endregion
  8164. /// <summary>
  8165. /// 计算防焊双层铜的铜厚
  8166. /// </summary>
  8167. /// <param name="imageContour">二值圖像</param>
  8168. /// <param name="imageGreen">绿色通道图</param>
  8169. /// <param name="tonghouY">輸出銅厚的上下縱坐標</param>
  8170. /// <param name="y">輸出銅厚的上下縱坐標</param>
  8171. /// <param name="b">b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
  8172. public void FanhanShuangcengTonghou(Mat imageContour, Mat imageGreen, out int[] tonghouY, out int[] y, out int[] b)
  8173. {
  8174. y = new int[2];
  8175. b = new int[4];
  8176. tonghouY = new int[2];
  8177. Scalar sum = new Scalar(0);
  8178. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  8179. Cv2.MorphologyEx(imageContour, imageContour, MorphTypes.Open, se);
  8180. for (int i = 0; i < imageContour.Rows; i++)
  8181. {
  8182. sum = imageContour[i, i + 1, 0, imageContour.Cols].Sum();
  8183. if ((int)sum > 200)
  8184. {
  8185. y[0] = i - 20;
  8186. break;
  8187. }
  8188. }
  8189. for (int i = y[0] + 21; i < imageContour.Rows; i++)
  8190. {
  8191. sum = imageContour[i, i + 1, 0, imageContour.Cols].Sum();
  8192. if ((int)sum == 0)
  8193. {
  8194. y[1] = i;
  8195. break;
  8196. }
  8197. }
  8198. //左右边界,从右边开始
  8199. for (int j = imageContour.Cols - 1; j > 0; j--)
  8200. {
  8201. sum = imageContour[y[0], y[1], j - 1, j].Sum();
  8202. if ((int)sum > 0)
  8203. {
  8204. b[3] = j;
  8205. break;
  8206. }
  8207. }
  8208. for (int j = b[3] - 1; j > 0; j--)
  8209. {
  8210. sum = imageContour[y[0], y[1], j - 1, j].Sum();
  8211. if ((int)sum == 0)
  8212. {
  8213. b[2] = j;
  8214. break;
  8215. }
  8216. }
  8217. for (int j = b[2] - 1; j > 0; j--)
  8218. {
  8219. sum = imageContour[y[0], y[1], j - 1, j].Sum();
  8220. if ((int)sum > 0)
  8221. {
  8222. b[1] = j;
  8223. break;
  8224. }
  8225. }
  8226. for (int j = b[1] - 1; j > 0; j--)
  8227. {
  8228. sum = imageContour[y[0], y[1], j - 1, j].Sum();
  8229. if ((int)sum == 0)
  8230. {
  8231. b[0] = j;
  8232. break;
  8233. }
  8234. }
  8235. //计算外层铜厚
  8236. Mat crop = imageContour[y[0], y[1], b[0], b[2]];
  8237. int tonghouX = b[1] - 200;
  8238. Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  8239. Mat dilate = new Mat();
  8240. Cv2.Dilate(crop, dilate, se2);
  8241. Mat result = dilate.Clone();
  8242. GetMaxContour(dilate, out result);
  8243. //ImageShow(result * 255);
  8244. for (int i = 0; i < result.Rows; i++)
  8245. {
  8246. if (result.Get<byte>(i, tonghouX - b[0]) > 0)
  8247. {
  8248. tonghouY[0] = i + y[0];
  8249. break;
  8250. }
  8251. }
  8252. for (int i = tonghouY[0] - y[0] + 20; i < result.Rows; i++)
  8253. {
  8254. if (result.Get<byte>(i, tonghouX - b[0]) > 0)
  8255. {
  8256. tonghouY[1] = i + y[0];
  8257. break;
  8258. }
  8259. }
  8260. double ordiante = 0;
  8261. InsideLine(imageGreen, tonghouY[0], tonghouY[1], tonghouX - 10, tonghouX + 10, out ordiante);
  8262. tonghouY[0] = (int)ordiante;
  8263. }
  8264. /// <summary>
  8265. /// 計算防焊厚度
  8266. /// </summary>
  8267. /// <param name="imageRed">原圖紅色通道圖片</param>
  8268. /// <param name="y">截取第一層銅的上下區域</param>
  8269. /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
  8270. /// <param name="tonghouY">銅厚的上下縱坐標</param>
  8271. /// <param name="fanghanhouduY">防焊厚度的上下縱坐標</param>
  8272. public void Fanghanhoudu2(Mat imageRed, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY)
  8273. {
  8274. int compensate = 5;
  8275. fanghanhouduY = new int[2];
  8276. int fanghanhouduX = b[1] - 100;
  8277. Mat crop = imageRed[tonghouY[0] - 100, tonghouY[0] - 20, /*fanghanhouduX-20, fanghanhouduX+20*/fanghanhouduX - 100, b[1]];
  8278. Mat thresh = new Mat();
  8279. double t = Cv2.Threshold(crop, thresh, 0, 1, ThresholdTypes.Otsu);
  8280. thresh = 1 - crop.Threshold(t - 10, 1, ThresholdTypes.Binary);
  8281. Mat seopen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 3));
  8282. Mat open = new Mat();
  8283. Cv2.MorphologyEx(thresh, open, MorphTypes.Close, seopen);
  8284. Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(1), -1);
  8285. Cv2.Rectangle(open, new Rect(0, thresh.Rows - 1, thresh.Cols, 1), new Scalar(1), -1);
  8286. Fill(open, out open, 1);
  8287. Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(0), -1);
  8288. Mat result;
  8289. GetMaxArea(open, out result);
  8290. Scalar sum = new Scalar(0);
  8291. for (int i = 0; i < result.Rows; i++)
  8292. {
  8293. sum = result[i, i + 1, 0, result.Cols].Sum();
  8294. if ((int)sum > 5)
  8295. {
  8296. fanghanhouduY[0] = i + tonghouY[0] - 100 + compensate;
  8297. break;
  8298. }
  8299. }
  8300. Mat crop3 = imageRed.Threshold(0, 1, ThresholdTypes.Otsu)[y[0], y[1], 0, imageRed.Cols];
  8301. double ordinate2 = 0;
  8302. ExtractLines(crop3, out ordinate2, fanghanhouduX - 5, fanghanhouduX + 5, 1);
  8303. fanghanhouduY[1] = (int)ordinate2 + y[0] + 2;
  8304. }
  8305. public void Fanghanhoudu3(Mat gray, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY, int a=0)
  8306. {
  8307. fanghanhouduY = new int[2];
  8308. int fanghanhouduX = b[1] - 100;
  8309. Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a*5), 255, ThresholdTypes.Binary);
  8310. fanghanhouduY[0] = tonghouY[0];
  8311. for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
  8312. {
  8313. if(thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
  8314. {
  8315. fanghanhouduY[1] = i;
  8316. break;
  8317. }
  8318. }
  8319. if (fanghanhouduY[1] == 0)
  8320. {
  8321. Fanghanhoudu3(gray, y, b, tonghouY, out fanghanhouduY, ++a);
  8322. }
  8323. }
  8324. public void Fanghanhoudu5(Mat gray, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY, int a=0)
  8325. {
  8326. fanghanhouduY = new int[2];
  8327. int fanghanhouduX = b[1] - 100;
  8328. Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  8329. fanghanhouduY[0] = tonghouY[0];
  8330. for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
  8331. {
  8332. if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
  8333. {
  8334. fanghanhouduY[1] = i;
  8335. break;
  8336. }
  8337. }
  8338. if (fanghanhouduY[1] == 0)
  8339. {
  8340. Fanghanhoudu5(gray, y, b, tonghouY, out fanghanhouduY, ++a);
  8341. }
  8342. }
  8343. //public void Fanghanhoudu2(Mat imageRed, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY)
  8344. /// <summary>
  8345. /// 計算防焊offset
  8346. /// </summary>
  8347. /// <param name="imageRed">紅色通道圖片</param>
  8348. /// <param name="tonghouY">銅厚縱坐標</param>
  8349. /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
  8350. /// <param name="offsetX">offset橫坐標</param>
  8351. public void FanghanOffset2(Mat imageRed, int[] tonghouY, int[] b, out int[] offsetX)
  8352. {
  8353. offsetX = new int[2];
  8354. Mat crop = imageRed[tonghouY[0] - 100, tonghouY[1] - 20, b[1] + 20, b[2]];
  8355. Mat thresh = new Mat();
  8356. double t = Cv2.Threshold(crop, thresh, 0, 1, ThresholdTypes.Otsu);
  8357. thresh = 1 - crop.Threshold(t - 25, 1, ThresholdTypes.Binary);
  8358. Mat seopen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  8359. Mat open = new Mat();
  8360. Cv2.MorphologyEx(thresh, open, MorphTypes.Close, seopen);
  8361. Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(1), -1);
  8362. Cv2.Rectangle(open, new Rect(0, thresh.Rows - 1, thresh.Cols, 1), new Scalar(1), -1);
  8363. Scalar max = new Scalar(0);
  8364. int maxline = 0;
  8365. for (int i = 0; i < open.Rows - 1; i++)
  8366. {
  8367. if ((int)open[i, i + 1, 0, open.Cols].Sum() > (int)max)
  8368. {
  8369. max = open[i, i + 1, 0, open.Cols].Sum();
  8370. maxline = i;
  8371. }
  8372. }
  8373. Cv2.Rectangle(open, new Rect(0, maxline, thresh.Cols, 1), new Scalar(1), -1);
  8374. Fill(open, out open, 1);
  8375. //Cv2.Rectangle(open, new Rect(0, maxline, thresh.Cols, 1), new Scalar(0), -1);
  8376. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 3));
  8377. Cv2.Erode(open, open, se);
  8378. Cv2.Rectangle(open, new Rect(0, thresh.Rows - 1, thresh.Cols, 1), new Scalar(0), -1);
  8379. Mat result;
  8380. GetMaxArea(open, out result);
  8381. //ImageShow(thresh * 255, open * 255, result * 255);
  8382. //ImageShow(open * 255, result * 255);
  8383. //Mat noCircle = new Mat();
  8384. //RemoveCirCles(crop, thresh, out noCircle);
  8385. //Mat close = new Mat();
  8386. //Cv2.MorphologyEx(noCircle, close, MorphTypes.Close, seopen);
  8387. ////Cv2.MorphologyEx(close, open, MorphTypes.Open, seopen);
  8388. Scalar sum = new Scalar(0);
  8389. for (int j = 20; j < result.Cols; j++)
  8390. {
  8391. sum = result[0, result.Rows, j, j + 1].Sum();
  8392. if ((int)sum < 2)
  8393. {
  8394. offsetX[0] = j + b[1] + 20 - 5;
  8395. break;
  8396. }
  8397. }
  8398. offsetX[1] = b[2];
  8399. //ImageShow(thresh * 255, noCircle * 255, close * 255, open * 255);
  8400. //RemoveCirCles(crop,fill, out noCircle);
  8401. //ImageShow(open*255,fill * 255, noCircle*255);
  8402. }
  8403. public void FanghanOffset3(Mat imageRed, int[] tonghouY, int[] b, out int[] offsetX)
  8404. {
  8405. offsetX = new int[2];
  8406. Mat crop = imageRed[tonghouY[0] - 100, tonghouY[0], b[1], b[2]].Clone();
  8407. Mat sobel = new Mat();
  8408. Sobel(crop, out sobel);
  8409. Mat thresh = sobel.Threshold(0, 255, ThresholdTypes.Otsu);
  8410. Mat fill = new Mat();
  8411. Fill(thresh, out fill, 255);
  8412. Mat deleteArea = new Mat();
  8413. GetArea(fill, out deleteArea, 10, true);
  8414. Cv2.Rectangle(deleteArea, new Rect(0, deleteArea.Rows - 1, deleteArea.Cols, 1), new Scalar(1), -1);
  8415. Cv2.Rectangle(deleteArea, new Rect(0, 0, 1, deleteArea.Rows), new Scalar(1), -1);
  8416. Fill(deleteArea, out deleteArea, 1);
  8417. Cv2.Rectangle(deleteArea, new Rect(0, deleteArea.Rows - 1, deleteArea.Cols, 1), new Scalar(0), -1);
  8418. Cv2.Rectangle(deleteArea, new Rect(0, 0, 1, deleteArea.Rows), new Scalar(0), -1);
  8419. Mat maxArea = new Mat();
  8420. GetMaxArea(deleteArea, out maxArea);
  8421. Scalar sum = new Scalar(0);
  8422. for (int j = maxArea.Cols - 1; j > 0; j--)
  8423. {
  8424. sum = maxArea[0, maxArea.Rows, j - 1, j].Sum();
  8425. if ((int)sum > 0)
  8426. {
  8427. offsetX[0] = j + b[1];
  8428. break;
  8429. }
  8430. }
  8431. offsetX[1] = b[2];
  8432. //ImageShow(thresh, fill, deleteArea * 255, maxArea * 255);
  8433. }
  8434. /// <summary>
  8435. /// 提取防焊没开口offset,利用大倍率边缘检测来实现
  8436. /// </summary>
  8437. /// <param name="imageRed"></param>
  8438. /// <param name="tonghouY"></param>
  8439. /// <param name="b"></param>
  8440. /// <param name="offsetX"></param>
  8441. public void FanghanOffset4(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX)
  8442. {
  8443. offsetX = new int[2];
  8444. //offsetY = 0;
  8445. Mat filter = new Mat();
  8446. PointEnhancement(gray, out filter);
  8447. Mat newGray = new Mat();
  8448. Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
  8449. //PointEnhancement(gray, out gray);
  8450. //Mat crop = gray[tonghouY[0] - 120, tonghouY[0] - 20, LPIHouduX - 10, LPIHouduX + 10];
  8451. Mat edge = new Mat();
  8452. //Sobel(gray, out edge);
  8453. Edge(newGray, out edge);
  8454. Mat threshEdge = new Mat();
  8455. threshEdge = edge.Threshold(35, 255, ThresholdTypes.Binary);
  8456. Cv2.Rectangle(threshEdge, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
  8457. Cv2.Rectangle(threshEdge, new Rect(0, 0, b[1], tonghouY[1]), new Scalar(255), -1);
  8458. Cv2.Rectangle(threshEdge, new Rect(0, tonghouY[1], threshEdge.Cols, threshEdge.Rows - tonghouY[1]), new Scalar(0), -1);
  8459. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 7));
  8460. Mat close = new Mat();
  8461. Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
  8462. Mat noMin = new Mat();
  8463. //GetArea(close, out noMin, 1000, true);
  8464. GetMaxArea(close, out noMin);
  8465. ImageShow(edge, threshEdge, close, noMin * 255);
  8466. Mat result = noMin.Clone();
  8467. Scalar sum = new Scalar(0);
  8468. for (int j = b[2] - 20; j > 0; j--)
  8469. {
  8470. sum = result[fanghanhouY[0], tonghouY[0] - 20, j - 1, j].Sum();
  8471. //if (result.Get<byte>(tonghouY[0] - 50, j) > 0)
  8472. if ((int)sum > 20)
  8473. {
  8474. offsetX[0] = j;
  8475. break;
  8476. }
  8477. }
  8478. //for (int i = fanghanhouY[0]; i < tonghouY[0]; i++)
  8479. //{
  8480. // if (result.Get<byte>(i, offsetX[0]) > 0)
  8481. // {
  8482. // offsetY = i;
  8483. // break;
  8484. // }
  8485. //}
  8486. offsetX[1] = b[2];
  8487. int compensate = 10;
  8488. offsetX[0] -= compensate;
  8489. }
  8490. /// <summary>
  8491. /// 用于提取防焊有开口的offset,因为图片质量差,所用的参数比没开口低
  8492. /// </summary>
  8493. /// <param name="gray"></param>
  8494. /// <param name="tonghouY"></param>
  8495. /// <param name="b"></param>
  8496. /// <param name="fanghanhouY"></param>
  8497. /// <param name="offsetX"></param>
  8498. public void FanghanOffset5(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX)
  8499. {
  8500. offsetX = new int[2];
  8501. //offsetY = 0;
  8502. Mat filter = new Mat();
  8503. PointEnhancement(gray, out filter);
  8504. Mat newGray = new Mat();
  8505. Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
  8506. //PointEnhancement(gray, out gray);
  8507. //Mat crop = gray[tonghouY[0] - 120, tonghouY[0] - 20, LPIHouduX - 10, LPIHouduX + 10];
  8508. Mat edge = new Mat();
  8509. //Sobel(gray, out edge);
  8510. Edge(newGray, out edge);
  8511. Mat threshEdge = new Mat();
  8512. threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
  8513. Cv2.Rectangle(threshEdge, new Rect(0, tonghouY[1], threshEdge.Cols, threshEdge.Rows - tonghouY[1]), new Scalar(0), -1);
  8514. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 7));
  8515. Mat close = new Mat();
  8516. Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
  8517. Mat noCircle = new Mat();
  8518. RemoveCircles(close, out noCircle);
  8519. Cv2.Rectangle(noCircle, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0] - 10), new Scalar(0), -1);
  8520. //Cv2.Rectangle(noCircle, new Rect(0, 0, b[1], tonghouY[1]), new Scalar(1), -1);
  8521. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 3));
  8522. Mat open = new Mat();
  8523. Cv2.MorphologyEx(noCircle, open, MorphTypes.Open, seOpen);
  8524. Mat noMin = new Mat();
  8525. GetArea(open, out noMin, 2000, true);
  8526. //GetMaxArea(close, out noMin);
  8527. ImageShow(threshEdge, close * 255, noMin * 255);
  8528. Mat result = noMin.Clone();
  8529. Scalar sum = new Scalar(0);
  8530. for (int j = b[2] - 20; j > 0; j--)
  8531. {
  8532. sum = result[fanghanhouY[0], tonghouY[0] - 20, j - 1, j].Sum();
  8533. //if (result.Get<byte>(tonghouY[0] - 50, j) > 0)
  8534. if ((int)sum > 20)
  8535. {
  8536. offsetX[0] = j;
  8537. break;
  8538. }
  8539. }
  8540. offsetX[1] = b[2];
  8541. int compensate = 10;
  8542. offsetX[0] -= compensate;
  8543. }
  8544. /// <summary>
  8545. /// 防焊 没有开口 offset
  8546. /// </summary>
  8547. /// <param name="gray"></param>
  8548. /// <param name="tonghouY"></param>
  8549. /// <param name="b"></param>
  8550. /// <param name="fanghanhouY"></param>
  8551. /// <param name="offsetX"></param>
  8552. /// <param name="i"></param>
  8553. public void FanghanOffset5_3(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX, int i=0)
  8554. {
  8555. offsetX = new int[2];
  8556. //二值化
  8557. Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i*5), 255, ThresholdTypes.BinaryInv);
  8558. //去碎屑
  8559. threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
  8560. for (int j = b[2] - 55; j > 0; j--)
  8561. {
  8562. if (threshEdge.At<byte>(tonghouY[0]-5, j) == 0)
  8563. {
  8564. Mat temp = ~threshEdge;
  8565. if (Cv2.FloodFill(temp, new Point(j, tonghouY[0]- 5), new Scalar(255)) > 2000)
  8566. {
  8567. offsetX[0] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 2).X - 3;
  8568. break;
  8569. }
  8570. }
  8571. }
  8572. offsetX[1] = b[2];
  8573. if (offsetX[0] <= 0 || Math.Abs(offsetX[1] - offsetX[0]) > 800)
  8574. {
  8575. FanghanOffset5_3(gray, tonghouY, b, fanghanhouY, out offsetX, ++i);
  8576. }
  8577. }
  8578. public void FanghanOffset7(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX, int i=0)
  8579. {
  8580. offsetX = new int[2];
  8581. //二值化
  8582. Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
  8583. //去碎屑
  8584. threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
  8585. for (int j = b[2] - 55; j > 0; j--)
  8586. {
  8587. if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0)
  8588. {
  8589. Mat temp = ~threshEdge;
  8590. if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 2000)
  8591. {
  8592. offsetX[0] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 2).X;
  8593. break;
  8594. }
  8595. }
  8596. }
  8597. offsetX[1] = b[2];
  8598. if (offsetX[0] <= 0 || Math.Abs(offsetX[1] - offsetX[0]) > 800)
  8599. {
  8600. FanghanOffset7(gray, tonghouY, b, fanghanhouY, out offsetX, ++i);
  8601. }
  8602. }
  8603. public void FanhanKaikou3(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] fanghankaikou, int i=0)
  8604. {
  8605. //二值化
  8606. Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 3), 255, ThresholdTypes.BinaryInv);
  8607. //去碎屑
  8608. threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 3000).CvtColor(ColorConversionCodes.BGRA2GRAY);
  8609. fanghankaikou = new int[2];
  8610. for (int j = b[3]; j < threshEdge.Cols - 1; j++)
  8611. {
  8612. if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0)
  8613. {
  8614. Mat temp = ~threshEdge;
  8615. if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 3000 && Math.Abs(b[3] - j)>20)
  8616. {
  8617. //fanghankaikou[1] = j;
  8618. //Mat edge = threshEdge.Canny(0, 255);
  8619. fanghankaikou[1] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 1).X - 3;
  8620. break;
  8621. }
  8622. }
  8623. }
  8624. if(i>=10)
  8625. {
  8626. fanghankaikou[1] = 1200;
  8627. }
  8628. else
  8629. {
  8630. if (fanghankaikou[1] == 0)
  8631. {
  8632. FanhanKaikou3(gray, tonghouY, b, fanghanhouY, out fanghankaikou, ++i);
  8633. }
  8634. }
  8635. }
  8636. /// <summary>
  8637. /// 計算防焊的LPI厚度
  8638. /// </summary>
  8639. /// <param name="imageRed">原圖紅色通道圖片</param>
  8640. /// <param name="tonghouY">銅厚的上下縱坐標</param>
  8641. /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
  8642. /// <param name="LPIHouduY"></param>
  8643. public void FanghanLPI(Mat imageRed, int[] tonghouY, int[] b, out int[] LPIHouduY)
  8644. {
  8645. int LPIHouduX = b[1] + 100;
  8646. LPIHouduY = new int[2];
  8647. Mat crop = imageRed[tonghouY[0] - 120, tonghouY[0] - 20, LPIHouduX - 10, LPIHouduX + 10];
  8648. Mat thresh = 1 - crop.Threshold(80, 1, ThresholdTypes.Binary);
  8649. Mat seopen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 3));
  8650. Mat open = new Mat();
  8651. Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seopen);
  8652. Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(1), -1);
  8653. Cv2.Rectangle(open, new Rect(0, thresh.Rows - 1, thresh.Cols, 1), new Scalar(1), -1);
  8654. Fill(open, out open, 1);
  8655. Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(0), -1);
  8656. Mat result;
  8657. GetMaxArea(open, out result);
  8658. //ImageShow(result*255, open * 255);
  8659. Scalar sum = new Scalar(0);
  8660. for (int i = 0; i < result.Rows; i++)
  8661. {
  8662. sum = result[i, i + 1, 0, result.Cols].Sum();
  8663. if ((int)sum > 10)
  8664. {
  8665. LPIHouduY[0] = i + tonghouY[0] - 120;
  8666. break;
  8667. }
  8668. }
  8669. }
  8670. /// <summary>
  8671. /// 705修改,提取LPI,使用去圆算法
  8672. /// </summary>
  8673. /// <param name="gray"></param>
  8674. /// <param name="tonghouY"></param>
  8675. /// <param name="b"></param>
  8676. /// <param name="fanghanhouY"></param>
  8677. /// <param name="LPIHouduY"></param>
  8678. public void FanghanLPI2_2(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a=0)
  8679. {
  8680. int LPIHouduX = b[1] + 100;
  8681. LPIHouduY = new int[2];
  8682. Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray)-10 + (a*5), 255, ThresholdTypes.Binary);
  8683. LPIHouduY[0] = tonghouY[1];
  8684. for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1]- tonghouY[0]); i < LPIHouduY[0] - 50; i++)
  8685. {
  8686. if(result1.At<byte>(i, LPIHouduX) ==0)
  8687. {
  8688. Mat temp = ~result1;
  8689. if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
  8690. {
  8691. LPIHouduY[1] = i;
  8692. break;
  8693. }
  8694. }
  8695. }
  8696. if (LPIHouduY[1] == 0)
  8697. {
  8698. FanghanLPI2_2(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
  8699. }
  8700. }
  8701. public void FanghanLPI2_3(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a=0)
  8702. {
  8703. int LPIHouduX = b[1] + 100;
  8704. LPIHouduY = new int[2];
  8705. Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
  8706. LPIHouduY[0] = tonghouY[1];
  8707. for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0]); i < LPIHouduY[0] - 50; i++)
  8708. {
  8709. if (result1.At<byte>(i, LPIHouduX) == 0)
  8710. {
  8711. Mat temp = ~result1;
  8712. if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
  8713. {
  8714. LPIHouduY[1] = i;
  8715. break;
  8716. }
  8717. }
  8718. }
  8719. if (LPIHouduY[1] == 0)
  8720. {
  8721. FanghanLPI2_3(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
  8722. }
  8723. /*int LPIHouduX = b[1] + 100;
  8724. LPIHouduY = new int[2];
  8725. //LPI上层
  8726. Mat newGray = new Mat();
  8727. Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
  8728. Mat edge = new Mat();
  8729. Edge(newGray, out edge);
  8730. Mat threshEdge = new Mat();
  8731. threshEdge = edge.Threshold(20, 255, ThresholdTypes.Binary);
  8732. Mat threshEdge1 = threshEdge.Clone();
  8733. Cv2.Rectangle(threshEdge1, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0] - 10), new Scalar(0), -1);
  8734. Cv2.Rectangle(threshEdge1, new Rect(0, tonghouY[0] - 20, threshEdge.Cols, tonghouY[1]), new Scalar(255), -1);
  8735. Cv2.Rectangle(threshEdge1, new Rect(0, tonghouY[1], threshEdge.Cols, threshEdge.Rows - tonghouY[1]), new Scalar(0), -1);
  8736. Mat noCircle = new Mat();
  8737. RemoveCircles(threshEdge1, out noCircle);
  8738. Mat result1 = noCircle.Clone();
  8739. Scalar sum = new Scalar(0);
  8740. for (int i = fanghanhouY[0]<=0?1: fanghanhouY[0]; i < tonghouY[0] - 20; i++)
  8741. {
  8742. sum = result1[i, i + 1, LPIHouduX - 50, LPIHouduX + 50].Sum();
  8743. if ((int)sum > 50)
  8744. {
  8745. LPIHouduY[0] = i;
  8746. break;
  8747. }
  8748. }
  8749. if (LPIHouduY[0] == 0)
  8750. {
  8751. for (int i = fanghanhouY[0] <= 0 ? 1 : fanghanhouY[0]; i < tonghouY[0] - 20; i++)
  8752. {
  8753. sum = result1[i, i + 1, LPIHouduX - 50, LPIHouduX + 50].Sum();
  8754. if ((int)sum > 30)
  8755. {
  8756. LPIHouduY[0] = i;
  8757. break;
  8758. }
  8759. }
  8760. }
  8761. //LPI下层
  8762. Mat newGray2 = new Mat();
  8763. Cv2.GaussianBlur(gray, newGray2, new Size(11, 11), 3, 3);
  8764. Mat edge2 = new Mat();
  8765. Edge(newGray2, out edge2);
  8766. Mat threshEdge2 = new Mat();
  8767. threshEdge2 = edge2.Threshold(20, 255, ThresholdTypes.Binary);
  8768. Mat threshEdge3 = threshEdge2.Clone();
  8769. Cv2.Rectangle(threshEdge3, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
  8770. Cv2.Rectangle(threshEdge3, new Rect(0, tonghouY[1] + 20, threshEdge.Cols, threshEdge.Rows - tonghouY[1] - 20), new Scalar(0), -1);
  8771. Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 3));
  8772. Mat close2 = new Mat();
  8773. Cv2.MorphologyEx(threshEdge3, close2, MorphTypes.Close, seClose2);
  8774. close2 = close2 / 255;
  8775. Mat result2 = close2.Clone();
  8776. for (int i = tonghouY[1] + 20; i > tonghouY[1] - 20; i--)
  8777. {
  8778. sum = result2[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
  8779. if ((int)sum > 80)
  8780. {
  8781. LPIHouduY[1] = i;
  8782. break;
  8783. }
  8784. }
  8785. int compensate = 5;
  8786. LPIHouduY[0] += compensate;
  8787. LPIHouduY[1] -= compensate;*/
  8788. }
  8789. /// <summary>
  8790. /// 计算防焊LPI厚度,暂时用在有开口上,从下往上,寻找横向像素点足够多的时候
  8791. /// </summary>
  8792. /// <param name="gray"></param>
  8793. /// <param name="tonghouY"></param>
  8794. /// <param name="b"></param>
  8795. /// <param name="fanghanhouY"></param>
  8796. /// <param name="LPIHouduY"></param>
  8797. public void FanghanLPI3(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY)
  8798. {
  8799. int LPIHouduX = b[1] + 100;
  8800. LPIHouduY = new int[2];
  8801. //LPI上层
  8802. Mat newGray = new Mat();
  8803. Cv2.GaussianBlur(gray, newGray, new Size(11, 11), 5, 5);
  8804. Mat edge = new Mat();
  8805. Edge(newGray, out edge);
  8806. Mat threshEdge = new Mat();
  8807. threshEdge = edge.Threshold(20, 255, ThresholdTypes.Binary);
  8808. Cv2.Rectangle(threshEdge, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
  8809. //Mat seClose =
  8810. Mat noMin = new Mat();
  8811. GetArea(threshEdge, out noMin, 1000, true);
  8812. //ImageShow(edge, threshEdge, noMin * 255);
  8813. Mat result = noMin.Clone();
  8814. Scalar sum1 = new Scalar(0);
  8815. for (int i = tonghouY[0]; i > fanghanhouY[0]; i--)
  8816. {
  8817. sum1 = result[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
  8818. if ((int)sum1 > 50)
  8819. {
  8820. LPIHouduY[0] = i;
  8821. break;
  8822. }
  8823. }
  8824. //LPI下层
  8825. Mat newGray2 = new Mat();
  8826. Cv2.GaussianBlur(gray, newGray2, new Size(11, 11), 3, 3);
  8827. Mat edge2 = new Mat();
  8828. Edge(newGray2, out edge2);
  8829. Mat threshEdge2 = new Mat();
  8830. threshEdge2 = edge2.Threshold(20, 255, ThresholdTypes.Binary);
  8831. //Mat fill = new Mat();
  8832. //Fill(threshEdge2, out fill, 255);
  8833. Mat threshEdge3 = threshEdge2.Clone();
  8834. Cv2.Rectangle(threshEdge3, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
  8835. Cv2.Rectangle(threshEdge3, new Rect(0, tonghouY[1] + 20, threshEdge.Cols, threshEdge.Rows - tonghouY[1] - 20), new Scalar(0), -1);
  8836. //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 3));
  8837. //Mat close2 = new Mat();
  8838. //Cv2.MorphologyEx(threshEdge3, close2, MorphTypes.Close, seClose2);
  8839. Mat noMin2 = new Mat();
  8840. GetArea(threshEdge3, out noMin2, 2000, true);
  8841. //ImageShow(edge2, threshEdge2, noMin2 * 255);
  8842. Mat result2 = noMin2.Clone();
  8843. Scalar sum = new Scalar(0);
  8844. for (int i = tonghouY[1] + 20; i > tonghouY[1] - 20; i--)
  8845. {
  8846. sum = result2[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
  8847. if ((int)sum > 80)
  8848. //if (result2.Get<byte>(i, LPIHouduX) > 0)
  8849. {
  8850. LPIHouduY[1] = i;
  8851. break;
  8852. }
  8853. }
  8854. int compensate1 = 5;
  8855. LPIHouduY[0] -= compensate1;
  8856. int compensate2 = 5;
  8857. LPIHouduY[1] -= compensate2;
  8858. }
  8859. public void FanghanLPI4(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY)
  8860. {
  8861. int LPIHouduX = b[1] + 100;
  8862. LPIHouduY = new int[2];
  8863. //LPI上层
  8864. Mat newGray = new Mat();
  8865. Cv2.GaussianBlur(gray, newGray, new Size(11, 11), 5, 5);
  8866. Mat edge = new Mat();
  8867. Edge(newGray, out edge);
  8868. Mat threshEdge = new Mat();
  8869. threshEdge = edge.Threshold(20, 255, ThresholdTypes.Binary);
  8870. Cv2.Rectangle(threshEdge, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0] - 10), new Scalar(0), -1);
  8871. //Mat seClose =
  8872. Mat noMin = new Mat();
  8873. GetArea(threshEdge, out noMin, 1000, true);
  8874. ImageShow(edge, threshEdge, noMin * 255);
  8875. Mat result = noMin.Clone();
  8876. Scalar sum1 = new Scalar(0);
  8877. for (int i = tonghouY[0]; i > fanghanhouY[0]; i--)
  8878. {
  8879. sum1 = result[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
  8880. if ((int)sum1 > 50)
  8881. {
  8882. LPIHouduY[0] = i;
  8883. break;
  8884. }
  8885. }
  8886. //LPI下层
  8887. Mat newGray2 = new Mat();
  8888. Cv2.GaussianBlur(gray, newGray2, new Size(11, 11), 3, 3);
  8889. Mat edge2 = new Mat();
  8890. Edge(newGray2, out edge2);
  8891. Mat threshEdge2 = new Mat();
  8892. threshEdge2 = edge2.Threshold(20, 255, ThresholdTypes.Binary);
  8893. //Mat fill = new Mat();
  8894. //Fill(threshEdge2, out fill, 255);
  8895. Mat threshEdge3 = threshEdge2.Clone();
  8896. Cv2.Rectangle(threshEdge3, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
  8897. Cv2.Rectangle(threshEdge3, new Rect(0, tonghouY[1] + 20, threshEdge.Cols, threshEdge.Rows - tonghouY[1] - 20), new Scalar(0), -1);
  8898. //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 3));
  8899. //Mat close2 = new Mat();
  8900. //Cv2.MorphologyEx(threshEdge3, close2, MorphTypes.Close, seClose2);
  8901. Mat noMin2 = new Mat();
  8902. GetArea(threshEdge3, out noMin2, 2000, true);
  8903. //ImageShow(edge2, threshEdge2, noMin2 * 255);
  8904. Mat result2 = noMin2.Clone();
  8905. Scalar sum = new Scalar(0);
  8906. for (int i = tonghouY[1] + 20; i > tonghouY[1] - 20; i--)
  8907. {
  8908. sum = result2[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
  8909. if ((int)sum > 80)
  8910. //if (result2.Get<byte>(i, LPIHouduX) > 0)
  8911. {
  8912. LPIHouduY[1] = i;
  8913. break;
  8914. }
  8915. }
  8916. int compensate1 = 5;
  8917. LPIHouduY[0] -= compensate1;
  8918. int compensate2 = 5;
  8919. LPIHouduY[1] -= compensate2;
  8920. }
  8921. /// <summary>
  8922. /// 計算防焊的undercut
  8923. /// </summary>
  8924. /// <param name="imageRed">原圖紅色通道圖片</param>
  8925. /// <param name="tonghouY">銅厚的上下縱坐標</param>
  8926. /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
  8927. /// <param name="undercutX">輸出防焊同第三条线的交点之间的水平距离</param>
  8928. public void FanghanUndercut(Mat imageRed, int[] tonghouY, int[] b, int[] offsetX, out int undercutX)
  8929. {
  8930. int compensate = 20;
  8931. undercutX = 0;
  8932. Mat crop = imageRed[tonghouY[0] - 100, tonghouY[1] + 20, b[1] + 20, offsetX[0]];
  8933. Mat thresh = new Mat();
  8934. double t = Cv2.Threshold(crop, thresh, 0, 1, ThresholdTypes.Otsu);
  8935. thresh = 1 - crop.Threshold(t - 30, 1, ThresholdTypes.Otsu);
  8936. Cv2.Rectangle(thresh, new Rect(0, 0, 20, thresh.Rows - 1), new Scalar(1), -1);
  8937. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  8938. Mat close = new Mat();
  8939. Cv2.MorphologyEx(thresh, close, MorphTypes.Close, se);
  8940. Mat fill = new Mat();
  8941. Fill(close, out fill, 1);
  8942. Mat result = fill;
  8943. GetMaxContour(result, out result);
  8944. //ImageShow(result * 255);
  8945. Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 5));
  8946. Mat dilate = new Mat();
  8947. Cv2.Dilate(result, dilate, se2);
  8948. //ImageShow(close * 255, dilate * 255);
  8949. result = dilate;
  8950. Scalar sum = new Scalar(0);
  8951. Scalar max = new Scalar(0);
  8952. int upper = 0;
  8953. for (int i = 120; i < crop.Rows - 10; i++)
  8954. {
  8955. sum = result[i, i + 1, 0, result.Cols].Sum();
  8956. if ((int)sum > (int)max)
  8957. {
  8958. max = sum;
  8959. upper = i;
  8960. }
  8961. }
  8962. for (int j = result.Cols - 20; j > 20; j--)
  8963. {
  8964. if (result.Get<byte>(upper, j) > 0)
  8965. {
  8966. undercutX = j + b[1] + 20 - compensate;
  8967. break;
  8968. }
  8969. }
  8970. }
  8971. /// <summary>
  8972. /// 计算防焊没开口undercut
  8973. /// </summary>
  8974. /// <param name="image">原图</param>
  8975. /// <param name="tonghouY">铜厚纵坐标</param>
  8976. /// <param name="b">铜的边界</param>
  8977. /// <param name="offsetX"></param>
  8978. /// <param name="undercutX">输出undercut位置</param>
  8979. public void FanghanUndercut2(Mat image, int[] tonghouY, int[] b, int[] offsetX, out int undercutX)
  8980. {
  8981. /*
  8982. * 利用canny边缘检测,闭运算连接后,填充,保留大面积,取反,关键部位每列和为1时记录
  8983. */
  8984. undercutX = 0;
  8985. Cv2.GaussianBlur(image, image, new Size(7, 7), 3, 3);
  8986. Mat edge = new Mat();
  8987. Cv2.Canny(image, edge, 10, 15);
  8988. //ceju.ImageShow(edge);
  8989. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  8990. Mat close = new Mat();
  8991. Cv2.MorphologyEx(edge, close, MorphTypes.Close, se);
  8992. Mat fill = new Mat();
  8993. Fill(close, out fill, 255);
  8994. //ImageShow(edge, close, fill);
  8995. Mat draw = new Mat();
  8996. GetArea(close, out draw, 10000, true);
  8997. //ImageShow(draw * 255);
  8998. Mat result = 1 - draw;
  8999. Mat crop = result[tonghouY[0] + 20, tonghouY[1] + 10, b[1] + 20, b[2]].Clone();
  9000. GetMaxArea(crop, out crop);
  9001. //ImageShow(crop * 255);
  9002. Scalar sum = new Scalar(0);
  9003. for (int j = 0; j < crop.Cols; j++)
  9004. {
  9005. sum = crop[0, crop.Rows, j, j + 1].Sum();
  9006. if ((int)sum > 0)
  9007. {
  9008. undercutX = j + b[1] + 20;
  9009. break;
  9010. }
  9011. }
  9012. int compensate = 25;
  9013. undercutX -= compensate;
  9014. }
  9015. public void FanghanUndercut3(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] fanghanhouY, out int undercutX)
  9016. {
  9017. undercutX = 0;
  9018. Mat filter = new Mat();
  9019. PointEnhancement(gray, out filter);
  9020. Mat newGray = new Mat();
  9021. Cv2.GaussianBlur(filter, newGray, new Size(11, 11), 5, 5);
  9022. Mat edge = new Mat();
  9023. Edge(newGray, out edge);
  9024. Mat threshEdge = new Mat();
  9025. threshEdge = edge.Threshold(20, 255, ThresholdTypes.Binary);
  9026. Mat fill = new Mat();
  9027. Fill(threshEdge, out fill, 255);
  9028. Cv2.Rectangle(fill, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
  9029. //Cv2.Rectangle(threshEdge, new Rect(0, 0, b[1], tonghouY[1]), new Scalar(255), -1);
  9030. Cv2.Rectangle(fill, new Rect(0, tonghouY[1] + 20, threshEdge.Cols, threshEdge.Rows - tonghouY[1] - 20), new Scalar(0), -1);
  9031. //Cv2.Rectangle(threshEdge, new Rect(offsetX[0], 0, threshEdge.Cols-offsetX[0], threshEdge.Rows), new Scalar(0), -1);
  9032. //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  9033. //Mat close = new Mat();
  9034. //Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
  9035. Mat max = new Mat();
  9036. //GetMaxArea(close, out max);
  9037. GetArea(fill, out max, 2000, true);
  9038. Mat fanse = 1 - max;
  9039. ImageShow(threshEdge, fill, max * 255, fanse * 255);
  9040. Mat result = fanse.Clone();
  9041. Scalar sum = new Scalar(0);
  9042. int lowerBound = 0, upperBound = 0;
  9043. int leftBound = (offsetX[0] - 130 > b[1] + 20) ? offsetX[0] - 130 : b[1] + 20;
  9044. for (int i = tonghouY[0]; i < tonghouY[1] + 20; i++)
  9045. {
  9046. sum = result[i, i + 1, leftBound, offsetX[0] - 20].Sum();
  9047. if ((int)sum == 0)
  9048. {
  9049. upperBound = i;
  9050. break;
  9051. }
  9052. }
  9053. for (int i = tonghouY[1] + 20; i > tonghouY[0]; i--)
  9054. {
  9055. sum = result[i - 1, i, leftBound, offsetX[0] - 20].Sum();
  9056. if ((int)sum == 0)
  9057. {
  9058. lowerBound = i;
  9059. break;
  9060. }
  9061. }
  9062. //Cv2.Line(fanse, new Point(leftBound,upperBound), new Point(leftBound,lowerBound), new Scalar(1), 2, LineTypes.Link8);
  9063. //ImageShow(fanse*255);
  9064. for (int j = leftBound; j < offsetX[0]; j++)
  9065. {
  9066. sum = result[upperBound, lowerBound, j, j + 1].Sum();
  9067. if ((int)sum > 0)
  9068. {
  9069. undercutX = j;
  9070. break;
  9071. }
  9072. }
  9073. int compensate = 30;
  9074. undercutX -= compensate;
  9075. }
  9076. public void FanghanUndercut4(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX)
  9077. {
  9078. undercutX = 0;
  9079. Mat newGray = new Mat();
  9080. Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
  9081. Mat edge = new Mat();
  9082. Edge(newGray, out edge);
  9083. Mat threshEdge = new Mat();
  9084. threshEdge = edge.Threshold(30, 255, ThresholdTypes.Binary);
  9085. Mat threshEdge1 = threshEdge.Clone();
  9086. Cv2.Rectangle(threshEdge1, new Rect(0, 0, b[1] + 20, threshEdge1.Rows), new Scalar(255), -1);
  9087. Mat noMin = new Mat();
  9088. GetArea(threshEdge1, out noMin, 50000, true);
  9089. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  9090. Mat close = new Mat();
  9091. Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
  9092. //ImageShow(threshEdge1, noMin * 255,close*255);
  9093. int[] x1 = new int[10];
  9094. int[] y1 = new int[10];
  9095. int count = 0;
  9096. for (int i = tonghouY[0] + 20; i < tonghouY[0] + 30; i++)
  9097. {
  9098. for (int j = offsetX[0] + 30; j > offsetX[0] - 100; j--)
  9099. {
  9100. if (close.Get<byte>(i, j) > 0)
  9101. {
  9102. x1[count] = j;
  9103. y1[count] = i;
  9104. count++;
  9105. break;
  9106. }
  9107. }
  9108. if (count == 10)
  9109. break;
  9110. }
  9111. int[] x2 = new int[10];
  9112. int[] y2 = new int[10];
  9113. count = 0;
  9114. for (int i = tonghouY[0] + 30; i < tonghouY[0] + 40; i++)
  9115. {
  9116. for (int j = offsetX[0] + 30; j > offsetX[0] - 100; j--)
  9117. {
  9118. if (close.Get<byte>(i, j) > 0)
  9119. {
  9120. x2[count] = j;
  9121. y2[count] = i;
  9122. count++;
  9123. break;
  9124. }
  9125. }
  9126. if (count == 10)
  9127. break;
  9128. }
  9129. double[] averOrdinate1 = new double[2];
  9130. double[] averOrdinate2 = new double[2];
  9131. AverOrdinate(x1, y1, out averOrdinate1);
  9132. AverOrdinate(x2, y2, out averOrdinate2);
  9133. LineShow(gray, (int)averOrdinate1[0], (int)averOrdinate1[1], (int)averOrdinate2[0], (int)averOrdinate2[1]);
  9134. //ImageShow(gray);
  9135. double slope = ((averOrdinate2[0] - averOrdinate1[0]) == 0) ? 0 : (averOrdinate2[1] - averOrdinate1[1]) / (averOrdinate2[0] - averOrdinate1[0]);
  9136. double intercept = averOrdinate1[1] - slope * averOrdinate1[0];
  9137. undercutX = (slope == 0) ? 0 : (int)((LPIHouY[1] - intercept) / slope);
  9138. int compensate = 10;
  9139. undercutX -= compensate;
  9140. }
  9141. /// <summary>
  9142. /// 提取undercut,由于有开口亮度和对比对低,参数不同。
  9143. /// </summary>
  9144. /// <param name="gray"></param>
  9145. /// <param name="tonghouY"></param>
  9146. /// <param name="b"></param>
  9147. /// <param name="offsetX"></param>
  9148. /// <param name="LPIHouY"></param>
  9149. /// <param name="undercutX"></param>
  9150. public void FanghanUndercut5(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX)
  9151. {
  9152. undercutX = 0;
  9153. Mat newGray = new Mat();
  9154. Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
  9155. Mat edge = new Mat();
  9156. Edge(newGray, out edge);
  9157. Mat threshEdge = new Mat();
  9158. threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
  9159. Mat threshEdge1 = threshEdge.Clone();
  9160. Cv2.Rectangle(threshEdge1, new Rect(0, 0, b[1] + 20, threshEdge1.Rows), new Scalar(255), -1);
  9161. Mat noMin = new Mat();
  9162. GetArea(threshEdge1, out noMin, 2000, true);
  9163. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  9164. Mat close = new Mat();
  9165. Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
  9166. //ImageShow(threshEdge1, noMin * 255, close * 255);
  9167. #region//获取平均坐标
  9168. int[] x1 = new int[10];
  9169. int[] y1 = new int[10];
  9170. int count = 0;
  9171. for (int i = tonghouY[0] - 20; i < tonghouY[0] - 10; i++)
  9172. {
  9173. for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
  9174. {
  9175. if (close.Get<byte>(i, j) > 0)
  9176. {
  9177. x1[count] = j;
  9178. y1[count] = i;
  9179. count++;
  9180. break;
  9181. }
  9182. }
  9183. if (count == 10)
  9184. break;
  9185. }
  9186. int[] x2 = new int[10];
  9187. int[] y2 = new int[10];
  9188. count = 0;
  9189. for (int i = tonghouY[0]; i < tonghouY[0] + 10; i++)
  9190. {
  9191. for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
  9192. {
  9193. if (close.Get<byte>(i, j) > 0)
  9194. {
  9195. x2[count] = j;
  9196. y2[count] = i;
  9197. count++;
  9198. break;
  9199. }
  9200. }
  9201. if (count == 10)
  9202. break;
  9203. }
  9204. double[] averOrdinate1 = new double[2];
  9205. double[] averOrdinate2 = new double[2];
  9206. AverOrdinate(x1, y1, out averOrdinate1);
  9207. AverOrdinate(x2, y2, out averOrdinate2);
  9208. #endregion
  9209. //LineShow(gray, (int)averOrdinate1[0], (int)averOrdinate1[1], (int)averOrdinate2[0], (int)averOrdinate2[1]);
  9210. //ImageShow(gray,close*255);
  9211. double slope = ((averOrdinate2[0] - averOrdinate1[0]) == 0) ? 0 : (averOrdinate2[1] - averOrdinate1[1]) / (averOrdinate2[0] - averOrdinate1[0]);
  9212. double intercept = averOrdinate1[1] - slope * averOrdinate1[0];
  9213. undercutX = (slope == 0) ? 0 : (int)((LPIHouY[1] - intercept) / slope);
  9214. int compensate = 10;
  9215. undercutX -= compensate;
  9216. }
  9217. public void FanghanUndercut5_2(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int i=0)
  9218. {
  9219. undercutX = 0;
  9220. Mat filter = new Mat();
  9221. PointEnhancement(gray, out filter);
  9222. Mat newGray = new Mat();
  9223. Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
  9224. Mat threshEdge = newGray.Threshold(BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5), 255, ThresholdTypes.BinaryInv);
  9225. Mat result = threshEdge.Canny(0, 255);
  9226. int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
  9227. //想左找
  9228. for (int j = offsetX[0] + 10; j > tempRange; j--)
  9229. {
  9230. byte v = result.Get<byte>(tonghouY[1] - 20, j);
  9231. if (v > 0)
  9232. {
  9233. if(Cv2.FloodFill(result, new Point(j, tonghouY[0] - 20), new Scalar(255)) > 300)
  9234. {
  9235. undercutX = Tools.GetLeftPoint(new Point(j, tonghouY[1] - 20), result).X;
  9236. break;
  9237. }
  9238. }
  9239. }
  9240. if (i > 10)
  9241. undercutX = offsetX[0] - 25;
  9242. else
  9243. {
  9244. if (undercutX == 0 || (undercutX > 0 && offsetX[0] - undercutX > 100))
  9245. {
  9246. FanghanUndercut5_2(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, ++i);
  9247. }
  9248. }
  9249. }
  9250. /// <summary>
  9251. /// 防焊双层铜undercut,采集点位置不同
  9252. /// </summary>
  9253. /// <param name="gray"></param>
  9254. /// <param name="tonghouY"></param>
  9255. /// <param name="b"></param>
  9256. /// <param name="offsetX"></param>
  9257. /// <param name="LPIHouY"></param>
  9258. /// <param name="undercutX"></param>
  9259. public void FanghanUndercut6(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX)
  9260. {
  9261. undercutX = 0;
  9262. Mat newGray = new Mat();
  9263. Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
  9264. Mat edge = new Mat();
  9265. Edge(newGray, out edge);
  9266. Mat threshEdge = new Mat();
  9267. threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
  9268. Mat threshEdge1 = threshEdge.Clone();
  9269. Cv2.Rectangle(threshEdge1, new Rect(0, 0, b[1] + 20, threshEdge1.Rows), new Scalar(255), -1);
  9270. Mat noMin = new Mat();
  9271. GetArea(threshEdge1, out noMin, 2000, true);
  9272. //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  9273. //Mat close = new Mat();
  9274. //Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
  9275. Mat result = noMin.Clone();
  9276. //ImageShow(threshEdge1, noMin * 255/*, close * 255*/);
  9277. int[] x1 = new int[10];
  9278. int[] y1 = new int[10];
  9279. int count = 0;
  9280. for (int i = tonghouY[0] - 10; i < tonghouY[0]; i += 1)
  9281. {
  9282. for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
  9283. {
  9284. if (result.Get<byte>(i, j) > 0)
  9285. {
  9286. x1[count] = j;
  9287. y1[count] = i;
  9288. count++;
  9289. break;
  9290. }
  9291. }
  9292. if (count == 10)
  9293. break;
  9294. }
  9295. int[] x2 = new int[10];
  9296. int[] y2 = new int[10];
  9297. count = 0;
  9298. for (int i = tonghouY[0] + 10; i < tonghouY[0] + 20; i += 1)
  9299. {
  9300. for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
  9301. {
  9302. if (result.Get<byte>(i, j) > 0)
  9303. {
  9304. x2[count] = j;
  9305. y2[count] = i;
  9306. count++;
  9307. break;
  9308. }
  9309. }
  9310. if (count == 10)
  9311. break;
  9312. }
  9313. double[] averOrdinate1 = new double[2];
  9314. double[] averOrdinate2 = new double[2];
  9315. AverOrdinate(x1, y1, out averOrdinate1);
  9316. AverOrdinate(x2, y2, out averOrdinate2);
  9317. //LineShow(gray, (int)averOrdinate1[0], (int)averOrdinate1[1], (int)averOrdinate2[0], (int)averOrdinate2[1]);
  9318. //ImageShow(gray);
  9319. double slope = ((averOrdinate2[0] - averOrdinate1[0]) == 0) ? 0 : (averOrdinate2[1] - averOrdinate1[1]) / (averOrdinate2[0] - averOrdinate1[0]);
  9320. double intercept = averOrdinate1[1] - slope * averOrdinate1[0];
  9321. undercutX = (slope == 0) ? 0 : (int)((LPIHouY[1] - intercept) / slope);
  9322. int compensate = 10;
  9323. undercutX -= compensate;
  9324. }
  9325. /// <summary>
  9326. /// 705改,提取undercut
  9327. /// </summary>
  9328. /// <param name="gray"></param>
  9329. /// <param name="tonghouY"></param>
  9330. /// <param name="b"></param>
  9331. /// <param name="offsetX"></param>
  9332. /// <param name="LPIHouY"></param>
  9333. /// <param name="undercutX"></param>
  9334. public void FanghanUndercut6_2(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX)
  9335. {
  9336. undercutX = 0;
  9337. Mat filter = new Mat();
  9338. PointEnhancement(gray, out filter);
  9339. Mat newGray = new Mat();
  9340. Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
  9341. //PointEnhancement(gray, out gray);
  9342. //Mat crop = gray[tonghouY[0] - 120, tonghouY[0] - 20, LPIHouduX - 10, LPIHouduX + 10];
  9343. Mat edge = new Mat();
  9344. //Sobel(gray, out edge);
  9345. Edge(newGray, out edge);
  9346. Mat threshEdge = new Mat();
  9347. threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
  9348. //Cv2.Rectangle(threshEdge, new Rect(0, tonghouY[0]-100, b[1], tonghouY[1]-tonghouY[0]+100), new Scalar(255), -1);
  9349. Mat noCircle = new Mat();
  9350. RemoveCircles(threshEdge, out noCircle);
  9351. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  9352. Mat close = new Mat();
  9353. Cv2.MorphologyEx(noCircle, close, MorphTypes.Close, seClose);
  9354. //Mat noMin = new Mat();
  9355. //GetArea(threshEdge1, out noMin, 2000, true);
  9356. //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  9357. //Mat close = new Mat();
  9358. //Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
  9359. Mat result = close.Clone();
  9360. //ImageShow(threshEdge, noCircle * 255, close * 255);
  9361. int[] x1 = new int[10];
  9362. int[] y1 = new int[10];
  9363. int count = 0;
  9364. for (int i = tonghouY[0] - 10; i < tonghouY[0]; i += 1)
  9365. {
  9366. for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
  9367. {
  9368. if (result.Get<byte>(i, j) > 0)
  9369. {
  9370. x1[count] = j;
  9371. y1[count] = i;
  9372. count++;
  9373. break;
  9374. }
  9375. }
  9376. if (count == 10)
  9377. break;
  9378. }
  9379. int[] x2 = new int[10];
  9380. int[] y2 = new int[10];
  9381. count = 0;
  9382. for (int i = tonghouY[0] + 10; i < tonghouY[0] + 20; i += 1)
  9383. {
  9384. for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
  9385. {
  9386. if (result.Get<byte>(i, j) > 0)
  9387. {
  9388. x2[count] = j;
  9389. y2[count] = i;
  9390. count++;
  9391. break;
  9392. }
  9393. }
  9394. if (count == 10)
  9395. break;
  9396. }
  9397. double[] averOrdinate1 = new double[2];
  9398. double[] averOrdinate2 = new double[2];
  9399. AverOrdinate(x1, y1, out averOrdinate1);
  9400. AverOrdinate(x2, y2, out averOrdinate2);
  9401. //LineShow(gray, (int)averOrdinate1[0], (int)averOrdinate1[1], (int)averOrdinate2[0], (int)averOrdinate2[1]);
  9402. //ImageShow(gray);
  9403. double slope = ((averOrdinate2[0] - averOrdinate1[0]) == 0) ? 0 : (averOrdinate2[1] - averOrdinate1[1]) / (averOrdinate2[0] - averOrdinate1[0]);
  9404. double intercept = averOrdinate1[1] - slope * averOrdinate1[0];
  9405. undercutX = (slope == 0) ? 0 : (int)((LPIHouY[1] - intercept) / slope);
  9406. int compensate = 10;
  9407. undercutX -= compensate;
  9408. }
  9409. //高度差
  9410. /// <summary>
  9411. /// 計算含非零點數最大列
  9412. /// </summary>
  9413. /// <param name="image"></param>
  9414. /// <param name="maxCol"></param>
  9415. public void MaxCol(Mat image, out int maxCol)
  9416. {
  9417. maxCol = 0;
  9418. Scalar sum = new Scalar(0);
  9419. Scalar max = new Scalar(0);
  9420. for (int j = 0; j < image.Cols; j++)
  9421. {
  9422. sum = image[0, image.Rows, j, j + 1].Sum();
  9423. if ((int)sum > (int)max)
  9424. {
  9425. max = sum;
  9426. maxCol = j;
  9427. }
  9428. }
  9429. }
  9430. /// <summary>
  9431. /// 得到高度差左右的测量区域
  9432. /// </summary>
  9433. /// <param name="thresh">二值图</param>
  9434. /// <param name="BX">高度差B横坐标</param>
  9435. /// <param name="B_X">高度差B*横坐标</param>
  9436. /// <param name="direction">方向,“left”;"right"</param>
  9437. public void GaoduchaGetDataArea(Mat thresh, out Mat result, out int BX, out int B_X, string direction)
  9438. {
  9439. BX = 0;
  9440. B_X = 0;
  9441. GetMaxArea(thresh, out thresh);
  9442. int[] b = new int[3];
  9443. MaxCol(thresh, out b[1]);
  9444. //ceju.ImageShow(thresh * 255);
  9445. double ordinate1 = 0;
  9446. double ordinate2 = 0;
  9447. switch (direction)
  9448. {
  9449. case "left":
  9450. ExtractLines(thresh, out ordinate1, b[1] - 500, b[1] - 400, 1);
  9451. ExtractLines(thresh, out ordinate2, b[1] - 500, b[1] - 400, ordinate1, -1);
  9452. break;
  9453. case "right":
  9454. ExtractLines(thresh, out ordinate1, b[1] + 400, b[1] + 500, 1);
  9455. ExtractLines(thresh, out ordinate2, b[1] + 400, b[1] + 500, ordinate1, -1);
  9456. break;
  9457. }
  9458. int[] firstAndTwo = new int[] { (int)ordinate1, (int)ordinate2 };
  9459. //int border = 0;
  9460. //switch (direction)
  9461. //{
  9462. // case "left":
  9463. // for (int j = b[1]; j>0;j--)
  9464. // {
  9465. // if ((int)thresh[(int)ordinate1, (int)ordinate2, j - 1, j].Sum() == 0)
  9466. // {
  9467. // border = j;
  9468. // break;
  9469. // }
  9470. // }
  9471. // break;
  9472. // case "right":
  9473. // for (int j = b[1]; j < thresh.Cols; j++)
  9474. // {
  9475. // if ((int)thresh[(int)ordinate1, (int)ordinate2, j - 1, j].Sum() == 0)
  9476. // {
  9477. // border = j;
  9478. // break;
  9479. // }
  9480. // }
  9481. // break;
  9482. //}
  9483. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  9484. Mat open = new Mat();
  9485. Cv2.MorphologyEx(thresh, open, MorphTypes.Open, se);
  9486. result = open.Clone();
  9487. //ceju.ImageShow(open * 255);
  9488. Scalar sum = new Scalar(0);
  9489. Scalar lastSum = new Scalar(0);
  9490. switch (direction)
  9491. {
  9492. case "left":
  9493. for (int j = b[1] - 600; j > 0; j -= 5)
  9494. {
  9495. sum = open[0, (int)ordinate2, j - 10, j].Sum();
  9496. if (((int)lastSum - (int)sum) > 60 && (int)lastSum != 0)//不用绝对值差防止凸起
  9497. {
  9498. b[0] = j;
  9499. break;
  9500. }
  9501. lastSum = sum;
  9502. }
  9503. if (b[0] == 0)
  9504. {
  9505. lastSum = 0;
  9506. sum = 0;
  9507. for (int j = b[1] - 600; j > 0; j -= 5)
  9508. {
  9509. sum = open[0, (int)ordinate2, j - 10, j].Sum();
  9510. if ((int)lastSum - (int)sum > 20 && (int)lastSum != 0)
  9511. {
  9512. b[0] = j;
  9513. break;
  9514. }
  9515. lastSum = sum;
  9516. }
  9517. }
  9518. B_X = b[0] + 15;
  9519. BX = b[0] + 50;
  9520. break;
  9521. case "right":
  9522. for (int j = b[1] + 500; j < open.Cols; j += 5)
  9523. {
  9524. sum = open[0, (int)ordinate2, j - 10, j].Sum();
  9525. if (Math.Abs((int)sum - (int)lastSum) > 50/*80*/ && (int)lastSum != 0
  9526. && Math.Abs((int)lastSum - (int)(open[0, (int)ordinate2, j + 0, j + 10].Sum())) > 50
  9527. && (int)sum - (int)(open[0, (int)ordinate2, j + 10, j + 20].Sum()) > 30)
  9528. {
  9529. b[2] = j;
  9530. break;
  9531. }
  9532. lastSum = sum;
  9533. }
  9534. B_X = b[2] - 15;
  9535. BX = b[2] - 50;
  9536. break;
  9537. }
  9538. }
  9539. /// <summary>
  9540. /// 计算高度差左右的测量线的纵坐标
  9541. /// </summary>
  9542. /// <param name="image">测量区域时的结果图</param>
  9543. /// <param name="imageRed">红色通道图</param>
  9544. /// <param name="BY">高度差B纵坐标坐标</param>
  9545. /// <param name="B_Y">高度差B*纵坐标坐标</param>
  9546. /// <param name="BX">高度差B横坐标</param>
  9547. /// <param name="B_X">高度差B*横坐标</param>
  9548. public void GaoduchaB(Mat image, Mat imageRed, out int[] BY, out int[] B_Y, int BX, int B_X)
  9549. {
  9550. BY = new int[2];
  9551. B_Y = new int[2];
  9552. for (int i = 0; i < image.Rows; i++)
  9553. {
  9554. if (image.Get<byte>(i, B_X) > 0)
  9555. {
  9556. B_Y[1] = i;
  9557. break;
  9558. }
  9559. }
  9560. for (int i = 0; i < image.Rows; i++)
  9561. {
  9562. if (image.Get<byte>(i, BX) > 0)
  9563. {
  9564. BY[1] = i;
  9565. break;
  9566. }
  9567. }
  9568. //Cv2.EqualizeHist(imageRed, imageRed);
  9569. Mat crop = imageRed[BY[1] - 60, BY[1] - 10, B_X - 5, BX + 5];
  9570. Mat thresh2 = 1 - crop.Threshold(0, 1, ThresholdTypes.Otsu);
  9571. //Mat thresh2 = new Mat();
  9572. //double t2 = Cv2.Threshold(crop, thresh2, 0, 1, ThresholdTypes.Otsu);
  9573. //thresh = 1 - crop.Threshold(t + 10, 1, ThresholdTypes.Binary);
  9574. Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  9575. Cv2.MorphologyEx(thresh2, thresh2, MorphTypes.Close, se2);
  9576. GetMaxArea(thresh2, out thresh2);
  9577. //ceju.ImageShow(thresh2 * 255);
  9578. for (int i = 0; i < thresh2.Rows; i++)
  9579. //for (int i = thresh2.Rows-5; i >0;i--)
  9580. {
  9581. if (thresh2.Get<byte>(i, 5) > 0)
  9582. {
  9583. B_Y[0] = i + BY[1] - 60 + 5;
  9584. break;
  9585. }
  9586. }
  9587. for (int i = 0; i < thresh2.Rows; i++)
  9588. //for (int i = thresh2.Rows - 5; i > 0; i--)
  9589. {
  9590. if (thresh2.Get<byte>(i, thresh2.Cols - 5) > 0)
  9591. {
  9592. BY[0] = i + BY[1] - 60 + 5;
  9593. break;
  9594. }
  9595. }
  9596. }
  9597. /// <summary>
  9598. /// 计算高度差纵坐标
  9599. /// </summary>
  9600. /// <param name="thresh"></param>
  9601. /// <param name="crop"></param>
  9602. /// <param name="BY"></param>
  9603. /// <param name="B_Y"></param>
  9604. /// <param name="BX"></param>
  9605. /// <param name="B_X"></param>
  9606. public void GaoduchaBOrdinateY(Mat thresh, Mat image, out int[] BY, out int[] B_Y, int BX, int B_X)
  9607. {
  9608. BY = new int[2];
  9609. B_Y = new int[2];
  9610. //计算下纵坐标
  9611. for (int i = 0; i < thresh.Rows; i++)
  9612. {
  9613. if (thresh.Get<byte>(i, B_X) > 0)
  9614. {
  9615. B_Y[1] = i;
  9616. break;
  9617. }
  9618. }
  9619. for (int i = 0; i < thresh.Rows; i++)
  9620. {
  9621. if (thresh.Get<byte>(i, BX) > 0)
  9622. {
  9623. BY[1] = i;
  9624. break;
  9625. }
  9626. }
  9627. //边缘检测
  9628. Mat edge = new Mat();
  9629. //PointEnhancement(image, out image);
  9630. Cv2.GaussianBlur(image, image, new Size(9, 9), 5, 5);
  9631. Cv2.Canny(image, edge, 15, 0);
  9632. //Mat crop = edge[B_Y[1]-60, edge.Rows, B_X - 5, BX + 5].Clone();
  9633. //Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  9634. //Mat open = new Mat();
  9635. //Cv2.MorphologyEx(edge, open, MorphTypes.Open, seOpen);
  9636. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 3));
  9637. Mat close = new Mat();
  9638. Cv2.MorphologyEx(edge, close, MorphTypes.Close, seClose);
  9639. //Cv2.MorphologyEx(close, close, MorphTypes.Close, seClose);
  9640. //Cv2.Rectangle(close, new Rect(B_X-5, B_Y[1]-60, 1, close.Rows-(B_Y[1]-60)), new Scalar(255), -1);
  9641. //Cv2.Rectangle(close, new Rect(BX+5, B_Y[1] - 60, 1, close.Rows - (B_Y[1] - 60)), new Scalar(255), -1);
  9642. Mat fill = new Mat();
  9643. Fill(close, out fill, 255);
  9644. Mat maxFill = new Mat();
  9645. //Cv2.Rectangle(fill, new Rect(B_X - 5, B_Y[0] - 60, 1, close.Rows - (B_Y[0] - 60)), new Scalar(0), -1);
  9646. //Cv2.Rectangle(fill, new Rect(BX + 5, B_Y[0] - 60, 1, close.Rows - (B_Y[0] - 60)), new Scalar(0), -1);
  9647. //GetMaxArea(fill, out maxFill);
  9648. GetArea(fill, out maxFill, 150, true);
  9649. //ImageShow(edge, close, fill, maxFill * 255);
  9650. //计算上纵坐标
  9651. for (int i = B_Y[1] - 10; i > 0; i--)
  9652. {
  9653. if (maxFill.Get<byte>(i, B_X) > 0)
  9654. {
  9655. B_Y[0] = i;
  9656. break;
  9657. }
  9658. }
  9659. for (int i = BY[1] - 10; i > 0; i--)
  9660. {
  9661. if (maxFill.Get<byte>(i, BX) > 0)
  9662. {
  9663. BY[0] = i;
  9664. break;
  9665. }
  9666. }
  9667. int compensate = 5;
  9668. BY[0] -= compensate;
  9669. B_Y[0] -= compensate;
  9670. }
  9671. public void GaoduchaBOrdinateY2(Mat thresh, Mat imageRed, Mat image, out int[] BY, out int[] B_Y, int BX, int B_X, string direction)
  9672. {
  9673. BY = new int[2];
  9674. B_Y = new int[2];
  9675. //计算下纵坐标
  9676. for (int i = 0; i < thresh.Rows; i++)
  9677. {
  9678. if (thresh.Get<byte>(i, B_X) > 0)
  9679. {
  9680. B_Y[1] = i;
  9681. break;
  9682. }
  9683. }
  9684. for (int i = 0; i < thresh.Rows; i++)
  9685. {
  9686. if (thresh.Get<byte>(i, BX) > 0)
  9687. {
  9688. BY[1] = i;
  9689. break;
  9690. }
  9691. }
  9692. #region//法一
  9693. //边缘检测
  9694. Mat sobelEdge = new Mat();
  9695. //PointEnhancement(image, out image);
  9696. Cv2.GaussianBlur(imageRed, imageRed, new Size(9, 9), 3, 3);
  9697. PointEnhancement(imageRed, out imageRed);
  9698. Sobel(imageRed, out sobelEdge);
  9699. Mat threshEdge = new Mat();
  9700. double t = Cv2.Threshold(sobelEdge, threshEdge, 0, 255, ThresholdTypes.Otsu);
  9701. if (t < 50)
  9702. threshEdge = sobelEdge.Threshold(10, 255, ThresholdTypes.Binary);
  9703. //Mat cannyEdge = new Mat();
  9704. //Cv2.GaussianBlur(image, image, new Size(9, 9), 5, 5);
  9705. //Cv2.Canny(image, cannyEdge, 15, 0);
  9706. //Mat edge = cannyEdge/2 + threshEdge / 2;
  9707. //Cv2.ConvertScaleAbs(edge, edge);
  9708. //横向连接
  9709. Mat text2 = new Mat();
  9710. switch (direction)
  9711. {
  9712. case "left":
  9713. text2 = threshEdge[B_Y[1] - 40, B_Y[1] - 10, B_X, BX] / 255;
  9714. break;
  9715. case "right":
  9716. text2 = threshEdge[B_Y[1] - 40, B_Y[1] - 10, BX, B_X] / 255;
  9717. break;
  9718. }
  9719. Mat seClose1 = new Mat();
  9720. if ((int)text2.Sum() < text2.Rows * text2.Cols / 2)
  9721. {
  9722. //Mat seClose1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  9723. seClose1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
  9724. }
  9725. else
  9726. {
  9727. seClose1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  9728. }
  9729. Mat close1 = new Mat();
  9730. Cv2.MorphologyEx(threshEdge, close1, MorphTypes.Close, seClose1);
  9731. //上方置零
  9732. Cv2.Rectangle(close1, new Rect(0, 0, close1.Cols, BY[1] - 60), new Scalar(0), -1);
  9733. //先将小点去掉
  9734. Mat noMin = new Mat();
  9735. GetArea(close1, out noMin, 10, true);
  9736. //闭运算后得到最大连通域
  9737. //如果边缘不明显
  9738. Mat text = new Mat();
  9739. switch (direction)
  9740. {
  9741. case "left":
  9742. text = noMin[B_Y[1] - 40, B_Y[1] - 10, B_X, BX];
  9743. break;
  9744. case "right":
  9745. text = noMin[B_Y[1] - 40, B_Y[1] - 10, BX, B_X];
  9746. break;
  9747. }
  9748. Mat result = new Mat();
  9749. Mat max = new Mat();
  9750. if ((int)text.Sum() < (text.Rows * text.Cols) / 3)
  9751. {
  9752. result = noMin.Clone();
  9753. }
  9754. else if ((int)text.Sum() < (text.Rows * text.Cols) / 2)
  9755. {
  9756. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  9757. Mat close = new Mat();
  9758. Cv2.MorphologyEx(noMin, close, MorphTypes.Close, se);
  9759. GetMaxArea(close, out max);
  9760. result = max.Clone();
  9761. }
  9762. else
  9763. {
  9764. Cv2.Rectangle(noMin, new Rect(B_X - 20, B_Y[1] - 40, 10, 60), new Scalar(1), -1);
  9765. GetMaxArea(noMin, out max);
  9766. result = max.Clone();
  9767. }
  9768. #endregion
  9769. //ImageShow(close1, noMin * 255, result * 255);
  9770. //计算上纵坐标
  9771. for (int i = B_Y[1] - 60; i < result.Rows; i++)
  9772. {
  9773. if (result.Get<byte>(i, B_X) > 0)
  9774. {
  9775. B_Y[0] = i;
  9776. break;
  9777. }
  9778. }
  9779. for (int i = BY[1] - 60; i < result.Rows; i++)
  9780. {
  9781. if (result.Get<byte>(i, BX) > 0)
  9782. {
  9783. BY[0] = i;
  9784. break;
  9785. }
  9786. }
  9787. int compensate = 5;
  9788. BY[0] += compensate;
  9789. B_Y[0] += compensate;
  9790. }
  9791. public void GaoduchaBOrdinateY3(Mat thresh, Mat image, out int[] BY, out int[] B_Y, int BX, int B_X, string direction)
  9792. {
  9793. BY = new int[2];
  9794. B_Y = new int[2];
  9795. #region//计算下纵坐标
  9796. for (int i = 0; i < thresh.Rows; i++)
  9797. {
  9798. if (thresh.Get<byte>(i, B_X) > 0)
  9799. {
  9800. B_Y[1] = i;
  9801. break;
  9802. }
  9803. }
  9804. for (int i = 0; i < thresh.Rows; i++)
  9805. {
  9806. if (thresh.Get<byte>(i, BX) > 0)
  9807. {
  9808. BY[1] = i;
  9809. break;
  9810. }
  9811. }
  9812. #endregion
  9813. #region//图像处理
  9814. //阈值分割,保留最大连通域
  9815. Mat crop = new Mat();
  9816. switch (direction)
  9817. {
  9818. case "left":
  9819. crop = image[BY[1] - 60, BY[1] - 10, B_X - 10, BX + 10].Clone();
  9820. Cv2.Rectangle(image, new Rect(B_X - 10, BY[1] - 60, BX + 10 - (B_X - 10), 50), new Scalar(255), 2);
  9821. break;
  9822. case "right":
  9823. crop = image[BY[1] - 60, BY[1] - 10, BX - 10, B_X + 10].Clone();
  9824. break;
  9825. }
  9826. Mat thresh2 = new Mat();
  9827. //Cv2.ImWrite(@"C:\Users\54434\Desktop\image.png", image * 1);
  9828. //Cv2.ImWrite(@"C:\Users\54434\Desktop\crop.png", crop * 1);
  9829. double t = Cv2.Threshold(crop, thresh2, 0, 1, ThresholdTypes.Otsu);
  9830. Mat fanse = 1 - thresh2;
  9831. Mat nomin = fanse.Clone();// new Mat();
  9832. //GetMaxArea(fanse, out nomin);
  9833. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
  9834. Mat open = new Mat();
  9835. Cv2.MorphologyEx(nomin, open, MorphTypes.Open, seOpen);
  9836. //ImageShow(fanse * 255, nomin * 255, open * 255,crop+open*100);
  9837. Mat result = new Mat();// open.Clone();//
  9838. Fill(open, out result, 1);
  9839. Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 255);
  9840. #endregion
  9841. #region//计算上纵坐标
  9842. int[] lower = new int[2];
  9843. int[] upper = new int[2];
  9844. int compensate = 7;
  9845. Scalar sum = new Scalar(0);
  9846. int upper_temp = 0;
  9847. int upper_max = 0;
  9848. switch (direction)
  9849. {
  9850. case "left":
  9851. //B*
  9852. for (int i = 0; i < result.Rows - 1; i++)
  9853. {
  9854. sum = result[i, i + 1, 0, 20].Sum();
  9855. if ((int)sum > 10)
  9856. {
  9857. if (upper_temp == 0)
  9858. {
  9859. if (upper[0] == 0 || upper_max < 13)
  9860. upper_temp = i;
  9861. else if ((int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols - 20)
  9862. upper_temp = i;
  9863. }
  9864. if ((int)sum > upper_max/*13*/)
  9865. upper_max = (int)sum;// break;//避免找到内部的干扰点
  9866. }
  9867. else if (upper_temp > 0)
  9868. {
  9869. upper[0] = upper_temp;
  9870. upper_temp = 0;
  9871. }
  9872. }
  9873. if (upper_temp > 0)
  9874. {
  9875. upper[0] = upper_temp;
  9876. upper_temp = 0;
  9877. }
  9878. upper_max = 0;
  9879. for (int i = result.Rows - 1; i > 1; i--)
  9880. {
  9881. sum = result[i - 1, i, 0, 20].Sum();
  9882. if ((int)sum > 10)
  9883. {
  9884. lower[0] = i;
  9885. break;
  9886. }
  9887. }
  9888. //B
  9889. for (int i = 0; i < result.Rows - 1; i++)
  9890. {
  9891. sum = result[i, i + 1, result.Cols - 20, result.Cols].Sum();
  9892. if ((int)sum > 10)
  9893. {
  9894. if (upper_temp == 0)
  9895. {
  9896. if(upper[1] == 0 || upper_max < 13)
  9897. upper_temp = i;
  9898. else if ((int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols - 20)
  9899. upper_temp = i;
  9900. }
  9901. if ((int)sum > upper_max/*13*/)
  9902. upper_max = (int)sum;// break;//避免找到内部的干扰点
  9903. }
  9904. else if (upper_temp > 0)
  9905. {
  9906. upper[1] = upper_temp;
  9907. upper_temp = 0;
  9908. }
  9909. }
  9910. if (upper_temp > 0)
  9911. {
  9912. upper[1] = upper_temp;
  9913. upper_temp = 0;
  9914. }
  9915. upper_max = 0;
  9916. for (int i = result.Rows - 1; i > 1; i--)
  9917. {
  9918. sum = result[i - 1, i, result.Cols - 20, result.Cols].Sum();
  9919. if ((int)sum > 10)
  9920. {
  9921. lower[1] = i;
  9922. break;
  9923. }
  9924. }
  9925. if (lower[0] - upper[0] > 10)
  9926. {
  9927. B_Y[0] = upper[0] + compensate + BY[1] - 60;
  9928. }
  9929. else
  9930. {
  9931. B_Y[0] = (upper[0] + lower[0]) / 2 + BY[1] - 60;
  9932. }
  9933. if (lower[1] - upper[1] > 10)
  9934. {
  9935. BY[0] = upper[1] + compensate + BY[1] - 60;
  9936. }
  9937. else
  9938. {
  9939. BY[0] = (upper[1] + lower[1]) / 2 + BY[1] - 60;
  9940. }
  9941. break;
  9942. case "right":
  9943. //B
  9944. for (int i = 0; i < result.Rows - 1; i++)
  9945. {
  9946. sum = result[i, i + 1, 0, 20].Sum();
  9947. if ((int)sum >= 10)
  9948. {
  9949. if (upper_temp == 0)
  9950. {
  9951. if (upper[0] == 0 || upper_max < 13)
  9952. upper_temp = i;
  9953. else if ((int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols - 20
  9954. || (int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols / 2)
  9955. upper_temp = i;
  9956. }
  9957. if ((int)sum > upper_max/*13*/)
  9958. upper_max = (int)sum;// break;//避免找到内部的干扰点
  9959. }
  9960. else if (upper_temp > 0 && (int)sum < 7)
  9961. {
  9962. upper[0] = upper_temp;
  9963. upper_temp = 0;
  9964. }
  9965. }
  9966. if (upper_temp > 0)
  9967. {
  9968. upper[0] = upper_temp;
  9969. upper_temp = 0;
  9970. }
  9971. upper_max = 0;
  9972. for (int i = result.Rows - 1; i > 1; i--)
  9973. {
  9974. sum = result[i - 1, i, 0, 20].Sum();
  9975. if ((int)sum > 10)
  9976. {
  9977. lower[0] = i;
  9978. break;
  9979. }
  9980. }
  9981. //B*
  9982. for (int i = 0; i < result.Rows - 1; i++)
  9983. {
  9984. sum = result[i, i + 1, result.Cols - 20, result.Cols].Sum();
  9985. if ((int)sum > 10)
  9986. {
  9987. if (upper_temp == 0)
  9988. {
  9989. if (upper[1] == 0 || upper_max < 13)
  9990. upper_temp = i;
  9991. else if ((int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols - 20
  9992. || (int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols / 2)
  9993. upper_temp = i;
  9994. }
  9995. if ((int)sum > upper_max/*13*/)
  9996. upper_max = (int)sum;// break;//避免找到内部的干扰点
  9997. }
  9998. else if (upper_temp > 0)
  9999. {
  10000. upper[1] = upper_temp;
  10001. upper_temp = 0;
  10002. }
  10003. }
  10004. if (upper_temp > 0)
  10005. {
  10006. upper[1] = upper_temp;
  10007. upper_temp = 0;
  10008. }
  10009. upper_max = 0;
  10010. for (int i = result.Rows - 1; i > 1; i--)
  10011. {
  10012. sum = result[i - 1, i, result.Cols - 20, result.Cols].Sum();
  10013. if ((int)sum > 10)
  10014. {
  10015. lower[1] = i;
  10016. break;
  10017. }
  10018. }
  10019. if (lower[0] - upper[0] > 10)
  10020. {
  10021. BY[0] = upper[0] + compensate + BY[1] - 60;
  10022. }
  10023. else
  10024. {
  10025. BY[0] = (upper[0] + lower[0]) / 2 + BY[1] - 60;
  10026. }
  10027. if (lower[1] - upper[1] > 10)
  10028. {
  10029. B_Y[0] = upper[1] + compensate + BY[1] - 60;
  10030. }
  10031. else
  10032. {
  10033. B_Y[0] = (upper[1] + lower[1]) / 2 + BY[1] - 60;
  10034. }
  10035. break;
  10036. }
  10037. #endregion
  10038. }
  10039. /// <summary>
  10040. /// 得到高度差全的提取区域
  10041. /// </summary>
  10042. /// <param name="imageGreen">绿色通道图</param>
  10043. /// <param name="result">输出提取区域图</param>
  10044. /// <param name="dataArea">提取区域</param>
  10045. /// <param name="y">提取区域的上下界</param>
  10046. public void GaoduchaQuanGetDataArea(Mat imageGreen, out Mat result, out int[] dataArea, out int[] y)
  10047. {
  10048. dataArea = new int[4];
  10049. y = new int[2];
  10050. Mat edge = new Mat();
  10051. Sobel(imageGreen, out edge);
  10052. Mat thresh = new Mat();
  10053. thresh = edge.Threshold(0, 255, ThresholdTypes.Otsu);
  10054. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  10055. Mat close = new Mat();
  10056. Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
  10057. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 3));
  10058. Mat open = new Mat();
  10059. Cv2.MorphologyEx(close, open, MorphTypes.Open, seOpen);
  10060. Mat seOpen2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  10061. Cv2.MorphologyEx(open, open, MorphTypes.Open, seOpen2);
  10062. Mat fill = new Mat();
  10063. Fill(open, out fill, 255);
  10064. fill = fill / 255;
  10065. Mat connect = fill.Clone();
  10066. Scalar sum = new Scalar(0);
  10067. int k = 0;
  10068. bool flag = true;//真是找大于200的,假时找等于0 的
  10069. int border = 0;
  10070. while (k < connect.Cols - 1)
  10071. {
  10072. if (flag)
  10073. {
  10074. for (k = border; k < fill.Cols; k++)
  10075. {
  10076. sum = fill[0, fill.Rows, k, k + 1].Sum();
  10077. if ((int)sum > 200)
  10078. {
  10079. Cv2.Rectangle(connect, new Rect(k, 0, 100, fill.Rows), new Scalar(1), -1);
  10080. border = k + 100;
  10081. flag = false;
  10082. break;
  10083. }
  10084. }
  10085. }
  10086. else
  10087. {
  10088. for (k = border; k < fill.Cols; k++)
  10089. {
  10090. sum = fill[0, fill.Rows, k, k + 1].Sum();
  10091. if ((int)sum == 0)
  10092. {
  10093. border = k;
  10094. flag = true;
  10095. break;
  10096. }
  10097. }
  10098. }
  10099. }
  10100. //Cv2.Rectangle(connect, new Rect(0, 0, fill.Cols, 1), new Scalar(1), -1);
  10101. //Cv2.Rectangle(connect, new Rect(0, fill.Rows-1, fill.Cols, 1), new Scalar(1), -1);
  10102. //保留前二大面积
  10103. Mat[] contours;
  10104. Mat hierachy = new Mat();
  10105. Cv2.FindContours(connect, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  10106. double largest = 0, secondLargest = 0;
  10107. int index1 = 0, index2 = 0;
  10108. for (int i = 0; i < contours.Count(); i++)
  10109. {
  10110. if (Cv2.ContourArea(contours[i]) > largest)
  10111. {
  10112. largest = Cv2.ContourArea(contours[i]);
  10113. index1 = i;
  10114. }
  10115. else if (Cv2.ContourArea(contours[i]) > secondLargest)
  10116. {
  10117. largest = Cv2.ContourArea(contours[i]);
  10118. index2 = i;
  10119. }
  10120. }
  10121. result = Mat.Zeros(connect.Rows, connect.Cols, connect.Type());
  10122. Cv2.DrawContours(result, contours, index1, new Scalar(1));
  10123. Cv2.DrawContours(result, contours, index2, new Scalar(1));
  10124. Mat fill2 = new Mat();
  10125. Fill(result, out fill2, 1);
  10126. Cv2.BitwiseAnd(fill, fill2, result);
  10127. for (int i = 0; i < result.Rows; i++)
  10128. {
  10129. sum = result[i, i + 1, 0, result.Cols].Sum();
  10130. if ((int)sum > 20)
  10131. {
  10132. y[0] = i - 20;
  10133. break;
  10134. }
  10135. }
  10136. for (int i = result.Rows - 1; i > 0; i--)
  10137. {
  10138. sum = result[i - 1, i, 0, result.Cols].Sum();
  10139. if ((int)sum > 20)
  10140. {
  10141. y[1] = i;
  10142. break;
  10143. }
  10144. }
  10145. for (int j = 0; j < result.Cols; j++)
  10146. {
  10147. sum = result[y[0], y[1], j, j + 1].Sum();
  10148. if ((int)sum > 0)
  10149. {
  10150. dataArea[0] = j;
  10151. break;
  10152. }
  10153. }
  10154. for (int j = dataArea[0]; j < result.Cols; j++)
  10155. {
  10156. sum = result[y[0], y[1], j, j + 1].Sum();
  10157. if ((int)sum == 0)
  10158. {
  10159. dataArea[1] = j;
  10160. break;
  10161. }
  10162. }
  10163. for (int j = result.Cols - 1; j > 0; j--)
  10164. {
  10165. sum = result[y[0], y[1], j - 1, j].Sum();
  10166. if ((int)sum > 0)
  10167. {
  10168. dataArea[3] = j;
  10169. break;
  10170. }
  10171. }
  10172. for (int j = dataArea[3]; j > 0; j--)
  10173. {
  10174. sum = result[y[0], y[1], j - 1, j].Sum();
  10175. if ((int)sum == 0)
  10176. {
  10177. dataArea[2] = j;
  10178. break;
  10179. }
  10180. }
  10181. //ImageShow(open, connect * 255, result * 255);
  10182. }
  10183. public void GaoduchaQuanGetDataArea2(Mat imageRed, out Mat result, out int[] dataArea, out int[] y)
  10184. {
  10185. //result = new Mat();
  10186. dataArea = new int[4];
  10187. y = new int[2];
  10188. Mat thresh = new Mat();
  10189. double t = Cv2.Threshold(imageRed, thresh, 0, 1, ThresholdTypes.Otsu);
  10190. if (t > 170)
  10191. thresh = imageRed.Threshold(t + 30, 1, ThresholdTypes.Binary);
  10192. //Mat max = new Mat();
  10193. //GetMaxArea(thresh, out max);
  10194. //ImageShow(thresh * 255);
  10195. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  10196. Mat close = new Mat();
  10197. Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
  10198. Mat fill = new Mat();
  10199. Fill(close, out fill, 255);
  10200. Mat connect = fill.Clone();
  10201. Scalar sum = new Scalar(0);
  10202. int k = 0;
  10203. bool flag = true;//真是找大于200的,假时找等于0 的
  10204. int border = 0;
  10205. while (k < connect.Cols - 1)
  10206. {
  10207. if (flag)
  10208. {
  10209. for (k = border; k < thresh.Cols; k++)
  10210. {
  10211. sum = thresh[0, thresh.Rows, k, k + 1].Sum();
  10212. if ((int)sum > 200)
  10213. {
  10214. Cv2.Rectangle(connect, new Rect(k, 0, 100, thresh.Rows), new Scalar(1), -1);
  10215. border = k + 100;
  10216. flag = false;
  10217. break;
  10218. }
  10219. }
  10220. }
  10221. else
  10222. {
  10223. for (k = border; k < thresh.Cols; k++)
  10224. {
  10225. sum = thresh[0, thresh.Rows, k, k + 1].Sum();
  10226. if ((int)sum == 0)
  10227. {
  10228. border = k;
  10229. flag = true;
  10230. break;
  10231. }
  10232. }
  10233. }
  10234. }
  10235. //保留前二大面积
  10236. Mat[] contours;
  10237. Mat hierachy = new Mat();
  10238. Cv2.FindContours(connect, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  10239. double largest = 0, secondLargest = 0;
  10240. int index1 = 0, index2 = 0;
  10241. for (int i = 0; i < contours.Count(); i++)
  10242. {
  10243. if (Cv2.ContourArea(contours[i]) > largest)
  10244. {
  10245. largest = Cv2.ContourArea(contours[i]);
  10246. index1 = i;
  10247. }
  10248. else if (Cv2.ContourArea(contours[i]) > secondLargest)
  10249. {
  10250. secondLargest = Cv2.ContourArea(contours[i]);
  10251. index2 = i;
  10252. }
  10253. }
  10254. result = Mat.Zeros(connect.Rows, connect.Cols, connect.Type());
  10255. Cv2.DrawContours(result, contours, index1, new Scalar(1));
  10256. Cv2.DrawContours(result, contours, index2, new Scalar(1));
  10257. Mat fill2 = new Mat();
  10258. Fill(result, out fill2, 1);
  10259. Cv2.BitwiseAnd(fill, fill2, result);
  10260. //ImageShow(connect * 255, result * 255);
  10261. for (int i = 0; i < result.Rows; i++)
  10262. {
  10263. sum = result[i, i + 1, 0, result.Cols].Sum();
  10264. if ((int)sum > 20)
  10265. {
  10266. y[0] = i - 20;
  10267. break;
  10268. }
  10269. }
  10270. for (int i = result.Rows - 1; i > 0; i--)
  10271. {
  10272. sum = result[i - 1, i, 0, result.Cols].Sum();
  10273. if ((int)sum > 20)
  10274. {
  10275. y[1] = i;
  10276. break;
  10277. }
  10278. }
  10279. for (int j = 0; j < result.Cols; j++)
  10280. {
  10281. sum = result[y[0], y[1], j, j + 1].Sum();
  10282. if ((int)sum > 0)
  10283. {
  10284. dataArea[0] = j;
  10285. break;
  10286. }
  10287. }
  10288. for (int j = dataArea[0]; j < result.Cols; j++)
  10289. {
  10290. sum = result[y[0], y[1], j, j + 1].Sum();
  10291. if ((int)sum == 0)
  10292. {
  10293. dataArea[1] = j;
  10294. break;
  10295. }
  10296. }
  10297. for (int j = result.Cols - 1; j > 0; j--)
  10298. {
  10299. sum = result[y[0], y[1], j - 1, j].Sum();
  10300. if ((int)sum > 0)
  10301. {
  10302. dataArea[3] = j;
  10303. break;
  10304. }
  10305. }
  10306. for (int j = dataArea[3]; j > 0; j--)
  10307. {
  10308. sum = result[y[0], y[1], j - 1, j].Sum();
  10309. if ((int)sum == 0)
  10310. {
  10311. dataArea[2] = j;
  10312. break;
  10313. }
  10314. }
  10315. }
  10316. public void GaoduchaQuanGetDataArea3(Mat imageRed, Mat image, out int[] leftTonghou, out int[] rightTonghou, out int[] dataArea, out int[] y)
  10317. {
  10318. dataArea = new int[4];
  10319. y = new int[2];
  10320. leftTonghou = new int[3];
  10321. rightTonghou = new int[3];
  10322. Mat thresh = new Mat();
  10323. double t = Cv2.Threshold(imageRed, thresh, 0, 1, ThresholdTypes.Otsu);
  10324. if (t > 170)
  10325. thresh = imageRed.Threshold(240, 1, ThresholdTypes.Binary);
  10326. //ImageShow(thresh * 255);
  10327. //当还是太亮时,需要判断,使用边缘检测加填充方法
  10328. Mat maxArea = new Mat();
  10329. GetMaxArea(thresh, out maxArea);
  10330. bool flag = false;
  10331. double maxAreaSum = (double)maxArea.Sum();
  10332. if ((int)maxArea.Sum() > 350000)
  10333. {
  10334. Mat gray = new Mat();
  10335. Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
  10336. thresh = gray.Threshold(200, 1, ThresholdTypes.Binary);
  10337. //Mat edge = new Mat();
  10338. //Sobel(imageRed, out edge);
  10339. //Mat edgeThresh = new Mat();
  10340. //edgeThresh = edge.Threshold(50, 1, ThresholdTypes.Binary);
  10341. //Mat edgeMax = new Mat();
  10342. //GetMaxArea(edgeThresh, out edgeMax);
  10343. //flag = true;
  10344. //ImageShow(edgeThresh * 255, edgeMax * 255);
  10345. }
  10346. //ImageShow(thresh * 255);
  10347. Cv2.Rectangle(thresh, new Rect(thresh.Cols - 1, 0, 1, thresh.Rows), new Scalar(1), -1);//当右侧出现非整体铜体时连接
  10348. Cv2.Rectangle(thresh, new Rect(0, 0, 1, thresh.Rows), new Scalar(1), -1);//也可能出现在左侧
  10349. //闭运算
  10350. Mat seClose = new Mat();
  10351. if (t < 170)
  10352. seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 9));
  10353. else
  10354. seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  10355. Mat close = new Mat();
  10356. Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
  10357. Fill(close, out close, 1);
  10358. //ImageShow(close * 255);
  10359. //保留前二大区域
  10360. Mat hierachy = new Mat();
  10361. Mat[] contoursMat;
  10362. Cv2.FindContours(close, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  10363. double max1 = 0, max2 = 0;
  10364. int idx1 = 0, idx2 = 0;
  10365. for (int i = 0; i < contoursMat.Count(); i++)
  10366. {
  10367. if (Cv2.ContourArea(contoursMat[i]) > max1)
  10368. {
  10369. max2 = max1;
  10370. idx2 = idx1;
  10371. max1 = Cv2.ContourArea(contoursMat[i]);
  10372. idx1 = i;
  10373. }
  10374. else if (Cv2.ContourArea(contoursMat[i]) > max2)
  10375. {
  10376. max2 = Cv2.ContourArea(contoursMat[i]);
  10377. idx2 = i;
  10378. }
  10379. }
  10380. Mat qianerArea = new Mat(thresh.Size(), thresh.Type());
  10381. Cv2.DrawContours(qianerArea, contoursMat, idx1, new Scalar(1));
  10382. Cv2.DrawContours(qianerArea, contoursMat, idx2, new Scalar(1));
  10383. Fill(qianerArea, out qianerArea, 1);
  10384. thresh = qianerArea;
  10385. Cv2.Rectangle(thresh, new Rect(thresh.Cols - 1, 0, 1, thresh.Rows), new Scalar(0), -1);//将连接线去掉
  10386. Cv2.Rectangle(thresh, new Rect(0, 0, 1, thresh.Rows), new Scalar(0), -1);//
  10387. //
  10388. int middle = imageRed.Cols / 2;
  10389. //ImageShow(thresh * 255);
  10390. Scalar sum = new Scalar(0);
  10391. Scalar max = new Scalar(0);
  10392. //左右列像素最多的地方,并且提取目标区域
  10393. int leftBorde = 0, rightBorder = 0;
  10394. for (int j = 0; j < middle; j++)
  10395. {
  10396. sum = thresh[0, thresh.Rows, j, j + 1].Sum();
  10397. if ((int)sum > (int)max)
  10398. {
  10399. max = sum;
  10400. leftBorde = j;
  10401. dataArea[0] = leftBorde;
  10402. }
  10403. }
  10404. max = 0;
  10405. for (int j = thresh.Cols - 1; j > middle; j--)
  10406. {
  10407. sum = thresh[0, thresh.Rows, j - 1, j].Sum();
  10408. if ((int)sum > (int)max)
  10409. {
  10410. max = sum;
  10411. rightBorder = j;
  10412. dataArea[3] = rightBorder;
  10413. }
  10414. }
  10415. //目标区域的上下界
  10416. for (int i = 0; i < thresh.Rows; i++)
  10417. {
  10418. sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
  10419. if ((int)sum > 0)
  10420. {
  10421. y[0] = i;
  10422. break;
  10423. }
  10424. }
  10425. for (int i = thresh.Rows - 1; i > 0; i--)
  10426. {
  10427. sum = thresh[i - 1, i, 0, thresh.Cols].Sum();
  10428. if ((int)sum > 0)
  10429. {
  10430. y[1] = i;
  10431. break;
  10432. }
  10433. }
  10434. //目标区域中间
  10435. if (flag == false)
  10436. {
  10437. //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh.png", thresh * 120);
  10438. for (int j = leftBorde; j < thresh.Cols; j++)
  10439. {
  10440. sum = thresh[y[0], y[1], j, j + 1].Sum();
  10441. if ((int)sum == 0)
  10442. {
  10443. dataArea[1] = j;
  10444. break;
  10445. }
  10446. }
  10447. for (int j = rightBorder - 1; j > middle; j--)
  10448. {
  10449. sum = thresh[y[0], y[1], j - 1, j].Sum();
  10450. if ((int)sum == 0)
  10451. {
  10452. dataArea[2] = j;
  10453. break;
  10454. }
  10455. }
  10456. }
  10457. //else
  10458. //{
  10459. // Scalar lastSum = new Scalar(0);
  10460. // for (int j = middle; j > 100; j-=5)
  10461. // {
  10462. // sum = thresh[y[0], y[1], j - 3, j].Sum();
  10463. // if ((int)sum - (int)lastSum > 100&& lastSum!=0)
  10464. // {
  10465. // dataArea[1] = j;
  10466. // break;
  10467. // }
  10468. // lastSum = sum;
  10469. // }
  10470. // lastSum = 0;
  10471. // for (int j = middle; j <thresh.Cols-100; j+=5)
  10472. // {
  10473. // sum = thresh[y[0], y[1], j , j+5].Sum();
  10474. // if ((int)sum - (int)lastSum > 150 && lastSum != 0)
  10475. // {
  10476. // dataArea[2] = j;
  10477. // break;
  10478. // }
  10479. // lastSum = sum;
  10480. // }
  10481. //}
  10482. //边缘检测并横向腐蚀,去掉小连通域,提取铜厚
  10483. Mat sobel = new Mat();
  10484. Sobel(thresh, out sobel);
  10485. Mat result = sobel.Clone();
  10486. //Mat sobel = new Mat();
  10487. //Cv2.GaussianBlur(imageRed, imageRed, new Size(11, 11), 5, 5);
  10488. //Cv2.MedianBlur(imageRed, imageRed, 5);
  10489. //Sobel(imageRed, out sobel);
  10490. //Mat sobelThresh = new Mat();
  10491. //double t2 = Cv2.Threshold(sobel, sobelThresh, 0, 255, ThresholdTypes.Otsu);
  10492. //sobelThresh = sobel.Threshold(5, 255, ThresholdTypes.Binary);
  10493. //Mat seErode = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(30, 1));
  10494. //Mat erode = new Mat();
  10495. //Cv2.Erode(sobelThresh, erode, seErode);
  10496. //Mat noMinArea = new Mat();
  10497. //GetArea(erode, out noMinArea, 500, true);
  10498. //Mat result = noMinArea.Clone();
  10499. //ImageShow(result * 255);
  10500. leftTonghou[0] = dataArea[0] + 100;
  10501. rightTonghou[0] = dataArea[3] - 100;
  10502. //计算线纵坐标
  10503. //左
  10504. for (int i = y[0]; i < y[1]; i++)
  10505. {
  10506. if (result.Get<byte>(i, leftTonghou[0]) > 0)
  10507. {
  10508. leftTonghou[1] = i;
  10509. break;
  10510. }
  10511. }
  10512. for (int i = leftTonghou[1] + 60; i < y[1]; i++)
  10513. {
  10514. if (result.Get<byte>(i, leftTonghou[0]) > 0)
  10515. {
  10516. leftTonghou[2] = i;
  10517. break;
  10518. }
  10519. }
  10520. for (int i = leftTonghou[2]; i < y[1]; i++)
  10521. {
  10522. if (result.Get<byte>(i, leftTonghou[0]) == 0)
  10523. {
  10524. leftTonghou[2] = i;
  10525. break;
  10526. }
  10527. }
  10528. //右
  10529. for (int i = y[0]; i < y[1]; i++)
  10530. {
  10531. if (result.Get<byte>(i, rightTonghou[0]) > 0)
  10532. {
  10533. rightTonghou[1] = i;
  10534. break;
  10535. }
  10536. }
  10537. for (int i = rightTonghou[1] + 60; i < y[1]; i++)
  10538. {
  10539. if (result.Get<byte>(i, rightTonghou[0]) > 0)
  10540. {
  10541. rightTonghou[2] = i;
  10542. break;
  10543. }
  10544. }
  10545. for (int i = rightTonghou[2]; i < y[1]; i++)
  10546. {
  10547. if (result.Get<byte>(i, rightTonghou[0]) == 0)
  10548. {
  10549. rightTonghou[2] = i;
  10550. break;
  10551. }
  10552. }
  10553. if (/*leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 > result.Rows
  10554. || */leftTonghou[2] - rightTonghou[2] > 300 && rightTonghou[2] * 3 > result.Rows)
  10555. {
  10556. if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 > result.Rows)
  10557. leftTonghou[0] = leftTonghou[0] + 400;
  10558. else if (leftTonghou[2] - rightTonghou[2] > 300 && rightTonghou[2] * 3 > result.Rows)
  10559. leftTonghou[0] = leftTonghou[0] + 430;
  10560. //计算线纵坐标
  10561. //左
  10562. for (int i = y[0]; i < y[1]; i++)
  10563. {
  10564. if (result.Get<byte>(i, leftTonghou[0]) > 0)
  10565. {
  10566. leftTonghou[1] = i;
  10567. break;
  10568. }
  10569. }
  10570. for (int i = leftTonghou[1] + 60; i < y[1]; i++)
  10571. {
  10572. if (result.Get<byte>(i, leftTonghou[0]) > 0)
  10573. {
  10574. leftTonghou[2] = i;
  10575. break;
  10576. }
  10577. }
  10578. for (int i = leftTonghou[2]; i < y[1]; i++)
  10579. {
  10580. if (result.Get<byte>(i, leftTonghou[0]) == 0)
  10581. {
  10582. leftTonghou[2] = i;
  10583. break;
  10584. }
  10585. }
  10586. }
  10587. if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 < result.Rows
  10588. || rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
  10589. {
  10590. if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 < result.Rows)
  10591. rightTonghou[0] = rightTonghou[0] - 400;
  10592. else if (rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
  10593. rightTonghou[0] = rightTonghou[0] - 550;
  10594. //右
  10595. for (int i = y[0]; i < y[1]; i++)
  10596. {
  10597. if (result.Get<byte>(i, rightTonghou[0]) > 0)
  10598. {
  10599. rightTonghou[1] = i;
  10600. break;
  10601. }
  10602. }
  10603. for (int i = rightTonghou[1] + 60; i < y[1]; i++)
  10604. {
  10605. if (result.Get<byte>(i, rightTonghou[0]) > 0)
  10606. {
  10607. rightTonghou[2] = i;
  10608. break;
  10609. }
  10610. }
  10611. for (int i = rightTonghou[2]; i < y[1]; i++)
  10612. {
  10613. if (result.Get<byte>(i, rightTonghou[0]) == 0)
  10614. {
  10615. rightTonghou[2] = i;
  10616. break;
  10617. }
  10618. }
  10619. if (rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
  10620. rightTonghou[2] = leftTonghou[2];
  10621. }
  10622. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 120);
  10623. ////ExtractLines(thresh,out left1,leftBorde+200)
  10624. ////ImageShow(sobelThresh,erode,maxArea*255);
  10625. }
  10626. public void GaoduchaQuanGetDataArea3_Center(Mat imageRed, Mat image, out int[] leftTonghou, out int[] rightTonghou, out int[] dataArea, out int[] y, int[] dataArea0)
  10627. {
  10628. dataArea = new int[4];
  10629. y = new int[2];
  10630. leftTonghou = new int[3];
  10631. rightTonghou = new int[3];
  10632. Mat thresh = new Mat();
  10633. double t = Cv2.Threshold(imageRed, thresh, 0, 1, ThresholdTypes.Otsu);
  10634. if (t > 170)
  10635. thresh = imageRed.Threshold(240, 1, ThresholdTypes.Binary);
  10636. //ImageShow(thresh * 255);
  10637. //当还是太亮时,需要判断,使用边缘检测加填充方法
  10638. Mat maxArea = new Mat();
  10639. GetMaxArea(thresh, out maxArea);
  10640. bool flag = false;
  10641. double maxAreaSum = (double)maxArea.Sum();
  10642. if ((int)maxArea.Sum() > 350000)
  10643. {
  10644. Mat gray = new Mat();
  10645. Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
  10646. thresh = gray.Threshold(200, 1, ThresholdTypes.Binary);
  10647. //Mat edge = new Mat();
  10648. //Sobel(imageRed, out edge);
  10649. //Mat edgeThresh = new Mat();
  10650. //edgeThresh = edge.Threshold(50, 1, ThresholdTypes.Binary);
  10651. //Mat edgeMax = new Mat();
  10652. //GetMaxArea(edgeThresh, out edgeMax);
  10653. //flag = true;
  10654. //ImageShow(edgeThresh * 255, edgeMax * 255);
  10655. }
  10656. //ImageShow(thresh * 255);
  10657. Cv2.Rectangle(thresh, new Rect(thresh.Cols - 1, 0, 1, thresh.Rows), new Scalar(1), -1);//当右侧出现非整体铜体时连接
  10658. Cv2.Rectangle(thresh, new Rect(0, 0, 1, thresh.Rows), new Scalar(1), -1);//也可能出现在左侧
  10659. //闭运算
  10660. Mat seClose = new Mat();
  10661. if (t < 170)
  10662. seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 9));
  10663. else
  10664. seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  10665. Mat close = new Mat();
  10666. Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
  10667. Fill(close, out close, 1);
  10668. //ImageShow(close * 255);
  10669. //保留前二大区域
  10670. Mat hierachy = new Mat();
  10671. Mat[] contoursMat;
  10672. Cv2.FindContours(close, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  10673. double max1 = 0, max2 = 0;
  10674. int idx1 = 0, idx2 = 0;
  10675. for (int i = 0; i < contoursMat.Count(); i++)
  10676. {
  10677. if (Cv2.ContourArea(contoursMat[i]) > max1)
  10678. {
  10679. max2 = max1;
  10680. idx2 = idx1;
  10681. max1 = Cv2.ContourArea(contoursMat[i]);
  10682. idx1 = i;
  10683. }
  10684. else if (Cv2.ContourArea(contoursMat[i]) > max2)
  10685. {
  10686. max2 = Cv2.ContourArea(contoursMat[i]);
  10687. idx2 = i;
  10688. }
  10689. }
  10690. Mat qianerArea = new Mat(thresh.Size(), thresh.Type());
  10691. Cv2.DrawContours(qianerArea, contoursMat, idx1, new Scalar(1));
  10692. Cv2.DrawContours(qianerArea, contoursMat, idx2, new Scalar(1));
  10693. Fill(qianerArea, out qianerArea, 1);
  10694. thresh = qianerArea;
  10695. Cv2.Rectangle(thresh, new Rect(thresh.Cols - 1, 0, 1, thresh.Rows), new Scalar(0), -1);//将连接线去掉
  10696. Cv2.Rectangle(thresh, new Rect(0, 0, 1, thresh.Rows), new Scalar(0), -1);//
  10697. //
  10698. int middle = imageRed.Cols / 2;
  10699. //ImageShow(thresh * 255);
  10700. Scalar sum = new Scalar(0);
  10701. Scalar max = new Scalar(0);
  10702. //左右列像素最多的地方,并且提取目标区域
  10703. int leftBorde = 0, rightBorder = 0;
  10704. for (int j = 0; j < middle; j++)
  10705. {
  10706. sum = thresh[0, thresh.Rows, j, j + 1].Sum();
  10707. if ((int)sum > (int)max)
  10708. {
  10709. max = sum;
  10710. leftBorde = dataArea0[0];// j;
  10711. dataArea[0] = dataArea0[0];// leftBorde;
  10712. }
  10713. }
  10714. max = 0;
  10715. for (int j = thresh.Cols - 1; j > middle; j--)
  10716. {
  10717. sum = thresh[0, thresh.Rows, j - 1, j].Sum();
  10718. if ((int)sum > (int)max)
  10719. {
  10720. max = sum;
  10721. rightBorder = dataArea0[3];// j;
  10722. dataArea[3] = dataArea0[3];// rightBorder;
  10723. }
  10724. }
  10725. //目标区域的上下界
  10726. for (int i = 0; i < thresh.Rows; i++)
  10727. {
  10728. sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
  10729. if ((int)sum > 0)
  10730. {
  10731. y[0] = i;
  10732. break;
  10733. }
  10734. }
  10735. for (int i = thresh.Rows - 1; i > 0; i--)
  10736. {
  10737. sum = thresh[i - 1, i, 0, thresh.Cols].Sum();
  10738. if ((int)sum > 0)
  10739. {
  10740. y[1] = i;
  10741. break;
  10742. }
  10743. }
  10744. //目标区域中间
  10745. if (flag == false)
  10746. {
  10747. for (int j = leftBorde; j < thresh.Cols; j++)
  10748. {
  10749. sum = thresh[y[0], y[1], j, j + 1].Sum();
  10750. if ((int)sum == 0)
  10751. {
  10752. dataArea[1] = j - 120;
  10753. break;
  10754. }
  10755. }
  10756. for (int j = rightBorder - 100; j > middle; j--)
  10757. {
  10758. sum = thresh[y[0], y[1], j - 1, j].Sum();
  10759. if ((int)sum == 0)
  10760. {
  10761. dataArea[2] = j - 120;
  10762. break;
  10763. }
  10764. }
  10765. }
  10766. //else
  10767. //{
  10768. // Scalar lastSum = new Scalar(0);
  10769. // for (int j = middle; j > 100; j-=5)
  10770. // {
  10771. // sum = thresh[y[0], y[1], j - 3, j].Sum();
  10772. // if ((int)sum - (int)lastSum > 100&& lastSum!=0)
  10773. // {
  10774. // dataArea[1] = j;
  10775. // break;
  10776. // }
  10777. // lastSum = sum;
  10778. // }
  10779. // lastSum = 0;
  10780. // for (int j = middle; j <thresh.Cols-100; j+=5)
  10781. // {
  10782. // sum = thresh[y[0], y[1], j , j+5].Sum();
  10783. // if ((int)sum - (int)lastSum > 150 && lastSum != 0)
  10784. // {
  10785. // dataArea[2] = j;
  10786. // break;
  10787. // }
  10788. // lastSum = sum;
  10789. // }
  10790. //}
  10791. //边缘检测并横向腐蚀,去掉小连通域,提取铜厚
  10792. Mat sobel = new Mat();
  10793. Sobel(thresh, out sobel);
  10794. Mat result = sobel.Clone();
  10795. //Mat sobel = new Mat();
  10796. //Cv2.GaussianBlur(imageRed, imageRed, new Size(11, 11), 5, 5);
  10797. //Cv2.MedianBlur(imageRed, imageRed, 5);
  10798. //Sobel(imageRed, out sobel);
  10799. //Mat sobelThresh = new Mat();
  10800. //double t2 = Cv2.Threshold(sobel, sobelThresh, 0, 255, ThresholdTypes.Otsu);
  10801. //sobelThresh = sobel.Threshold(5, 255, ThresholdTypes.Binary);
  10802. //Mat seErode = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(30, 1));
  10803. //Mat erode = new Mat();
  10804. //Cv2.Erode(sobelThresh, erode, seErode);
  10805. //Mat noMinArea = new Mat();
  10806. //GetArea(erode, out noMinArea, 500, true);
  10807. //Mat result = noMinArea.Clone();
  10808. //ImageShow(result * 255);
  10809. leftTonghou[0] = dataArea[0] + 100;
  10810. rightTonghou[0] = dataArea[3] - 100;
  10811. //计算线纵坐标
  10812. //左
  10813. for (int i = y[0]; i < y[1]; i++)
  10814. {
  10815. if (result.Get<byte>(i, leftTonghou[0]) > 0)
  10816. {
  10817. leftTonghou[1] = i;
  10818. break;
  10819. }
  10820. }
  10821. for (int i = leftTonghou[1] + 60; i < y[1]; i++)
  10822. {
  10823. if (result.Get<byte>(i, leftTonghou[0]) > 0)
  10824. {
  10825. leftTonghou[2] = i;
  10826. break;
  10827. }
  10828. }
  10829. for (int i = leftTonghou[2]; i < y[1]; i++)
  10830. {
  10831. if (result.Get<byte>(i, leftTonghou[0]) == 0)
  10832. {
  10833. leftTonghou[2] = i;
  10834. break;
  10835. }
  10836. }
  10837. //右
  10838. for (int i = y[0]; i < y[1]; i++)
  10839. {
  10840. if (result.Get<byte>(i, rightTonghou[0]) > 0)
  10841. {
  10842. rightTonghou[1] = i;
  10843. break;
  10844. }
  10845. }
  10846. for (int i = rightTonghou[1] + 60; i < y[1]; i++)
  10847. {
  10848. if (result.Get<byte>(i, rightTonghou[0]) > 0)
  10849. {
  10850. rightTonghou[2] = i;
  10851. break;
  10852. }
  10853. }
  10854. for (int i = rightTonghou[2]; i < y[1]; i++)
  10855. {
  10856. if (result.Get<byte>(i, rightTonghou[0]) == 0)
  10857. {
  10858. rightTonghou[2] = i;
  10859. break;
  10860. }
  10861. }
  10862. if (/*leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 > result.Rows
  10863. || */leftTonghou[2] - rightTonghou[2] > 300 && rightTonghou[2] * 3 > result.Rows)
  10864. {
  10865. if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 > result.Rows)
  10866. leftTonghou[0] = leftTonghou[0] + 400;
  10867. else if (leftTonghou[2] - rightTonghou[2] > 300 && rightTonghou[2] * 3 > result.Rows)
  10868. leftTonghou[0] = leftTonghou[0] + 430;
  10869. //计算线纵坐标
  10870. //左
  10871. for (int i = y[0]; i < y[1]; i++)
  10872. {
  10873. if (result.Get<byte>(i, leftTonghou[0]) > 0)
  10874. {
  10875. leftTonghou[1] = i;
  10876. break;
  10877. }
  10878. }
  10879. for (int i = leftTonghou[1] + 60; i < y[1]; i++)
  10880. {
  10881. if (result.Get<byte>(i, leftTonghou[0]) > 0)
  10882. {
  10883. leftTonghou[2] = i;
  10884. break;
  10885. }
  10886. }
  10887. for (int i = leftTonghou[2]; i < y[1]; i++)
  10888. {
  10889. if (result.Get<byte>(i, leftTonghou[0]) == 0)
  10890. {
  10891. leftTonghou[2] = i;
  10892. break;
  10893. }
  10894. }
  10895. }
  10896. if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 < result.Rows
  10897. || rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
  10898. {
  10899. if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 < result.Rows)
  10900. rightTonghou[0] = rightTonghou[0] - 400;
  10901. else if (rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
  10902. rightTonghou[0] = rightTonghou[0] - 550;
  10903. //右
  10904. for (int i = y[0]; i < y[1]; i++)
  10905. {
  10906. if (result.Get<byte>(i, rightTonghou[0]) > 0)
  10907. {
  10908. rightTonghou[1] = i;
  10909. break;
  10910. }
  10911. }
  10912. for (int i = rightTonghou[1] + 60; i < y[1]; i++)
  10913. {
  10914. if (result.Get<byte>(i, rightTonghou[0]) > 0)
  10915. {
  10916. rightTonghou[2] = i;
  10917. break;
  10918. }
  10919. }
  10920. for (int i = rightTonghou[2]; i < y[1]; i++)
  10921. {
  10922. if (result.Get<byte>(i, rightTonghou[0]) == 0)
  10923. {
  10924. rightTonghou[2] = i;
  10925. break;
  10926. }
  10927. }
  10928. if (rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
  10929. rightTonghou[2] = leftTonghou[2];
  10930. }
  10931. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 120);
  10932. ////ExtractLines(thresh,out left1,leftBorde+200)
  10933. ////ImageShow(sobelThresh,erode,maxArea*255);
  10934. }
  10935. /// <summary>
  10936. /// 计算高度差全的铜厚
  10937. /// </summary>
  10938. /// <param name="contour">二值图</param>
  10939. /// <param name="leftTonghou">左侧铜厚,0:横坐标;1:上纵坐标;2:下纵坐标</param>
  10940. /// <param name="rightTonghou">右侧铜厚,0:横坐标;1:上纵坐标;2:下纵坐标</param>
  10941. /// <param name="dataArea">提取区域</param>
  10942. /// <param name="y">目标区域上下边界</param>
  10943. public void GaoduchaTonghou(Mat contour, out int[] leftTonghou, out int[] rightTonghou, int[] dataArea, int[] y)
  10944. {
  10945. leftTonghou = new int[3];
  10946. rightTonghou = new int[3];
  10947. leftTonghou[0] = dataArea[0] + 200;
  10948. rightTonghou[0] = dataArea[3] - 200;
  10949. Mat crop = contour[y[0], y[1], 0, contour.Cols].Clone();
  10950. double leftOrdinate1 = 0;
  10951. ExtractLines(crop, out leftOrdinate1, leftTonghou[0] - 5, leftTonghou[0] + 5, 1);
  10952. double leftOrdinate2 = 0;
  10953. ExtractLines(crop, out leftOrdinate2, leftTonghou[0] - 5, leftTonghou[0] + 5, leftOrdinate1, -1);
  10954. double rightOrdinate1 = 0;
  10955. ExtractLines(crop, out rightOrdinate1, rightTonghou[0] - 5, rightTonghou[0] + 5, 1);
  10956. double rightOrdinate2 = 0;
  10957. ExtractLines(crop, out rightOrdinate2, rightTonghou[0] - 5, rightTonghou[0] + 5, rightOrdinate1, -1);
  10958. leftTonghou[1] = (int)leftOrdinate1 + y[0];
  10959. leftTonghou[2] = (int)leftOrdinate2 + y[0];
  10960. rightTonghou[1] = (int)rightOrdinate1 + y[0];
  10961. rightTonghou[2] = (int)rightOrdinate2 + y[0];
  10962. }
  10963. /// <summary>
  10964. /// 计算高度差全的防焊厚度
  10965. /// </summary>
  10966. /// <param name="imageGreen">绿色通道图</param>
  10967. /// <param name="leftFanghanhou">左侧防焊厚度,0:横坐标;1:上纵坐标;2:下纵坐标</param>
  10968. /// <param name="rightFanghanhou">右侧防焊厚度,0:横坐标;1:上纵坐标;2:下纵坐标</param>
  10969. /// <param name="dataArea">提取区域</param>
  10970. /// <param name="tonghou">其中一个铜厚坐标</param>
  10971. public void GaoduchaFanhanhou(Mat imageGreen, out int[] leftFanghanhou, out int[] rightFanghanhou, int[] dataArea, int[] tonghou)
  10972. {
  10973. leftFanghanhou = new int[3];
  10974. rightFanghanhou = new int[3];
  10975. int range = 30;
  10976. int range2 = 80;
  10977. //将目标区域选出来
  10978. PointEnhancement(imageGreen, out imageGreen);
  10979. Cv2.GaussianBlur(imageGreen, imageGreen, new Size(5, 5), 3, 3);
  10980. Mat crop = imageGreen[tonghou[1] - range, tonghou[2] + range, dataArea[1] + range2, dataArea[2] - range2].Clone();
  10981. Mat contour = new Mat();
  10982. double t = Cv2.Threshold(crop, contour, 0, 1, ThresholdTypes.Otsu);
  10983. contour = 255 - crop.Threshold(t - 5, 255, ThresholdTypes.Binary);
  10984. Mat edge = new Mat();
  10985. Sobel(crop, out edge);
  10986. Mat thresh = edge.Threshold(0, 255, ThresholdTypes.Otsu);
  10987. //新增
  10988. Mat maxArea1 = new Mat();
  10989. GetMaxArea(contour, out maxArea1);
  10990. maxArea1 = maxArea1 * 255;
  10991. Mat and = new Mat();
  10992. Cv2.BitwiseAnd(thresh, maxArea1, and);
  10993. //ImageShow(contour, thresh,and);
  10994. //Mat delete = new Mat();
  10995. //GetArea(thresh, out delete, 10,true);
  10996. //delete = delete * 255;
  10997. //
  10998. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 2));
  10999. Mat close = new Mat();
  11000. Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
  11001. Mat fill = new Mat();
  11002. Fill(close, out fill, 255);
  11003. //ImageShow(thresh, close, fill);
  11004. Mat maxArea = new Mat();
  11005. GetMaxArea(fill, out maxArea);
  11006. Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 3));
  11007. Cv2.MorphologyEx(maxArea, close, MorphTypes.Close, seClose2);
  11008. Fill(close, out fill, 255);
  11009. Mat result = fill.Clone() / 255;
  11010. //得到提取点
  11011. Scalar sum = new Scalar(0);
  11012. for (int j = 0; j < result.Cols; j++)
  11013. {
  11014. sum = result[0, result.Rows, j, j + 40].Sum();
  11015. if ((int)sum > 2800)
  11016. {
  11017. leftFanghanhou[0] = j + 100;
  11018. break;
  11019. }
  11020. }
  11021. for (int j = result.Cols - 1; j > 0; j--)
  11022. {
  11023. sum = result[0, result.Rows, j - 40, j].Sum();
  11024. if ((int)sum > 2800)
  11025. {
  11026. rightFanghanhou[0] = j - 100;
  11027. break;
  11028. }
  11029. }
  11030. //计算高度
  11031. double leftOrdinate1 = 0;
  11032. ExtractLines(and, out leftOrdinate1, leftFanghanhou[0] - 5, leftFanghanhou[0] + 5, 1);
  11033. double leftOrdinate2 = 0;
  11034. and = and / 255;
  11035. for (int i = and.Rows - 1; i > and.Rows / 2; i--)
  11036. {
  11037. sum = and[i - 1, i, 0, leftFanghanhou[0]].Sum();
  11038. if ((int)sum > 5)
  11039. {
  11040. leftOrdinate2 = i;
  11041. break;
  11042. }
  11043. }
  11044. //ExtractLines(result, out leftOrdinate2, leftFanghanhou[0] - 5, leftFanghanhou[0] + 5,leftOrdinate1, -1);
  11045. //ExtractLines2(and, out leftOrdinate2, 5, 15, 1);
  11046. double rightOrdinate1 = 0;
  11047. ExtractLines(and, out rightOrdinate1, rightFanghanhou[0] - 5, rightFanghanhou[0] + 5, 1);
  11048. double rightOrdinate2 = 0;
  11049. for (int i = and.Rows - 1; i > and.Rows / 2; i--)
  11050. {
  11051. sum = and[i - 1, i, rightFanghanhou[0], and.Cols].Sum();
  11052. if ((int)sum > 5)
  11053. {
  11054. rightOrdinate2 = i;
  11055. break;
  11056. }
  11057. }
  11058. //ExtractLines(result, out rightOrdinate2, rightFanghanhou[0] - 5, rightFanghanhou[0] + 5, rightOrdinate1, -1);
  11059. //ExtractLines2(and, out rightOrdinate2, and.Cols-15, and.Cols- 5, 1);
  11060. leftFanghanhou[0] += dataArea[1] + range2;
  11061. leftFanghanhou[1] = (int)leftOrdinate1 + tonghou[1] - range;
  11062. leftFanghanhou[2] = (int)leftOrdinate2 + tonghou[1] - range;
  11063. rightFanghanhou[0] += dataArea[1] + range2;
  11064. rightFanghanhou[1] = (int)rightOrdinate1 + tonghou[1] - range;
  11065. rightFanghanhou[2] = (int)rightOrdinate2 + tonghou[1] - range;
  11066. //ImageShow(close*255, fill, maxArea * 255);
  11067. //ImageShow(result * 255);
  11068. }
  11069. public void GaoduchaFanhanhou2(Mat imageRed, out int[] leftFanghanhou, out int[] rightFanghanhou, int[] dataArea, int[] tonghou)
  11070. {
  11071. leftFanghanhou = new int[3];
  11072. rightFanghanhou = new int[3];
  11073. int range = 30;
  11074. int range2 = 80;
  11075. //将目标区域选出来
  11076. PointEnhancement(imageRed, out imageRed);
  11077. Cv2.GaussianBlur(imageRed, imageRed, new Size(9, 9), 3, 3);
  11078. Mat crop = imageRed[tonghou[1] - range, tonghou[2] + range, dataArea[1] + range2, dataArea[2] - range2].Clone();
  11079. Mat sobel = new Mat();
  11080. Sobel(crop, out sobel);
  11081. Mat thresh = new Mat();
  11082. double t = Cv2.Threshold(sobel, thresh, 0, 255, ThresholdTypes.Otsu);
  11083. thresh = sobel.Threshold(5, 255, ThresholdTypes.Binary);
  11084. Mat tangle = new Mat();
  11085. Cv2.Rectangle(thresh, new Rect(0, 0, 10, thresh.Rows), new Scalar(255), -1);
  11086. Cv2.Rectangle(thresh, new Rect(thresh.Cols - 11, 0, 10, thresh.Rows), new Scalar(255), -1);
  11087. //清楚小点
  11088. Mat noMin = new Mat();
  11089. GetArea(thresh, out noMin, 10, true);
  11090. //闭运算
  11091. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  11092. Mat close = new Mat();
  11093. Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
  11094. Mat max = new Mat();
  11095. GetMaxArea(close, out max);
  11096. //再次闭运算全连接
  11097. Mat seClsoe2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 3));
  11098. Mat close2 = new Mat();
  11099. Cv2.MorphologyEx(max, close2, MorphTypes.Close, seClsoe2);
  11100. //ImageShow( thresh,noMin*255,max*255,close2*255);
  11101. Mat result = close2.Clone();
  11102. //得到提取点
  11103. Scalar sum = new Scalar(0);
  11104. for (int j = 20; j < result.Cols; j++)
  11105. {
  11106. sum = result[0, result.Rows, j, j + 40].Sum();
  11107. if ((int)sum > 2800)
  11108. {
  11109. leftFanghanhou[0] = j + 100;
  11110. break;
  11111. }
  11112. }
  11113. for (int j = result.Cols - 21; j > 0; j--)
  11114. {
  11115. sum = result[0, result.Rows, j - 40, j].Sum();
  11116. if ((int)sum > 2800)
  11117. {
  11118. rightFanghanhou[0] = j - 100;
  11119. break;
  11120. }
  11121. }
  11122. //计算高度
  11123. for (int i = 0; i < result.Rows; i++)
  11124. {
  11125. sum = result[i, i + 1, leftFanghanhou[0] - 5, leftFanghanhou[0] + 5].Sum();
  11126. if ((int)sum > 5)
  11127. {
  11128. leftFanghanhou[1] = i;
  11129. break;
  11130. }
  11131. }
  11132. for (int i = result.Rows - 1; i > 0; i--)
  11133. {
  11134. sum = result[i - 1, i, leftFanghanhou[0] - 5, leftFanghanhou[0] + 5].Sum();
  11135. if ((int)sum > 5)
  11136. {
  11137. leftFanghanhou[2] = i;
  11138. break;
  11139. }
  11140. }
  11141. for (int i = 0; i < result.Rows; i++)
  11142. {
  11143. sum = result[i, i + 1, rightFanghanhou[0] - 5, rightFanghanhou[0] + 5].Sum();
  11144. if ((int)sum > 5)
  11145. {
  11146. rightFanghanhou[1] = i;
  11147. break;
  11148. }
  11149. }
  11150. for (int i = result.Rows - 1; i > 0; i--)
  11151. {
  11152. sum = result[i - 1, i, rightFanghanhou[0] - 5, rightFanghanhou[0] + 5].Sum();
  11153. if ((int)sum > 5)
  11154. {
  11155. rightFanghanhou[2] = i;
  11156. break;
  11157. }
  11158. }
  11159. int compensate = 13;
  11160. leftFanghanhou[0] += dataArea[1] + range2;
  11161. leftFanghanhou[1] += tonghou[1] - range;
  11162. leftFanghanhou[2] += tonghou[1] - range - compensate;
  11163. rightFanghanhou[0] += dataArea[1] + range2;
  11164. rightFanghanhou[1] += tonghou[1] - range;
  11165. rightFanghanhou[2] += tonghou[1] - range - compensate;
  11166. }
  11167. public void GaoduchaFanghanhoudu3(Mat image, out int[] leftFanghanhoudu, out int[] rightFanghanhoudu, int[] dataArea, int[] tonghou)
  11168. {
  11169. leftFanghanhoudu = new int[3];
  11170. rightFanghanhoudu = new int[3];
  11171. Mat gray = new Mat();
  11172. Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
  11173. Cv2.GaussianBlur(gray, gray, new Size(9, 9), 3, 3);
  11174. //Cv2.MedianBlur(gray, gray, 5);
  11175. Mat sobel = new Mat();
  11176. //Sobel(gray, out sobel);
  11177. EdgeY(gray, out sobel);
  11178. Mat sobelThresh = new Mat();
  11179. double t = Cv2.Threshold(sobel, sobelThresh, 0, 255, ThresholdTypes.Otsu);
  11180. sobelThresh = sobel.Threshold(10, 255, ThresholdTypes.Binary);
  11181. //Mat seClsoe = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  11182. //Mat close = new Mat();
  11183. //Cv2.MorphologyEx(sobelThresh, close, MorphTypes.Close, seClsoe);
  11184. Mat seErode = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
  11185. Mat erode = new Mat();
  11186. Cv2.Erode(sobelThresh, erode, seErode);
  11187. Mat crop = erode[tonghou[1] - 60, tonghou[2] + 40, dataArea[1], dataArea[2]].Clone();
  11188. Mat noMinArea = new Mat();
  11189. GetArea(crop, out noMinArea, 50, true);
  11190. Mat result = new Mat();
  11191. RemoveCircles(noMinArea, out result);
  11192. Scalar sum = new Scalar(0);
  11193. int startBorder = 0;
  11194. for (int i = 10; i < result.Rows - 2; i++)
  11195. {
  11196. sum = result[i, i + 2, 0, result.Cols].Sum();
  11197. if ((int)sum > 600)
  11198. {
  11199. startBorder = i;
  11200. break;
  11201. }
  11202. }
  11203. Cv2.Rectangle(result, new Rect(0, 0, result.Cols, startBorder), new Scalar(0), -1);
  11204. GetArea(result, out result, 2000, true);
  11205. //ImageShow(result * 255);
  11206. int middle = crop.Cols / 2;
  11207. leftFanghanhoudu[0] = middle - 150;
  11208. rightFanghanhoudu[0] = middle + 150;
  11209. double left1 = 0, left2 = 0;
  11210. double right1 = 0, right2 = 0;
  11211. ExtractLines(result, out left1, leftFanghanhoudu[0] - 10, leftFanghanhoudu[0] + 10, 1);
  11212. ExtractLines(result, out right1, rightFanghanhoudu[0] - 10, rightFanghanhoudu[0] + 10, 1);
  11213. //计算防焊下坐标
  11214. Mat edgeLower = new Mat();
  11215. Sobel(gray, out edgeLower);
  11216. Mat edgeLowerThresh = edgeLower.Threshold(5, 1, ThresholdTypes.Binary);
  11217. //ImageShow(edgeLowerThresh * 255);
  11218. Mat crop2 = edgeLowerThresh[tonghou[2] - 20, tonghou[2] + 40, dataArea[1], dataArea[2]].Clone();
  11219. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  11220. Mat close2 = new Mat();
  11221. Cv2.MorphologyEx(crop2, close2, MorphTypes.Close, seOpen);
  11222. Mat open = new Mat();
  11223. Cv2.MorphologyEx(close2, open, MorphTypes.Open, seOpen);
  11224. Mat bigArea = new Mat();
  11225. //GetMaxArea(open, out bigArea);
  11226. GetArea(open, out bigArea, 500, true);
  11227. //ImageShow(bigArea * 255);
  11228. //Mat cannyEdge = new Mat();
  11229. //Cv2.Canny(gray, cannyEdge, 18, 14);
  11230. //ImageShow(cannyEdge);
  11231. int bigAreaSum = (int)bigArea.Sum();
  11232. if (bigAreaSum < 5000)
  11233. bigArea = result[result.Rows - 60, result.Rows, 0, result.Cols].Clone();
  11234. ExtractLines2(bigArea, out left2, leftFanghanhoudu[0] - 10, leftFanghanhoudu[0] + 10, 1);
  11235. ExtractLines2(bigArea, out right2, rightFanghanhoudu[0] - 10, rightFanghanhoudu[0] + 10, 1);
  11236. int compensate1 = 10;
  11237. int compensate2 = 0;
  11238. leftFanghanhoudu[0] += dataArea[1];
  11239. leftFanghanhoudu[1] = (int)left1 + tonghou[1] - 60 + compensate1;
  11240. leftFanghanhoudu[2] = (int)left2 + tonghou[2] - 20 - compensate2;
  11241. rightFanghanhoudu[0] += dataArea[1];
  11242. rightFanghanhoudu[1] = (int)right1 + tonghou[1] - 60 + compensate1;
  11243. rightFanghanhoudu[2] = (int)right2 + tonghou[2] - 20 - compensate2;
  11244. //ImageShow(sobelThresh, crop, result * 255);
  11245. }
  11246. public void GaoduchaFanghanhoudu4(Mat image_0, out int[] leftFanghanhoudu, out int[] rightFanghanhoudu, int[] dataArea, int[] tonghou)
  11247. {
  11248. leftFanghanhoudu = new int[3];
  11249. rightFanghanhoudu = new int[3];
  11250. //防焊厚上层
  11251. Mat gray = new Mat();
  11252. Cv2.CvtColor(image_0, gray, ColorConversionCodes.BGR2GRAY);
  11253. //Mat filter = new Mat();
  11254. //PointEnhancement(gray, out filter);
  11255. Mat newGray = gray.Clone();// new Mat();//
  11256. //Cv2.GaussianBlur(gray, newGray, new Size(11, 11), 3, 3);
  11257. Mat blur = new Mat();
  11258. Cv2.MedianBlur(newGray, blur, 5);
  11259. Mat edge;
  11260. //Cv2.ImWrite(@"C:\Users\54434\Desktop\blur.png", blur);
  11261. EdgeY(blur, out edge);
  11262. //Edge(blur, out edge);
  11263. //Cv2.ImWrite(@"C:\Users\54434\Desktop\edge.png", edge);
  11264. int meanV = (int)(edge[tonghou[1] - 30, tonghou[2], dataArea[1], dataArea[2]].Mean().Val0);
  11265. if (meanV > 50) meanV -= 25;
  11266. else if (meanV > 18)
  11267. {
  11268. if ((int)(edge[tonghou[1], tonghou[1] / 2 - 15 + tonghou[2] / 2, dataArea[1], dataArea[2]].Mean().Val0) * 2 < meanV)
  11269. meanV = (int)(edge[tonghou[1], tonghou[1] / 2 - 15 + tonghou[2] / 2, dataArea[1], dataArea[2]].Mean().Val0) / 2;
  11270. else if (meanV > 20)
  11271. meanV /= 2;
  11272. else
  11273. meanV -= 5;
  11274. }
  11275. //meanV = 2;// (int)(edge[tonghou[1], tonghou[1] / 2 - 15 + tonghou[2] / 2, dataArea[1], dataArea[2]].Mean().Val0)/2;// 2;// (int)(edge[tonghou[1], tonghou[1] / 2 - 15 + tonghou[2] / 2, dataArea[1], dataArea[2]].Mean().Val0);
  11276. meanV = meanV < 13 ? meanV * 2 : meanV;
  11277. Mat thresh = edge.Threshold(meanV/*15*/, 255, ThresholdTypes.Binary);
  11278. Mat thresh2 = thresh.Clone();
  11279. //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh2_0.png", thresh2);
  11280. Cv2.Rectangle(thresh2, new Rect(0, 0, thresh.Cols, tonghou[1] - 30), new Scalar(0), -1);
  11281. Cv2.Rectangle(thresh2, new Rect(0, tonghou[2] + 20, thresh.Cols, thresh.Rows - tonghou[2] - 20), new Scalar(0), -1);
  11282. Cv2.Rectangle(thresh2, new Rect(0, tonghou[1] + 40, thresh.Cols, tonghou[2] - tonghou[1]), new Scalar(255), -1);
  11283. //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh2.png", thresh2);
  11284. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  11285. //Mat close = new Mat();
  11286. //Cv2.MorphologyEx(thresh2, close, MorphTypes.Close, seClose);
  11287. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  11288. Mat open = new Mat();
  11289. Cv2.MorphologyEx(thresh2, open, MorphTypes.Open, seOpen);
  11290. Mat max = new Mat();
  11291. //GetMaxArea(thresh2, out max);
  11292. //Cv2.ImWrite(@"C:\Users\54434\Desktop\open.png", open);
  11293. //GetMaxArea(open, out max/*thresh, out maxThresh*/);
  11294. GetArea(open, out max, 500, true);
  11295. //ImageShow(thresh, thresh2,open, max * 255);
  11296. Mat result = max.Clone();
  11297. int middle = (dataArea[1] + dataArea[2]) / 2;
  11298. leftFanghanhoudu[0] = middle - 120;// 150;
  11299. rightFanghanhoudu[0] = middle + 120;// 150;
  11300. Mat seOpen11 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(70/*15*/, 1/*水平线<--3, 3*/));// 开运算
  11301. //Mat open11 = new Mat();
  11302. ////OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
  11303. Cv2.MorphologyEx(result, result, MorphTypes.Open, seOpen11);
  11304. //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 120);
  11305. //if (result.Get<byte>(tonghou[2] - 10, leftFanghanhoudu[0]) > 0)
  11306. //{
  11307. // for (int i = tonghou[2] - 10; i > tonghou[1] - 30; i--)
  11308. // {
  11309. // if (result.Get<byte>(i, leftFanghanhoudu[0]) == 0)
  11310. // {
  11311. // leftFanghanhoudu[1] = i;
  11312. // break;
  11313. // }
  11314. // }
  11315. //}
  11316. //else
  11317. {
  11318. for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
  11319. {
  11320. if (result/*result*/.Get<byte>(i, leftFanghanhoudu[0]) > 0)
  11321. {
  11322. leftFanghanhoudu[1] = i;
  11323. if (max[i - 1, i, leftFanghanhoudu[0], rightFanghanhoudu[0]].Sum().Val0 >
  11324. (rightFanghanhoudu[0] - leftFanghanhoudu[0]) / 2 + 15
  11325. || Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
  11326. break;//对弧形的判定!
  11327. }
  11328. }
  11329. }
  11330. int upperLine = leftFanghanhoudu[1];
  11331. for (int i = leftFanghanhoudu[1]; i > leftFanghanhoudu[1] - 5; i--)
  11332. {
  11333. if (max.Get<byte>(i, leftFanghanhoudu[0]) > 0)
  11334. upperLine = i;
  11335. else break;
  11336. }
  11337. leftFanghanhoudu[1] = upperLine;
  11338. //if (result.Get<byte>(tonghou[2] - 10, rightFanghanhoudu[0]) > 0)
  11339. //{
  11340. // for (int i = tonghou[2] - 10; i > tonghou[1] - 30; i--)
  11341. // {
  11342. // if (result.Get<byte>(i, rightFanghanhoudu[0]) == 0)
  11343. // {
  11344. // rightFanghanhoudu[1] = i;
  11345. // break;
  11346. // }
  11347. // }
  11348. //}
  11349. //else
  11350. {
  11351. for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
  11352. {
  11353. if (result/*result*/.Get<byte>(i, rightFanghanhoudu[0]) > 0)
  11354. {
  11355. rightFanghanhoudu[1] = i;
  11356. if (max[i - 1, i, leftFanghanhoudu[0], rightFanghanhoudu[0]].Sum().Val0 >
  11357. (rightFanghanhoudu[0] - leftFanghanhoudu[0]) / 2 + 15
  11358. || Cv2.FloodFill(max, new Point(rightFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
  11359. break;//对弧形的判定!
  11360. }
  11361. }
  11362. }
  11363. upperLine = rightFanghanhoudu[1];
  11364. for (int i = rightFanghanhoudu[1]; i > rightFanghanhoudu[1] - 5; i--)
  11365. {
  11366. if (max.Get<byte>(i, rightFanghanhoudu[0]) > 0)
  11367. upperLine = i;
  11368. else break;
  11369. }
  11370. rightFanghanhoudu[1] = upperLine;
  11371. Scalar color = new Scalar(255/*2*//*127*//*255*/);
  11372. //颜色
  11373. Cv2.Line(gray, new Point(leftFanghanhoudu[0], leftFanghanhoudu[1]), new Point(leftFanghanhoudu[0], leftFanghanhoudu[2]), color, 2, LineTypes.Link8);
  11374. Cv2.Line(gray, new Point(rightFanghanhoudu[0], rightFanghanhoudu[1]), new Point(rightFanghanhoudu[0], rightFanghanhoudu[2]), color, 2, LineTypes.Link8);
  11375. Rect rectMin = new Rect(Math.Max(0, leftFanghanhoudu[0] - 130), Math.Max(0, leftFanghanhoudu[1] - 200),
  11376. Math.Min(max.Cols-1, rightFanghanhoudu[0]+130)- Math.Max(0, leftFanghanhoudu[0] - 130),
  11377. Math.Min(max.Rows-1, leftFanghanhoudu[1]+30)- Math.Max(0, leftFanghanhoudu[1] - 200));
  11378. Cv2.Rectangle(gray, rectMin, color, 1);
  11379. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray/*max * 127*//*1*//*120*/);
  11380. int[] compensate1 = new int[] { 10, 10 };
  11381. //去掉球 以及 微调补偿数值
  11382. Mat roundTemp = image_0[Math.Max(0, leftFanghanhoudu[1] - 200), Math.Min(max.Rows - 1, leftFanghanhoudu[1] + 30),
  11383. Math.Max(0, leftFanghanhoudu[0] - 130), Math.Min(max.Cols - 1, rightFanghanhoudu[0] + 130)].CvtColor(ColorConversionCodes.BGR2GRAY);
  11384. //meanV = (int)(roundTemp.Mean().Val0);
  11385. //Mat mat_er = roundTemp.Threshold(meanV/*15*/, 255, ThresholdTypes.Binary);
  11386. CircleSegment[] circles = Cv2.HoughCircles(roundTemp, HoughMethods.Gradient, 1, 30, 70, 30, 10, 60/*60*/);//, dp, minDist, param1, param2, minRadius, maxRadius);
  11387. //CircleSegment[] circleSegment;
  11388. //Base.AutoMeasure.Ceju.RemoveCircles(image1, image2, out result, 1, 30, 70, 30, 10, 60);
  11389. if (circles.Length > 0)
  11390. {
  11391. int range = 10;// 5;// 10;
  11392. for (int ic = 0; ic < circles.Length; ic++)
  11393. {
  11394. Point center = (Point)circles[ic].Center;
  11395. int radius = (int)circles[ic].Radius;
  11396. //for(int j=1;j<radius;j++)
  11397. //Cv2.Circle(result, center, j, new Scalar(1));
  11398. Cv2.Rectangle(roundTemp, new Rect(center.X - radius - range, center.Y - radius - range, 2 * (radius + range), 2 * (radius + range)), new Scalar(255/*1*/), /*-*/1);
  11399. Cv2.Rectangle(max, new Rect(Math.Max(0, leftFanghanhoudu[0] - 130) + center.X - radius - range, Math.Max(0, leftFanghanhoudu[1] - 200) + center.Y - radius - range, 2 * (radius + range), 2 * (radius + range)), new Scalar(0), -1);
  11400. }
  11401. }
  11402. else
  11403. {
  11404. Mat tempEr = roundTemp.Threshold(/*15*/(int)(roundTemp.Mean().Val0) - 5, 255, ThresholdTypes.BinaryInv);
  11405. ////GetMaxArea(tempEr, out tempEr); tempEr = tempEr * 255;
  11406. //Cv2.ImWrite(@"C:\Users\54434\Desktop\tempEr.png", tempEr/*max * 120*//*roundTemp*/);
  11407. circles = Cv2.HoughCircles(tempEr, HoughMethods.Gradient, 1, 30, 70, 30, 10, 90/*60*/);//, dp, minDist, param1, param2, minRadius, maxRadius);
  11408. //Base.AutoMeasure.Ceju.RemoveCircles(image1, image2, out result, 1, 30, 70, 30, 10, 60);
  11409. if (circles.Length > 0)
  11410. {
  11411. int range = 0;// 5;// 10;
  11412. for (int ic = 0; ic < circles.Length; ic++)
  11413. {
  11414. Point center = (Point)circles[ic].Center;
  11415. int radius = (int)circles[ic].Radius;
  11416. //for(int j=1;j<radius;j++)
  11417. //Cv2.Circle(result, center, j, new Scalar(1));
  11418. Cv2.Rectangle(roundTemp, new Rect(center.X - radius - range, center.Y - radius - range, 2 * (radius + range), 2 * (radius + range)), new Scalar(255/*1*/), /*-*/1);
  11419. Cv2.Rectangle(max, new Rect(Math.Max(0, leftFanghanhoudu[0] - 130) + center.X - radius - range, Math.Max(0, leftFanghanhoudu[1] - 200) + center.Y - radius - range, 2 * (radius + range), 2 * (radius + range)), new Scalar(0), -1);
  11420. }
  11421. }
  11422. }
  11423. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", roundTemp/*max * 120*//*roundTemp*/);
  11424. //{
  11425. // for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
  11426. // {
  11427. // if (result/*result*/.Get<byte>(i, leftFanghanhoudu[0]) > 0)
  11428. // {
  11429. // leftFanghanhoudu[1] = i;
  11430. // if (max[i - 1, i, leftFanghanhoudu[0], rightFanghanhoudu[0]].Sum().Val0 >
  11431. // (rightFanghanhoudu[0] - leftFanghanhoudu[0]) / 2 + 15
  11432. // || Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
  11433. // break;//对弧形的判定!
  11434. // }
  11435. // }
  11436. //}
  11437. int comp = 5;
  11438. if (rightFanghanhoudu[1] > leftFanghanhoudu[1] + 10)
  11439. comp = 10;
  11440. upperLine = leftFanghanhoudu[1] + comp;
  11441. for (int i = leftFanghanhoudu[1]; i < leftFanghanhoudu[1] + comp; i++)
  11442. {
  11443. if (max.Get<byte>(i, leftFanghanhoudu[0]) > 0)
  11444. {
  11445. upperLine = i;
  11446. break;
  11447. }
  11448. }
  11449. leftFanghanhoudu[1] = upperLine;
  11450. if (rightFanghanhoudu[1] < leftFanghanhoudu[1])
  11451. {
  11452. comp = 5;
  11453. upperLine = rightFanghanhoudu[1] + 5;
  11454. for (int i = rightFanghanhoudu[1]; i < rightFanghanhoudu[1] + 5; i++)
  11455. {
  11456. if (max.Get<byte>(i, rightFanghanhoudu[0]) > 0)
  11457. {
  11458. upperLine = i;
  11459. break;
  11460. }
  11461. }
  11462. rightFanghanhoudu[1] = upperLine;
  11463. }
  11464. //{
  11465. // for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
  11466. // {
  11467. // if (result/*result*/.Get<byte>(i, rightFanghanhoudu[0]) > 0)
  11468. // {
  11469. // rightFanghanhoudu[1] = i;
  11470. // if (max[i - 1, i, leftFanghanhoudu[0], rightFanghanhoudu[0]].Sum().Val0 >
  11471. // (rightFanghanhoudu[0] - leftFanghanhoudu[0]) / 2 + 15
  11472. // || Cv2.FloodFill(max, new Point(rightFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
  11473. // break;//对弧形的判定!
  11474. // }
  11475. // }
  11476. //}
  11477. ////Mat roundGray = new Mat();
  11478. ////Cv2.CvtColor(roundTemp, roundGray, ColorConversionCodes.BGR2GRAY);
  11479. ////int mean0; Scalar mean_rgb;
  11480. //////去掉圆并灰度化,怎么能更快?
  11481. ////Scalar mean = roundGray.Mean(/*gray*/);
  11482. ////mean0 = (int)mean[0];
  11483. ////mean_rgb = roundTemp/*srcImage*/.Mean();
  11484. /////*final = */
  11485. ////bool foundCircle;
  11486. ////roundTemp = FangHanTools.RemoveCircle(roundTemp, mean0, mean_rgb, out foundCircle);//.CvtColor(ColorConversionCodes.BGR2GRAY);
  11487. ///////*Mat roundTemp = */FangHanTools.RemoveCircle(roundTemp, out mean0, out roundTemp);
  11488. //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", roundTemp/*max * 127*//*1*//*120*/);
  11489. ////通过连接区域的大小判定是否在圆球范围内
  11490. //int fillArea1 = 0;
  11491. //for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
  11492. //{
  11493. // if (result.Get<byte>(i, leftFanghanhoudu[0]) > 0)
  11494. // {
  11495. // fillArea1 = Cv2.FloodFill(result.Clone()/*, mask1*/, new Point(leftFanghanhoudu[0], i), new Scalar(255/*127*//*255*/));
  11496. // leftFanghanhoudu[1] = i;
  11497. // break;
  11498. // }
  11499. //}
  11500. //Console.WriteLine("fillArea1:" + fillArea1);
  11501. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 120);
  11502. ////通过连接区域的大小判定是否在圆球范围内
  11503. //int fillArea2 = 0;
  11504. //for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
  11505. //{
  11506. // if (result.Get<byte>(i, rightFanghanhoudu[0]) > 0)
  11507. // {
  11508. // fillArea2 = Cv2.FloodFill(result.Clone()/*, mask1*/, new Point(rightFanghanhoudu[0], i), new Scalar(255/*127*//*255*/));
  11509. // rightFanghanhoudu[1] = i;
  11510. // break;
  11511. // }
  11512. //}
  11513. //Console.WriteLine("fillArea2:" + fillArea2);
  11514. if (Math.Abs(leftFanghanhoudu[1] - rightFanghanhoudu[1]) > 100)
  11515. {
  11516. if (leftFanghanhoudu[1] < rightFanghanhoudu[1])
  11517. leftFanghanhoudu[1] = rightFanghanhoudu[1];
  11518. else
  11519. rightFanghanhoudu[1] = leftFanghanhoudu[1];
  11520. }
  11521. //if (fillArea1 != fillArea2)
  11522. //{
  11523. // if (leftFanghanhoudu[1] < rightFanghanhoudu[1])
  11524. // leftFanghanhoudu[1] = rightFanghanhoudu[1];
  11525. // else
  11526. // rightFanghanhoudu[1] = leftFanghanhoudu[1];
  11527. //}
  11528. //防焊厚下层
  11529. Mat newGray2 = new Mat();
  11530. Cv2.GaussianBlur(gray, newGray2, new Size(15, 15), 5, 5);
  11531. Mat filter2 = new Mat();
  11532. Cv2.MedianBlur(newGray2, filter2, 3);
  11533. Mat edge2 = new Mat();
  11534. Edge(filter2, out edge2);
  11535. Mat thresh3 = new Mat();
  11536. thresh3 = edge2.Threshold(15, 255, ThresholdTypes.Binary);
  11537. Mat thresh4 = thresh3.Clone();
  11538. Cv2.Rectangle(thresh4, new Rect(0, 0, thresh.Cols, tonghou[2] - 30), new Scalar(255), -1);
  11539. Cv2.Rectangle(thresh4, new Rect(0, tonghou[2] + 40, thresh.Cols, thresh.Rows - tonghou[2] - 40), new Scalar(0), -1);
  11540. Mat seOpen2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  11541. Mat open2 = new Mat();
  11542. Cv2.MorphologyEx(thresh4, open2, MorphTypes.Open, seOpen2);
  11543. Mat crop2 = open2[tonghou[2] - 20, tonghou[2] + 20, leftFanghanhoudu[0], rightFanghanhoudu[0]] / 255;
  11544. int openSum = (int)crop2.Sum();
  11545. int compensate2 = 8;
  11546. if (openSum < 3000)
  11547. {
  11548. compensate2 = 0;
  11549. Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 3));
  11550. Cv2.MorphologyEx(thresh4, open2, MorphTypes.Close, seClose2);
  11551. }
  11552. Mat max2 = new Mat();
  11553. GetMaxArea(thresh2, out max2);
  11554. GetArea(open2, out max2, 500, true);
  11555. //ImageShow(thresh3, thresh4, max2 * 255);
  11556. //Cv2.ImWrite(@"C:\Users\54434\Desktop\max2.png", max2 * 255);
  11557. Mat result2 = max2.Clone();
  11558. //防焊厚上层
  11559. Mat gray_1 = new Mat();
  11560. Cv2.CvtColor(image_0, gray_1, ColorConversionCodes.BGR2GRAY);
  11561. Scalar sum2 = new Scalar(0);
  11562. for (int i = tonghou[2] + 20; i > tonghou[2] - 20; i--)
  11563. {
  11564. sum2 = result2[i - 1, i, leftFanghanhoudu[0] - 50, leftFanghanhoudu[0] + 50].Sum();
  11565. if ((int)sum2 > 70)
  11566. //if(result2.Get<byte>(i,leftFanghanhoudu[0])>0)
  11567. {
  11568. leftFanghanhoudu[2] = i;
  11569. break;
  11570. }
  11571. }
  11572. if (leftFanghanhoudu[2] == 0)
  11573. leftFanghanhoudu[2] = tonghou[2];
  11574. for (int i = tonghou[2] + 20; i > tonghou[2] - 20; i--)
  11575. {
  11576. sum2 = result2[i - 1, i, rightFanghanhoudu[0] - 50, rightFanghanhoudu[0] + 50].Sum();
  11577. if ((int)sum2 > 70)
  11578. //if(result2.Get<byte>(i, rightFanghanhoudu[0]) > 0)
  11579. {
  11580. rightFanghanhoudu[2] = i;
  11581. break;
  11582. }
  11583. }
  11584. if (rightFanghanhoudu[2] == 0)
  11585. rightFanghanhoudu[2] = tonghou[2];
  11586. //for (int i = tonghou[2] + 20; i > tonghou[2] - 20; i--)
  11587. //{
  11588. // sum2 = result2[i - 1, i, rightFanghanhoudu[0] - 50, rightFanghanhoudu[0] + 50].Sum();
  11589. // if ((int)sum2 > 60)
  11590. // {
  11591. // rightFanghanhoudu[2] = i;
  11592. // break;
  11593. // }
  11594. //}
  11595. //int compensate1 = 10;
  11596. leftFanghanhoudu[1] += compensate1[0];
  11597. rightFanghanhoudu[1] += compensate1[1];
  11598. int fanghanX1 = rightFanghanhoudu[0] - 15;// 10;// += 140;// 145;
  11599. int fanghanX2 = rightFanghanhoudu[0] + 15;// 10;// -= 140;// 145;
  11600. int leftY0 = leftFanghanhoudu[1];
  11601. int rightY0 = rightFanghanhoudu[1];
  11602. {
  11603. int fanghanhouduY1 = leftFanghanhoudu[1];
  11604. int minGray__2 = 255 * 300;// minGray;
  11605. int fanghanTop = fanghanhouduY1 - 25;// 20;// 25;// 20;// 10;
  11606. int fanghanhouduY_0 = fanghanhouduY1 + 60/*50*/ + 20;
  11607. Mat grayRect = gray_1[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
  11608. int fanghanhouduY1__2_bottom;
  11609. fanghanhouduY1 -= fanghanTop;
  11610. int fanghanhouduY1__2 = fanghanhouduY1;
  11611. Gaoduchahoudu_ACC(grayRect, fanghanhouduY1 - 0/*19*//*15*//*5*//*(isLeft ? 8 : 1)*/, out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2, 0);
  11612. if (true && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 10/* <<15 16*//*11*//*<-10*//*20*//*10*/
  11613. || (fanghanhouduY1 - fanghanhouduY1__2 > 0 && fanghanhouduY1 - fanghanhouduY1__2 < 12/* <<15.12 <<10 *//*10*//*20*/))
  11614. //|| (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/))
  11615. fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
  11616. else
  11617. fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
  11618. Console.WriteLine("leftFanghanhoudu[1]:" + leftFanghanhoudu[1] + ";fanghanhouduY1:" + fanghanhouduY1);
  11619. leftFanghanhoudu[1] = fanghanhouduY1;
  11620. }
  11621. {
  11622. int fanghanhouduY1 = rightFanghanhoudu[1];
  11623. int minGray__2 = 255 * 300;// minGray;
  11624. int fanghanTop = fanghanhouduY1 - 25;// 20;// 25;// 20;// 10;
  11625. int fanghanhouduY_0 = fanghanhouduY1 + 60/*50*/ + 20;
  11626. Mat grayRect = gray_1[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
  11627. int fanghanhouduY1__2_bottom;
  11628. fanghanhouduY1 -= fanghanTop;
  11629. int fanghanhouduY1__2 = fanghanhouduY1;
  11630. Gaoduchahoudu_ACC(grayRect, fanghanhouduY1 - 0/*19*//*15*//*5*//*(isLeft ? 8 : 1)*/, out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2, 1);
  11631. if (true && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 10/* <<15 16*//*11*//*<-10*//*20*//*10*/
  11632. || (fanghanhouduY1 - fanghanhouduY1__2 > 0 && fanghanhouduY1 - fanghanhouduY1__2 < 12/* <<15.12 <<10 *//*10*//*20*/))
  11633. //|| (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/))
  11634. fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
  11635. else
  11636. fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
  11637. Console.WriteLine("rightFanghanhoudu[1]:" + rightFanghanhoudu[1] + ";fanghanhouduY1:" + fanghanhouduY1);
  11638. rightFanghanhoudu[1] = fanghanhouduY1;
  11639. }
  11640. if (Math.Abs(leftFanghanhoudu[1] - rightFanghanhoudu[1]) - Math.Abs(leftY0 - rightY0) > 2)
  11641. {//纠错2(1).JPG
  11642. if (leftY0 == leftFanghanhoudu[1])
  11643. rightFanghanhoudu[1] = rightY0;
  11644. else if (rightY0 == rightFanghanhoudu[1])
  11645. if (Math.Abs(rightY0 - leftY0) > 10)
  11646. {
  11647. leftFanghanhoudu[1] = rightY0;// leftY0;
  11648. }
  11649. else
  11650. leftFanghanhoudu[1] = leftY0;
  11651. }
  11652. //Mat grayRect = gray_1[leftFanghanhoudu[1] - 10/*fanghanTop*/, leftFanghanhoudu[1] + 50/*fanghanhouduY_0 - 50*/, leftFanghanhoudu[0] - 10, leftFanghanhoudu[0] + 10];
  11653. //Gaoduchahoudu_ACC(grayRect/*gray*/, y, marginTop/*150*//*fanghanhouduX*/, tonghouY, out fanghanhouduY1, out minGray);
  11654. //int fanghanhouduY1__2 = fanghanhouduY1;
  11655. //int minGray__2 = minGray;
  11656. //fanghanX1 += 140;// 145;
  11657. //fanghanX2 -= 140;// 145;
  11658. //grayRect = gray[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
  11659. //int fanghanhouduY1__2_bottom;
  11660. //FanghanhouduForYouKaiKou_ACC(grayRect, y, marginTop, fanghanhouduY1 - (isLeft ? 8/*5*/ : 1), out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2);
  11661. //if (true && (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 16/*11*//*<-10*//*20*//*10*/
  11662. // || (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/)))
  11663. // fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
  11664. //else
  11665. // fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
  11666. leftFanghanhoudu[2] -= compensate2;
  11667. rightFanghanhoudu[2] -= compensate2;
  11668. }
  11669. /// <summary>
  11670. /// 防焊 有开口 厚度 精确计算
  11671. /// </summary>
  11672. /// <param name="gray"></param>
  11673. /// <param name="y"></param>
  11674. /// <param name="b"></param>
  11675. /// <param name="tonghouY"></param>
  11676. /// <param name="fanghanhouduY"></param>
  11677. /// <param name="a"></param>
  11678. private void Gaoduchahoudu_ACC(Mat gray0, int fanghanhouduY1__0, out int fanghanhouduY1, out int fanghanhouduY1Bottom, out int minGray, int a = 0)
  11679. {
  11680. int bottomYDistance = 15;// 30;
  11681. int fanghanhouduY1__noSharp = -1;// fanghanhouduY1;
  11682. {
  11683. minGray = 300 * 255;
  11684. int minRowIndex = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  11685. fanghanhouduY1Bottom = 0;
  11686. for (int i = Math.Max(0, fanghanhouduY1__0 - 19/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + bottomYDistance/*25*/, gray0.Rows) - 5; i++)
  11687. {
  11688. curGray = this.FanghanhouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  11689. curGrayList.Add(curGray);
  11690. if (curGray < minGray)
  11691. {
  11692. minRowIndex = i;
  11693. fanghanhouduY1Bottom = i;
  11694. minGray = curGray;
  11695. }
  11696. }
  11697. for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 19/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i += 2)
  11698. {
  11699. if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
  11700. {
  11701. minRowIndex += 1;
  11702. fanghanhouduY1Bottom += 2;
  11703. }
  11704. else
  11705. break;
  11706. }
  11707. fanghanhouduY1__noSharp = minRowIndex;// 84;// 72;// minRowIndex;
  11708. }
  11709. {
  11710. minGray = 300 * 255;
  11711. //锐化
  11712. //Mat left_small_sharp = BinaryTools.BlurMaskFunction(left_small).CvtColor(ColorConversionCodes.BGRA2GRAY);
  11713. Mat gray = BinaryTools.BlurMaskFunction(gray0.Clone()/*grayRect*/, 4f * 3.14f, 1, 10f).CvtColor(ColorConversionCodes.BGRA2GRAY);
  11714. //Cv2.ImWrite(@"C:\Users\win10SSD\Desktop\BlurMask" + a + "_.jpg", gray);
  11715. int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  11716. fanghanhouduY1Bottom = 0;
  11717. for (int i = Math.Max(0, fanghanhouduY1__0 - 19/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + bottomYDistance/*25*/, gray.Rows) - 5; i++)
  11718. {
  11719. curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  11720. curGrayList.Add(curGray);
  11721. if (curGray < minGray)
  11722. {
  11723. minRowIndex = i;
  11724. fanghanhouduY1Bottom = i;
  11725. minGray = curGray;
  11726. }
  11727. }
  11728. for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 19/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i += 2)
  11729. {
  11730. if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
  11731. {
  11732. minRowIndex += 1;
  11733. fanghanhouduY1Bottom += 2;
  11734. }
  11735. else
  11736. break;
  11737. }
  11738. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  11739. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  11740. fanghanhouduY1 = minRowIndex;// 84;// 72;// minRowIndex;
  11741. }
  11742. Console.Write("noSharp:" + fanghanhouduY1__noSharp + ";fanghanhouduY1:" + fanghanhouduY1 +"...");
  11743. if (Math.Abs(fanghanhouduY1__noSharp - fanghanhouduY1) < 7/* <<7 8*//* << 6 *//*5*/
  11744. || (fanghanhouduY1__noSharp - fanghanhouduY1 > 0 && fanghanhouduY1__noSharp - fanghanhouduY1 < 20))
  11745. {
  11746. fanghanhouduY1 = fanghanhouduY1__noSharp;
  11747. }
  11748. else
  11749. Console.WriteLine("fanghanhouduY1 far away from fanghanhouduY1__noSharp.");
  11750. }
  11751. //叠构
  11752. /// <summary>
  11753. /// 得到叠构有导电布时铜层的数量以及当铜层大于二时的平均厚度
  11754. /// </summary>
  11755. /// <param name="gray"></param>
  11756. /// <param name="tongshu"></param>
  11757. /// <param name="tonghou"></param>
  11758. public void GetTongCengShuliang(Mat gray, out int tongshu, out int tonghou, out int jianhou)
  11759. {
  11760. tongshu = 0;
  11761. tonghou = 0;
  11762. jianhou = 0;
  11763. Mat thresh = new Mat();
  11764. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  11765. //ImageShow(thresh * 255);
  11766. Mat nomin = new Mat();//去除小点防止干扰
  11767. GetArea(thresh, out nomin, 500, true);
  11768. Mat result = nomin.Clone();
  11769. int[] y = new int[8];
  11770. int middle = thresh.Cols / 2;//根据中间选取部分范围防止倾斜的干扰
  11771. Scalar sum = new Scalar(0);
  11772. for (int i = 0; i < thresh.Rows; i++)
  11773. {
  11774. sum = result[i, i + 1, middle - 100, middle + 100].Sum();
  11775. if ((int)sum > 0)
  11776. {
  11777. y[0] = i;
  11778. break;
  11779. }
  11780. }
  11781. for (int i = y[0]; i < thresh.Rows; i++)
  11782. {
  11783. sum = result[i, i + 1, middle - 100, middle + 100].Sum();
  11784. if ((int)sum == 0)
  11785. {
  11786. y[1] = i;
  11787. break;
  11788. }
  11789. }
  11790. for (int i = y[1]; i < thresh.Rows; i++)
  11791. {
  11792. sum = result[i, i + 1, middle - 100, middle + 100].Sum();
  11793. if ((int)sum > 0)
  11794. {
  11795. y[2] = i;
  11796. break;
  11797. }
  11798. }
  11799. if (y[2] != 0)
  11800. {
  11801. for (int i = y[2]; i < thresh.Rows; i++)
  11802. {
  11803. sum = result[i, i + 1, middle - 100, middle + 100].Sum();
  11804. if ((int)sum == 0)
  11805. {
  11806. y[3] = i;
  11807. break;
  11808. }
  11809. }
  11810. for (int i = y[3]; i < thresh.Rows; i++)
  11811. {
  11812. sum = result[i, i + 1, middle - 100, middle + 100].Sum();
  11813. if ((int)sum > 0)
  11814. {
  11815. y[4] = i;
  11816. break;
  11817. }
  11818. }
  11819. if (y[4] != 0)
  11820. {
  11821. for (int i = y[4]; i < thresh.Rows; i++)
  11822. {
  11823. sum = result[i, i + 1, middle - 100, middle + 100].Sum();
  11824. if ((int)sum == 0)
  11825. {
  11826. y[5] = i;
  11827. break;
  11828. }
  11829. }
  11830. for (int i = y[5]; i < thresh.Rows; i++)
  11831. {
  11832. sum = result[i, i + 1, middle - 100, middle + 100].Sum();
  11833. if ((int)sum > 0)
  11834. {
  11835. y[6] = i;
  11836. break;
  11837. }
  11838. }
  11839. if (y[6] != 0)
  11840. {
  11841. for (int i = y[6]; i < thresh.Rows; i++)
  11842. {
  11843. sum = result[i, i + 1, middle - 100, middle + 100].Sum();
  11844. if ((int)sum == 0)
  11845. {
  11846. y[7] = i;
  11847. break;
  11848. }
  11849. }
  11850. }
  11851. }
  11852. }
  11853. if (y[2] == 0)
  11854. tongshu = 1;
  11855. else if (y[4] == 0)
  11856. tongshu = 2;
  11857. else if (y[6] == 0)
  11858. tongshu = 3;
  11859. else
  11860. tongshu = 4;
  11861. if (tongshu == 1)
  11862. {
  11863. tonghou = y[1] - y[0];
  11864. }
  11865. else if (tongshu == 2)
  11866. {
  11867. tonghou = (y[1] - y[0] + y[3] - y[2]) / 2;
  11868. jianhou = y[2] - y[1];
  11869. }
  11870. }
  11871. /// <summary>
  11872. /// 提取有导电布时单层铜的坐标
  11873. /// </summary>
  11874. /// <param name="gray"></param>
  11875. /// <param name="ordinates"></param>
  11876. public void DaoidanbuDanceng(Mat gray, out List<int> ordinates)
  11877. {
  11878. ordinates = new List<int>();
  11879. //图片处理
  11880. //无预处理图片边缘检测+阈值分割+去小面积用来提取导电布
  11881. //高斯滤波+边缘检测+阈值分割+铜区域闭运算-反色-掩膜-+掩膜+去小面积提取中间线条
  11882. //无预处理
  11883. Mat sobel1 = new Mat();
  11884. EdgeY2(gray, out sobel1);
  11885. Mat threshSobel1 = new Mat();
  11886. threshSobel1 = sobel1.Threshold(100, 1, ThresholdTypes.Binary);
  11887. Mat nomin1 = new Mat();
  11888. GetArea(threshSobel1, out nomin1, 500, true);
  11889. Mat result1 = nomin1.Clone();
  11890. //ImageShow(result1 * 255);
  11891. //有预处理高斯滤波
  11892. Mat filter = new Mat();
  11893. Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
  11894. Mat sobel = new Mat();
  11895. EdgeY2(filter, out sobel);
  11896. Mat threshSobel = new Mat();
  11897. threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
  11898. Mat thresh = new Mat();
  11899. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  11900. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  11901. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  11902. Mat fanse = 1 - thresh;
  11903. Mat and = new Mat();
  11904. Cv2.BitwiseAnd(fanse, threshSobel, and);
  11905. Mat nomin = new Mat();
  11906. GetArea(and, out nomin, 500, true);
  11907. //ImageShow(threshSobel * 255, thresh * 255, and * 255,nomin*255);
  11908. //提取坐标
  11909. Mat result = nomin.Clone();
  11910. int middle = result.Cols / 2;
  11911. Scalar sum = new Scalar();
  11912. for (int i = 0; i < result.Rows - 1; i++)
  11913. {
  11914. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  11915. if ((int)sum > 100)
  11916. {
  11917. int newOrdinate = i;
  11918. ordinates.Add(newOrdinate);
  11919. i += 30;
  11920. }
  11921. }
  11922. if (ordinates[1] - ordinates[0] < 50)
  11923. ordinates.RemoveAt(1);
  11924. for (int i = ordinates[0] + 10; i < result.Rows; i++)
  11925. {
  11926. sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
  11927. if ((int)sum == 0)
  11928. {
  11929. int newOrdinate = i;
  11930. ordinates.Insert(1, newOrdinate);
  11931. break;
  11932. }
  11933. }
  11934. //if (ordinates[ordinates.Count - 1] - ordinates[ordinates.Count - 2] < 40)
  11935. if (ordinates.Count == 7 || ordinates.Count == 9)
  11936. ordinates.RemoveAt(ordinates.Count - 1);
  11937. for (int i = ordinates[ordinates.Count - 1] + 10; i < result.Rows; i++)
  11938. {
  11939. sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
  11940. if ((int)sum == 0)
  11941. {
  11942. int newOrdinate = i;
  11943. ordinates.Add(newOrdinate);
  11944. break;
  11945. }
  11946. }
  11947. }
  11948. /// <summary>
  11949. /// 得到叠构的总厚
  11950. /// </summary>
  11951. /// <param name="gray"></param>
  11952. /// <param name="zonghou"></param>
  11953. public void DiegouGetZonghou(Mat gray, out int zonghou)
  11954. {
  11955. zonghou = 0;
  11956. Mat sobel = new Mat();
  11957. EdgeY2(gray, out sobel);
  11958. Mat thresh = sobel.Threshold(100, 1, ThresholdTypes.Binary);
  11959. Mat nomin = new Mat();
  11960. GetArea(thresh, out nomin, 500, true);
  11961. Scalar sum = new Scalar();
  11962. int[] y = new int[2];
  11963. int middle = nomin.Cols / 2;
  11964. for (int i = 0; i < thresh.Rows - 1; i++)
  11965. {
  11966. sum = nomin[i, i + 1, middle - 100, middle + 100].Sum();
  11967. if ((int)sum > 100)
  11968. {
  11969. y[0] = i;
  11970. break;
  11971. }
  11972. }
  11973. for (int i = thresh.Rows - 1; i > 1; i--)
  11974. {
  11975. sum = nomin[i - 1, i, middle - 100, middle + 100].Sum();
  11976. if ((int)sum > 100)
  11977. {
  11978. y[1] = i;
  11979. break;
  11980. }
  11981. }
  11982. zonghou = y[1] - y[0];
  11983. }
  11984. /// <summary>
  11985. /// 无导电布单层时测总厚
  11986. /// </summary>
  11987. /// <param name="gray"></param>
  11988. /// <param name="zonghou"></param>
  11989. public void DiegouGetZonghou2(Mat gray, out int zonghou)
  11990. {
  11991. zonghou = 0;
  11992. Mat sobel = new Mat();
  11993. EdgeY2(gray, out sobel);
  11994. Mat thresh = sobel.Threshold(40, 1, ThresholdTypes.Binary);
  11995. Mat nomin = new Mat();
  11996. GetArea(thresh, out nomin, 500, true);
  11997. //ImageShow(nomin * 255);
  11998. Scalar sum = new Scalar();
  11999. int[] y = new int[2];
  12000. int middle = nomin.Cols / 2;
  12001. for (int i = 0; i < thresh.Rows - 1; i++)
  12002. {
  12003. sum = nomin[i, i + 1, 0, nomin.Cols].Sum();
  12004. if ((int)sum > 100)
  12005. {
  12006. y[0] = i;
  12007. break;
  12008. }
  12009. }
  12010. for (int i = thresh.Rows - 1; i > 1; i--)
  12011. {
  12012. sum = nomin[i - 1, i, 0, nomin.Cols].Sum();
  12013. if ((int)sum > 100)
  12014. {
  12015. y[1] = i;
  12016. break;
  12017. }
  12018. }
  12019. zonghou = y[1] - y[0];
  12020. if (zonghou > 600)//如果大于600,说明是亮图由于阈值太低而出现干扰,划到亮图里面去
  12021. {
  12022. zonghou = 200;
  12023. }
  12024. }
  12025. /// <summary>
  12026. /// 提取有导电布双层铜总厚大时的坐标
  12027. /// </summary>
  12028. /// <param name="gray"></param>
  12029. /// <param name="ordinates"></param>
  12030. public void DaodianbuShuangcengZongHou(Mat gray, out List<int> ordinates)
  12031. {
  12032. ordinates = new List<int>();
  12033. //图片处理
  12034. //无预处理图片边缘检测+阈值分割+去小面积用来提取导电布
  12035. //高斯滤波+边缘检测+阈值分割+铜区域闭运算-反色-掩膜-+掩膜+去小面积提取中间线条
  12036. //无预处理
  12037. Mat sobel1 = new Mat();
  12038. EdgeY2(gray, out sobel1);
  12039. Mat threshSobel1 = new Mat();
  12040. threshSobel1 = sobel1.Threshold(100, 1, ThresholdTypes.Binary);
  12041. Mat nomin1 = new Mat();
  12042. GetArea(threshSobel1, out nomin1, 500, true);
  12043. Mat result1 = nomin1.Clone();
  12044. //ImageShow(result1 * 255);
  12045. //有预处理高斯滤波
  12046. Mat filter = new Mat();
  12047. Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
  12048. Mat sobel = new Mat();
  12049. EdgeY2(filter, out sobel);
  12050. Mat threshSobel = new Mat();
  12051. threshSobel = sobel.Threshold(100, 1, ThresholdTypes.Binary);
  12052. Mat thresh = new Mat();
  12053. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12054. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12055. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12056. Mat fanse = 1 - thresh;
  12057. Mat and = new Mat();
  12058. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12059. Mat nomin = new Mat();
  12060. GetArea(and, out nomin, 500, true);
  12061. //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255+gray);
  12062. //提取坐标
  12063. Mat result = nomin.Clone();
  12064. int middle = result.Cols / 2;
  12065. Scalar sum = new Scalar();
  12066. for (int i = 0; i < result.Rows - 1; i++)
  12067. {
  12068. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12069. if ((int)sum > 100)
  12070. {
  12071. int newOrdinate = i;
  12072. ordinates.Add(newOrdinate);
  12073. i += 30;
  12074. }
  12075. }
  12076. if (ordinates[5] - ordinates[4] < 50)
  12077. ordinates.RemoveAt(5);
  12078. for (int i = ordinates[4] + 10; i < result.Rows; i++)
  12079. {
  12080. sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
  12081. if ((int)sum == 0)
  12082. {
  12083. int newOrdinate = i;
  12084. ordinates.Insert(5, newOrdinate);
  12085. break;
  12086. }
  12087. }
  12088. if (ordinates[ordinates.Count - 1] - ordinates[ordinates.Count - 2] < 40)
  12089. ordinates.RemoveAt(ordinates.Count - 1);
  12090. for (int i = ordinates[ordinates.Count - 1] + 10; i < result.Rows; i++)
  12091. {
  12092. sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
  12093. if ((int)sum == 0)
  12094. {
  12095. int newOrdinate = i;
  12096. ordinates.Add(newOrdinate);
  12097. break;
  12098. }
  12099. }
  12100. }
  12101. /// <summary>
  12102. /// 提取有导电布时三层铜时的坐标
  12103. /// </summary>
  12104. /// <param name="gray"></param>
  12105. /// <param name="ordinates"></param>
  12106. public void DaodianbuSanceng(Mat gray, out List<int> ordinates)
  12107. {
  12108. ordinates = new List<int>();
  12109. //图片处理
  12110. //无预处理图片边缘检测+阈值分割+去小面积用来提取导电布
  12111. //高斯滤波+边缘检测+阈值分割+铜区域闭运算-反色-掩膜-+掩膜+去小面积提取中间线条
  12112. //无预处理
  12113. Mat sobel1 = new Mat();
  12114. EdgeY2(gray, out sobel1);
  12115. Mat threshSobel1 = new Mat();
  12116. threshSobel1 = sobel1.Threshold(100, 1, ThresholdTypes.Binary);
  12117. Mat nomin1 = new Mat();
  12118. GetArea(threshSobel1, out nomin1, 500, true);
  12119. Mat result1 = nomin1.Clone();
  12120. //ImageShow(result1 * 255);
  12121. //有预处理高斯滤波
  12122. Mat filter = new Mat();
  12123. Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
  12124. Mat sobel = new Mat();
  12125. EdgeY2(filter, out sobel);
  12126. Mat threshSobel = new Mat();
  12127. threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
  12128. Mat thresh = new Mat();
  12129. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12130. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12131. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12132. Mat fanse = 1 - thresh;
  12133. Mat and = new Mat();
  12134. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12135. Mat nomin = new Mat();
  12136. GetArea(and, out nomin, 500, true);
  12137. //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
  12138. //提取坐标
  12139. Mat result = nomin.Clone();
  12140. int middle = result.Cols / 2;
  12141. Scalar sum = new Scalar();
  12142. for (int i = 0; i < result.Rows - 1; i++)
  12143. {
  12144. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12145. if ((int)sum > 100)
  12146. {
  12147. int newOrdinate = i;
  12148. ordinates.Add(newOrdinate);
  12149. i += 30;
  12150. }
  12151. }
  12152. //第一层
  12153. if (ordinates[1] - ordinates[0] < 50)
  12154. ordinates.RemoveAt(1);
  12155. for (int i = ordinates[0] + 10; i < result.Rows; i++)
  12156. {
  12157. sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
  12158. if ((int)sum == 0)
  12159. {
  12160. int newOrdinate = i;
  12161. ordinates.Insert(1, newOrdinate);
  12162. break;
  12163. }
  12164. }
  12165. //倒数第三层和倒数第四层,求倒数第四层
  12166. if (ordinates[ordinates.Count - 3] - ordinates[ordinates.Count - 4] < 40)
  12167. ordinates.RemoveAt(ordinates.Count - 3);
  12168. for (int i = ordinates[ordinates.Count - 3] + 10; i < result.Rows; i++)
  12169. {
  12170. sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
  12171. if ((int)sum == 0)
  12172. {
  12173. int newOrdinate = i;
  12174. ordinates.Insert(ordinates.Count - 2, newOrdinate);
  12175. break;
  12176. }
  12177. }
  12178. }
  12179. /// <summary>
  12180. /// 提取无导电布时单层铜厚大时的坐标
  12181. /// </summary>
  12182. /// <param name="gray"></param>
  12183. /// <param name="ordinates"></param>
  12184. public void WuDaodianbuDancengHou(Mat gray, out List<int> ordinates)
  12185. {
  12186. ordinates = new List<int>();
  12187. //图像处理
  12188. Mat filter = new Mat();
  12189. Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
  12190. Mat sobel = new Mat();
  12191. EdgeY2(filter, out sobel);
  12192. Mat threshSobel = new Mat();
  12193. threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
  12194. Mat thresh = new Mat();
  12195. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12196. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12197. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12198. Mat fanse = 1 - thresh;
  12199. Mat and = new Mat();
  12200. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12201. Mat nomin = new Mat();
  12202. GetArea(and, out nomin, 500, true);
  12203. //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
  12204. //坐标提取
  12205. Mat result = nomin.Clone();
  12206. int middle = result.Cols / 2;
  12207. Scalar sum = new Scalar();
  12208. for (int i = 0; i < result.Rows - 1; i++)
  12209. {
  12210. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12211. if ((int)sum > 100)
  12212. {
  12213. int newOrdinate = i;
  12214. ordinates.Add(newOrdinate);
  12215. i += 100;
  12216. }
  12217. }
  12218. }
  12219. /// <summary>
  12220. /// 提取无导电布时,单层铜,总厚较窄时的坐标
  12221. /// </summary>
  12222. /// <param name="gray"></param>
  12223. /// <param name="ordinates"></param>
  12224. public void WuDaodianbuDancengZhaiZonghouZhai(Mat gray, out List<int> ordinates)
  12225. {
  12226. ordinates = new List<int>();
  12227. //图像处理
  12228. Mat filter = new Mat();
  12229. Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
  12230. Mat sobel = new Mat();
  12231. EdgeY2(filter, out sobel);
  12232. Mat threshSobel = new Mat();
  12233. threshSobel = sobel.Threshold(60, 1, ThresholdTypes.Binary);
  12234. Mat thresh = new Mat();
  12235. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12236. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12237. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12238. Mat fanse = 1 - thresh;
  12239. Mat and = new Mat();
  12240. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12241. //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  12242. //Mat close2 = new Mat();
  12243. //Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
  12244. Mat nomin = new Mat();
  12245. GetArea(and, out nomin, 500, true);
  12246. //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
  12247. //坐标提取
  12248. Mat result = nomin.Clone();
  12249. int middle = result.Cols / 2;
  12250. Scalar sum = new Scalar();
  12251. for (int i = 0; i < result.Rows - 1; i++)
  12252. {
  12253. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12254. if ((int)sum > 100)
  12255. {
  12256. int newOrdinate = i;
  12257. ordinates.Add(newOrdinate);
  12258. i += 30;
  12259. }
  12260. }
  12261. int compensate = 5;
  12262. ordinates[ordinates.Count - 1] += compensate;
  12263. }
  12264. /// <summary>
  12265. /// 提取无导电布时,单层铜,总厚较宽时的坐标
  12266. /// </summary>
  12267. /// <param name="gray"></param>
  12268. /// <param name="ordinates"></param>
  12269. public void WuDaodianbuDancengZhaiZonghouKuan(Mat gray, out List<int> ordinates)
  12270. {
  12271. ordinates = new List<int>();
  12272. //图像处理
  12273. Mat filter = new Mat();
  12274. Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
  12275. Mat sobel = new Mat();
  12276. EdgeY2(filter, out sobel);
  12277. Mat threshSobel = new Mat();
  12278. threshSobel = sobel.Threshold(20, 1, ThresholdTypes.Binary);
  12279. Mat thresh = new Mat();
  12280. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12281. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12282. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12283. Mat fill = new Mat();
  12284. Fill(thresh, out fill, 1);
  12285. Mat fanse = 1 - fill;
  12286. Mat and = new Mat();
  12287. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12288. Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  12289. Mat close2 = new Mat();
  12290. Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
  12291. Mat nomin = new Mat();
  12292. GetArea(close2, out nomin, 2000, true);
  12293. //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
  12294. //坐标提取
  12295. Mat result = nomin.Clone();
  12296. int middle = result.Cols / 2;
  12297. Scalar sum = new Scalar();
  12298. int count = 0;
  12299. for (int i = 0; i < result.Rows - 1; i++)
  12300. {
  12301. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12302. if ((int)sum > 100)
  12303. {
  12304. int newOrdinate = i;
  12305. ordinates.Add(newOrdinate);
  12306. i += 30;
  12307. count++;
  12308. }
  12309. if (count == 3)
  12310. break;
  12311. }
  12312. //最后一行
  12313. for (int i = result.Rows - 1; i > 2; i--)
  12314. {
  12315. sum = result[i - 1, i, middle - 100, middle + 100].Sum();
  12316. if ((int)sum > 100)
  12317. {
  12318. int newOrdinate = i;
  12319. ordinates.Add(newOrdinate);
  12320. break;
  12321. }
  12322. }
  12323. for (int i = ordinates[ordinates.Count - 1] - 30; i > 2; i--)
  12324. {
  12325. sum = result[i - 2, i, middle - 100, middle + 100].Sum();
  12326. if ((int)sum > 100)
  12327. {
  12328. int newOrdinate = i;
  12329. ordinates.Insert(ordinates.Count - 1, newOrdinate);
  12330. break;
  12331. }
  12332. }
  12333. int compensate = 5;
  12334. ordinates[0] += compensate;
  12335. //ordinates[ordinates.Count - 1] += compensate;
  12336. //提取中间线
  12337. Mat crop = gray[ordinates[0], ordinates[1], middle - 100, middle + 100].Clone();
  12338. //Mat threshCrop = crop.Threshold(160, 1, ThresholdTypes.Binary);
  12339. Mat cropSobel = new Mat();
  12340. EdgeY2(crop, out cropSobel);
  12341. Mat threshCropSobel = 1 - cropSobel.Threshold(40, 1, ThresholdTypes.Binary);
  12342. Mat result2 = threshCropSobel.Clone();
  12343. for (int i = 20; i < result2.Rows - 1; i++)
  12344. {
  12345. sum = result2[i, i + 2, 0, result2.Cols].Sum();
  12346. if ((int)sum > 100)
  12347. {
  12348. int newOrdinate = i;
  12349. ordinates.Insert(1, newOrdinate + ordinates[0] + 20);
  12350. break;
  12351. }
  12352. }
  12353. //ImageShow(crop, threshCropSobel * 255);
  12354. }
  12355. /// <summary>
  12356. /// 提取无导电布双层铜层厚较大时
  12357. /// </summary>
  12358. /// <param name="gray"></param>
  12359. /// <param name="ordinates"></param>
  12360. public void WuDaodianbuShuangcengCenghou(Mat gray, out List<int> ordinates)
  12361. {
  12362. ordinates = new List<int>();
  12363. //图像处理
  12364. Mat filter = new Mat();
  12365. Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
  12366. Mat sobel = new Mat();
  12367. EdgeY2(filter, out sobel);
  12368. Mat threshSobel = new Mat();
  12369. threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
  12370. Mat thresh = new Mat();
  12371. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12372. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12373. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12374. Mat fill = new Mat();
  12375. Fill(thresh, out fill, 1);
  12376. Mat fanse = 1 - fill;
  12377. Mat and = new Mat();
  12378. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12379. //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  12380. //Mat close2 = new Mat();
  12381. //Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
  12382. Mat nomin = new Mat();
  12383. GetArea(and, out nomin, 2000, true);
  12384. //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
  12385. //坐标提取
  12386. Mat result = nomin.Clone();
  12387. int middle = result.Cols / 2;
  12388. Scalar sum = new Scalar();
  12389. for (int i = 0; i < result.Rows - 1; i++)
  12390. {
  12391. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12392. if ((int)sum > 100)
  12393. {
  12394. int newOrdinate = i;
  12395. ordinates.Add(newOrdinate + 5);
  12396. i += 100;
  12397. }
  12398. }
  12399. }
  12400. public void WudaodianbuShuangcengD(Mat gray, out List<int> ordinates)
  12401. {
  12402. ordinates = new List<int>();
  12403. //图像处理
  12404. Mat filter = new Mat();
  12405. Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
  12406. Mat sobel = new Mat();
  12407. EdgeY2(filter, out sobel);
  12408. Mat threshSobel = new Mat();
  12409. threshSobel = sobel.Threshold(40, 1, ThresholdTypes.Binary);
  12410. Mat thresh = new Mat();
  12411. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12412. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12413. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12414. Mat fill = new Mat();
  12415. Fill(thresh, out fill, 1);
  12416. Mat fanse = 1 - fill;
  12417. Mat and = new Mat();
  12418. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12419. Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  12420. Mat close2 = new Mat();
  12421. Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
  12422. Mat nomin = new Mat();
  12423. GetArea(close2, out nomin, 3000, true);
  12424. //Mat noCircle = new Mat();
  12425. //RemoveCircles(nomin, out noCircle);
  12426. //ImageShow(thresh * 255, and * 255,close2*255, nomin * 255 + gray);
  12427. //坐标提取
  12428. Mat result = nomin.Clone();
  12429. int middle = result.Cols / 2;
  12430. Scalar sum = new Scalar();
  12431. for (int i = 0; i < result.Rows - 1; i++)
  12432. {
  12433. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12434. if ((int)sum > 100)
  12435. {
  12436. int newOrdinate = i;
  12437. ordinates.Add(newOrdinate + 5);
  12438. i += 30;
  12439. }
  12440. }
  12441. //迭代
  12442. if (ordinates.Count < 11)
  12443. {
  12444. for (int i = 0; i < ordinates[0]; i++)
  12445. {
  12446. sum = result[i, i + 2, 0, result.Cols].Sum();
  12447. if ((int)sum > 100)
  12448. {
  12449. int newOrdinate = i;
  12450. ordinates.Insert(0, newOrdinate + 5);
  12451. break;
  12452. }
  12453. }
  12454. }
  12455. if (ordinates.Count == 12)
  12456. ordinates.RemoveAt(11);
  12457. if (ordinates.Count == 11)
  12458. {
  12459. ordinates[6] += 6;
  12460. ordinates[8] += 6;
  12461. ordinates[10] += 10;
  12462. }
  12463. }
  12464. public void WudaodianbuShuangcengQita(Mat gray, out List<int> ordinates)
  12465. {
  12466. ordinates = new List<int>();
  12467. //图像处理
  12468. Mat filter = new Mat();
  12469. Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
  12470. Mat sobel = new Mat();
  12471. EdgeY2(filter, out sobel);
  12472. Mat threshSobel = new Mat();
  12473. threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
  12474. Mat thresh = new Mat();
  12475. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12476. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12477. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12478. Mat fill = new Mat();
  12479. Fill(thresh, out fill, 1);
  12480. Mat fanse = 1 - fill;
  12481. Mat and = new Mat();
  12482. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12483. //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  12484. //Mat close2 = new Mat();
  12485. //Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
  12486. Mat nomin = new Mat();
  12487. GetArea(and, out nomin, 2000, true);
  12488. //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
  12489. //坐标提取
  12490. Mat result = nomin.Clone();
  12491. int middle = result.Cols / 2;
  12492. Scalar sum = new Scalar();
  12493. for (int i = 0; i < result.Rows - 1; i++)
  12494. {
  12495. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12496. if ((int)sum > 100)
  12497. {
  12498. int newOrdinate = i;
  12499. ordinates.Add(newOrdinate + 5);
  12500. i += 30;
  12501. }
  12502. }
  12503. }
  12504. /// <summary>
  12505. /// 提取无导电布三层坐标
  12506. /// </summary>
  12507. /// <param name="gray"></param>
  12508. /// <param name="ordinates"></param>
  12509. public void WudaodianbuSanceng(Mat gray, out List<int> ordinates)
  12510. {
  12511. ordinates = new List<int>();
  12512. //图像处理
  12513. Mat filter = new Mat();
  12514. Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
  12515. Mat sobel = new Mat();
  12516. EdgeY2(filter, out sobel);
  12517. Mat threshSobel = new Mat();
  12518. threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
  12519. Mat thresh = new Mat();
  12520. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12521. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12522. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12523. Mat fill = new Mat();
  12524. Fill(thresh, out fill, 1);
  12525. Mat fanse = 1 - fill;
  12526. Mat and = new Mat();
  12527. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12528. //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  12529. //Mat close2 = new Mat();
  12530. //Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
  12531. Mat nomin = new Mat();
  12532. GetArea(and, out nomin, 2000, true);
  12533. //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
  12534. //坐标提取
  12535. Mat result = nomin.Clone();
  12536. int middle = result.Cols / 2;
  12537. Scalar sum = new Scalar();
  12538. for (int i = 0; i < result.Rows - 1; i++)
  12539. {
  12540. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12541. if ((int)sum > 100)
  12542. {
  12543. int newOrdinate = i;
  12544. ordinates.Add(newOrdinate + 5);
  12545. i += 30;
  12546. }
  12547. }
  12548. if (ordinates[1] - ordinates[0] > 300)
  12549. {
  12550. if (ordinates[2] - ordinates[1] > 120)
  12551. {
  12552. for (int i = ordinates[1] + 30; i < ordinates[2]; i++)
  12553. {
  12554. sum = result[i, i + 2, 0, result.Cols].Sum();
  12555. if ((int)sum > 100)
  12556. {
  12557. int newOrdinate = i;
  12558. ordinates.Insert(2, newOrdinate);
  12559. break;
  12560. }
  12561. }
  12562. }
  12563. }
  12564. else
  12565. {
  12566. if (ordinates[3] - ordinates[2] > 120)
  12567. {
  12568. for (int i = ordinates[2] + 30; i < ordinates[3]; i++)
  12569. {
  12570. sum = result[i, i + 2, 0, result.Cols].Sum();
  12571. if ((int)sum > 100)
  12572. {
  12573. int newOrdinate = i;
  12574. ordinates.Insert(3, newOrdinate);
  12575. break;
  12576. }
  12577. }
  12578. }
  12579. }
  12580. ////添加超出范围的线条
  12581. //Mat stright = new Mat();
  12582. //List<int> strights = new List<int>();
  12583. //KeepStraight(result, out stright, out strights);
  12584. ////插入原坐标没有的点
  12585. //for (int i = 0; i < strights.Count; i++)
  12586. //{
  12587. // for (int j = 0; j < ordinates.Count - 1; j++)
  12588. // {
  12589. // if (Math.Abs(strights[i] - ordinates[j]) < 30 || Math.Abs(strights[i] - ordinates[j + 1]) < 30)
  12590. // break;
  12591. // else if (strights[i] > ordinates[j] && strights[i] < ordinates[j + 1])
  12592. // {
  12593. // ordinates.Insert(j + 1, strights[i]);
  12594. // break;
  12595. // }
  12596. // }
  12597. //}
  12598. ////删除过近的坐标
  12599. //List<int> ordinatesCopy = ordinates.ToList();
  12600. //int count = 0;
  12601. //for (int i = 0; i < ordinates.Count - 1; i++)
  12602. //{
  12603. // if (ordinates[i + 1] - ordinates[i] < 10)
  12604. // {
  12605. // ordinatesCopy.RemoveAt(i - count);
  12606. // count++;
  12607. // }
  12608. //}
  12609. //ordinates = ordinatesCopy.ToList();
  12610. //增加最下层坐标
  12611. if (ordinates.Count < 10)
  12612. {
  12613. Mat bottom = gray.Clone();
  12614. Cv2.Rectangle(bottom, new Rect(0, 0, bottom.Cols, ordinates[ordinates.Count - 1]), new Scalar(0), -1);
  12615. Cv2.GaussianBlur(bottom, bottom, new Size(5, 5), 3);
  12616. Mat edge = new Mat();
  12617. EdgeY(bottom, out edge);
  12618. Mat threshEdge = new Mat();
  12619. threshEdge = edge.Threshold(30, 1, ThresholdTypes.Binary);
  12620. Mat seClose3 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  12621. Mat close = new Mat();
  12622. Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose3);
  12623. Mat nomin3 = new Mat();
  12624. GetArea(close, out nomin3, 500, true);
  12625. //Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
  12626. //Mat open = new Mat();
  12627. //Cv2.MorphologyEx(threshEdge, open, MorphTypes.Open, seOpen);
  12628. //ImageShow(threshEdge * 255,close*255,nomin3*255);
  12629. Mat result2 = nomin3.Clone();
  12630. for (int i = ordinates[ordinates.Count - 1] + 30; i < result2.Rows - 1; i++)
  12631. {
  12632. sum = result2[i, i + 2, middle - 100, middle + 100].Sum();
  12633. if ((int)sum > 100)
  12634. {
  12635. int newOrdinate = i;
  12636. ordinates.Add(newOrdinate);
  12637. break;
  12638. }
  12639. }
  12640. }
  12641. //else
  12642. //{
  12643. //}
  12644. }
  12645. /// <summary>
  12646. /// 提取无导电布四层铜的坐标
  12647. /// </summary>
  12648. /// <param name="gray"></param>
  12649. /// <param name="ordinates"></param>
  12650. public void WuDaodianbuSiceng(Mat gray, out List<int> ordinates)
  12651. {
  12652. ordinates = new List<int>();
  12653. //图像处理
  12654. Mat filter = new Mat();
  12655. Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
  12656. Mat sobel = new Mat();
  12657. EdgeY2(filter, out sobel);
  12658. Mat threshSobel = new Mat();
  12659. threshSobel = sobel.Threshold(60, 1, ThresholdTypes.Binary);
  12660. Mat thresh = new Mat();
  12661. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12662. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12663. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12664. Mat fill = new Mat();
  12665. Fill(thresh, out fill, 1);
  12666. Mat fanse = 1 - fill;
  12667. Mat and = new Mat();
  12668. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12669. Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  12670. Mat close2 = new Mat();
  12671. Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
  12672. Mat nomin = new Mat();
  12673. GetArea(close2, out nomin, 2000, true);
  12674. //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
  12675. //坐标提取
  12676. Mat result = nomin.Clone();
  12677. int middle = result.Cols / 2;
  12678. Scalar sum = new Scalar();
  12679. for (int i = 0; i < result.Rows - 1; i++)
  12680. {
  12681. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12682. if ((int)sum > 100)
  12683. {
  12684. int newOrdinate = i;
  12685. ordinates.Add(newOrdinate + 5);
  12686. i += 30;
  12687. }
  12688. }
  12689. }
  12690. /// <summary>
  12691. /// 提取有内部线条的坐标
  12692. /// </summary>
  12693. /// <param name="gray"></param>
  12694. /// <param name="ordinates"></param>
  12695. public void WuDaodianbuQicengtong(Mat gray, out List<int> ordinates)
  12696. {
  12697. ordinates = new List<int>();
  12698. //图像处理
  12699. Mat filter = new Mat();
  12700. Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
  12701. Mat sobel = new Mat();
  12702. EdgeY2(filter, out sobel);
  12703. Mat threshSobel = new Mat();
  12704. threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
  12705. Mat thresh = new Mat();
  12706. thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
  12707. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
  12708. Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
  12709. Mat fill = new Mat();
  12710. Fill(thresh, out fill, 1);
  12711. Mat fanse = 1 - fill;
  12712. Mat and = new Mat();
  12713. Cv2.BitwiseAnd(fanse, threshSobel, and);
  12714. Mat nomin = new Mat();
  12715. GetArea(and, out nomin, 2000, true);
  12716. Mat threshSobel2 = new Mat();
  12717. threshSobel2 = sobel.Threshold(180, 1, ThresholdTypes.Binary);
  12718. //ImageShow(threshSobel * 255, and * 255, nomin * 255 + gray,threshSobel2*255);
  12719. //坐标提取
  12720. Mat result = nomin.Clone();
  12721. int middle = result.Cols / 2;
  12722. Scalar sum = new Scalar();
  12723. for (int i = 0; i < result.Rows - 1; i++)
  12724. {
  12725. sum = result[i, i + 2, middle - 100, middle + 100].Sum();
  12726. if ((int)sum > 100)
  12727. {
  12728. int newOrdinate = i;
  12729. ordinates.Add(newOrdinate + 5);
  12730. i += 30;
  12731. }
  12732. }
  12733. //对内部线进行提取
  12734. //对内部进行阈值分割
  12735. int start1 = ordinates[0];
  12736. int start2 = ordinates[2];
  12737. Mat crop1 = gray[ordinates[0], ordinates[1], middle - 100, middle + 100].Clone();
  12738. Mat crop2 = gray[ordinates[2], ordinates[3], middle - 100, middle + 100].Clone();
  12739. Mat threshCrop1 = 1 - crop1.Threshold(0, 1, ThresholdTypes.Otsu);
  12740. Mat threshCrop2 = 1 - crop2.Threshold(0, 1, ThresholdTypes.Otsu);
  12741. //ImageShow(threshCrop1 * 255, threshCrop2 * 255);
  12742. for (int i = 10; i < threshCrop1.Rows - 1; i++)
  12743. {
  12744. sum = threshCrop1[i, i + 2, 0, threshCrop1.Cols].Sum();
  12745. if ((int)sum > 100)
  12746. {
  12747. int newOrdinate = i;
  12748. ordinates.Insert(1, newOrdinate + ordinates[0]);
  12749. break;
  12750. }
  12751. }
  12752. for (int i = threshCrop1.Rows - 30; i > 2; i--)
  12753. {
  12754. sum = threshCrop1[i - 2, i, 0, threshCrop1.Cols].Sum();
  12755. if ((int)sum > 100)
  12756. {
  12757. int newOrdinate = i;
  12758. ordinates.Insert(2, newOrdinate + ordinates[0]);
  12759. break;
  12760. }
  12761. }
  12762. for (int i = 30; i < threshCrop2.Rows - 2; i++)
  12763. {
  12764. sum = threshCrop2[i, i + 2, 0, threshCrop2.Cols].Sum();
  12765. if ((int)sum > 100)
  12766. {
  12767. int newOrdinate = i;
  12768. ordinates.Insert(5, newOrdinate + start2);
  12769. break;
  12770. }
  12771. }
  12772. for (int i = threshCrop2.Rows - 10; i > 2; i--)
  12773. {
  12774. sum = threshCrop2[i - 2, i, 0, threshCrop2.Cols].Sum();
  12775. if ((int)sum > 100)
  12776. {
  12777. int newOrdinate = i;
  12778. ordinates.Insert(6, newOrdinate + start2);
  12779. break;
  12780. }
  12781. }
  12782. }
  12783. /// <summary>
  12784. /// 通过计算二值图最集中的区域来得到叠构的提取区域
  12785. /// </summary>
  12786. /// <param name="contour"></param>
  12787. /// <param name="dataArea"></param>
  12788. public void DiegouDataArea(Mat contour, out int dataArea)
  12789. {
  12790. Scalar sum = new Scalar(0);
  12791. Scalar max = new Scalar(0);
  12792. int maxArea = 0;
  12793. for (int j = contour.Cols / 5; j < contour.Cols / 5 * 4 && j < contour.Cols - 40; j++)
  12794. {
  12795. sum = contour[0, contour.Rows, j, j + 40].Sum();
  12796. if ((int)sum > (int)max)
  12797. {
  12798. max = sum;
  12799. maxArea = j;
  12800. }
  12801. }
  12802. dataArea = maxArea + 20;
  12803. }
  12804. /// <summary>
  12805. /// 得到去噪后的边缘线
  12806. /// </summary>
  12807. /// <param name="image"></param>
  12808. /// <param name="edge"></param>
  12809. public void GetEdge(Mat image, out Mat edge)
  12810. {
  12811. Mat filter = new Mat();
  12812. Cv2.GaussianBlur(image, filter, new Size(11, 11), 5, 5);
  12813. edge = new Mat();
  12814. Cv2.Canny(filter, edge, 10, 10);
  12815. //去除小连通域噪声
  12816. Mat[] contours;
  12817. Mat hierachy = new Mat();
  12818. Cv2.FindContours(edge, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  12819. int area = 2;
  12820. Mat noise = Mat.Zeros(edge.Rows, edge.Cols, edge.Type());
  12821. for (int i = 0; i < contours.Count(); i++)
  12822. {
  12823. if (Cv2.ContourArea(contours[i]) > area)
  12824. {
  12825. Cv2.DrawContours(noise, contours, i, new Scalar(1));
  12826. }
  12827. }
  12828. //减去噪声+开运算
  12829. Mat noNoise = edge - noise * 255;
  12830. //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  12831. //Mat close = new Mat();
  12832. //Cv2.MorphologyEx(noNoise, close, MorphTypes.Close, seClose);
  12833. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
  12834. Mat open = new Mat();
  12835. Cv2.MorphologyEx(noNoise, open, MorphTypes.Open, seOpen);
  12836. edge = open;
  12837. }
  12838. /// <summary>
  12839. /// 计算线的坐标,当10行内点的数量大于10时记录平均坐标,用的是非膨胀后开运算结果图
  12840. /// </summary>
  12841. /// <param name="edge"></param>
  12842. /// <param name="dataArea"></param>
  12843. /// <param name="ordinates"></param>
  12844. public void GetOrdinate(Mat edge, int dataArea, out List<int> ordinates)
  12845. {
  12846. int upper = 0;
  12847. int lower = 0;
  12848. int upperBound = 0, lowerBound = 0;
  12849. int count = 0;
  12850. int meanOrdinate = 0;
  12851. ordinates = new List<int>();
  12852. Scalar sum = new Scalar(0);
  12853. for (int i = 0; i < edge.Rows; i++)
  12854. {
  12855. sum = edge[i, i + 3, 0, edge.Cols].Sum();
  12856. if ((int)sum > 50)
  12857. {
  12858. upper = i;
  12859. break;
  12860. }
  12861. }
  12862. for (int i = edge.Rows - 1; i > 0; i--)
  12863. {
  12864. sum = edge[i - 3, i, 0, edge.Cols].Sum();
  12865. if ((int)sum > 40)
  12866. {
  12867. lower = i;
  12868. break;
  12869. }
  12870. }
  12871. upperBound = upper - 10;
  12872. while (lowerBound < lower + 50)
  12873. {
  12874. count = 0;
  12875. meanOrdinate = 0;
  12876. while (count < 10 /*&& lowerBound + 20 < edge.Rows*/)
  12877. {
  12878. upperBound += 5;
  12879. lowerBound = upperBound + 5;
  12880. for (int i = upperBound; i < lowerBound; i++)
  12881. {
  12882. for (int j = dataArea - 25; j < dataArea + 25; j++)
  12883. {
  12884. if (edge.Get<byte>(i, j) > 0)
  12885. {
  12886. meanOrdinate += i;
  12887. count++;
  12888. }
  12889. }
  12890. }
  12891. }
  12892. if (count > 5)
  12893. {
  12894. int newOrdinate = new int();
  12895. newOrdinate = meanOrdinate / count;
  12896. if (ordinates.Count > 0 && newOrdinate > ordinates[ordinates.Count - 1] + 40)
  12897. {
  12898. ordinates.Add(newOrdinate);
  12899. upper += 10;
  12900. }
  12901. else if (ordinates.Count == 0)
  12902. {
  12903. ordinates.Add(newOrdinate);
  12904. upper += 10;
  12905. }
  12906. }
  12907. }
  12908. }
  12909. public void GetOrdinate2(Mat edge, int dataArea, out List<int> ordinates)
  12910. {
  12911. int upper = 0;
  12912. int lower = 0;
  12913. int upperBound = 0, lowerBound = 0;
  12914. int count = 0;
  12915. int meanOrdinate = 0;
  12916. ordinates = new List<int>();
  12917. Scalar sum = new Scalar(0);
  12918. for (int i = 0; i < edge.Rows; i++)
  12919. {
  12920. sum = edge[i, i + 3, 0, edge.Cols].Sum();
  12921. if ((int)sum > 70)
  12922. {
  12923. upper = i;
  12924. break;
  12925. }
  12926. }
  12927. for (int i = edge.Rows - 1; i > 0; i--)
  12928. {
  12929. sum = edge[i - 3, i, 0, edge.Cols].Sum();
  12930. if ((int)sum > 40)
  12931. {
  12932. lower = i;
  12933. break;
  12934. }
  12935. }
  12936. upperBound = upper - 10;
  12937. int j = 0;
  12938. while (upperBound + 20 < lower)
  12939. {
  12940. for (int i = upperBound; i < lower + 50; i += 1)
  12941. {
  12942. sum = edge[i, i + 5, dataArea - 50, dataArea + 50].Sum();
  12943. if ((int)sum > 10)
  12944. {
  12945. meanOrdinate = i + 1;
  12946. if (ordinates.Count == 0)
  12947. {
  12948. ordinates.Add(meanOrdinate);
  12949. upperBound = i + 10;
  12950. break;
  12951. }
  12952. else if (ordinates.Count > 0 && meanOrdinate > ordinates[ordinates.Count - 1] + 40)
  12953. {
  12954. ordinates.Add(meanOrdinate);
  12955. upperBound = (i + 10);
  12956. break;
  12957. }
  12958. }
  12959. j = i;
  12960. }
  12961. if (j == lower + 49)
  12962. break;
  12963. }
  12964. }
  12965. /// <summary>
  12966. /// 对叠构的上区域重新进行处理并判断是否有线条存在
  12967. /// </summary>
  12968. /// <param name="image"></param>
  12969. /// <param name="ordinate"></param>
  12970. /// <param name="border"></param>
  12971. public void UpperProcess(Mat image, out int ordinate, int[] border)
  12972. {
  12973. ordinate = 0;
  12974. Mat result = new Mat();
  12975. Mat crop = image[border[0], border[1], 0, image.Cols].Clone();
  12976. //ImageShow(crop);
  12977. Cv2.GaussianBlur(crop, crop, new Size(11, 11), 5, 5);
  12978. //ImageShow(crop);
  12979. //Cv2.MedianBlur(image, image, 11);
  12980. //ImageShow(image);
  12981. Mat edgeSobel = new Mat();
  12982. EdgeY(crop, out edgeSobel);
  12983. //Sobel(image, out edgeSobel);
  12984. Mat threshEdge = new Mat();
  12985. double t2 = Cv2.Threshold(edgeSobel, threshEdge, 0, 255, ThresholdTypes.Otsu);
  12986. threshEdge = edgeSobel.Threshold(15, 255, ThresholdTypes.Binary);
  12987. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
  12988. Mat close = new Mat();
  12989. Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
  12990. close = close / 255;
  12991. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
  12992. Mat open = new Mat();
  12993. Cv2.MorphologyEx(close, open, MorphTypes.Open, seOpen);
  12994. GetArea(open, out close, 400, true);
  12995. result = close.Clone();
  12996. //ImageShow(crop, edgeSobel, threshEdge, result * 255);
  12997. Scalar sum = new Scalar(0);
  12998. for (int i = 0; i < crop.Rows - 5; i++)
  12999. {
  13000. sum = result[i, i + 2, result.Cols / 3, result.Cols / 3 * 2].Sum();
  13001. if ((int)sum > result.Cols / 2)
  13002. {
  13003. ordinate = i;
  13004. break;
  13005. }
  13006. }
  13007. }
  13008. public void DiegouImageProcess(Mat gray, out Mat result)
  13009. {
  13010. result = new Mat();
  13011. //大致轮廓线
  13012. Mat newGray = new Mat();
  13013. Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
  13014. Mat filter = new Mat();
  13015. //Cv2.MedianBlur(newGray, filter, 9);
  13016. PointEnhancement(newGray, out filter);
  13017. //Mat blur = new Mat();
  13018. //Cv2.MedianBlur(filter, blur, 7);
  13019. Mat edgeSobel = new Mat();
  13020. Edge(filter, out edgeSobel);
  13021. Mat threshEdge = new Mat();
  13022. double t2 = Cv2.Threshold(edgeSobel, threshEdge, 0, 255, ThresholdTypes.Otsu);
  13023. threshEdge = edgeSobel.Threshold(20, 255, ThresholdTypes.Binary);
  13024. //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
  13025. //Mat close = new Mat();
  13026. //Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
  13027. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(11, 1));
  13028. Mat open = new Mat();
  13029. Cv2.MorphologyEx(threshEdge, open, MorphTypes.Open, seOpen);
  13030. Mat sobel4 = new Mat();
  13031. Sobel(open, out sobel4);
  13032. //Mat nomin = new Mat();
  13033. //GetArea(open, out nomin, 20, true);
  13034. //Mat nocircle = new Mat();
  13035. //RemoveCircles(open, out nocircle);
  13036. //ImageShow(edgeSobel, threshEdge, open/*, nocircle*255*/);
  13037. //铜线
  13038. Mat thresh = new Mat();
  13039. thresh = gray.Threshold(0, 255, ThresholdTypes.Otsu);
  13040. Mat fill = new Mat();
  13041. Fill(thresh, out fill, 255);
  13042. Mat edge2 = new Mat();
  13043. Sobel(fill, out edge2);
  13044. //铜内部线
  13045. Mat edge3 = new Mat();
  13046. Sobel(filter, out edge3);
  13047. Mat thresh3 = edge3.Threshold(10, 255, ThresholdTypes.Binary);
  13048. Mat and = new Mat();
  13049. Cv2.BitwiseAnd(thresh, thresh3, and);
  13050. Mat seErode = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 3));
  13051. Mat erode = new Mat();
  13052. Cv2.Erode(and, erode, seErode);
  13053. Mat seOpen2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 3));
  13054. Mat open2 = new Mat();
  13055. Cv2.MorphologyEx(and, open2, MorphTypes.Open, seOpen2);
  13056. Mat nomin2 = new Mat();
  13057. GetArea(open2, out nomin2, 200, true);
  13058. //ImageShow( and,erode,open2,nomin2*255);
  13059. //铜内部线2
  13060. Mat and2 = new Mat();
  13061. Cv2.BitwiseAnd(gray, thresh, and2);
  13062. Scalar sum = and2.Sum();
  13063. int num = 0;
  13064. Mat idx = new Mat();
  13065. Cv2.FindNonZero(and2, idx);
  13066. num = idx.Rows;
  13067. int T = (int)sum / num;
  13068. Mat thresh4 = and2.Threshold(T, 255, ThresholdTypes.Binary);
  13069. Mat seClose3 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  13070. Mat close3 = new Mat();
  13071. Cv2.MorphologyEx(thresh4, close3, MorphTypes.Close, seClose3);
  13072. Mat fill2 = new Mat();
  13073. Fill(close3, out fill2, 255);
  13074. Mat nomin3 = new Mat();
  13075. GetArea(fill2, out nomin3, 10000, true);
  13076. Mat fanse = 255 - nomin3 * 255;
  13077. Mat edge4 = new Mat();
  13078. Sobel(fanse, out edge4);
  13079. //ImageShow(thresh4, edge4);
  13080. //最终
  13081. Mat fanse2 = 255 - thresh;
  13082. Mat and3 = new Mat();
  13083. Cv2.BitwiseAnd(fanse2, sobel4, and3);
  13084. result = and3 + edge2 + edge4;/*nomin2 * 255;*/
  13085. //ImageShow(result);
  13086. }
  13087. public void DiegouImageProcess2(Mat gray, out Mat result)
  13088. {
  13089. result = new Mat();
  13090. Mat filter = new Mat();
  13091. Mat junheng = new Mat();
  13092. Mat blur = new Mat();
  13093. Mat zengqiang = new Mat();
  13094. Mat edge = new Mat();
  13095. Mat thresh = new Mat();
  13096. //PointEnhancement(gray, out zengqiang);
  13097. //Cv2.EqualizeHist(zengqiang, junheng);
  13098. //Cv2.GaussianBlur(junheng, filter, new Size(15,15), 5, 5);
  13099. ////PointEnhancement(filter, out zengqiang);
  13100. //Cv2.Blur(filter, blur, new Size (3,3));
  13101. //Cv2.GaussianBlur(gray, filter, new Size(15, 15), 5, 5);
  13102. Cv2.Blur(gray, blur, new Size(3, 3));
  13103. PointEnhancement(blur, out zengqiang);
  13104. //Cv2.EqualizeHist(zengqiang, junheng);
  13105. //ImageShow(gray, zengqiang, junheng, /*filter,*/blur);
  13106. Edge(zengqiang, out edge);
  13107. Mat threshEdge = edge.Threshold(30, 255, ThresholdTypes.Binary);
  13108. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  13109. Mat open = new Mat();
  13110. Cv2.MorphologyEx(threshEdge, open, MorphTypes.Open, seOpen);
  13111. double t = Cv2.Threshold(gray, thresh, 0, 255, ThresholdTypes.Otsu);
  13112. thresh = gray.Threshold(t - 20, 255, ThresholdTypes.Binary);
  13113. Mat fill = new Mat();
  13114. Fill(thresh, out fill, 255);
  13115. Mat fanse = 255 - fill;
  13116. Mat and = new Mat();
  13117. Cv2.BitwiseAnd(fanse, open, and);
  13118. //Mat seOpen2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
  13119. //Mat open2 = new Mat();
  13120. //Cv2.MorphologyEx(and, open2, MorphTypes.Open, seOpen2);
  13121. Mat nomin = new Mat();
  13122. GetArea(and, out nomin, 500, true);
  13123. gray = gray + nomin * 100;
  13124. ImageShow(thresh, fill, fanse);
  13125. ImageShow(and, nomin * 255, gray);
  13126. //Cv2.Canny(filter, edge, 7, 0);
  13127. //Mat nomin = new Mat();
  13128. //GetArea(edge, out nomin, 5, true);
  13129. //ImageShow(edge, nomin * 255);
  13130. //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
  13131. //CvTrackbarCallback cvTrackbarCallback2 = new CvTrackbarCallback(Text2);
  13132. //Window window = new Window("tbar");//创建一个新窗口"tbar"
  13133. //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 25, 500, cvTrackbarCallback);
  13134. //CvTrackbar cvTrackbar2 = new CvTrackbar("bar2", "tbar", 35, 500, cvTrackbarCallback2);
  13135. //Cv2.WaitKey();
  13136. //void Text(int value)
  13137. //{
  13138. // minValue = value;
  13139. // Cv2.Canny(filter, edge, minValue, maxValue);
  13140. // new Window("tbar", edge);
  13141. //}
  13142. //void Text2(int value)
  13143. //{
  13144. // maxValue = value;
  13145. // Cv2.Canny(filter, edge, minValue, maxValue);
  13146. // new Window("tbar", edge);
  13147. //}
  13148. //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
  13149. //CvTrackbarCallback cvTrackbarCallback2 = new CvTrackbarCallback(Text2);
  13150. //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 25, 225, cvTrackbarCallback);
  13151. //CvTrackbar cvTrackbar2 = new CvTrackbar("bar2", "tbar", 35, 225, cvTrackbarCallback2);
  13152. //Cv2.WaitKey();
  13153. //void Text(int value)
  13154. //{
  13155. // color = value;
  13156. // Cv2.BilateralFilter(gray, filter, d, color, 3);
  13157. // PointEnhancement(filter, out zengqiang);
  13158. // Sobel(zengqiang, out edge);
  13159. // thresh = edge.Threshold(5, 255, ThresholdTypes.Binary);
  13160. // new Window("tbar", WindowMode.Normal, thresh);
  13161. //}
  13162. //void Text2(int value)
  13163. //{
  13164. // d = value;
  13165. // Cv2.BilateralFilter(gray, filter, d, color, 3);
  13166. // PointEnhancement(filter, out zengqiang);
  13167. // Sobel(zengqiang, out edge);
  13168. // thresh = edge.Threshold(5, 255, ThresholdTypes.Binary);
  13169. // new Window("tbar", WindowMode.Normal, thresh);
  13170. //}
  13171. }
  13172. public void DiegouImageProcess3(Mat gray, out Mat result, out Mat edgeSobel)
  13173. {
  13174. result = new Mat();
  13175. //大致轮廓线
  13176. Mat newGray = new Mat();
  13177. Cv2.GaussianBlur(gray, newGray, new Size(11, 11), 3, 3);
  13178. Mat blur = new Mat();
  13179. Cv2.Blur(newGray, blur, new Size(3, 3));
  13180. Mat filter = new Mat();
  13181. PointEnhancement(blur, out filter);
  13182. edgeSobel = new Mat();
  13183. //Edge(filter, out edgeSobel);
  13184. EdgeY2(filter, out edgeSobel);
  13185. Mat threshEdge = new Mat();
  13186. double t2 = Cv2.Threshold(edgeSobel, threshEdge, 0, 255, ThresholdTypes.Otsu);
  13187. threshEdge = edgeSobel.Threshold(80, 255, ThresholdTypes.Binary);
  13188. Mat thresh = gray.Threshold(0, 255, ThresholdTypes.Otsu);
  13189. Mat fill = new Mat();
  13190. Fill(thresh, out fill, 255);
  13191. Mat sobel2 = new Mat();
  13192. Sobel(fill, out sobel2);
  13193. Mat fanse = 255 - fill;
  13194. Mat and = new Mat();
  13195. Cv2.BitwiseAnd(fanse, threshEdge, and);
  13196. //ImageShow( threshEdge,sobel2, and,gray+and);
  13197. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  13198. Mat close = new Mat();
  13199. Cv2.MorphologyEx(and, close, MorphTypes.Close, seClose);
  13200. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
  13201. Mat open = new Mat();
  13202. Cv2.MorphologyEx(close, open, MorphTypes.Open, seOpen);
  13203. Mat nomin4 = new Mat();
  13204. GetArea(open, out nomin4, 2000, true);
  13205. Mat sobel4 = new Mat();
  13206. Sobel(nomin4, out sobel4);
  13207. //ImageShow(open,close,nomin4*255);
  13208. result = sobel4 * 100 + sobel2;/*nomin2 * 255;*/
  13209. //ImageShow(open,nomin4*255+gray ,result+gray);
  13210. }
  13211. public void DiegouImageProcess4(Mat gray, out Mat result)
  13212. {
  13213. result = new Mat(gray.Size(), gray.Type());
  13214. int middle = gray.Cols / 2;
  13215. Scalar sum = new Scalar();
  13216. Scalar lastSum = new Scalar(0);
  13217. byte[] cha = new byte[gray.Rows];
  13218. for (int i = 0; i < gray.Rows; i++)
  13219. {
  13220. sum = gray[i, i + 1, middle - 200, middle + 200].Sum();
  13221. if (i == 0)
  13222. {
  13223. cha[i] = 0;
  13224. }
  13225. else
  13226. {
  13227. cha[i] = (byte)Math.Abs((int)sum - (int)lastSum);
  13228. }
  13229. lastSum = sum;
  13230. }
  13231. for (int i = 0; i < gray.Rows; i++)
  13232. {
  13233. for (int j = 0; j < gray.Cols; j++)
  13234. {
  13235. result.Set<byte>(i, j, cha[i]);
  13236. }
  13237. }
  13238. ImageShow(result);
  13239. }
  13240. public void DiegouOrdinates(Mat image, Mat gray, Mat edgeSobel, bool daodianbu, out List<int> ordinates)
  13241. {
  13242. ordinates = new List<int>();
  13243. Scalar sum = new Scalar(0);
  13244. int middle = image.Cols / 2;
  13245. image = image.Threshold(10, 1, ThresholdTypes.Binary);
  13246. //Mat hierachy = new Mat();
  13247. //Mat[] contoursMat;
  13248. //Cv2.FindContours(image, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  13249. Mat thresh = gray.Threshold(0, 255, ThresholdTypes.Otsu);
  13250. //ImageShow(thresh);
  13251. int[] y = new int[4];
  13252. int b = 0;
  13253. for (int i = 0; i < thresh.Rows; i++)
  13254. {
  13255. sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
  13256. if ((int)sum > 50)
  13257. {
  13258. y[0] = i;
  13259. break;
  13260. }
  13261. }
  13262. for (int i = y[0] + 50; i < thresh.Rows; i++)
  13263. {
  13264. sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
  13265. if ((int)sum == 0)
  13266. {
  13267. y[1] = i;
  13268. break;
  13269. }
  13270. }
  13271. for (int i = y[1] + 50; i < thresh.Rows; i++)
  13272. {
  13273. sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
  13274. if ((int)sum > 50)
  13275. {
  13276. y[2] = i;
  13277. break;
  13278. }
  13279. }
  13280. for (int i = y[2]; i < thresh.Rows; i++)
  13281. {
  13282. sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
  13283. if ((int)sum == 0)
  13284. {
  13285. y[3] = i;
  13286. break;
  13287. }
  13288. }
  13289. Mat newThresh = thresh / 255;
  13290. for (int j = 0; j < thresh.Cols; j++)
  13291. {
  13292. sum = newThresh[0, thresh.Rows, j, j + 1].Sum();
  13293. if ((int)sum > 300)
  13294. {
  13295. b = j;
  13296. break;
  13297. }
  13298. }
  13299. int type = 0;
  13300. int[] cha = new int[] { y[1] - y[0], y[3] - y[2], y[2] - y[1] };
  13301. if ((y[1] - y[0]) > 180 && (y[3] - y[2]) > 180)
  13302. {
  13303. type = 1;//单独两个铜
  13304. }
  13305. else if (((cha[0] > 300) && y[2] == 0 && y[3] == 0 && b < thresh.Cols / 3) || ((cha[0] > 100 && cha[0] < 180) && (cha[1] > 100 && cha[1] < 180) && (cha[2] > 150)))
  13306. {
  13307. type = 2;//有内部线
  13308. }
  13309. if (type == 1)//当是只有两层铜的时候
  13310. {
  13311. for (int i = 0; i < image.Rows - 20; i++)
  13312. {
  13313. sum = image[i, i + 2, middle - 100, middle + 100].Sum();
  13314. if ((int)sum > 150)
  13315. {
  13316. int newOrdinate = i;
  13317. ordinates.Add(newOrdinate /*+ 10*/);
  13318. i += 150;
  13319. }
  13320. }
  13321. if (ordinates.Count <= 3)
  13322. {
  13323. ordinates.Clear();
  13324. for (int i = 0; i < image.Rows - 20; i++)
  13325. {
  13326. sum = image[i, i + 5, middle - 100, middle + 100].Sum();
  13327. if ((int)sum > 150)
  13328. {
  13329. int newOrdinate = i;
  13330. ordinates.Add(newOrdinate /*+ 10*/);
  13331. i += 150;
  13332. }
  13333. }
  13334. }
  13335. }
  13336. else if (type == 2)
  13337. {
  13338. Mat threshEdge = edgeSobel.Threshold(180, 255, ThresholdTypes.Binary);
  13339. Mat fanse = 255 - threshEdge;
  13340. //ImageShow(threshEdge, fanse);
  13341. for (int i = 0; i < image.Rows - 20; i++)
  13342. {
  13343. sum = image[i, i + 2, middle - 100, middle + 100].Sum();
  13344. if ((int)sum > 150)
  13345. {
  13346. int newOrdinate = i;
  13347. ordinates.Add(newOrdinate + 10);
  13348. i += 30;
  13349. }
  13350. }
  13351. for (int i = ordinates[0]; i < ordinates[0] + 30; i++)
  13352. {
  13353. sum = fanse[i, i + 2, middle - 100, middle + 100].Sum();
  13354. if ((int)sum > 50)
  13355. {
  13356. ordinates.Insert(1, i + 7);
  13357. break;
  13358. }
  13359. }
  13360. for (int i = ordinates[0] + 40; i < ordinates[2]; i++)
  13361. {
  13362. sum = threshEdge[i, i + 2, middle - 100, middle + 100].Sum();
  13363. if ((int)sum > 150)
  13364. {
  13365. ordinates.Insert(2, i + 10);
  13366. break;
  13367. }
  13368. }
  13369. for (int i = ordinates[0] + 350; i < threshEdge.Rows; i++)
  13370. {
  13371. sum = threshEdge[i, i + 2, middle - 100, middle + 100].Sum();
  13372. if ((int)sum > 150)
  13373. {
  13374. ordinates.Insert(5, i + 12);
  13375. break;
  13376. }
  13377. }
  13378. for (int i = ordinates[6] - 15; i > 0; i--)
  13379. {
  13380. sum = fanse[i - 2, i, middle - 100, middle + 100].Sum();
  13381. if ((int)sum > 50)
  13382. {
  13383. ordinates.Insert(6, i - 7);
  13384. break;
  13385. }
  13386. }
  13387. }
  13388. else
  13389. {
  13390. int t = 2;
  13391. for (int i = 0; i < image.Rows - 20; i++)
  13392. {
  13393. if (ordinates.Count == 0 && daodianbu)
  13394. t = 180;
  13395. else
  13396. t = 150;
  13397. sum = image[i, i + 2, middle - 100, middle + 100].Sum();
  13398. if ((int)sum > t)
  13399. {
  13400. int newOrdinate = i;
  13401. ordinates.Add(newOrdinate + 5);
  13402. i += 30;
  13403. //if (ordinates.Count == 1)
  13404. // i += 10;
  13405. //else
  13406. // i += 30;
  13407. }
  13408. }
  13409. ordinates[0] += 5;
  13410. //ordinates[1] -= 5;
  13411. Mat threshEdge = edgeSobel.Threshold(40, 1, ThresholdTypes.Binary);
  13412. //ImageShow(threshEdge * 255);
  13413. if (daodianbu)
  13414. {
  13415. for (int i = ordinates[0] - 40; i < ordinates[0] - 10; i++)
  13416. {
  13417. sum = threshEdge[i, i + 2, middle - 50, middle + 50].Sum();
  13418. if ((int)sum > 75)
  13419. {
  13420. int newOrdinate = i;
  13421. ordinates.Insert(0, i);
  13422. break;
  13423. }
  13424. }
  13425. for (int i = ordinates[ordinates.Count - 1] + 40; i > ordinates[ordinates.Count - 1] + 10; i--)
  13426. {
  13427. sum = threshEdge[i - 2, i, middle - 50, middle + 50].Sum();
  13428. if ((int)sum > 75)
  13429. {
  13430. int newOrdinate = i;
  13431. ordinates.Add(i);
  13432. break;
  13433. }
  13434. }
  13435. }
  13436. //for (int i = ordinates[ordinates.Count-1] + 40; i > ordinates[ordinates.Count-1] + 5&&i<thresh.Rows-20; i--)
  13437. //{
  13438. // sum = image[i-5, i , middle - 100, middle + 100].Sum();
  13439. // if ((int)sum > 100)
  13440. // {
  13441. // int newOrdinate = i;
  13442. // ordinates.Add(newOrdinate /*+ 10*/);
  13443. // break;
  13444. // }
  13445. //}
  13446. //for (int i = ordinates[0] - 40; i < ordinates[0] - 5; i++)
  13447. //{
  13448. // sum = image[i , i+5, middle - 100, middle + 100].Sum();
  13449. // if ((int)sum > 100)
  13450. // {
  13451. // ordinates.Insert(0, i);
  13452. // break;
  13453. // }
  13454. //}
  13455. if (ordinates.Count <= 3)
  13456. {
  13457. ordinates.Clear();
  13458. for (int i = 0; i < image.Rows - 20; i++)
  13459. {
  13460. sum = image[i, i + 5, middle - 100, middle + 100].Sum();
  13461. if ((int)sum > 150)
  13462. {
  13463. int newOrdinate = i;
  13464. ordinates.Add(newOrdinate /*+ 10*/);
  13465. i += 150;
  13466. }
  13467. }
  13468. }
  13469. }
  13470. }
  13471. public void DieGouTopBottom(Mat gray, out int ordinate, int[] range, string direction)
  13472. {
  13473. ordinate = 0;
  13474. Mat crop = gray[range[0], range[1], gray.Cols / 2 - 100, gray.Cols / 2 + 100].Clone();
  13475. Cv2.GaussianBlur(crop, crop, new Size(11, 11), 5, 5);
  13476. Mat edgeSobel = new Mat();
  13477. EdgeY(crop, out edgeSobel);
  13478. Mat threshEdge = new Mat();
  13479. double t2 = Cv2.Threshold(edgeSobel, threshEdge, 0, 255, ThresholdTypes.Otsu);
  13480. threshEdge = edgeSobel.Threshold(15, 255, ThresholdTypes.Binary);
  13481. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  13482. Mat close = new Mat();
  13483. Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
  13484. close = close / 255;
  13485. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  13486. Mat open = new Mat();
  13487. Cv2.MorphologyEx(close, open, MorphTypes.Open, seOpen);
  13488. GetArea(open, out close, 1000, true);
  13489. Mat result = close.Clone();
  13490. //ImageShow(crop, edgeSobel, threshEdge, result * 255);
  13491. Scalar sum = new Scalar(0);
  13492. switch (direction)
  13493. {
  13494. case "top":
  13495. for (int i = 0; i < crop.Rows - 10; i++)
  13496. {
  13497. sum = result[i, i + 2, 0, result.Cols].Sum();
  13498. if ((int)sum > 100)
  13499. {
  13500. ordinate = i;
  13501. break;
  13502. }
  13503. }
  13504. break;
  13505. case "bottom":
  13506. for (int i = crop.Rows - 1; i > 2; i--)
  13507. {
  13508. sum = result[i - 2, i, 0, result.Cols].Sum();
  13509. if ((int)sum > 100)
  13510. {
  13511. ordinate = i;
  13512. break;
  13513. }
  13514. }
  13515. break;
  13516. }
  13517. }
  13518. //锡膏
  13519. //锡膏H
  13520. public void XigaoKaikouXiangnei(Mat gray, int[] dataArea, int start, out int[] leftShangOrdinates, out int[] rightShangOrdinates, out int[] leftXiaOrdinates, out int[] rightXiaOrdinates)
  13521. {
  13522. leftShangOrdinates = new int[3];
  13523. rightShangOrdinates = new int[3];
  13524. leftXiaOrdinates = new int[3];
  13525. rightXiaOrdinates = new int[3];
  13526. #region//找上下边界
  13527. int[] leftExtractionRange = new int[2];
  13528. int[] rightExtractionRange = new int[2];
  13529. Mat thresh = new Mat();
  13530. double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
  13531. thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
  13532. //ImageShow(thresh * 255);
  13533. Scalar sum = new Scalar();
  13534. Mat result1 = thresh.Clone();
  13535. //for (int i = start; i < result1.Rows; i++)//左侧上界
  13536. //{
  13537. // sum = result1[i, i + 1, dataArea[1]-350, dataArea[1] - 100].Sum();
  13538. // if ((int)sum > 100)
  13539. // {
  13540. // leftExtractionRange[0] = i;
  13541. // break;
  13542. // }
  13543. //}
  13544. for (int i = start; i < result1.Rows; i++)//左侧上界
  13545. {
  13546. //sum = result1[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
  13547. if (result1.Get<byte>(i, dataArea[1] - 100) > 0)
  13548. {
  13549. leftExtractionRange[0] = i;
  13550. break;
  13551. }
  13552. }
  13553. for (int i = leftExtractionRange[0] + 50; i < result1.Rows; i++)//左侧下界
  13554. {
  13555. sum = result1[i, i + 1, dataArea[1] - 200, dataArea[1] - 100].Sum();
  13556. if ((int)sum == 0)
  13557. {
  13558. leftExtractionRange[1] = i;
  13559. break;
  13560. }
  13561. }
  13562. //for (int i = start; i < result1.Rows; i++)//右侧上界
  13563. //{
  13564. // sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2]+350].Sum();
  13565. // if ((int)sum > 100)
  13566. // {
  13567. // rightExtractionRange[0] = i;
  13568. // break;
  13569. // }
  13570. //}
  13571. for (int i = start; i < result1.Rows; i++)//右侧上界
  13572. {
  13573. //sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
  13574. if (result1.Get<byte>(i, dataArea[2] + 100) > 0)
  13575. {
  13576. rightExtractionRange[0] = i;
  13577. break;
  13578. }
  13579. }
  13580. for (int i = rightExtractionRange[0] + 50; i < result1.Rows; i++)//右侧下界
  13581. {
  13582. sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 200].Sum();
  13583. if ((int)sum == 0)
  13584. {
  13585. rightExtractionRange[1] = i;
  13586. break;
  13587. }
  13588. }
  13589. //LineShow(gray, dataArea[1], leftExtractionRange[0], dataArea[1], leftExtractionRange[1]);
  13590. //LineShow(gray, dataArea[2], rightExtractionRange[0], dataArea[2], rightExtractionRange[1]);
  13591. //ImageShow(gray);
  13592. #endregion
  13593. #region//找质量好的位置
  13594. int[] maxSpacing = new int[2];
  13595. Scalar max = new Scalar(0);
  13596. for (int j = dataArea[1] - 300; j < dataArea[1]; j += 40)
  13597. {
  13598. sum = result1[leftExtractionRange[0], leftExtractionRange[1], j - 20, j + 20].Sum();
  13599. if ((int)max < (int)sum)
  13600. {
  13601. max = sum;
  13602. maxSpacing[0] = j;
  13603. }
  13604. }
  13605. max = new Scalar(0);
  13606. for (int j = dataArea[2]; j < dataArea[2] + 300; j += 40)
  13607. {
  13608. sum = result1[rightExtractionRange[0], rightExtractionRange[1], j - 20, j + 20].Sum();
  13609. if ((int)max < (int)sum)
  13610. {
  13611. max = sum;
  13612. maxSpacing[1] = j;
  13613. }
  13614. }
  13615. //LineShow(gray, dataArea[0], start, dataArea[1], start);
  13616. //LineShow(gray, dataArea[2], start, dataArea[3], start);
  13617. //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
  13618. //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
  13619. //ImageShow(gray);
  13620. #endregion
  13621. Mat sobel = new Mat();
  13622. Sobel(thresh, out sobel);
  13623. Mat nomin = new Mat();
  13624. GetArea(sobel, out nomin, 500, true);
  13625. Mat result = nomin.Clone();
  13626. #region//左上
  13627. for (int i = leftExtractionRange[0] - 30; i < leftExtractionRange[0]; i++)
  13628. {
  13629. sum = result[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
  13630. if ((int)sum > 0)
  13631. {
  13632. leftShangOrdinates[1] = i;
  13633. break;
  13634. }
  13635. }
  13636. //if (leftExtractionRange[0] - leftShangOrdinates[1] < 5)
  13637. // leftShangOrdinates[1] = 0;
  13638. if (leftShangOrdinates[1] != 0)
  13639. {
  13640. for (int j = dataArea[1] - 350; j < dataArea[1] - 100; j++)
  13641. {
  13642. if (result.Get<byte>(leftShangOrdinates[1], j) > 0)
  13643. {
  13644. leftShangOrdinates[0] = j;
  13645. leftShangOrdinates[2] = leftExtractionRange[0];
  13646. break;
  13647. }
  13648. }
  13649. }
  13650. #endregion
  13651. #region //右上
  13652. for (int i = rightExtractionRange[0] - 30; i < rightExtractionRange[0]; i++)
  13653. {
  13654. sum = result[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
  13655. if ((int)sum > 0)
  13656. {
  13657. rightShangOrdinates[1] = i;
  13658. break;
  13659. }
  13660. }
  13661. //if (rightExtractionRange[0] - rightShangOrdinates[1] < 5)
  13662. // rightShangOrdinates[1] = 0;
  13663. if (rightShangOrdinates[1] != 0)
  13664. {
  13665. for (int j = dataArea[2] + 100; j < dataArea[2] + 350; j++)
  13666. {
  13667. if (result.Get<byte>(rightShangOrdinates[1], j) > 0)
  13668. {
  13669. rightShangOrdinates[0] = j;
  13670. rightShangOrdinates[2] = rightExtractionRange[0];
  13671. break;
  13672. }
  13673. }
  13674. }
  13675. #endregion
  13676. Mat zengqiang = new Mat();
  13677. PointEnhancement(gray, out zengqiang);
  13678. Mat sobel2 = new Mat();
  13679. //EdgeY2(gray, out sobel2);
  13680. EdgeY2(zengqiang, out sobel2);
  13681. Mat thresh2 = new Mat();
  13682. thresh2 = sobel2.Threshold(200, 1, ThresholdTypes.Binary);
  13683. Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
  13684. Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
  13685. Mat nomin2 = new Mat();
  13686. GetArea(thresh2, out nomin2, 50, true);
  13687. Mat result2 = nomin2.Clone();
  13688. //ImageShow(nomin2 * 255);
  13689. #region//左下
  13690. int compensate = 5;
  13691. for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 20; i++)
  13692. {
  13693. sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  13694. if ((int)sum > 20)
  13695. {
  13696. leftXiaOrdinates[1] = i;
  13697. break;
  13698. }
  13699. }
  13700. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
  13701. {
  13702. sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  13703. if ((int)sum > 20)
  13704. {
  13705. leftXiaOrdinates[2] = i;
  13706. break;
  13707. }
  13708. }
  13709. leftXiaOrdinates[0] = maxSpacing[0];
  13710. leftXiaOrdinates[1] += compensate;
  13711. #endregion
  13712. #region//右下
  13713. for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 20; i++)
  13714. {
  13715. sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  13716. if ((int)sum > 20)
  13717. {
  13718. rightXiaOrdinates[1] = i;
  13719. break;
  13720. }
  13721. }
  13722. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
  13723. {
  13724. sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  13725. if ((int)sum > 20)
  13726. {
  13727. rightXiaOrdinates[2] = i;
  13728. break;
  13729. }
  13730. }
  13731. rightXiaOrdinates[0] = maxSpacing[1];
  13732. rightXiaOrdinates[1] += compensate;
  13733. #endregion
  13734. #region//下层迭代
  13735. if (leftXiaOrdinates[2] == 0 || rightXiaOrdinates[2] == 0)
  13736. {
  13737. int leftThresh = 200, rightThresh = 200;
  13738. while (leftXiaOrdinates[2] == 0)
  13739. {
  13740. if (leftThresh == 0)
  13741. break;
  13742. leftThresh -= 10;
  13743. thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
  13744. Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
  13745. GetArea(thresh2, out nomin2, 500, true);
  13746. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
  13747. {
  13748. sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  13749. if ((int)sum > 20)
  13750. {
  13751. leftXiaOrdinates[2] = i;
  13752. break;
  13753. }
  13754. }
  13755. }
  13756. while (rightXiaOrdinates[2] == 0)
  13757. {
  13758. if (rightThresh == 0)
  13759. break;
  13760. rightThresh -= 10;
  13761. thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
  13762. Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
  13763. GetArea(thresh2, out nomin2, 500, true);
  13764. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
  13765. {
  13766. sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  13767. if ((int)sum > 20)
  13768. {
  13769. rightXiaOrdinates[2] = i;
  13770. break;
  13771. }
  13772. }
  13773. }
  13774. }
  13775. #endregion
  13776. if (leftShangOrdinates[2] - leftShangOrdinates[1] < 5)
  13777. {
  13778. leftShangOrdinates[0] = 0;
  13779. leftShangOrdinates[1] = 0;
  13780. leftShangOrdinates[2] = 0;
  13781. }
  13782. if (rightShangOrdinates[2] - rightShangOrdinates[1] < 5)
  13783. {
  13784. rightShangOrdinates[0] = 0;
  13785. rightShangOrdinates[1] = 0;
  13786. rightShangOrdinates[2] = 0;
  13787. }
  13788. //LineShow(gray, leftXiaOrdinates[0], leftXiaOrdinates[1], leftXiaOrdinates[0], leftXiaOrdinates[2]);
  13789. //LineShow(gray, rightXiaOrdinates[0], rightXiaOrdinates[1], rightXiaOrdinates[0], rightXiaOrdinates[2]);
  13790. //ImageShow(gray);
  13791. }
  13792. public void XigaoYouheikuai(Mat gray, int[] dataArea, int start, out int[] leftShangOrdinates, out int[] rightShangOrdinates, out int[] leftXiaOrdinates, out int[] rightXiaOrdinates)
  13793. {
  13794. leftShangOrdinates = new int[3];
  13795. rightShangOrdinates = new int[3];
  13796. leftXiaOrdinates = new int[3];
  13797. rightXiaOrdinates = new int[3];
  13798. #region//提取上下边界与黑框边界
  13799. int[] leftExtractionRange = new int[2];
  13800. int[] rightExtractionRange = new int[2];
  13801. int[] blackBorder = new int[2];
  13802. Mat thresh = new Mat();
  13803. double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
  13804. thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
  13805. Scalar sum = new Scalar();
  13806. Mat result1 = thresh.Clone();
  13807. for (int i = start; i < result1.Rows; i++)//左侧上界
  13808. {
  13809. sum = result1[i, i + 1, dataArea[0], dataArea[1] - 100].Sum();
  13810. if ((int)sum > 100)
  13811. {
  13812. leftExtractionRange[0] = i;
  13813. break;
  13814. }
  13815. }
  13816. for (int i = leftExtractionRange[0]; i < result1.Rows; i++)//左侧下界
  13817. {
  13818. sum = result1[i, i + 1, dataArea[0], dataArea[1] - 100].Sum();
  13819. if ((int)sum == 0)
  13820. {
  13821. leftExtractionRange[1] = i;
  13822. break;
  13823. }
  13824. }
  13825. for (int i = start; i < result1.Rows; i++)//右侧上界
  13826. {
  13827. sum = result1[i, i + 1, dataArea[2] + 100, dataArea[3]].Sum();
  13828. if ((int)sum > 100)
  13829. {
  13830. rightExtractionRange[0] = i;
  13831. break;
  13832. }
  13833. }
  13834. for (int i = rightExtractionRange[0]; i < result1.Rows; i++)//右侧下界
  13835. {
  13836. sum = result1[i, i + 1, dataArea[2] + 100, dataArea[3]].Sum();
  13837. if ((int)sum == 0)
  13838. {
  13839. rightExtractionRange[1] = i;
  13840. break;
  13841. }
  13842. }
  13843. Mat sobel = new Mat();
  13844. Sobel(gray, out sobel);
  13845. Mat threshSobel = new Mat();
  13846. threshSobel = sobel.Threshold(10, 1, ThresholdTypes.Binary);
  13847. Mat nomin4 = new Mat();
  13848. GetArea(threshSobel, out nomin4, 500, true);
  13849. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  13850. Mat open = new Mat();
  13851. Cv2.MorphologyEx(threshSobel, open, MorphTypes.Open, seOpen);
  13852. //ImageShow(threshSobel*255, nomin4 * 255);
  13853. Mat result2 = nomin4.Clone();
  13854. for (int j = dataArea[0]; j < dataArea[1]; j++)//提取左黑块边界
  13855. {
  13856. sum = result2[leftExtractionRange[0] - 100, leftExtractionRange[0] - 10, j, j + 1].Sum();
  13857. if ((int)sum > 20)
  13858. {
  13859. blackBorder[0] = j;
  13860. break;
  13861. }
  13862. }
  13863. for (int j = dataArea[3]; j > dataArea[2]; j--)//提取右黑块边界
  13864. {
  13865. sum = result2[rightExtractionRange[0] - 100, rightExtractionRange[0] - 10, j - 1, j].Sum();
  13866. if ((int)sum > 20)
  13867. {
  13868. blackBorder[1] = j;
  13869. break;
  13870. }
  13871. }
  13872. //LineShow(gray, dataArea[0], leftExtractionRange[0], dataArea[0], leftExtractionRange[1]);
  13873. //LineShow(gray, dataArea[2], rightExtractionRange[0], dataArea[2], rightExtractionRange[1]);
  13874. //LineShow(gray, blackBorder[0], leftExtractionRange[0] - 100, blackBorder[0], leftExtractionRange[0] - 10);
  13875. //LineShow(gray, blackBorder[1], rightExtractionRange[0] - 100, blackBorder[1], rightExtractionRange[0] - 10);
  13876. //ImageShow(gray, threshSobel * 255,open*255);
  13877. #endregion
  13878. Mat thresh2 = new Mat();
  13879. thresh2 = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
  13880. Mat sobel2 = new Mat();
  13881. Sobel(thresh2, out sobel2);
  13882. Mat nomin3 = new Mat();
  13883. GetArea(sobel2, out nomin3, 500, true);
  13884. Mat result3 = nomin3.Clone();
  13885. //ImageShow(nomin3 * 255);
  13886. #region//左上
  13887. for (int i = leftExtractionRange[0] - 30; i < leftExtractionRange[0]; i++)
  13888. {
  13889. sum = result3[i, i + 1, dataArea[0], blackBorder[0]].Sum();
  13890. if ((int)sum > 0)
  13891. {
  13892. leftShangOrdinates[1] = i;
  13893. break;
  13894. }
  13895. }
  13896. if (leftShangOrdinates[1] != 0)
  13897. {
  13898. for (int j = dataArea[0]; j < blackBorder[0]; j++)
  13899. {
  13900. if (result3.Get<byte>(leftShangOrdinates[1], j) > 0)
  13901. {
  13902. leftShangOrdinates[0] = j;
  13903. leftShangOrdinates[2] = leftExtractionRange[0];
  13904. break;
  13905. }
  13906. }
  13907. }
  13908. #endregion
  13909. #region//右上
  13910. for (int i = rightExtractionRange[0] - 30; i < rightExtractionRange[0]; i++)
  13911. {
  13912. sum = result3[i, i + 1, blackBorder[1], dataArea[3]].Sum();
  13913. if ((int)sum > 0)
  13914. {
  13915. rightShangOrdinates[1] = i;
  13916. break;
  13917. }
  13918. }
  13919. if (rightShangOrdinates[1] != 0)
  13920. {
  13921. for (int j = blackBorder[1]; j < dataArea[3]; j++)
  13922. {
  13923. if (result3.Get<byte>(rightShangOrdinates[1], j) > 0)
  13924. {
  13925. rightShangOrdinates[0] = j;
  13926. rightShangOrdinates[2] = rightExtractionRange[0];
  13927. break;
  13928. }
  13929. }
  13930. }
  13931. #endregion
  13932. //LineShow(gray, leftShangOrdinates[0], leftShangOrdinates[1], leftShangOrdinates[0], leftShangOrdinates[2]);
  13933. //LineShow(gray, rightShangOrdinates[0], rightShangOrdinates[1], rightShangOrdinates[0], rightShangOrdinates[2]);
  13934. //ImageShow(gray, sobel2 * 255);
  13935. #region //提取最大间距的位置
  13936. int[] maxSpacing = new int[2];
  13937. Scalar max = new Scalar(0);
  13938. for (int j = blackBorder[0]; j < dataArea[1]; j += 40)
  13939. {
  13940. sum = result1[leftExtractionRange[0], leftExtractionRange[1], j - 20, j + 20].Sum();
  13941. if ((int)max < (int)sum)
  13942. {
  13943. max = sum;
  13944. maxSpacing[0] = j;
  13945. }
  13946. }
  13947. max = new Scalar(0);
  13948. for (int j = dataArea[2]; j < blackBorder[1]; j += 40)
  13949. {
  13950. sum = result1[rightExtractionRange[0], rightExtractionRange[1], j - 20, j + 20].Sum();
  13951. if ((int)max < (int)sum)
  13952. {
  13953. max = sum;
  13954. maxSpacing[1] = j;
  13955. }
  13956. }
  13957. //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
  13958. //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
  13959. //ImageShow(gray);
  13960. #endregion
  13961. Mat sobel3 = new Mat();
  13962. EdgeY2(gray, out sobel3);
  13963. Mat thresh3 = new Mat();
  13964. thresh3 = sobel3.Threshold(200, 1, ThresholdTypes.Binary);
  13965. Cv2.Rectangle(thresh3, new Rect(maxSpacing[0], 0, 1, thresh3.Rows), new Scalar(0), -1);
  13966. Cv2.Rectangle(thresh3, new Rect(maxSpacing[1], 0, 1, thresh3.Rows), new Scalar(0), -1);
  13967. Mat nomin2 = new Mat();
  13968. GetArea(thresh3, out nomin2, 500, true);
  13969. //ImageShow(gray, thresh3 * 255,nomin2*255,gray+nomin2*100);
  13970. #region//左下
  13971. int compensate = 5;
  13972. Mat result4 = nomin2.Clone();
  13973. for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 20; i++)
  13974. {
  13975. sum = result4[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  13976. if ((int)sum > 20)
  13977. {
  13978. leftXiaOrdinates[1] = i;
  13979. break;
  13980. }
  13981. }
  13982. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
  13983. {
  13984. sum = result4[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  13985. if ((int)sum > 20)
  13986. {
  13987. leftXiaOrdinates[2] = i;
  13988. break;
  13989. }
  13990. }
  13991. leftXiaOrdinates[0] = maxSpacing[0];
  13992. leftXiaOrdinates[1] += compensate;
  13993. #endregion
  13994. #region//右下
  13995. for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 20; i++)
  13996. {
  13997. sum = result4[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  13998. if ((int)sum > 20)
  13999. {
  14000. rightXiaOrdinates[1] = i;
  14001. break;
  14002. }
  14003. }
  14004. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
  14005. {
  14006. sum = result4[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14007. if ((int)sum > 20)
  14008. {
  14009. rightXiaOrdinates[2] = i;
  14010. break;
  14011. }
  14012. }
  14013. rightXiaOrdinates[0] = maxSpacing[1];
  14014. rightXiaOrdinates[1] += compensate;
  14015. #endregion
  14016. #region//下层迭代
  14017. if (leftXiaOrdinates[2] == 0 || rightXiaOrdinates[2] == 0)
  14018. {
  14019. int leftThresh = 200, rightThresh = 200;
  14020. while (leftXiaOrdinates[2] == 0)
  14021. {
  14022. if (leftThresh == 0)
  14023. break;
  14024. leftThresh -= 10;
  14025. thresh3 = sobel3.Threshold(leftThresh, 1, ThresholdTypes.Binary);
  14026. Cv2.Rectangle(thresh3, new Rect(maxSpacing[0], 0, 1, thresh3.Rows), new Scalar(0), -1);
  14027. GetArea(thresh3, out nomin2, 500, true);
  14028. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
  14029. {
  14030. sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  14031. if ((int)sum > 20)
  14032. {
  14033. leftXiaOrdinates[2] = i;
  14034. break;
  14035. }
  14036. }
  14037. }
  14038. while (rightXiaOrdinates[2] == 0)
  14039. {
  14040. if (rightThresh == 0)
  14041. break;
  14042. rightThresh -= 10;
  14043. thresh3 = sobel3.Threshold(rightThresh, 1, ThresholdTypes.Binary);
  14044. Cv2.Rectangle(thresh3, new Rect(maxSpacing[1], 0, 1, thresh3.Rows), new Scalar(0), -1);
  14045. GetArea(thresh3, out nomin2, 500, true);
  14046. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
  14047. {
  14048. sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14049. if ((int)sum > 20)
  14050. {
  14051. rightXiaOrdinates[2] = i;
  14052. break;
  14053. }
  14054. }
  14055. }
  14056. }
  14057. #endregion
  14058. //LineShow(gray, leftXiaOrdinates[0], leftXiaOrdinates[1], leftXiaOrdinates[0], leftXiaOrdinates[2]);
  14059. //LineShow(gray, rightXiaOrdinates[0], rightXiaOrdinates[1], rightXiaOrdinates[0], rightXiaOrdinates[2]);
  14060. //ImageShow(gray,nomin2*255);
  14061. }
  14062. public void XigaoXiangShangWanqu(Mat gray, int[] dataArea, int start, out int[] leftShangOrdinates, out int[] rightShangOrdinates, out int[] leftXiaOrdinates, out int[] rightXiaOrdinates)
  14063. {
  14064. leftShangOrdinates = new int[3];
  14065. rightShangOrdinates = new int[3];
  14066. leftXiaOrdinates = new int[3];
  14067. rightXiaOrdinates = new int[3];
  14068. #region//上下边界
  14069. int[] leftExtractionRange = new int[2];
  14070. int[] rightExtractionRange = new int[2];
  14071. Mat thresh = new Mat();
  14072. double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
  14073. thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
  14074. //ImageShow(thresh * 255);
  14075. Scalar sum = new Scalar();
  14076. Mat result1 = thresh.Clone();
  14077. for (int i = start + 300; i < result1.Rows; i++)//左侧上界
  14078. {
  14079. //sum = result1[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
  14080. if (result1.Get<byte>(i, dataArea[1] + 50) > 0)
  14081. {
  14082. leftExtractionRange[0] = i;
  14083. break;
  14084. }
  14085. }
  14086. for (int i = leftExtractionRange[0] + 50; i < result1.Rows; i++)//左侧下界
  14087. {
  14088. sum = result1[i, i + 1, dataArea[1] - 200, dataArea[1] - 100].Sum();
  14089. if ((int)sum == 0)
  14090. {
  14091. leftExtractionRange[1] = i;
  14092. break;
  14093. }
  14094. }
  14095. for (int i = start + 300; i < result1.Rows; i++)//右侧上界
  14096. {
  14097. //sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
  14098. if (result1.Get<byte>(i, dataArea[2] - 50) > 0)
  14099. {
  14100. rightExtractionRange[0] = i;
  14101. break;
  14102. }
  14103. }
  14104. for (int i = rightExtractionRange[0] + 50; i < result1.Rows; i++)//右侧下界
  14105. {
  14106. sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 200].Sum();
  14107. if ((int)sum == 0)
  14108. {
  14109. rightExtractionRange[1] = i;
  14110. break;
  14111. }
  14112. }
  14113. //LineShow(gray, dataArea[1] + 50, leftExtractionRange[0], dataArea[1] + 50, leftExtractionRange[1]);
  14114. //LineShow(gray, dataArea[2] - 50, rightExtractionRange[0], dataArea[2] - 50, rightExtractionRange[1]);
  14115. //ImageShow(gray);
  14116. #endregion
  14117. #region//找质量好的位置
  14118. int[] maxSpacing = new int[2];
  14119. Scalar max = new Scalar(0);
  14120. for (int j = dataArea[1] - 250; j < dataArea[1]; j += 40)
  14121. {
  14122. sum = result1[leftExtractionRange[0], leftExtractionRange[1], j - 20, j + 20].Sum();
  14123. if ((int)max < (int)sum)
  14124. {
  14125. max = sum;
  14126. maxSpacing[0] = j;
  14127. }
  14128. }
  14129. max = new Scalar(0);
  14130. for (int j = dataArea[2]; j < dataArea[2] + 250; j += 40)
  14131. {
  14132. sum = result1[rightExtractionRange[0], rightExtractionRange[1], j - 20, j + 20].Sum();
  14133. if ((int)max < (int)sum)
  14134. {
  14135. max = sum;
  14136. maxSpacing[1] = j;
  14137. }
  14138. }
  14139. //LineShow(gray, dataArea[0], start, dataArea[1], start);
  14140. //LineShow(gray, dataArea[2], start, dataArea[3], start);
  14141. //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
  14142. //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
  14143. //ImageShow(gray);
  14144. #endregion
  14145. #region//左上
  14146. for (int i = leftExtractionRange[0] - 50; i < leftExtractionRange[0]; i++)
  14147. {
  14148. sum = thresh[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
  14149. if ((int)sum > 0)
  14150. {
  14151. leftShangOrdinates[1] = i;
  14152. break;
  14153. }
  14154. }
  14155. if (leftShangOrdinates[1] != 0)
  14156. {
  14157. for (int j = dataArea[1] - 350; j < dataArea[1] - 100; j++)
  14158. {
  14159. if (thresh.Get<byte>(leftShangOrdinates[1], j) > 0)
  14160. {
  14161. leftShangOrdinates[0] = j;
  14162. leftShangOrdinates[2] = leftExtractionRange[0];
  14163. break;
  14164. }
  14165. }
  14166. }
  14167. #endregion
  14168. #region //右上
  14169. for (int i = rightExtractionRange[0] - 50; i < rightExtractionRange[0]; i++)
  14170. {
  14171. sum = thresh[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
  14172. if ((int)sum > 0)
  14173. {
  14174. rightShangOrdinates[1] = i;
  14175. break;
  14176. }
  14177. }
  14178. //if (rightExtractionRange[0] - rightShangOrdinates[1] < 5)
  14179. // rightShangOrdinates[1] = 0;
  14180. if (rightShangOrdinates[1] != 0)
  14181. {
  14182. for (int j = dataArea[2] + 100; j < dataArea[2] + 350; j++)
  14183. {
  14184. if (thresh.Get<byte>(rightShangOrdinates[1], j) > 0)
  14185. {
  14186. rightShangOrdinates[0] = j;
  14187. rightShangOrdinates[2] = rightExtractionRange[0];
  14188. break;
  14189. }
  14190. }
  14191. }
  14192. #endregion
  14193. //LineShow(gray, leftShangOrdinates[0], leftShangOrdinates[1], leftShangOrdinates[0], leftShangOrdinates[2]);
  14194. //LineShow(gray, rightShangOrdinates[0], rightShangOrdinates[1], rightShangOrdinates[0], rightShangOrdinates[2]);
  14195. //ImageShow(gray);
  14196. Mat zengqiang = new Mat();
  14197. PointEnhancement(gray, out zengqiang);
  14198. Mat sobel2 = new Mat();
  14199. //EdgeY2(gray, out sobel2);
  14200. EdgeY2(zengqiang, out sobel2);
  14201. Mat thresh2 = new Mat();
  14202. thresh2 = sobel2.Threshold(200, 1, ThresholdTypes.Binary);
  14203. Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14204. Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14205. Mat nomin2 = new Mat();
  14206. GetArea(thresh2, out nomin2, 50, true);
  14207. Mat result2 = nomin2.Clone();
  14208. //ImageShow(nomin2 * 255);
  14209. #region//左下
  14210. int compensate = 5;
  14211. for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 40; i++)
  14212. {
  14213. sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  14214. if ((int)sum > 20)
  14215. {
  14216. leftXiaOrdinates[1] = i;
  14217. break;
  14218. }
  14219. }
  14220. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
  14221. {
  14222. sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  14223. if ((int)sum > 20)
  14224. {
  14225. leftXiaOrdinates[2] = i;
  14226. break;
  14227. }
  14228. }
  14229. leftXiaOrdinates[0] = maxSpacing[0];
  14230. leftXiaOrdinates[1] += compensate;
  14231. #endregion
  14232. #region//右下
  14233. for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 40; i++)
  14234. {
  14235. sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14236. if ((int)sum > 20)
  14237. {
  14238. rightXiaOrdinates[1] = i;
  14239. break;
  14240. }
  14241. }
  14242. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
  14243. {
  14244. sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14245. if ((int)sum > 20)
  14246. {
  14247. rightXiaOrdinates[2] = i;
  14248. break;
  14249. }
  14250. }
  14251. rightXiaOrdinates[0] = maxSpacing[1];
  14252. rightXiaOrdinates[1] += compensate;
  14253. #endregion
  14254. #region//下层迭代
  14255. if (leftXiaOrdinates[1] < 10 || rightXiaOrdinates[1] < 10)
  14256. {
  14257. int leftThresh = 200, rightThresh = 200;
  14258. while (leftXiaOrdinates[1] < 10)
  14259. {
  14260. if (leftThresh == 0)
  14261. break;
  14262. leftThresh -= 10;
  14263. thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
  14264. Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14265. GetArea(thresh2, out nomin2, 500, true);
  14266. for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 30; i++)
  14267. {
  14268. sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  14269. if ((int)sum > 20)
  14270. {
  14271. leftXiaOrdinates[1] = i;
  14272. break;
  14273. }
  14274. }
  14275. }
  14276. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
  14277. {
  14278. sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  14279. if ((int)sum > 20)
  14280. {
  14281. leftXiaOrdinates[2] = i;
  14282. break;
  14283. }
  14284. }
  14285. while (rightXiaOrdinates[1] < 10)
  14286. {
  14287. if (rightThresh == 0)
  14288. break;
  14289. rightThresh -= 10;
  14290. thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
  14291. Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14292. GetArea(thresh2, out nomin2, 500, true);
  14293. for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 30; i++)
  14294. {
  14295. sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14296. if ((int)sum > 20)
  14297. {
  14298. rightXiaOrdinates[1] = i;
  14299. break;
  14300. }
  14301. }
  14302. }
  14303. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
  14304. {
  14305. sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14306. if ((int)sum > 20)
  14307. {
  14308. rightXiaOrdinates[2] = i;
  14309. break;
  14310. }
  14311. }
  14312. }
  14313. if (leftXiaOrdinates[2] == 0 || rightXiaOrdinates[2] == 0)
  14314. {
  14315. int leftThresh = 200, rightThresh = 200;
  14316. while (leftXiaOrdinates[2] == 0)
  14317. {
  14318. if (leftThresh == 0)
  14319. break;
  14320. leftThresh -= 10;
  14321. thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
  14322. Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14323. GetArea(thresh2, out nomin2, 500, true);
  14324. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
  14325. {
  14326. sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  14327. if ((int)sum > 20)
  14328. {
  14329. leftXiaOrdinates[2] = i;
  14330. break;
  14331. }
  14332. }
  14333. }
  14334. while (rightXiaOrdinates[2] == 0)
  14335. {
  14336. if (rightThresh == 0)
  14337. break;
  14338. rightThresh -= 10;
  14339. thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
  14340. Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14341. GetArea(thresh2, out nomin2, 500, true);
  14342. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
  14343. {
  14344. sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14345. if ((int)sum > 20)
  14346. {
  14347. rightXiaOrdinates[2] = i;
  14348. break;
  14349. }
  14350. }
  14351. }
  14352. }
  14353. #endregion
  14354. //LineShow(gray, leftXiaOrdinates[0], leftXiaOrdinates[1], leftXiaOrdinates[0], leftXiaOrdinates[2]);
  14355. //LineShow(gray, rightXiaOrdinates[0], rightXiaOrdinates[1], rightXiaOrdinates[0], rightXiaOrdinates[2]);
  14356. //ImageShow(gray);
  14357. }
  14358. public void XigaoXiangXiaWanqu(Mat gray, int[] dataArea, int start, out int[] leftShangOrdinates, out int[] rightShangOrdinates, out int[] leftXiaOrdinates, out int[] rightXiaOrdinates)
  14359. {
  14360. leftShangOrdinates = new int[3];
  14361. rightShangOrdinates = new int[3];
  14362. leftXiaOrdinates = new int[3];
  14363. rightXiaOrdinates = new int[3];
  14364. #region//上下边界
  14365. int[] leftExtractionRange = new int[2];
  14366. int[] rightExtractionRange = new int[2];
  14367. Mat thresh = new Mat();
  14368. double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
  14369. thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
  14370. //ImageShow(thresh * 255);
  14371. Scalar sum = new Scalar();
  14372. Mat result1 = thresh.Clone();
  14373. for (int i = start + 300; i < result1.Rows; i++)//左侧上界
  14374. {
  14375. //sum = result1[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
  14376. if (result1.Get<byte>(i, dataArea[1] - 50) > 0)
  14377. {
  14378. leftExtractionRange[0] = i;
  14379. break;
  14380. }
  14381. }
  14382. for (int i = leftExtractionRange[0] + 50; i < result1.Rows; i++)//左侧下界
  14383. {
  14384. sum = result1[i, i + 1, dataArea[0], dataArea[1] - 100].Sum();
  14385. if ((int)sum < 100)
  14386. {
  14387. leftExtractionRange[1] = i;
  14388. break;
  14389. }
  14390. }
  14391. for (int i = start + 300; i < result1.Rows; i++)//右侧上界
  14392. {
  14393. //sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
  14394. if (result1.Get<byte>(i, dataArea[2] + 50) > 0)
  14395. {
  14396. rightExtractionRange[0] = i;
  14397. break;
  14398. }
  14399. }
  14400. for (int i = rightExtractionRange[0] + 50; i < result1.Rows; i++)//右侧下界
  14401. {
  14402. sum = result1[i, i + 1, dataArea[2] + 100, dataArea[3]].Sum();
  14403. if ((int)sum < 100)
  14404. {
  14405. rightExtractionRange[1] = i;
  14406. break;
  14407. }
  14408. }
  14409. //LineShow(gray, dataArea[1] + 50, leftExtractionRange[0], dataArea[1] + 50, leftExtractionRange[1]);
  14410. //LineShow(gray, dataArea[2] - 50, rightExtractionRange[0], dataArea[2] - 50, rightExtractionRange[1]);
  14411. //ImageShow(gray);
  14412. #endregion
  14413. #region//找质量好的位置
  14414. int[] maxSpacing = new int[2];
  14415. Scalar max = new Scalar(0);
  14416. for (int j = dataArea[1] - 300; j < dataArea[1]; j += 40)
  14417. {
  14418. sum = result1[leftExtractionRange[0], leftExtractionRange[1], j - 20, j + 20].Sum();
  14419. if ((int)max < (int)sum)
  14420. {
  14421. max = sum;
  14422. maxSpacing[0] = j;
  14423. }
  14424. }
  14425. max = new Scalar(0);
  14426. for (int j = dataArea[2]; j < dataArea[2] + 300; j += 40)
  14427. {
  14428. sum = result1[rightExtractionRange[0], rightExtractionRange[1], j - 20, j + 20].Sum();
  14429. if ((int)max < (int)sum)
  14430. {
  14431. max = sum;
  14432. maxSpacing[1] = j;
  14433. }
  14434. }
  14435. //LineShow(gray, dataArea[0], start, dataArea[1], start);
  14436. //LineShow(gray, dataArea[2], start, dataArea[3], start);
  14437. //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
  14438. //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
  14439. //ImageShow(gray);
  14440. #endregion
  14441. //Mat zengqiang = new Mat();
  14442. //PointEnhancement(gray, out zengqiang);
  14443. Mat sobel3 = new Mat();
  14444. EdgeY2(gray, out sobel3);
  14445. Mat thresh3 = new Mat();
  14446. thresh3 = sobel3.Threshold(200, 1, ThresholdTypes.Binary);
  14447. Mat nomin3 = new Mat();
  14448. GetArea(thresh3, out nomin3, 500, true);
  14449. //ImageShow(thresh3 * 255,nomin3*255);
  14450. Mat result3 = nomin3.Clone();
  14451. #region//左上
  14452. for (int i = leftExtractionRange[0] - 50; i < result3.Rows; i++)
  14453. {
  14454. if (result3.Get<byte>(i, dataArea[1] - 250) > 0)
  14455. {
  14456. leftShangOrdinates[1] = i;
  14457. break;
  14458. }
  14459. }
  14460. for (int i = leftShangOrdinates[1] + 50; i > leftShangOrdinates[1] + 5; i--)
  14461. {
  14462. if (result3.Get<byte>(i, dataArea[1] - 200) > 0)
  14463. {
  14464. leftShangOrdinates[2] = i;
  14465. break;
  14466. }
  14467. }
  14468. if (leftShangOrdinates[2] != 0)
  14469. leftShangOrdinates[0] = dataArea[1] - 250;
  14470. #endregion
  14471. #region//右上
  14472. for (int i = rightExtractionRange[0] - 50; i < result3.Rows; i++)
  14473. {
  14474. if (result3.Get<byte>(i, dataArea[2] + 250) > 0)
  14475. {
  14476. rightShangOrdinates[1] = i;
  14477. break;
  14478. }
  14479. }
  14480. for (int i = rightShangOrdinates[1] + 50; i > rightShangOrdinates[1] + 5; i--)
  14481. {
  14482. if (result3.Get<byte>(i, dataArea[2] + 200) > 0)
  14483. {
  14484. rightShangOrdinates[2] = i;
  14485. break;
  14486. }
  14487. }
  14488. if (rightShangOrdinates[2] != 0)
  14489. rightShangOrdinates[0] = dataArea[2] + 250;
  14490. #endregion
  14491. maxSpacing[0] = dataArea[1] - 100;
  14492. maxSpacing[1] = dataArea[2] + 100;
  14493. //LineShow(gray, leftShangOrdinates[0], leftShangOrdinates[1], leftShangOrdinates[0], leftShangOrdinates[2]);
  14494. //LineShow(gray, rightShangOrdinates[0], rightShangOrdinates[1], rightShangOrdinates[0], rightShangOrdinates[2]);
  14495. //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
  14496. //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
  14497. //ImageShow(gray);
  14498. Mat zengqiang = new Mat();
  14499. PointEnhancement(gray, out zengqiang);
  14500. Mat sobel2 = new Mat();
  14501. EdgeY2(gray, out sobel2);
  14502. //EdgeY2(zengqiang, out sobel2);
  14503. //Sobel(zengqiang, out sobel2);
  14504. Mat thresh2 = new Mat();
  14505. thresh2 = sobel2.Threshold(200, 1, ThresholdTypes.Binary);
  14506. Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14507. Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14508. Cv2.Rectangle(thresh2, new Rect(0, 0, thresh2.Cols, leftExtractionRange[0]), new Scalar(0), -1);
  14509. Cv2.Rectangle(thresh2, new Rect(0, leftExtractionRange[1] + 20, thresh2.Cols, thresh2.Rows - leftExtractionRange[1] - 20), new Scalar(0), -1);
  14510. Mat nomin2 = new Mat();
  14511. GetArea(thresh2, out nomin2, 10, true);
  14512. Mat result2 = nomin2.Clone();
  14513. //ImageShow(thresh2*255, nomin2 * 255,nomin2*100+gray);
  14514. #region//左下
  14515. int compensate = 5;
  14516. for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 40; i++)
  14517. {
  14518. sum = result2[i, i + 1, maxSpacing[0] - 10, maxSpacing[0] + 10].Sum();
  14519. if ((int)sum > 10)
  14520. {
  14521. leftXiaOrdinates[1] = i;
  14522. break;
  14523. }
  14524. }
  14525. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
  14526. {
  14527. sum = result2[i, i + 1, maxSpacing[0] - 10, maxSpacing[0] + 10].Sum();
  14528. if ((int)sum > 10)
  14529. {
  14530. leftXiaOrdinates[2] = i;
  14531. break;
  14532. }
  14533. }
  14534. leftXiaOrdinates[0] = maxSpacing[0];
  14535. leftXiaOrdinates[1] += compensate;
  14536. #endregion
  14537. #region//右下
  14538. for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 40; i++)
  14539. {
  14540. sum = result2[i, i + 1, maxSpacing[1] - 10, maxSpacing[1] + 10].Sum();
  14541. if ((int)sum > 20)
  14542. {
  14543. rightXiaOrdinates[1] = i;
  14544. break;
  14545. }
  14546. }
  14547. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
  14548. {
  14549. sum = result2[i, i + 1, maxSpacing[1] - 10, maxSpacing[1] + 10].Sum();
  14550. if ((int)sum > 10)
  14551. {
  14552. rightXiaOrdinates[2] = i;
  14553. break;
  14554. }
  14555. }
  14556. rightXiaOrdinates[0] = maxSpacing[1];
  14557. rightXiaOrdinates[1] += compensate;
  14558. #endregion
  14559. #region//下层迭代
  14560. if (leftXiaOrdinates[1] < 10 || rightXiaOrdinates[1] < 10)
  14561. {
  14562. int leftThresh = 200, rightThresh = 200;
  14563. while (leftXiaOrdinates[1] < 10)
  14564. {
  14565. if (leftThresh == 0)
  14566. break;
  14567. leftThresh -= 10;
  14568. thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
  14569. Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14570. GetArea(thresh2, out nomin2, 500, true);
  14571. for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 30; i++)
  14572. {
  14573. sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  14574. if ((int)sum > 20)
  14575. {
  14576. leftXiaOrdinates[1] = i;
  14577. break;
  14578. }
  14579. }
  14580. }
  14581. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
  14582. {
  14583. sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  14584. if ((int)sum > 20)
  14585. {
  14586. leftXiaOrdinates[2] = i;
  14587. break;
  14588. }
  14589. }
  14590. while (rightXiaOrdinates[1] < 10)
  14591. {
  14592. if (rightThresh == 0)
  14593. break;
  14594. rightThresh -= 10;
  14595. thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
  14596. Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14597. GetArea(thresh2, out nomin2, 500, true);
  14598. for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 30; i++)
  14599. {
  14600. sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14601. if ((int)sum > 20)
  14602. {
  14603. rightXiaOrdinates[1] = i;
  14604. break;
  14605. }
  14606. }
  14607. }
  14608. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
  14609. {
  14610. sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14611. if ((int)sum > 20)
  14612. {
  14613. rightXiaOrdinates[2] = i;
  14614. break;
  14615. }
  14616. }
  14617. }
  14618. if (leftXiaOrdinates[2] == 0 || rightXiaOrdinates[2] == 0)
  14619. {
  14620. int leftThresh = 200, rightThresh = 200;
  14621. while (leftXiaOrdinates[2] == 0)
  14622. {
  14623. if (leftThresh == 0)
  14624. break;
  14625. leftThresh -= 10;
  14626. thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
  14627. Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14628. GetArea(thresh2, out nomin2, 500, true);
  14629. for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
  14630. {
  14631. sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
  14632. if ((int)sum > 20)
  14633. {
  14634. leftXiaOrdinates[2] = i;
  14635. break;
  14636. }
  14637. }
  14638. }
  14639. while (rightXiaOrdinates[2] == 0)
  14640. {
  14641. if (rightThresh == 0)
  14642. break;
  14643. rightThresh -= 10;
  14644. thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
  14645. Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
  14646. GetArea(thresh2, out nomin2, 500, true);
  14647. for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
  14648. {
  14649. sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
  14650. if ((int)sum > 20)
  14651. {
  14652. rightXiaOrdinates[2] = i;
  14653. break;
  14654. }
  14655. }
  14656. }
  14657. }
  14658. #endregion
  14659. //LineShow(gray, leftShangOrdinates[0], leftShangOrdinates[1], leftShangOrdinates[0], leftShangOrdinates[2]);
  14660. //LineShow(gray, rightShangOrdinates[0], rightShangOrdinates[1], rightShangOrdinates[0], rightShangOrdinates[2]);
  14661. //LineShow(gray, leftXiaOrdinates[0], leftXiaOrdinates[1], leftXiaOrdinates[0], leftXiaOrdinates[2]);
  14662. //LineShow(gray, rightXiaOrdinates[0], rightXiaOrdinates[1], rightXiaOrdinates[0], rightXiaOrdinates[2]);
  14663. //ImageShow(gray);
  14664. }
  14665. public void GetXigaoArea0(Mat image, out int[] dataArea, out int start)
  14666. {
  14667. dataArea = new int[4];
  14668. start = 0;
  14669. Scalar leftMax = new Scalar(0);
  14670. Scalar rightMax = new Scalar(0);
  14671. int thresh1 = 120, thresh2 = 200;
  14672. for (int j = 0; j < image.Cols; j++)
  14673. {
  14674. leftMax = image[0, image.Rows, j, j + 1].Sum();
  14675. if ((int)leftMax > thresh1)
  14676. {
  14677. dataArea[0] = j;
  14678. break;
  14679. }
  14680. }
  14681. for (int j = dataArea[0]; j < image.Cols; j++)
  14682. {
  14683. leftMax = image[0, image.Rows, j, j + 1].Sum();
  14684. if ((int)leftMax > thresh2)
  14685. {
  14686. dataArea[1] = j;
  14687. break;
  14688. }
  14689. }
  14690. for (int j = image.Cols - 1; j > 0; j--)
  14691. {
  14692. rightMax = image[0, image.Rows, j - 1, j].Sum();
  14693. if ((int)rightMax > thresh1)
  14694. {
  14695. dataArea[3] = j;
  14696. break;
  14697. }
  14698. }
  14699. for (int j = dataArea[3]; j > 0; j--)
  14700. {
  14701. rightMax = image[0, image.Rows, j - 1, j].Sum();
  14702. if ((int)rightMax > thresh2)
  14703. {
  14704. dataArea[2] = j;
  14705. break;
  14706. }
  14707. }
  14708. Scalar sum = new Scalar(0);
  14709. for (int i = 0; i < image.Rows; i++)
  14710. {
  14711. sum = image[i, i + 1, dataArea[0], dataArea[3]].Sum();
  14712. if ((int)sum > 0)
  14713. {
  14714. start = i;
  14715. break;
  14716. }
  14717. }
  14718. }
  14719. public void GetXigaoArea(Mat image, out int[] dataArea, out int start)
  14720. {
  14721. dataArea = new int[4];
  14722. start = 0;
  14723. Scalar leftMax = new Scalar(0);
  14724. Scalar rightMax = new Scalar(0);
  14725. int thresh1 = 140, thresh2 = 300;
  14726. for (int j = 0; j < image.Cols; j++)
  14727. {
  14728. leftMax = image[0, image.Rows, j, j + 1].Sum();
  14729. if ((int)leftMax > thresh1)
  14730. {
  14731. dataArea[0] = j;
  14732. break;
  14733. }
  14734. }
  14735. for (int j = dataArea[0]; j < image.Cols; j++)
  14736. {
  14737. leftMax = image[0, image.Rows, j, j + 1].Sum();
  14738. if ((int)leftMax > thresh2)
  14739. {
  14740. dataArea[1] = j;
  14741. break;
  14742. }
  14743. }
  14744. for (int j = image.Cols - 1; j > 0; j--)
  14745. {
  14746. rightMax = image[0, image.Rows, j - 1, j].Sum();
  14747. if ((int)rightMax > thresh1)
  14748. {
  14749. dataArea[3] = j;
  14750. break;
  14751. }
  14752. }
  14753. for (int j = dataArea[3]; j > 0; j--)
  14754. {
  14755. rightMax = image[0, image.Rows, j - 1, j].Sum();
  14756. if ((int)rightMax > thresh2)
  14757. {
  14758. dataArea[2] = j;
  14759. break;
  14760. }
  14761. }
  14762. Scalar sum = new Scalar(0);
  14763. for (int i = 0; i < image.Rows; i++)
  14764. {
  14765. sum = image[i, i + 1, dataArea[0], dataArea[3]].Sum();
  14766. if ((int)sum > 0)
  14767. {
  14768. start = i;
  14769. break;
  14770. }
  14771. }
  14772. }
  14773. /// <summary>
  14774. /// 判断正向黑块是否突出
  14775. /// </summary>
  14776. /// <param name="image">原图</param>
  14777. /// <param name="contour">二值图</param>
  14778. /// <param name="tuchu">黑块是否突出,true:突出;false:不突出;</param>
  14779. /// <param name="dataArea">提取区域</param>
  14780. /// <param name="start">上像素边界</param>
  14781. /// <param name="Hang"></param>
  14782. public void HeiKuai(Mat image, Mat contour, out bool tuchu, int[] dataArea, int start, out int[] Hang)
  14783. {
  14784. tuchu = new bool();
  14785. Scalar sum = new Scalar(0);
  14786. Hang = new int[4];
  14787. Mat imageBlur = new Mat();
  14788. Cv2.GaussianBlur(image, imageBlur, new Size(11, 11), 5, 5);//模糊
  14789. int minThresh = 18;
  14790. int maxThresh = 20;
  14791. Mat edge = new Mat();
  14792. Cv2.Canny(imageBlur, edge, minThresh, maxThresh);//边缘检测
  14793. ////闭运算
  14794. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  14795. Mat close = new Mat();
  14796. Cv2.MorphologyEx(edge, edge, MorphTypes.Close, se);
  14797. //new Window("edge", WindowMode.Normal, edge * 255);
  14798. //左侧
  14799. for (int i = start; i < edge.Rows; i++)//求左边缘检测图行
  14800. {
  14801. sum = edge[i, i + 1, 500, dataArea[1] - 50].Sum();
  14802. if ((int)sum > 255)
  14803. {
  14804. Hang[0] = i;
  14805. break;
  14806. }
  14807. }
  14808. for (int i = start; i < contour.Rows; i++)//求左二值图行
  14809. {
  14810. sum = contour[i, i + 1, 500, dataArea[1] - 50].Sum();
  14811. if ((int)sum > 5)
  14812. {
  14813. Hang[1] = i;
  14814. break;
  14815. }
  14816. }
  14817. for (int i = start; i < edge.Rows; i++)//求右边缘检测图行
  14818. {
  14819. sum = edge[i, i + 1, dataArea[2] + 50, dataArea[3] - 200].Sum();
  14820. if ((int)sum > 255)
  14821. {
  14822. Hang[2] = i;
  14823. break;
  14824. }
  14825. }
  14826. for (int i = start; i < contour.Rows; i++)//求右二值图行
  14827. {
  14828. sum = contour[i, i + 1, dataArea[2] + 50, dataArea[3] - 200].Sum();
  14829. if ((int)sum > 5)
  14830. {
  14831. Hang[3] = i;
  14832. break;
  14833. }
  14834. }
  14835. if (Hang[1] - Hang[0] > 20 & Hang[3] - Hang[2] > 20)
  14836. {
  14837. tuchu = true;
  14838. }
  14839. else
  14840. {
  14841. tuchu = false;
  14842. }
  14843. }
  14844. public void Heikuai2(Mat gray, int start, int[] dataArea, out bool tuchu)
  14845. {
  14846. tuchu = false;
  14847. #region//測試
  14848. //Mat thresh4 = new Mat();
  14849. //double t3 = Cv2.Threshold(gray, thresh4, 0, 1, ThresholdTypes.Otsu);
  14850. //Mat sobel4 = new Mat();
  14851. //Sobel(thresh4, out sobel4);
  14852. //Mat zengqiang2 = new Mat();
  14853. //Cv2.EqualizeHist(gray, zengqiang2);
  14854. ////ceju.ImageShow(sobel2 * 255);
  14855. //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
  14856. //Window window = new Window("tbar", WindowMode.Normal);//创建一个新窗口"tbar"
  14857. //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 100, 255, cvTrackbarCallback);
  14858. //Cv2.WaitKey();
  14859. //void Text(int value)
  14860. //{
  14861. // //thresh4 = gray.Threshold(t3 - value, 1, ThresholdTypes.Binary);
  14862. // //sobel4 = new Mat();
  14863. // //ceju.Sobel(thresh4, out sobel4);
  14864. // //new Window("tbar", WindowMode.Normal, sobel4 * 255);
  14865. // //EdgeY(zengqiang2, out sobel4);
  14866. // Sobel(gray, out sobel4);
  14867. // thresh4 = sobel4.Threshold(value, 1, ThresholdTypes.Binary);
  14868. // new Window("tbar", WindowMode.Normal, thresh4 * 255);
  14869. //}
  14870. #endregion
  14871. Scalar sum = new Scalar();
  14872. Mat thresh2 = new Mat();
  14873. double t = Cv2.Threshold(gray, thresh2, 0, 1, ThresholdTypes.Otsu);
  14874. thresh2 = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
  14875. Mat sobel2 = new Mat();
  14876. Sobel(thresh2, out sobel2);
  14877. Mat result2 = sobel2.Clone();
  14878. int leftStart = 0;
  14879. for (int i = start - 300; i < result2.Rows; i++)
  14880. {
  14881. sum = result2[i, i + 1, dataArea[0], dataArea[1]].Sum();
  14882. if ((int)sum > 50)
  14883. {
  14884. leftStart = i;
  14885. break;
  14886. }
  14887. }
  14888. Mat sobel = new Mat();
  14889. Sobel(gray, out sobel);
  14890. Mat thresh = new Mat();
  14891. thresh = sobel.Threshold(10, 1, ThresholdTypes.Binary);
  14892. Cv2.Rectangle(thresh, new Rect(dataArea[1] - 50, 0, thresh.Cols - dataArea[1] + 100, thresh.Rows), new Scalar(0), -1);
  14893. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  14894. Mat close = new Mat();
  14895. Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
  14896. //ImageShow(close * 255);
  14897. Mat nomin = new Mat();
  14898. GetArea(close, out nomin, 100, true);
  14899. Mat result1 = nomin.Clone();
  14900. //ImageShow(nomin * 255);
  14901. int heixian = 0;
  14902. for (int i = start + 300; i < leftStart; i++)
  14903. {
  14904. sum = result1[i, i + 1, dataArea[0], dataArea[1]].Sum();
  14905. if ((int)sum > 30)
  14906. {
  14907. heixian = i;
  14908. break;
  14909. }
  14910. }
  14911. //LineShow(gray, 0, leftStart, gray.Cols, leftStart);
  14912. //LineShow(gray, 0, heixian, gray.Cols, heixian);
  14913. //ImageShow(gray);
  14914. if ((leftStart - heixian) > 30 && heixian != 0)
  14915. tuchu = true;
  14916. }
  14917. public void Heikuai3(Mat gray, int start, int[] dataArea, out bool tuchu)
  14918. {
  14919. tuchu = false;
  14920. Mat thresh = new Mat();
  14921. double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
  14922. thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
  14923. Mat result1 = thresh.Clone();
  14924. Mat sobel = new Mat();
  14925. Sobel(gray, out sobel);
  14926. Mat threshSobel = new Mat();
  14927. threshSobel = sobel.Threshold(10, 1, ThresholdTypes.Binary);
  14928. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  14929. Mat open = new Mat();
  14930. Cv2.MorphologyEx(threshSobel, open, MorphTypes.Open, seOpen);
  14931. //Mat nomin = new Mat();
  14932. //GetArea(open, out nomin, 500, true);
  14933. Mat result2 = open.Clone();
  14934. //ImageShow(result2 * 255);
  14935. int leftStart = 0;
  14936. Scalar sum = new Scalar(0);
  14937. for (int i = start + 300; i < result1.Rows; i++)
  14938. {
  14939. sum = result1[i, i + 1, dataArea[0], dataArea[1] - 100].Sum();
  14940. if ((int)sum > 20)
  14941. {
  14942. leftStart = i;
  14943. break;
  14944. }
  14945. }
  14946. //LineShow(gray, dataArea[1] - 400, leftStart - 100, dataArea[1] - 100, leftStart - 10);
  14947. //ImageShow(gray, result2 * 255,result1*255);
  14948. int leftBlackBorder = 0;
  14949. for (int j = dataArea[1] - 400; j < dataArea[1] - 100; j++)
  14950. {
  14951. sum = result2[leftStart - 100, leftStart - 10, j, j + 1].Sum();
  14952. if ((int)sum > 20)
  14953. {
  14954. leftBlackBorder = j;
  14955. break;
  14956. }
  14957. }
  14958. if (leftBlackBorder != 0)
  14959. tuchu = true;
  14960. }
  14961. //判断是否弯曲
  14962. public void WanQu(Mat contour, out bool wanQu, int[] dataArea, int start, out int[] a)
  14963. {
  14964. a = new int[3];
  14965. wanQu = new bool();
  14966. //int max=new int();
  14967. for (int i = start; i < contour.Rows; i++)
  14968. {
  14969. if (contour.Get<byte>(i, dataArea[1] - 150) > 0)
  14970. {
  14971. a[0] = i;
  14972. break;
  14973. }
  14974. }
  14975. for (int i = start; i < contour.Rows; i++)
  14976. {
  14977. if (contour.Get<byte>(i, dataArea[1] - 50) > 0)
  14978. {
  14979. a[1] = i;
  14980. break;
  14981. }
  14982. }
  14983. //int[] zongzuobiao = new int[150 - 51];
  14984. int[] zongzuobiao = new int[dataArea[1] - dataArea[0] - 200];
  14985. int count = 0;
  14986. //for (int j = dataArea[1] - 149; j < dataArea[1] - 51; j++)
  14987. for (int j = dataArea[0] + 100; j < dataArea[1] - 100; j++)
  14988. {
  14989. for (int i = start; i < contour.Rows; i++)
  14990. {
  14991. if (contour.Get<byte>(i, j) > 0)
  14992. {
  14993. zongzuobiao[count] = i;
  14994. count++;
  14995. break;
  14996. }
  14997. }
  14998. }
  14999. int max = zongzuobiao.Max();
  15000. int min = zongzuobiao.Min();
  15001. int idxMax = 0, idxMin = 0;
  15002. for (int i = 0; i < zongzuobiao.Length; i++)
  15003. {
  15004. if (zongzuobiao[i] == max)
  15005. idxMax = i;
  15006. if (zongzuobiao[i] == min)
  15007. idxMin = i;
  15008. }
  15009. //判断是否在中间
  15010. if (idxMax > zongzuobiao.Length / 3 && idxMax < zongzuobiao.Length / 3 * 2)
  15011. {
  15012. wanQu = false;
  15013. }
  15014. else
  15015. {
  15016. wanQu = true;
  15017. }
  15018. }
  15019. public void WanQu2(Mat gray, out bool wanQu, int[] dataArea, int start, out int[] a)
  15020. {
  15021. a = new int[3];
  15022. wanQu = false;
  15023. int leftStart = 0;
  15024. Mat thresh = new Mat();
  15025. double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
  15026. thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
  15027. for (int i = start + 300; i < thresh.Rows; i++)
  15028. {
  15029. if (thresh.Get<byte>(i, dataArea[1] + 50) > 0)
  15030. {
  15031. leftStart = i;
  15032. break;
  15033. }
  15034. }
  15035. Scalar sum = new Scalar();
  15036. for (int i = start + 300; i < leftStart; i++)
  15037. {
  15038. sum = thresh[i, i + 1, dataArea[1] - 200, dataArea[1] - 100].Sum();
  15039. if ((int)sum > 0)
  15040. {
  15041. wanQu = true;
  15042. }
  15043. }
  15044. //LineShow(gray, dataArea[1] + 50, leftStart, dataArea[1] + 50, start);
  15045. //ImageShow(gray,thresh*255);
  15046. }
  15047. public void youCexiangsu(Mat contour, out bool cunzai, out int lowrow, out int[] zuidalie, int[] dataArea)
  15048. {
  15049. cunzai = new bool();
  15050. zuidalie = new int[2];
  15051. lowrow = new int();
  15052. //左最大列
  15053. Scalar max = new Scalar(0);
  15054. for (int j = 0; j < contour.Cols / 2; j++)
  15055. {
  15056. Scalar sum = new Scalar(0);
  15057. sum = contour[0, contour.Rows, j, j + 1].Sum();
  15058. if ((int)sum > (int)max)
  15059. {
  15060. max = sum;
  15061. zuidalie[0] = j;
  15062. }
  15063. }
  15064. ////右最大列
  15065. //Scalar max1 = new Scalar(0);
  15066. //for (int j = contour.Cols; j > contour.Cols / 2; j--)
  15067. //{
  15068. // Scalar sum = new Scalar(0);
  15069. // sum = contour[0, contour.Rows, j-1, j].Sum();
  15070. // if ((int)sum > (int)max1)
  15071. // {
  15072. // max1 = sum;
  15073. // zuidalie[1] = j;
  15074. // }
  15075. //}
  15076. //左底行
  15077. for (int i = 0; i < contour.Rows; i++)
  15078. {
  15079. if (contour.Get<byte>(i, dataArea[1] - 150) > 0)
  15080. {
  15081. lowrow = i;
  15082. break;
  15083. }
  15084. }
  15085. Scalar sum1 = new Scalar(0);
  15086. sum1 = contour[0, lowrow - 150, zuidalie[0] + 100, zuidalie[0] + 101].Sum();
  15087. //Console.WriteLine(sum1);
  15088. if ((int)sum1 > 0)
  15089. {
  15090. cunzai = true;
  15091. }
  15092. else
  15093. {
  15094. cunzai = false;
  15095. }
  15096. }
  15097. /// <summary>
  15098. /// 计算锡膏的上层坐标
  15099. /// </summary>
  15100. /// <param name="contour">二值图</param>
  15101. /// <param name="filter">红色通道增强后图</param>
  15102. /// <param name="leftShang">左边上层坐标,0:横坐标;1:上纵坐标;2:下纵坐标</param>
  15103. /// <param name="rightShang">右边上层坐标,0:横坐标;1:上纵坐标;2:下纵坐标</param>
  15104. /// <param name="upperBorder">上界</param>
  15105. /// <param name="dataArea">数据提取区域</param>
  15106. public void Shang(Mat contour, Mat filter, out int[] leftShang, out int[] rightShang, int upperBorder, int[] dataArea)
  15107. {
  15108. leftShang = new int[3];//0:横坐标;1:上纵坐标;2:下纵坐标
  15109. rightShang = new int[3];
  15110. Scalar sum = new Scalar(0);
  15111. //左侧
  15112. for (int i = upperBorder; i < contour.Rows; i++)//求上纵坐标
  15113. {
  15114. sum = contour[i, i + 1, dataArea[0], dataArea[1] - 50].Sum();
  15115. if ((int)sum > 0)
  15116. {
  15117. leftShang[1] = i;
  15118. break;
  15119. }
  15120. }
  15121. for (int j = dataArea[1] - 50; j > dataArea[0]; j--)//求横坐标
  15122. {
  15123. if (contour.Get<byte>(leftShang[1], j) > 0)
  15124. {
  15125. leftShang[0] = j;
  15126. break;
  15127. }
  15128. }
  15129. Mat cropLeft = filter[leftShang[1] + 2, leftShang[1] + 20, leftShang[0] - 1, leftShang[0] + 1].Clone();
  15130. Mat threshLeft = cropLeft.Threshold(0, 1, ThresholdTypes.Otsu);
  15131. for (int i = 0; i < threshLeft.Rows; i++)//求下纵坐标
  15132. {
  15133. sum = threshLeft[i, i + 1, 0, threshLeft.Cols].Sum();
  15134. if ((int)sum == 0)
  15135. {
  15136. leftShang[2] = i + leftShang[1] + 2;
  15137. break;
  15138. }
  15139. }
  15140. //右侧
  15141. for (int i = upperBorder; i < contour.Rows; i++)//求上纵坐标
  15142. {
  15143. sum = contour[i, i + 1, dataArea[2] + 50, dataArea[3]].Sum();
  15144. if ((int)sum > 0)
  15145. {
  15146. rightShang[1] = i;
  15147. break;
  15148. }
  15149. }
  15150. for (int j = dataArea[2] + 50; j < dataArea[3]; j++)//求横坐标
  15151. {
  15152. if (contour.Get<byte>(rightShang[1], j) > 0)
  15153. {
  15154. rightShang[0] = j;
  15155. break;
  15156. }
  15157. }
  15158. Mat cropRight = filter[rightShang[1] + 2, rightShang[1] + 20, rightShang[0] - 1, rightShang[0] + 1].Clone();
  15159. Mat threshRight = cropRight.Threshold(0, 1, ThresholdTypes.Otsu);
  15160. for (int i = 0; i < threshRight.Rows; i++)//求下纵坐标
  15161. {
  15162. sum = threshRight[i, i + 1, 0, threshRight.Cols].Sum();
  15163. if ((int)sum == 0)
  15164. {
  15165. rightShang[2] = i + rightShang[1] + 2;
  15166. break;
  15167. }
  15168. }
  15169. }
  15170. /// <summary>
  15171. /// 计算锡膏下层坐标
  15172. /// </summary>
  15173. /// <param name="image">原图</param>
  15174. /// <param name="contour">二值图</param>
  15175. /// <param name="leftXia">左边坐标,0:横坐标;1:上纵坐标;2:下纵坐标</param>
  15176. /// <param name="rightXia">右边坐标,0:横坐标;1:上纵坐标;2:下纵坐标</param>
  15177. /// <param name="dataArea">提取区域</param>
  15178. /// <param name="leftUpper">左边上层,leftShangOrdinate[1]</param>
  15179. /// <param name="rightUpper">右边上层,rightShangOrdinate[1]</param>
  15180. public void Xia(Mat image, Mat contour, out int[] leftXia, out int[] rightXia, int[] dataArea, int leftUpper, int rightUpper)
  15181. {
  15182. leftXia = new int[3];
  15183. rightXia = new int[3];
  15184. //leftXia[0] = dataArea[1] - 50;
  15185. //rightXia[0] = dataArea[2] + 50;
  15186. //找下界
  15187. Scalar sum = new Scalar(0);
  15188. int leftLower = 0, rightLower = 0;
  15189. for (int i = leftUpper; i < contour.Rows; i++)
  15190. {
  15191. sum = contour[i, i + 1, dataArea[0], dataArea[1]].Sum();
  15192. if ((int)sum == 0)
  15193. {
  15194. leftLower = i;
  15195. break;
  15196. }
  15197. }
  15198. for (int i = rightUpper; i < contour.Rows; i++)
  15199. {
  15200. sum = contour[i, i + 1, dataArea[2], dataArea[3]].Sum();
  15201. if ((int)sum == 0)
  15202. {
  15203. rightLower = i;
  15204. break;
  15205. }
  15206. }
  15207. //找到中间画线处,局部最大值
  15208. int rangeInside = 30;
  15209. int rangeOutside = 100;
  15210. Scalar max = new Scalar(0);
  15211. for (int j = dataArea[0] + rangeOutside; j < dataArea[1] - rangeInside; j++)
  15212. {
  15213. sum = contour[leftUpper + 20, leftLower - 5, j, j + 50].Sum();
  15214. if ((int)sum > (int)max)
  15215. {
  15216. leftXia[0] = j + 25;
  15217. max = sum;
  15218. }
  15219. }
  15220. max = 0;
  15221. for (int j = dataArea[2] + rangeInside; j < dataArea[3] - rangeInside; j++)
  15222. {
  15223. sum = contour[rightUpper + 20, rightLower - 5, j, j + 50].Sum();
  15224. if ((int)sum > (int)max)
  15225. {
  15226. max = sum;
  15227. rightXia[0] = j + 25;
  15228. }
  15229. }
  15230. //边缘检测
  15231. int minThresh = 30;
  15232. int maxThresh = 55;
  15233. Mat edge = new Mat();
  15234. Cv2.Canny(image, edge, minThresh, maxThresh);
  15235. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  15236. Mat close = new Mat();
  15237. Cv2.MorphologyEx(edge, close, MorphTypes.Close, se);
  15238. Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  15239. Mat erode = new Mat();
  15240. Cv2.Erode(close, erode, se2);
  15241. //ImageShow(edge,close,erode);
  15242. erode = erode / 255;
  15243. //计算高度
  15244. for (int i = leftUpper + 20; i < leftLower - 5; i++)//左边坐标上层
  15245. {
  15246. sum = erode[i, i + 1, leftXia[0] - 10, leftXia[0] + 10].Sum();
  15247. if ((int)sum > 5)
  15248. {
  15249. leftXia[1] = i;
  15250. break;
  15251. }
  15252. }
  15253. for (int i = leftXia[1] + 10; i < leftLower - 5; i++)//左边坐标下层
  15254. {
  15255. sum = erode[i, i + 1, leftXia[0] - 10, leftXia[0] + 10].Sum();
  15256. if ((int)sum > 10)
  15257. {
  15258. leftXia[2] = i;
  15259. break;
  15260. }
  15261. }
  15262. for (int i = rightUpper + 20; i < rightLower - 5; i++)//右边坐标上层
  15263. {
  15264. sum = erode[i, i + 1, rightXia[0] - 10, rightXia[0] + 10].Sum();
  15265. if ((int)sum > 10)
  15266. {
  15267. rightXia[1] = i;
  15268. break;
  15269. }
  15270. }
  15271. for (int i = rightXia[1] + 10; i < rightLower - 5; i++)//右边坐标下层
  15272. {
  15273. sum = erode[i, i + 1, rightXia[0] - 10, rightXia[0] + 10].Sum();
  15274. if ((int)sum > 5)
  15275. {
  15276. rightXia[2] = i;
  15277. break;
  15278. }
  15279. }
  15280. //Mat cropLeft = erode[leftUpper + 20, leftLower - 5, leftXia[0] - 10, leftXia[0] + 10].Clone();
  15281. //Mat cropRight = erode[rightUpper + 20, rightLower - 5, rightXia[0] - 10, rightXia[0] + 10].Clone();
  15282. //ImageShow(cropLeft, cropRight);
  15283. //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
  15284. //CvTrackbarCallback cvTrackbarCallback2 = new CvTrackbarCallback(Text2);
  15285. //Window window = new Window("tbar");//创建一个新窗口"tbar"
  15286. //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 20, 100, cvTrackbarCallback);
  15287. //CvTrackbar cvTrackbar2 = new CvTrackbar("bar2", "tbar", 50, 100, cvTrackbarCallback2);
  15288. //Cv2.WaitKey();
  15289. //LineShow(image, leftXia[0], leftXia[1], leftXia[0], leftXia[2]);
  15290. //LineShow(image, rightXia[0], rightXia[1], rightXia[0], rightXia[2]);
  15291. //ImageShow(image);
  15292. //ImageShow(threshLeft, threshRight);
  15293. //ImageShow(sobelLeft, sobelRight);
  15294. //void Text(int value)
  15295. //{
  15296. // minThresh = value;
  15297. // Cv2.Canny(image, edge, minThresh, maxThresh);
  15298. // new Window("tbar", edge);
  15299. //}
  15300. //void Text2(int value)
  15301. //{
  15302. // maxThresh = value;
  15303. // Cv2.Canny(image, edge, minThresh, maxThresh);
  15304. // new Window("tbar", edge);
  15305. //}
  15306. }
  15307. /// <summary>
  15308. /// 锡膏z 上面测量线的 精确计算
  15309. /// </summary>
  15310. /// <param name="gray"></param>
  15311. /// <param name="y"></param>
  15312. /// <param name="b"></param>
  15313. /// <param name="tonghouY"></param>
  15314. /// <param name="fanghanhouduY"></param>
  15315. /// <param name="a"></param>
  15316. public void Xigaozhoudu_ACC_upLines(Mat gray0, out int xigaohouduY1, out int xigaohouduY2, out int existOneTwoPoint, out int xigaoTopY)
  15317. {
  15318. xigaohouduY1 = 0;// 30;
  15319. xigaohouduY2 = 40;
  15320. xigaoTopY = 0;
  15321. existOneTwoPoint = 0;
  15322. {
  15323. int minGray = 300 * 255;
  15324. int minRowIndex = 0; int minRowInStart = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  15325. xigaohouduY1 = 15;// 10;
  15326. for (int i = Math.Min(55/*65*//*55*/, gray0.Rows) - 5; i > xigaohouduY1; i--)
  15327. {
  15328. curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  15329. curGrayList.Insert(0, curGray);
  15330. if (curGray < minGray)
  15331. {
  15332. minRowIndex = i;
  15333. minGray = curGray;
  15334. }
  15335. else if (curGray - minGray > 100/*400*//*参数调试-阈值*/)
  15336. {
  15337. minRowInStart = i;
  15338. break;
  15339. }
  15340. }
  15341. if (minRowIndex < 30)//继续往下多走10个像素
  15342. {
  15343. minRowInStart = 0; curGrayList.Clear(); minGray = 300 * 255;
  15344. for (int i = Math.Min(65/*55*/, gray0.Rows) - 5; i > xigaohouduY1; i--)
  15345. {
  15346. curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  15347. curGrayList.Insert(0, curGray);
  15348. if (curGray < minGray)
  15349. {
  15350. minRowIndex = i;
  15351. minGray = curGray;
  15352. }
  15353. else if (curGray - minGray > 400/*100*//*400*//*参数调试-阈值*/)
  15354. {
  15355. minRowInStart = i;
  15356. break;
  15357. }
  15358. }
  15359. }
  15360. if (minRowIndex < 30 && minRowInStart > 5)//继续往上走找上端点用于最后的优化,以便找到模糊的
  15361. {
  15362. List<int> upGrays = new List<int>(); int upMinG = 300 * 255;
  15363. for (int i = minRowIndex - 5; i > 5; i--)
  15364. {
  15365. curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  15366. upGrays.Insert(0, curGray);
  15367. if (curGray < minGray)
  15368. {
  15369. xigaoTopY = i;
  15370. break;
  15371. }
  15372. //if (curGray < minGray)
  15373. //{
  15374. // minRowIndex = i;
  15375. // minGray = curGray;
  15376. //}
  15377. //else if (curGray - minGray > 400/*100*//*400*//*参数调试-阈值*/)
  15378. //{
  15379. // minRowInStart = i;
  15380. // break;
  15381. //}
  15382. }
  15383. upMinG = upGrays.Min();
  15384. }
  15385. List<int> upGrayList = new List<int>();
  15386. for (int i = 0; i < minRowIndex; i ++)
  15387. {
  15388. curGray = this.XigaohouduForAreaMin(gray0, i);
  15389. upGrayList.Add(curGray);
  15390. }
  15391. xigaohouduY2 = minRowIndex;
  15392. if (xigaohouduY2 > 30)
  15393. {
  15394. existOneTwoPoint = 1;
  15395. }
  15396. }
  15397. if (false)
  15398. {
  15399. int minGray = 300 * 255;
  15400. int minRowIndex = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  15401. xigaohouduY1 = 10;
  15402. for (int i = xigaohouduY1; i < Math.Min(50, gray0.Rows) - 5; i++)
  15403. {
  15404. int v = gray0.Row[i].CountNonZero();
  15405. if (v > colEnd / 2)
  15406. {
  15407. xigaohouduY1 = i;
  15408. break;
  15409. }
  15410. }
  15411. if (xigaohouduY1 < 36)
  15412. {
  15413. for (int i = xigaohouduY1 + 5/*10*//*5*/; i < Math.Min(55, gray0.Rows) - 5; i++)
  15414. {
  15415. curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  15416. curGrayList.Add(curGray);
  15417. if (curGray < minGray)
  15418. {
  15419. minRowIndex = i;
  15420. minGray = curGray;
  15421. }
  15422. }
  15423. for (int i = (minRowIndex - xigaohouduY1 - 5/*10*/) + 2; i < curGrayList.Count; i += 2)
  15424. {
  15425. if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
  15426. {
  15427. minRowIndex += 1;
  15428. }
  15429. else
  15430. break;
  15431. }
  15432. }
  15433. else
  15434. minRowIndex = xigaohouduY1 + 1;
  15435. xigaohouduY2 = minRowIndex;// 84;// 72;// minRowIndex;
  15436. }
  15437. Console.WriteLine("noSharp:" + xigaohouduY2 + ";xigaohouduY1:" + xigaohouduY1 + "...");
  15438. }
  15439. /// <summary>
  15440. /// 锡膏z 上面测量线的 精确计算
  15441. /// </summary>
  15442. /// <param name="gray"></param>
  15443. /// <param name="y"></param>
  15444. /// <param name="b"></param>
  15445. /// <param name="tonghouY"></param>
  15446. /// <param name="fanghanhouduY"></param>
  15447. /// <param name="a"></param>
  15448. public void Xigaozhoudu_ACC_upLines__0(Mat gray0, out int xigaohouduY1, out int xigaohouduY2)
  15449. {
  15450. xigaohouduY1 = 30;
  15451. xigaohouduY2 = 40;
  15452. {
  15453. int minGray = 300 * 255;
  15454. int minRowIndex = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  15455. xigaohouduY1 = 10;
  15456. for (int i = xigaohouduY1; i < Math.Min(50, gray0.Rows) - 5; i++)
  15457. {
  15458. int v = gray0.Row[i].CountNonZero();
  15459. if (v > colEnd / 2)
  15460. {
  15461. xigaohouduY1 = i;
  15462. break;
  15463. }
  15464. }
  15465. if (xigaohouduY1 < 36)
  15466. {
  15467. for (int i = xigaohouduY1 + 5/*10*//*5*/; i < Math.Min(55, gray0.Rows) - 5; i++)
  15468. {
  15469. curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  15470. curGrayList.Add(curGray);
  15471. if (curGray < minGray)
  15472. {
  15473. minRowIndex = i;
  15474. minGray = curGray;
  15475. }
  15476. }
  15477. for (int i = (minRowIndex - xigaohouduY1 - 5/*10*/) + 2; i < curGrayList.Count; i += 2)
  15478. {
  15479. if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
  15480. {
  15481. minRowIndex += 1;
  15482. }
  15483. else
  15484. break;
  15485. }
  15486. }
  15487. else
  15488. minRowIndex = xigaohouduY1 + 1;
  15489. xigaohouduY2 = minRowIndex;// 84;// 72;// minRowIndex;
  15490. }
  15491. Console.WriteLine("noSharp:" + xigaohouduY2 + ";xigaohouduY1:" + xigaohouduY1 + "...");
  15492. }
  15493. //获取当前行附件最暗的总和
  15494. private int XigaohouduForAreaMin(Mat gray, int rowIndex)
  15495. {
  15496. int areaMin = 0;
  15497. for (int i = 0; i < gray.Cols; i++)
  15498. {
  15499. int colMin = 255;
  15500. for (int j = rowIndex - 0/*1*//*Math.Max(0, rowIndex - 5)*/; j < rowIndex + 1; j++)
  15501. if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
  15502. areaMin += colMin;
  15503. }
  15504. return areaMin;
  15505. }
  15506. /// <summary>
  15507. /// 锡膏z 有开口 厚度 精确计算
  15508. /// </summary>
  15509. /// <param name="gray"></param>
  15510. /// <param name="y"></param>
  15511. /// <param name="b"></param>
  15512. /// <param name="tonghouY"></param>
  15513. /// <param name="fanghanhouduY"></param>
  15514. /// <param name="a"></param>
  15515. public void Xigaozhoudu_ACC(Mat gray0, int fanghanhouduY1__0, out int fanghanhouduY1, out int fanghanhouduY1Bottom, out int minGray, out bool skipUpLine, int a = 0)
  15516. {
  15517. int bottomYDistance = 20;// 25;// 15;// 30;
  15518. int fanghanhouduY1__noSharp = -1;// fanghanhouduY1;
  15519. skipUpLine = false;
  15520. {
  15521. minGray = 300 * 255;
  15522. int minRowIndex = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  15523. fanghanhouduY1Bottom = 0;
  15524. for (int i = Math.Max(0, fanghanhouduY1__0 - 4/*1*//*4*//*19*//*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + bottomYDistance/*25*/, gray0.Rows) - 5; i++)
  15525. {
  15526. curGray = this.FanghanhouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  15527. curGrayList.Add(curGray);
  15528. if (curGray < minGray)
  15529. {
  15530. minRowIndex = i;
  15531. fanghanhouduY1Bottom = i;
  15532. minGray = curGray;
  15533. }
  15534. }
  15535. while (minRowIndex <= 6 && minRowIndex > 1)
  15536. {
  15537. curGray = this.FanghanhouduForAreaMin(gray0, minRowIndex - 1);
  15538. if (curGray < minGray || Math.Abs(curGray - minGray) < 10) minRowIndex -= 1;
  15539. else break;
  15540. }
  15541. for (int i = Math.Max(0, minRowIndex - Math.Max(0, fanghanhouduY1__0 - 4)) + 2; i < curGrayList.Count; i += 2)
  15542. {
  15543. if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
  15544. {
  15545. minRowIndex += 1;
  15546. fanghanhouduY1Bottom += 2;
  15547. }
  15548. else
  15549. break;
  15550. }
  15551. fanghanhouduY1__noSharp = minRowIndex;// 84;// 72;// minRowIndex;
  15552. }
  15553. {
  15554. minGray = 300 * 255;
  15555. //锐化
  15556. //Mat left_small_sharp = BinaryTools.BlurMaskFunction(left_small).CvtColor(ColorConversionCodes.BGRA2GRAY);
  15557. Mat gray = BinaryTools.BlurMaskFunction(gray0.Clone()/*grayRect*/, 4f * 3.14f, 1, 10f).CvtColor(ColorConversionCodes.BGRA2GRAY);
  15558. //Cv2.ImWrite(@"C:\Users\win10SSD\Desktop\BlurMask" + a + "_.jpg", gray);
  15559. int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
  15560. fanghanhouduY1Bottom = 0;
  15561. for (int i = Math.Max(0, fanghanhouduY1__0 - 4/*1*//*4*//*19*//*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + bottomYDistance/*25*/, gray.Rows) - 5; i++)
  15562. {
  15563. curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
  15564. curGrayList.Add(curGray);
  15565. if (curGray < minGray)
  15566. {
  15567. minRowIndex = i;
  15568. fanghanhouduY1Bottom = i;
  15569. minGray = curGray;
  15570. }
  15571. }
  15572. while (minRowIndex <= 6 && minRowIndex > 1)
  15573. {
  15574. curGray = this.FanghanhouduForAreaMin(gray0, minRowIndex - 1);
  15575. if (curGray < minGray || Math.Abs(curGray - minGray) < 10) minRowIndex -= 1;
  15576. else break;
  15577. }
  15578. for (int i = Math.Max(0, minRowIndex - Math.Max(0, fanghanhouduY1__0 - 4)) + 2; i < curGrayList.Count; i += 2)
  15579. {
  15580. if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
  15581. {
  15582. minRowIndex += 1;
  15583. fanghanhouduY1Bottom += 2;
  15584. }
  15585. else
  15586. break;
  15587. }
  15588. //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
  15589. ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
  15590. fanghanhouduY1 = minRowIndex;// 84;// 72;// minRowIndex;
  15591. }
  15592. Console.Write("noSharp:" + fanghanhouduY1__noSharp + ";fanghanhouduY1:" + fanghanhouduY1 + "...");
  15593. if (fanghanhouduY1__noSharp < 8 && fanghanhouduY1 < 8 || fanghanhouduY1__noSharp == 1 && fanghanhouduY1 > 10)
  15594. {
  15595. skipUpLine = true;// false;// true;
  15596. }
  15597. if (Math.Abs(fanghanhouduY1__noSharp - fanghanhouduY1) < 7/* <<7 8*//* << 6 *//*5*/
  15598. || (fanghanhouduY1__noSharp - fanghanhouduY1 </*>*/ 0 && fanghanhouduY1__noSharp - fanghanhouduY1 >/*<*/ -10/*20*/))
  15599. {
  15600. fanghanhouduY1 = fanghanhouduY1__noSharp;
  15601. }
  15602. else
  15603. Console.WriteLine("fanghanhouduY1 far away from fanghanhouduY1__noSharp.");
  15604. }
  15605. //锡膏Z
  15606. /// <summary>
  15607. /// 得到锡膏Z的数据提取区域
  15608. /// </summary>
  15609. /// <param name="contour"></param>
  15610. /// <param name="imageRed"></param>
  15611. /// <param name="dataArea"></param>
  15612. public void GetXigaoZArea(Mat contour, Mat imageRed, out Mat resulet, out int[] dataArea, out int upperBorder, out int lowerBorder)
  15613. {
  15614. resulet = new Mat();
  15615. //闭运算
  15616. Mat close = new Mat();
  15617. Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(27, 27));
  15618. Cv2.MorphologyEx(contour, close, MorphTypes.Open, seClose);
  15619. if ((int)close.Sum() < 200000)
  15620. {
  15621. seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
  15622. Cv2.MorphologyEx(contour, close, MorphTypes.Open, seClose);
  15623. }
  15624. contour = close;
  15625. //将中间目标区域通过掩膜弄出来
  15626. int leftBorder = 0, rightBorder = 0;
  15627. upperBorder = 0; lowerBorder = 0;
  15628. Scalar sum = new Scalar(0);
  15629. for (int i = 0; i < contour.Rows; i++)
  15630. {
  15631. sum = contour[i, i + 1, 0, contour.Cols].Sum();
  15632. if ((int)sum > 200)
  15633. {
  15634. upperBorder = i - 20;
  15635. break;
  15636. }
  15637. }
  15638. for (int i = upperBorder + 100; i < contour.Rows; i++)
  15639. {
  15640. sum = contour[i, i + 1, 0, contour.Cols].Sum();
  15641. if ((int)sum < 200)
  15642. {
  15643. lowerBorder = i;
  15644. break;
  15645. }
  15646. }
  15647. contour = imageRed.Threshold(0, 1, ThresholdTypes.Otsu);
  15648. Mat delete = contour.Clone();
  15649. Cv2.Rectangle(delete, new Rect(0, upperBorder, contour.Cols, lowerBorder - upperBorder), new Scalar(0), -1);
  15650. contour = contour - delete;
  15651. Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  15652. Cv2.MorphologyEx(contour, contour, MorphTypes.Open, seOpen);
  15653. resulet = contour.Clone();
  15654. //选出两个目标的提取区域
  15655. dataArea = new int[4];
  15656. int middle = contour.Cols / 2;
  15657. sum = contour[0, contour.Rows, middle, middle + 1].Sum();
  15658. if ((int)sum > 0)
  15659. {
  15660. for (int j = middle; j > 0; j--)
  15661. {
  15662. sum = contour[0, contour.Rows, j - 1, j].Sum();
  15663. if ((int)sum == 0)
  15664. {
  15665. dataArea[1] = j;
  15666. break;
  15667. }
  15668. }
  15669. for (int j = middle; j < contour.Cols; j++)
  15670. {
  15671. sum = contour[0, contour.Rows, j, j + 1].Sum();
  15672. if ((int)sum == 0)
  15673. {
  15674. dataArea[1] = j;
  15675. break;
  15676. }
  15677. }
  15678. for (int j = dataArea[1] + 10; j < contour.Cols; j++)
  15679. {
  15680. sum = contour[0, contour.Rows, j, j + 1].Sum();
  15681. if ((int)sum > 0)
  15682. {
  15683. dataArea[2] = j;
  15684. break;
  15685. }
  15686. }
  15687. for (int j = dataArea[2] + 10; j < contour.Cols; j++)
  15688. {
  15689. sum = contour[0, contour.Rows, j, j + 1].Sum();
  15690. if ((int)sum == 0)
  15691. {
  15692. dataArea[3] = j;
  15693. break;
  15694. }
  15695. }
  15696. }
  15697. else
  15698. {
  15699. for (int j = middle; j < contour.Cols; j++)
  15700. {
  15701. sum = contour[0, contour.Rows, j, j + 1].Sum();
  15702. if ((int)sum > 0)
  15703. {
  15704. dataArea[0] = j;
  15705. break;
  15706. }
  15707. }
  15708. for (int j = dataArea[0] + 10; j < contour.Cols; j++)
  15709. {
  15710. sum = contour[0, contour.Rows, j, j + 1].Sum();
  15711. if ((int)sum == 0)
  15712. {
  15713. dataArea[1] = j;
  15714. break;
  15715. }
  15716. }
  15717. for (int j = dataArea[1] + 10; j < contour.Cols; j++)
  15718. {
  15719. sum = contour[0, contour.Rows, j, j + 1].Sum();
  15720. if ((int)sum > 0)
  15721. {
  15722. dataArea[2] = j;
  15723. break;
  15724. }
  15725. }
  15726. for (int j = dataArea[2] + 10; j < contour.Cols; j++)
  15727. {
  15728. sum = contour[0, contour.Rows, j, j + 1].Sum();
  15729. if ((int)sum == 0)
  15730. {
  15731. dataArea[3] = j;
  15732. break;
  15733. }
  15734. }
  15735. }
  15736. }
  15737. /// <summary>
  15738. /// 得到所求的坐标
  15739. /// </summary>
  15740. /// <param name="image"></param>
  15741. /// <param name="contour"></param>
  15742. /// <param name="leftOrdinate">0:横坐标;1:上纵坐标;2:下纵坐标</param>
  15743. /// <param name="rightOrdinate">0:横坐标;1:上纵坐标;2:下纵坐标</param>
  15744. /// <param name="upperBorder"></param>
  15745. /// <param name="lowerBorder"></param>
  15746. /// <param name="dataArea"></param>
  15747. public void GetXigaoZOrdinate(Mat image, Mat contour, out int[] leftOrdinate, out int[] rightOrdinate, int upperBorder, int lowerBorder, int[] dataArea)
  15748. {
  15749. //边缘检测
  15750. //ceju.ImageShow(image);
  15751. LineEnhancement(image, out image);
  15752. //ceju.ImageShow(image);
  15753. Cv2.GaussianBlur(image, image, new Size(9, 9), 3, 3);
  15754. Mat edge = new Mat();
  15755. Cv2.Canny(image, edge, 20, 25);
  15756. edge = edge / 255;
  15757. //ceju.ImageShow(edge * 255);
  15758. Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
  15759. Cv2.MorphologyEx(edge, edge, MorphTypes.Close, se);
  15760. //每个目标的第一条线
  15761. int leftFirstLine = 0, rightFirstLine = 0;
  15762. Scalar sum = new Scalar(0);
  15763. for (int i = upperBorder; i < lowerBorder; i++)
  15764. {
  15765. sum = contour[i, i + 1, dataArea[0], dataArea[1]].Sum();
  15766. if ((int)sum > 0)
  15767. {
  15768. leftFirstLine = i; break;
  15769. }
  15770. }
  15771. for (int i = upperBorder; i < lowerBorder; i++)
  15772. {
  15773. sum = contour[i, i + 1, dataArea[2], dataArea[3]].Sum();
  15774. if ((int)sum > 0)
  15775. {
  15776. rightFirstLine = i; break;
  15777. }
  15778. }
  15779. //通过最大值找出测距点
  15780. int leftPoint = 0, rightPoint = 0;
  15781. Scalar max = new Scalar(0);
  15782. int middle1 = (dataArea[0] + dataArea[1]) / 2;
  15783. int middle2 = (dataArea[2] + dataArea[3]) / 2;
  15784. for (int j = middle1 - 40; j < middle1 + 40; j++)
  15785. {
  15786. sum = contour[leftFirstLine + 20, lowerBorder, j, j + 40].Sum();
  15787. if ((int)sum > (int)max)
  15788. {
  15789. max = sum;
  15790. leftPoint = j + 20;
  15791. }
  15792. }
  15793. max = 0;
  15794. for (int j = middle2 - 40; j < middle2 + 40; j++)
  15795. {
  15796. sum = contour[rightFirstLine + 20, lowerBorder, j, j + 40].Sum();
  15797. if ((int)sum > (int)max)
  15798. {
  15799. max = sum;
  15800. rightPoint = j + 20;
  15801. }
  15802. }
  15803. //测距
  15804. leftOrdinate = new int[3];
  15805. rightOrdinate = new int[3];
  15806. int range = 5;
  15807. for (int i = leftFirstLine + 40; i < lowerBorder; i++)
  15808. {
  15809. sum = edge[i, i + 3, leftPoint - range - 5, leftPoint + range + 5].Sum();
  15810. if ((int)sum > 5)
  15811. {
  15812. leftOrdinate[1] = i;
  15813. break;
  15814. }
  15815. }
  15816. for (int i = leftOrdinate[0] + 20; i < lowerBorder; i++)
  15817. {
  15818. sum = edge[i, i + 3, leftPoint - range, leftPoint + range].Sum();
  15819. if ((int)sum > 5)
  15820. {
  15821. leftOrdinate[2] = i;
  15822. break;
  15823. }
  15824. }
  15825. for (int i = rightFirstLine + 40; i < lowerBorder; i++)
  15826. {
  15827. sum = edge[i, i + 3, rightPoint - range - 5, rightPoint + range + 5].Sum();
  15828. if ((int)sum > 5)
  15829. {
  15830. rightOrdinate[1] = i;
  15831. break;
  15832. }
  15833. }
  15834. for (int i = rightOrdinate[0] + 20; i < lowerBorder; i++)
  15835. {
  15836. sum = edge[i, i + 3, rightPoint - range, rightPoint + range].Sum();
  15837. if ((int)sum > 5)
  15838. {
  15839. rightOrdinate[2] = i;
  15840. break;
  15841. }
  15842. }
  15843. leftOrdinate[0] = leftPoint;
  15844. rightOrdinate[0] = rightPoint;
  15845. }
  15846. public void GetXigaoZOrdinate2(Mat gray, out int[] leftUpprOrdinate, out int[] rightUpperOrdinate, out int[] leftLwerOrdinate, out int[] rightLowerOrdinate, int upperBorder, int lowerBorder, int[] dataArea)
  15847. {
  15848. if (upperBorder < 0) upperBorder = 0;
  15849. leftUpprOrdinate = new int[3];
  15850. rightUpperOrdinate = new int[3];
  15851. leftLwerOrdinate = new int[3];
  15852. rightLowerOrdinate = new int[3];
  15853. Mat zengqiang = new Mat();
  15854. PointEnhancement(gray, out zengqiang);
  15855. Mat thresh = new Mat();
  15856. double t = Cv2.Threshold(zengqiang, thresh, 0, 255, ThresholdTypes.Otsu);
  15857. thresh = zengqiang.Threshold(t + 65, 1, ThresholdTypes.Binary);
  15858. Scalar sum = new Scalar(0);
  15859. int[] b = new int[2];
  15860. //尋找質量好的位置
  15861. for (int j = 300; j < thresh.Cols - 200; j++)
  15862. {
  15863. sum = thresh[upperBorder, lowerBorder, j, j + 150].Sum();
  15864. if ((int)sum > 4000)
  15865. {
  15866. b[0] = j;
  15867. break;
  15868. }
  15869. }
  15870. if (b[0] == 0)
  15871. {
  15872. for (int j = 300; j < thresh.Cols - 200; j++)
  15873. {
  15874. sum = thresh[upperBorder, lowerBorder, j, j + 150].Sum();
  15875. if ((int)sum > 3000)
  15876. {
  15877. b[0] = j;
  15878. break;
  15879. }
  15880. }
  15881. }
  15882. for (int j = b[0] + 300; j < thresh.Cols - 200; j++)
  15883. {
  15884. sum = thresh[upperBorder, lowerBorder, j, j + 150].Sum();
  15885. if ((int)sum > 4000)
  15886. {
  15887. b[1] = j;
  15888. break;
  15889. }
  15890. }
  15891. if (b[1] == 0)
  15892. {
  15893. for (int j = b[0] + 300; j < thresh.Cols - 200; j++)
  15894. {
  15895. sum = thresh[upperBorder, lowerBorder, j, j + 150].Sum();
  15896. if ((int)sum > 3000)
  15897. {
  15898. b[1] = j;
  15899. break;
  15900. }
  15901. }
  15902. }
  15903. //精確目標邊界
  15904. int[] border = new int[4];
  15905. for (int j = b[0]; j < thresh.Cols; j += 5)
  15906. {
  15907. sum = thresh[upperBorder, lowerBorder, j, j + 1].Sum();
  15908. if ((int)sum > 0)
  15909. {
  15910. border[0] = j;
  15911. break;
  15912. }
  15913. }
  15914. for (int j = border[0] + 50; j < thresh.Cols; j += 5)
  15915. {
  15916. sum = thresh[upperBorder, lowerBorder, j, (j + 10)>= thresh.Width ? thresh.Width-1: (j + 10)].Sum();
  15917. if ((int)sum == 0)
  15918. {
  15919. border[1] = j;
  15920. break;
  15921. }
  15922. }
  15923. for (int j = b[1]; j < thresh.Cols; j += 5)
  15924. {
  15925. sum = thresh[upperBorder, lowerBorder, j, j + 1].Sum();
  15926. if ((int)sum > 0)
  15927. {
  15928. border[2] = j;
  15929. break;
  15930. }
  15931. }
  15932. for (int j = border[2] + 50; j < thresh.Cols; j += 5)
  15933. {
  15934. sum = thresh[upperBorder, lowerBorder, j, (j + 10) >= thresh.Width ? thresh.Width - 1 : (j + 10)].Sum();
  15935. if ((int)sum == 0)
  15936. {
  15937. border[3] = j;
  15938. break;
  15939. }
  15940. }
  15941. //LineShow(gray, b[0], upperBorder, b[0], lowerBorder);
  15942. //LineShow(gray, b[1], upperBorder, b[1], lowerBorder);
  15943. //LineShow(gray, border[0], upperBorder, border[0], lowerBorder);
  15944. //LineShow(gray, border[1], upperBorder, border[1], lowerBorder);
  15945. //LineShow(gray, border[2], upperBorder, border[2], lowerBorder);
  15946. //LineShow(gray, border[3], upperBorder, border[3], lowerBorder);
  15947. //ImageShow(gray, thresh * 255);
  15948. //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
  15949. //Mat close = new Mat();
  15950. //Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
  15951. Mat sobel = new Mat();
  15952. Sobel(thresh, out sobel);
  15953. Mat result = sobel.Clone();
  15954. //ImageShow(sobel * 255);
  15955. int leftMiddle = (border[0] + border[1]) / 2;
  15956. int rightMiddle = (border[2] + border[3]) / 2;
  15957. int compensate = 5;
  15958. //計算下層測量綫長度
  15959. for (int i = upperBorder + 50; i < upperBorder + 70; i++)
  15960. {
  15961. sum = result[i, i + 1, leftMiddle - 40<0?0: leftMiddle - 40, leftMiddle + 40].Sum();
  15962. if ((int)sum > 50)
  15963. {
  15964. leftLwerOrdinate[1] = i + compensate;
  15965. break;
  15966. }
  15967. }
  15968. if (leftLwerOrdinate[1] == 0)
  15969. {
  15970. for (int i = upperBorder + 50; i < upperBorder + 70; i++)
  15971. {
  15972. sum = result[i, i + 2, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, leftMiddle + 40].Sum();
  15973. if ((int)sum > 20)
  15974. {
  15975. leftLwerOrdinate[1] = i + compensate;
  15976. break;
  15977. }
  15978. }
  15979. }
  15980. for (int i = leftLwerOrdinate[1] + 15; i < lowerBorder + 20; i++)
  15981. {
  15982. sum = result[i, i + 1, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, leftMiddle + 40].Sum();
  15983. if ((int)sum > 50)
  15984. {
  15985. leftLwerOrdinate[2] = i + compensate;
  15986. break;
  15987. }
  15988. }
  15989. if (leftLwerOrdinate[2] == 0)
  15990. {
  15991. for (int i = leftLwerOrdinate[1] + 20; i < lowerBorder + 20; i++)
  15992. {
  15993. sum = result[i, i + 1, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, leftMiddle + 40].Sum();
  15994. if ((int)sum > 20)
  15995. {
  15996. leftLwerOrdinate[2] = i + compensate;
  15997. break;
  15998. }
  15999. }
  16000. }
  16001. leftLwerOrdinate[0] = (border[0] + border[1]) / 2;
  16002. for (int i = upperBorder + 50; i < upperBorder + 70; i++)
  16003. {
  16004. sum = result[i, i + 1, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, rightMiddle + 40].Sum();
  16005. if ((int)sum > 50)
  16006. {
  16007. rightLowerOrdinate[1] = i + compensate;
  16008. break;
  16009. }
  16010. }
  16011. if (rightLowerOrdinate[1] == 0)
  16012. {
  16013. for (int i = upperBorder + 50; i < upperBorder + 70; i++)
  16014. {
  16015. sum = result[i, i + 2, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, rightMiddle + 40].Sum();
  16016. if ((int)sum > 20)
  16017. {
  16018. rightLowerOrdinate[1] = i + compensate;
  16019. break;
  16020. }
  16021. }
  16022. }
  16023. for (int i = rightLowerOrdinate[1] + 15; i < lowerBorder + 20; i++)
  16024. {
  16025. sum = result[i, i + 1, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, rightMiddle + 40].Sum();
  16026. if ((int)sum > 50)
  16027. {
  16028. rightLowerOrdinate[2] = i + compensate;
  16029. break;
  16030. }
  16031. }
  16032. rightLowerOrdinate[0] = (border[2] + border[3]) / 2;
  16033. //LineShow(gray, leftLwerOrdinate[0], leftLwerOrdinate[1], leftLwerOrdinate[0], leftLwerOrdinate[2]);
  16034. //LineShow(gray, rightLowerOrdinate[0], rightLowerOrdinate[1], rightLowerOrdinate[0], rightLowerOrdinate[2]);
  16035. //ImageShow(gray, sobel * 255);
  16036. //計算上層測量綫距離
  16037. Mat thresh2 = zengqiang.Threshold(t - 20, 1, ThresholdTypes.Binary);
  16038. Mat sobel2 = new Mat();
  16039. Sobel(thresh2, out sobel2);
  16040. Mat thresh3 = zengqiang.Threshold(t - 10, 1, ThresholdTypes.Binary);
  16041. Mat sobel3 = new Mat();
  16042. Sobel(thresh3, out sobel3);
  16043. for (int i = upperBorder; i < lowerBorder; i++)
  16044. {
  16045. sum = sobel2[i, i + 1, leftMiddle - 60<0?0: leftMiddle - 60, leftMiddle + 60].Sum();
  16046. //if ((int)sum > 5)
  16047. //{
  16048. // leftUpprOrdinate[1] = 0;
  16049. // break;
  16050. //}
  16051. //else
  16052. if ((int)sum > 0)
  16053. {
  16054. leftUpprOrdinate[1] = i;
  16055. break;
  16056. }
  16057. }
  16058. for (int i = leftUpprOrdinate[1] + 30; i > leftUpprOrdinate[1] + 7; i--)
  16059. {
  16060. sum = sobel3[i - 1, i, leftMiddle - 60 < 0 ? 0 : leftMiddle - 60, leftMiddle + 60].Sum();
  16061. if ((int)sum > 50)
  16062. {
  16063. leftUpprOrdinate[2] = i;
  16064. break;
  16065. }
  16066. }
  16067. leftUpprOrdinate[0] = (border[0] + border[1]) / 2;
  16068. for (int i = upperBorder; i < lowerBorder; i++)
  16069. {
  16070. sum = sobel2[i, i + 1, rightMiddle - 60<0?0 : rightMiddle - 60, rightMiddle + 60].Sum();
  16071. //if ((int)sum > 5)
  16072. //{
  16073. // rightUpperOrdinate[1] = 0;
  16074. // break;
  16075. //}
  16076. //else
  16077. if ((int)sum > 0)
  16078. {
  16079. rightUpperOrdinate[1] = i;
  16080. break;
  16081. }
  16082. }
  16083. for (int i = rightUpperOrdinate[1] + 30; i > rightUpperOrdinate[1] + 7; i--)
  16084. {
  16085. sum = sobel3[i - 1, i, rightMiddle - 60 < 0 ? 0 : rightMiddle - 60, rightMiddle + 60].Sum();
  16086. if ((int)sum > 50)
  16087. {
  16088. rightUpperOrdinate[2] = i;
  16089. break;
  16090. }
  16091. }
  16092. rightUpperOrdinate[0] = (border[2] + border[3]) / 2;
  16093. //LineShow(gray, leftUpprOrdinate[0], leftUpprOrdinate[1], leftUpprOrdinate[0], leftUpprOrdinate[2]);
  16094. //LineShow(gray, rightUpperOrdinate[0], rightUpperOrdinate[1], rightUpperOrdinate[0], rightUpperOrdinate[2]);
  16095. //ImageShow(gray, (sobel2+sobel3) * 100,sobel3*255);
  16096. if (leftUpprOrdinate[1] == 0 || leftUpprOrdinate[2] == 0)
  16097. {
  16098. leftUpprOrdinate[0] = 0;
  16099. leftUpprOrdinate[1] = 0;
  16100. leftUpprOrdinate[2] = 0;
  16101. }
  16102. if (rightUpperOrdinate[1] == 0 || rightUpperOrdinate[2] == 0)
  16103. {
  16104. rightUpperOrdinate[0] = 0;
  16105. rightUpperOrdinate[1] = 0;
  16106. rightUpperOrdinate[2] = 0;
  16107. }
  16108. if (leftUpprOrdinate[1] != 0 || rightUpperOrdinate[1] != 0)
  16109. {
  16110. for (int i = upperBorder - 10; i < lowerBorder; i++)
  16111. {
  16112. sum = sobel2[i, i + 1, leftMiddle - 60, leftMiddle + 60].Sum();
  16113. //if ((int)sum > 5)
  16114. //{
  16115. // leftUpprOrdinate[1] = 0;
  16116. // break;
  16117. //}
  16118. //else
  16119. if ((int)sum > 0)
  16120. {
  16121. leftUpprOrdinate[1] = i;
  16122. break;
  16123. }
  16124. }
  16125. for (int i = leftUpprOrdinate[1] + 30; i > leftUpprOrdinate[1] + 7; i--)
  16126. {
  16127. sum = sobel3[i - 1, i, leftMiddle - 60, leftMiddle + 60].Sum();
  16128. if ((int)sum > 50)
  16129. {
  16130. leftUpprOrdinate[2] = i;
  16131. break;
  16132. }
  16133. }
  16134. leftUpprOrdinate[0] = (border[0] + border[1]) / 2;
  16135. for (int i = upperBorder - 10; i < lowerBorder; i++)
  16136. {
  16137. sum = sobel2[i, i + 1, rightMiddle - 60, rightMiddle + 60].Sum();
  16138. //if ((int)sum > 5)
  16139. //{
  16140. // rightUpperOrdinate[1] = 0;
  16141. // break;
  16142. //}
  16143. //else
  16144. if ((int)sum > 0)
  16145. {
  16146. rightUpperOrdinate[1] = i;
  16147. break;
  16148. }
  16149. }
  16150. for (int i = rightUpperOrdinate[1] + 30; i > rightUpperOrdinate[1] + 7; i--)
  16151. {
  16152. sum = sobel3[i - 1, i, rightMiddle - 60, rightMiddle + 60].Sum();
  16153. if ((int)sum > 50)
  16154. {
  16155. rightUpperOrdinate[2] = i;
  16156. break;
  16157. }
  16158. }
  16159. }
  16160. //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
  16161. //Window window = new Window("tbar");//创建一个新窗口"tbar"
  16162. //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 0, 255, cvTrackbarCallback);
  16163. //Cv2.WaitKey();
  16164. //void Text(int value)
  16165. //{
  16166. // thresh = zengqiang.Threshold(t - value, 255, ThresholdTypes.Binary);
  16167. // new Window("tbar", thresh);
  16168. //}
  16169. }
  16170. //公用
  16171. /// <summary>
  16172. /// 去除圓形
  16173. /// </summary>
  16174. /// <param name="image1"></param>
  16175. /// <param name="image2"></param>
  16176. /// <param name="result"></param>
  16177. /// <param name="dp">累加器分辨率与图像分辨率的反比。默认=1</param>
  16178. /// <param name="minDist">检测到的圆的中心之间的最小距离。</param>
  16179. /// <param name="param1">第一个方法特定的参数</param>
  16180. /// <param name="param2">第二个方法特定于参数</param>
  16181. /// <param name="minRadius">最小半径</param>
  16182. /// <param name="maxRadius">最大半径</param>
  16183. public void RemoveCircles(Mat image1, Mat image2, out Mat result, double dp, double minDist, double param1, double param2, int minRadius, int maxRadius)
  16184. {
  16185. result = 1 - image2.Clone();
  16186. int range = 10;
  16187. //ImageShow(result*255);
  16188. //dp = 1;
  16189. //minDist = 30;
  16190. //param1 = 70;
  16191. //param2 = 30;
  16192. //minRadius = 10;
  16193. //maxRadius = 60;
  16194. CircleSegment[] circles = Cv2.HoughCircles(image1, HoughMethods.Gradient, dp, minDist, param1, param2, minRadius, maxRadius);
  16195. for (int i = 0; i < circles.Length; i++)
  16196. {
  16197. Point center = (Point)circles[i].Center;
  16198. int radius = (int)circles[i].Radius;
  16199. //for(int j=1;j<radius;j++)
  16200. //Cv2.Circle(result, center, j, new Scalar(1));
  16201. Cv2.Rectangle(result, new Rect(center.X - radius - range, center.Y - radius - range, 2 * (radius + range), 2 * (radius + range)), new Scalar(1), -1);
  16202. }
  16203. result = 1 - result;
  16204. //ImageShow(result*255);
  16205. }
  16206. public void RemoveCircles(Mat image1, Mat image2, out Mat result)
  16207. {
  16208. RemoveCircles(image1, image2, out result, 1, 30, 70, 30, 10, 60);
  16209. }
  16210. public void RemoveCircles(Mat image, out Mat result)
  16211. {
  16212. result = new Mat(image.Size(), image.Type());
  16213. Mat hierachy = new Mat();
  16214. Mat[] contoursMat;
  16215. Cv2.FindContours(image, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16216. Point[][] contours;
  16217. HierarchyIndex[] hierarchyIndices;
  16218. Cv2.FindContours(image, out contours, out hierarchyIndices, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16219. for (int i = 0; i < contours.Count(); i++)
  16220. {
  16221. if (contours[i].Count() < 10)
  16222. continue;
  16223. else
  16224. {
  16225. int[] x = new int[contours[i].Count()];
  16226. int[] y = new int[contours[i].Count()];
  16227. for (int j = 0; j < contours[i].Count(); j++)
  16228. {
  16229. x[j] = contours[i][j].X;
  16230. y[j] = contours[i][j].Y;
  16231. }
  16232. double maxY = y.Max();
  16233. double minY = y.Min();
  16234. double maxX = x.Max();
  16235. double minX = x.Min();
  16236. double hengzongbi = (double)((maxX - minX) / (maxY - minY));
  16237. if (hengzongbi > 1.8 || hengzongbi < 0.5)
  16238. {
  16239. Cv2.DrawContours(result, contoursMat, i, new Scalar(1));
  16240. }
  16241. }
  16242. }
  16243. Fill(result, out result, 1);
  16244. }
  16245. /// <summary>
  16246. /// 保留近似直线
  16247. /// </summary>
  16248. /// <param name="image"></param>
  16249. /// <param name="result"></param>
  16250. public void KeepStraight(Mat image, out Mat result, out List<int> ordinates)
  16251. {
  16252. result = new Mat(image.Size(), image.Type());
  16253. ordinates = new List<int>();
  16254. Mat hierachy = new Mat();
  16255. Mat[] contoursMat;
  16256. Cv2.FindContours(image, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16257. Point[][] contours;
  16258. HierarchyIndex[] hierarchyIndices;
  16259. Cv2.FindContours(image, out contours, out hierarchyIndices, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16260. for (int i = 0; i < contours.Count(); i++)
  16261. {
  16262. if (contours[i].Count() < 10)
  16263. continue;
  16264. else
  16265. {
  16266. int[] x = new int[contours[i].Count()];
  16267. int[] y = new int[contours[i].Count()];
  16268. for (int j = 0; j < contours[i].Count(); j++)
  16269. {
  16270. x[j] = contours[i][j].X;
  16271. y[j] = contours[i][j].Y;
  16272. }
  16273. double maxY = y.Max();
  16274. double minY = y.Min();
  16275. double maxX = x.Max();
  16276. double minX = x.Min();
  16277. double hengzongbi = (double)((maxX - minX) / (maxY - minY));
  16278. if (hengzongbi > 20 || hengzongbi < 0.1)
  16279. {
  16280. Cv2.DrawContours(result, contoursMat, i, new Scalar(1));
  16281. //int newOrdinate = (int)(maxY+minY)/2;
  16282. int newOrdinate = (int)minY + 5;
  16283. ordinates.Add(newOrdinate);
  16284. }
  16285. }
  16286. }
  16287. //Fill(result, out result, 1);
  16288. }
  16289. /// <summary>
  16290. /// 保留最大连通域为1,其余为0
  16291. /// </summary>
  16292. /// <param name="image"></param>
  16293. /// <param name="result"></param>
  16294. public void GetMaxArea(Mat image, out Mat result)
  16295. {
  16296. Mat[] contours;
  16297. Mat hierachy = new Mat();
  16298. Cv2.FindContours(image, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16299. double max_area = 0;
  16300. int index = 0;
  16301. for (int i = 0; i < contours.Count(); i++)
  16302. {
  16303. if (Cv2.ContourArea(contours[i]) > max_area)
  16304. {
  16305. max_area = Cv2.ContourArea(contours[i]);
  16306. index = i;
  16307. }
  16308. }
  16309. result = Mat.Zeros(image.Rows, image.Cols, image.Type());
  16310. Cv2.DrawContours(result, contours, index, new Scalar(1));
  16311. Fill(result, out result, 1);
  16312. //ImageShow(image*255,result * 255);
  16313. }
  16314. /// <summary>
  16315. /// 保留面积大于或小于某个值的连通域
  16316. /// </summary>
  16317. /// <param name="contour"></param>
  16318. /// <param name="result"></param>
  16319. /// <param name="value"></param>
  16320. /// <param name="compare">true:大于;false:小于</param>
  16321. public void GetArea(Mat contour, out Mat result, int value, bool compare)
  16322. {
  16323. result = Mat.Zeros(contour.Rows, contour.Cols, contour.Type());
  16324. Mat[] contours;
  16325. Mat hierachy = new Mat();
  16326. Cv2.FindContours(contour, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16327. for (int i = 0; i < contours.Count(); i++)
  16328. {
  16329. switch (compare)
  16330. {
  16331. case true:
  16332. if (Cv2.ContourArea(contours[i]) > value)
  16333. {
  16334. Cv2.DrawContours(result, contours, i, new Scalar(1));
  16335. }
  16336. break;
  16337. case false:
  16338. if (Cv2.ContourArea(contours[i]) < value)
  16339. {
  16340. Cv2.DrawContours(result, contours, i, new Scalar(1));
  16341. }
  16342. break;
  16343. }
  16344. }
  16345. Fill(result, out result, 1);
  16346. }
  16347. public void GetAreaNoFill(Mat contour, out Mat result, int value, bool compare)
  16348. {
  16349. result = Mat.Zeros(contour.Rows, contour.Cols, contour.Type());
  16350. Mat[] contours;
  16351. Mat hierachy = new Mat();
  16352. Cv2.FindContours(contour, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16353. for (int i = 0; i < contours.Count(); i++)
  16354. {
  16355. switch (compare)
  16356. {
  16357. case true:
  16358. if (Cv2.ContourArea(contours[i]) > value)
  16359. {
  16360. Cv2.DrawContours(result, contours, i, new Scalar(1));
  16361. }
  16362. break;
  16363. case false:
  16364. if (Cv2.ContourArea(contours[i]) < value)
  16365. {
  16366. Cv2.DrawContours(result, contours, i, new Scalar(1));
  16367. }
  16368. break;
  16369. }
  16370. }
  16371. //Fill(result, out result, 1);
  16372. }
  16373. /// <summary>
  16374. /// 得到最大连通域的轮廓图
  16375. /// </summary>
  16376. /// <param name="image"></param>
  16377. /// <param name="result"></param>
  16378. public void GetMaxContour(Mat image, out Mat result)
  16379. {
  16380. Mat[] contours;
  16381. Mat hierachy = new Mat();
  16382. Cv2.FindContours(image, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16383. double max_area = 0;
  16384. int index = 0;
  16385. for (int i = 0; i < contours.Count(); i++)
  16386. {
  16387. if (Cv2.ContourArea(contours[i]) > max_area)
  16388. {
  16389. max_area = Cv2.ContourArea(contours[i]);
  16390. index = i;
  16391. }
  16392. }
  16393. result = Mat.Zeros(image.Rows, image.Cols, image.Type());
  16394. Cv2.DrawContours(result, contours, index, new Scalar(1));
  16395. }
  16396. /// <summary>
  16397. /// 得到图像中非零点的坐标
  16398. /// </summary>
  16399. /// <param name="image"></param>
  16400. /// <param name="ordinates"></param>
  16401. public void FindContourOrdinate(Mat image, out Point[] ordinates)
  16402. {
  16403. int nonZeroNumber = Cv2.CountNonZero(image);
  16404. ordinates = new Point[nonZeroNumber];
  16405. int count = 0;
  16406. for (int i = 0; i < image.Rows; i++)
  16407. {
  16408. for (int j = 0; j < image.Cols; j++)
  16409. {
  16410. if (image.Get<byte>(i, j) > 0)
  16411. {
  16412. ordinates[count].X = j;
  16413. ordinates[count].Y = i;
  16414. }
  16415. }
  16416. }
  16417. }
  16418. /// <summary>
  16419. /// 填充图片孔洞
  16420. /// </summary>
  16421. /// <param name="image">输入二值图像</param>
  16422. /// <param name="result">输出图像</param>
  16423. /// <param name="value">填充像素值</param>
  16424. public void Fill(Mat image, out Mat result, int value)
  16425. {
  16426. Size imageSize = image.Size();
  16427. Mat kuozhan = Mat.Zeros(imageSize.Height + 2, imageSize.Width + 2, image.Type());
  16428. image.CopyTo(kuozhan[1, imageSize.Height + 1, 1, imageSize.Width + 1]);
  16429. Cv2.FloodFill(kuozhan, new Point(0, 0), new Scalar(value));
  16430. Mat crop = kuozhan[1, imageSize.Height + 1, 1, imageSize.Width + 1];
  16431. result = image + value - crop;
  16432. }
  16433. /// <summary>
  16434. /// 对图片进行sobel边缘检测
  16435. /// </summary>
  16436. /// <param name="imageContour"></param>
  16437. /// <param name="imageSobel"></param>
  16438. public void Sobel(Mat imageContour, out Mat imageSobel)
  16439. {
  16440. //横向检测
  16441. Mat grad_x = new Mat();
  16442. Mat grad_x2 = new Mat();
  16443. Cv2.Sobel(imageContour, grad_x, MatType.CV_16S, 1, 0);
  16444. Cv2.ConvertScaleAbs(grad_x, grad_x2);
  16445. //纵向检测
  16446. Mat grad_y = new Mat();
  16447. Mat grad_y2 = new Mat();
  16448. Cv2.Sobel(imageContour, grad_y, MatType.CV_16S, 0, 1);
  16449. Cv2.ConvertScaleAbs(grad_y, grad_y2);
  16450. //横纵向混合平均
  16451. imageSobel = new Mat();
  16452. Cv2.AddWeighted(grad_x2, 0.5, grad_y2, 0.5, 0, imageSobel);
  16453. }
  16454. /// <summary>
  16455. /// 比较两个数的大小
  16456. /// </summary>
  16457. /// <param name="a"></param>
  16458. /// <param name="b"></param>
  16459. /// <param name="world">输入选项,big,small</param>
  16460. /// <param name="result">输出大值或小值</param>
  16461. public void ChooseSize(double a, double b, string world, out double result)
  16462. {
  16463. result = 0;
  16464. switch (world)
  16465. {
  16466. case "big":
  16467. if (a > b)
  16468. {
  16469. result = a;
  16470. }
  16471. else
  16472. {
  16473. result = b;
  16474. }
  16475. break;
  16476. case "small":
  16477. if (a < b)
  16478. {
  16479. result = a;
  16480. }
  16481. else
  16482. {
  16483. result = b;
  16484. }
  16485. break;
  16486. }
  16487. }
  16488. /// <summary>
  16489. /// 判断是否有水平直线
  16490. /// </summary>
  16491. /// <param name="image"></param>
  16492. /// <param name="ordinate">输出直线纵坐标</param>
  16493. /// <param name="border">判断区域</param>
  16494. public void DetectStraightLine(Mat image, out int ordinate, int[] border)
  16495. {
  16496. /*
  16497. * 得到每个连通域的坐标集
  16498. * 求每个连通域坐标集最大最小横纵坐标
  16499. * 求横纵长度
  16500. * 同时满足左右长度和上下长度的阈值条件,判定为直线
  16501. */
  16502. ordinate = 0;
  16503. Mat crop = image[border[0], border[1], 0, image.Cols].Clone();
  16504. //Mat hierachy = new Mat();
  16505. //Mat[] contours;
  16506. //Cv2.FindContours(crop, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16507. Point[][] contours;
  16508. HierarchyIndex[] hierarchyIndices;
  16509. Cv2.FindContours(crop, out contours, out hierarchyIndices, RetrievalModes.External, ContourApproximationModes.ApproxNone);
  16510. Mat idx = new Mat();
  16511. for (int i = 0; i < contours.Count(); i++)
  16512. {
  16513. if (contours[i].Count() < 200 || contours[i].Count() > 4000)
  16514. continue;
  16515. else
  16516. {
  16517. int[] x = new int[contours[i].Count()];
  16518. int[] y = new int[contours[i].Count()];
  16519. for (int j = 0; j < contours[i].Count(); j++)
  16520. {
  16521. x[j] = contours[i][j].X;
  16522. y[j] = contours[i][j].Y;
  16523. }
  16524. int maxY = y.Max();
  16525. int minY = y.Min();
  16526. int maxX = x.Max();
  16527. int minX = x.Min();
  16528. if ((maxY - minY) < 25 && (maxX - minX) > 250)
  16529. {
  16530. ordinate = (maxY + minY) / 2 + border[0];
  16531. break;
  16532. }
  16533. }
  16534. }
  16535. }
  16536. public void LineShow(Mat image, int x1, int y1, int x2, int y2)
  16537. {
  16538. Point p1 = new Point();
  16539. Point p2 = new Point();
  16540. p1.X = x1;
  16541. p1.Y = y1;
  16542. p2.X = x2;
  16543. p2.Y = y2;
  16544. Scalar color = new Scalar(0, 0, 255);
  16545. //颜色
  16546. Cv2.Line(image, p1, p2, color, 2, LineTypes.Link8);
  16547. }
  16548. /// <summary>
  16549. /// 图像画线,线颜色是红色
  16550. /// </summary>
  16551. /// <param name="image"></param>
  16552. /// <param name="point1"></param>
  16553. /// <param name="point2"></param>
  16554. public void LineShow(Mat image, Point point1, Point point2)
  16555. {
  16556. Scalar color = new Scalar(0, 0, 255);
  16557. //颜色
  16558. Cv2.Line(image, point1, point2, color, 2, LineTypes.Link8);
  16559. }
  16560. /// <summary>
  16561. /// 图像画线,线条颜色可以选择红,绿,蓝
  16562. /// </summary>
  16563. /// <param name="image">画线图像</param>
  16564. /// <param name="x1"></param>
  16565. /// <param name="y1"></param>
  16566. /// <param name="x2"></param>
  16567. /// <param name="y2"></param>
  16568. /// <param name="colorChoose">颜色选择,red、blue、green</param>
  16569. public void LineShow(Mat image, int x1, int y1, int x2, int y2, string colorChoose)
  16570. {
  16571. Point p1 = new Point();
  16572. Point p2 = new Point();
  16573. p1.X = x1;
  16574. p1.Y = y1;
  16575. p2.X = x2;
  16576. p2.Y = y2;
  16577. Scalar color = new Scalar();//颜色
  16578. switch (colorChoose)
  16579. {
  16580. case "blue":
  16581. color = new Scalar(255, 0, 0);
  16582. break;
  16583. case "green":
  16584. color = new Scalar(0, 255, 0);
  16585. break;
  16586. case "red":
  16587. color = new Scalar(0, 0, 255);
  16588. break;
  16589. default:
  16590. break;
  16591. }
  16592. Cv2.Line(image, p1, p2, color, 2, LineTypes.Link8);
  16593. }
  16594. /// <summary>
  16595. /// 图像画线,线条颜色可以选择红,绿,蓝
  16596. /// </summary>
  16597. /// <param name="image"></param>
  16598. /// <param name="point1"></param>
  16599. /// <param name="point2"></param>
  16600. /// <param name="colorChoose">颜色选择,red、blue、green</param>
  16601. public void LineShow(Mat image, Point point1, Point point2, string colorChoose)
  16602. {
  16603. Scalar color = new Scalar();//颜色
  16604. switch (colorChoose)
  16605. {
  16606. case "blue":
  16607. color = new Scalar(255, 0, 0);
  16608. break;
  16609. case "green":
  16610. color = new Scalar(0, 255, 0);
  16611. break;
  16612. case "red":
  16613. color = new Scalar(0, 0, 255);
  16614. break;
  16615. default:
  16616. break;
  16617. }
  16618. Cv2.Line(image, point1, point2, color, 2, LineTypes.Link8);
  16619. }
  16620. /// <summary>
  16621. /// 图片显示数字,两位小数
  16622. /// </summary>
  16623. /// <param name="image"></param>
  16624. /// <param name="data"></param>
  16625. /// <param name="x"></param>
  16626. /// <param name="y"></param>
  16627. public void TextShow(Mat image, double data, int x, int y)
  16628. {
  16629. data = Math.Round(data, 2);
  16630. Scalar color = new Scalar(255, 0, 0);
  16631. Point p = new Point();
  16632. p.X = x;
  16633. p.Y = y;
  16634. Cv2.PutText(image, data.ToString() + "um", p, HersheyFonts.HersheyComplex, 0.8, color, 2, LineTypes.Link8);
  16635. }
  16636. /// <summary>
  16637. /// 图像线条标记(竖向)
  16638. /// </summary>
  16639. /// <param name="image"></param>
  16640. /// <param name="point1"></param>
  16641. /// <param name="point2"></param>
  16642. /// <param name="data"></param>
  16643. public void LabelVertical(Mat image, Point point1, Point point2, double data)
  16644. {
  16645. double proportion = 0.2689;
  16646. data = data * proportion;
  16647. double middle = (point1.Y + point2.Y) / 2;
  16648. LineShow(image, point1, point2, "blue");
  16649. LineShow(image, point1.X - 5, point1.Y, point1.X + 5, point1.Y, "blue");
  16650. LineShow(image, point2.X - 5, point2.Y, point2.X + 5, point2.Y, "blue");
  16651. TextShow(image, data, point1.X, (int)middle);
  16652. }
  16653. /// <summary>
  16654. /// 图像线条标记(橫向)
  16655. /// </summary>
  16656. /// <param name="image"></param>
  16657. /// <param name="point1"></param>
  16658. /// <param name="point2"></param>
  16659. /// <param name="data"></param>
  16660. public void LabelHorizontal(Mat image, Point point1, Point point2, double data)
  16661. {
  16662. double proportion = 0.2689;
  16663. data = data * proportion;
  16664. double middle = (point1.X + point2.X) / 2;
  16665. LineShow(image, point1, point2, "blue");
  16666. LineShow(image, point1.X, point1.Y - 5, point1.X, point1.Y + 5, "blue");
  16667. LineShow(image, point2.X, point2.Y - 5, point2.X, point2.Y + 5, "blue");
  16668. TextShow(image, data, (int)middle, point1.Y);
  16669. }
  16670. public void ImageShow(Mat image1)
  16671. {
  16672. new Window("image1", WindowMode.Normal, image1);
  16673. Cv2.WaitKey();
  16674. }
  16675. public void ImageShow(Mat image1, Mat image2)
  16676. {
  16677. new Window("image1", WindowMode.Normal, image1);
  16678. new Window("image2", WindowMode.Normal, image2);
  16679. Cv2.WaitKey();
  16680. }
  16681. public void ImageShow(Mat image1, Mat image2, Mat image3)
  16682. {
  16683. new Window("image1", WindowMode.Normal, image1);
  16684. new Window("image2", WindowMode.Normal, image2);
  16685. new Window("image3", WindowMode.Normal, image3);
  16686. Cv2.WaitKey();
  16687. }
  16688. public void ImageShow(Mat image1, Mat image2, Mat image3, Mat image4)
  16689. {
  16690. new Window("image1", WindowMode.Normal, image1);
  16691. new Window("image2", WindowMode.Normal, image2);
  16692. new Window("image3", WindowMode.Normal, image3);
  16693. new Window("image4", WindowMode.Normal, image4);
  16694. Cv2.WaitKey();
  16695. }
  16696. public void ImageShow(Mat image1, Mat image2, Mat image3, Mat image4, Mat image5)
  16697. {
  16698. new Window("image1", WindowMode.Normal, image1);
  16699. new Window("image2", WindowMode.Normal, image2);
  16700. new Window("image3", WindowMode.Normal, image3);
  16701. new Window("image4", WindowMode.Normal, image4);
  16702. new Window("image5", WindowMode.Normal, image5);
  16703. Cv2.WaitKey();
  16704. }
  16705. /// <summary>
  16706. /// 点增强
  16707. /// </summary>
  16708. /// <param name="image"></param>
  16709. /// <param name="result"></param>
  16710. public void PointEnhancement(Mat image, out Mat result)
  16711. {
  16712. result = new Mat(image.Size(), image.Type());
  16713. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
  16714. Cv2.Filter2D(image, result, -1, kernel);
  16715. Cv2.ConvertScaleAbs(result, result);
  16716. }
  16717. public void LineEnhancement(Mat image, out Mat result)
  16718. {
  16719. result = new Mat(image.Size(), image.Type());
  16720. InputArray kernel = InputArray.Create<int>(new int[3, 3] { { -1, -1, -1 }, { 1, 5, 1 }, { -1, -1, -1 } });
  16721. Cv2.Filter2D(image, result, -1, kernel);
  16722. //result += 100;
  16723. Cv2.ConvertScaleAbs(result, result);
  16724. }
  16725. /// <summary>
  16726. /// 纵向边缘检测
  16727. /// </summary>
  16728. /// <param name="image"></param>
  16729. /// <param name="result"></param>
  16730. public void EdgeY(Mat image, out Mat result)
  16731. {
  16732. InputArray kernel1 = InputArray.Create<int>(new int[3, 3] { { -5, -10, -5 }, { 0, 0, 0 }, { 5, 10, 5 } });
  16733. InputArray kernel2 = InputArray.Create<int>(new int[3, 3] { { 5, 10, 5 }, { 0, 0, 0 }, { -5, -10, -5 } });
  16734. Mat result1 = new Mat();
  16735. Mat result2 = new Mat();
  16736. Cv2.Filter2D(image, result1, MatType.CV_16SC1, kernel1);
  16737. Cv2.Filter2D(image, result2, MatType.CV_16SC1, kernel2);
  16738. Cv2.ConvertScaleAbs(result1, result1);
  16739. Cv2.ConvertScaleAbs(result2, result2);
  16740. result = new Mat();
  16741. Cv2.AddWeighted(result1, 0.5, result2, 0.5, 0, result);
  16742. }
  16743. public void EdgeY2(Mat image, out Mat result)
  16744. {
  16745. InputArray kernel1 = InputArray.Create<int>(new int[3, 9] { { -5, -5, -5, -5, -5, -5, -5, -5, -5 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 5, 5, 5, 5, 5, 5, 5, 5, 5 } });
  16746. InputArray kernel2 = InputArray.Create<int>(new int[3, 9] { { 5, 5, 5, 5, 5, 5, 5, 5, 5 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { -5, -5, -5, -5, -5, -5, -5, -5, -5 } });
  16747. Mat result1 = new Mat();
  16748. Mat result2 = new Mat();
  16749. Cv2.Filter2D(image, result1, MatType.CV_16SC1, kernel1);
  16750. Cv2.Filter2D(image, result2, MatType.CV_16SC1, kernel2);
  16751. Cv2.ConvertScaleAbs(result1, result1);
  16752. Cv2.ConvertScaleAbs(result2, result2);
  16753. result = new Mat();
  16754. Cv2.AddWeighted(result1, 0.5, result2, 0.5, 0, result);
  16755. }
  16756. /// <summary>
  16757. /// 横向边缘检测
  16758. /// </summary>
  16759. /// <param name="image"></param>
  16760. /// <param name="result"></param>
  16761. public void EdgeX(Mat image, out Mat result)
  16762. {
  16763. InputArray kernel1 = InputArray.Create<int>(new int[3, 3] { { -5, 0, 5 }, { -10, 0, 10 }, { -5, 0, 5 } });
  16764. InputArray kernel2 = InputArray.Create<int>(new int[3, 3] { { 5, 0, -5 }, { 10, 0, -10 }, { 5, 0, -5 } });
  16765. Mat result1 = new Mat();
  16766. Mat result2 = new Mat();
  16767. Cv2.Filter2D(image, result1, MatType.CV_16SC1, kernel1);
  16768. Cv2.Filter2D(image, result2, MatType.CV_16SC1, kernel2);
  16769. Cv2.ConvertScaleAbs(result1, result1);
  16770. Cv2.ConvertScaleAbs(result2, result2);
  16771. result = new Mat();
  16772. Cv2.AddWeighted(result1, 0.5, result2, 0.5, 0, result);
  16773. }
  16774. /// <summary>
  16775. /// 边缘检测,横向边缘检测与纵向边缘检测的均值
  16776. /// </summary>
  16777. /// <param name="image"></param>
  16778. /// <param name="result"></param>
  16779. public void Edge(Mat image, out Mat result)
  16780. {
  16781. result = new Mat();
  16782. Mat result1 = new Mat();
  16783. Mat result2 = new Mat();
  16784. EdgeX(image, out result1);
  16785. EdgeY(image, out result2);
  16786. Cv2.AddWeighted(result1, 0.5, result2, 0.5, 0, result);
  16787. }
  16788. /// <summary>
  16789. /// 求一组坐标点的平均坐标值,去掉最大横纵坐标和最小横纵坐标
  16790. /// </summary>
  16791. /// <param name="x"></param>
  16792. /// <param name="y"></param>
  16793. /// <param name="averOrdinate"></param>
  16794. public void AverOrdinate(int[] x, int[] y, out double[] averOrdinate)
  16795. {
  16796. averOrdinate = new double[2];
  16797. int length = x.Length;
  16798. int maxX = 0, minX = 0, maxY = 0, minY = 0;
  16799. maxX = x.Max();
  16800. minX = x.Min();
  16801. maxY = y.Max();
  16802. minY = y.Min();
  16803. int[] newX = new int[length - 2];
  16804. int[] newY = new int[length - 2];
  16805. bool maxTag = false;
  16806. bool minTag = false;
  16807. int count = 0;
  16808. int zeroX = 0;
  16809. int zeroY = 0;
  16810. for (int i = 0; i < length; i++)
  16811. {
  16812. if (x[i] == maxX && maxTag == false)
  16813. {
  16814. maxTag = true;
  16815. continue;
  16816. }
  16817. else if (x[i] == minX && minTag == false)
  16818. {
  16819. minTag = true;
  16820. continue;
  16821. }
  16822. else if (x[i] != 0)
  16823. {
  16824. newX[count] = x[i];
  16825. count++;
  16826. }
  16827. else if (x[i] == 0)
  16828. zeroX++;
  16829. }
  16830. maxTag = false;
  16831. minTag = false;
  16832. count = 0;
  16833. for (int i = 0; i < length; i++)
  16834. {
  16835. if (y[i] == maxY && maxTag == false)
  16836. {
  16837. maxTag = true;
  16838. continue;
  16839. }
  16840. else if (y[i] == minY && minTag == false)
  16841. {
  16842. minTag = true;
  16843. continue;
  16844. }
  16845. else if (y[i] != 0)
  16846. {
  16847. newY[count] = y[i];
  16848. count++;
  16849. }
  16850. else if (y[i] == 0)
  16851. zeroY++;
  16852. }
  16853. double averX = ((newX.Length - zeroX) == 0) ? 0 : newX.Sum() / (newX.Length - zeroX);
  16854. double averY = ((newY.Length - zeroY) == 0) ? 0 : newY.Sum() / (newY.Length - zeroY);
  16855. averOrdinate[0] = averX;
  16856. averOrdinate[1] = averY;
  16857. }
  16858. /// <summary>
  16859. /// 得到图中非零点的坐标
  16860. /// </summary>
  16861. /// <param name="image"></param>
  16862. /// <param name="idx"></param>
  16863. public void FindNonZeros(Mat image, out Point[] idx)
  16864. {
  16865. Mat thresh = image.Threshold(0, 1, ThresholdTypes.Binary);
  16866. int num = (int)thresh.Sum();//不为0点的数量
  16867. idx = new Point[num];
  16868. int count = 0;
  16869. for (int i = 0; i < image.Rows; i++)
  16870. {
  16871. if ((int)image[i, i + 1, 0, image.Cols].Sum() != 0)
  16872. {
  16873. for (int j = 0; j < image.Cols; j++)
  16874. {
  16875. if (image.Get<byte>(i, j) > 0)
  16876. {
  16877. idx[count].X = j;
  16878. idx[count].Y = i;
  16879. count++;
  16880. }
  16881. }
  16882. }
  16883. }
  16884. }
  16885. /// <summary>
  16886. /// 得到图中非零点最左上角的坐标
  16887. /// </summary>
  16888. /// <param name="image"></param>
  16889. /// <param name="leftTop"></param>
  16890. public void FindLeftTop(Mat image, out Point leftTop)
  16891. {
  16892. leftTop = new Point();
  16893. Point[] idx;
  16894. FindNonZeros(image, out idx);
  16895. int min = 1000000;
  16896. for (int i = 0; i < idx.Length; i++)
  16897. {
  16898. if (min > (idx[i].X + idx[i].Y))
  16899. {
  16900. min = idx[i].X + idx[i].Y;
  16901. leftTop.X = idx[i].X;
  16902. leftTop.Y = idx[i].Y;
  16903. }
  16904. }
  16905. }
  16906. }
  16907. }