1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using OpenCvSharp;
- using SmartCoalApplication.Base.CommTool;
- namespace SmartCoalApplication.Base.AutoMeasure
- {
- public class Ceju
- {
- /// <summary>
- /// 对图像进行二值化,得到产品目标区域
- /// </summary>
- /// <param name="image">单通道图像,一般是红色通道</param>
- /// <param name="imageContour">输出二值图</param>
- public void GetContour(Mat image, out Mat imageContour)
- {
-
- // 均值滤波
- Mat imageFilter = new Mat();
- Cv2.Blur(image, imageFilter, new OpenCvSharp.Size(5, 5));
- // 二值化,获得目标区域
- imageContour = imageFilter.Threshold(0, 1, ThresholdTypes.Otsu);
- //new Window("contour", WindowMode.Normal, imageContour * 255);
- // 闭运算,消除小孔
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 10));// 结构元素
- Cv2.MorphologyEx(imageContour, imageContour, MorphTypes.Close, se);
- Mat fillContour = new Mat();
- Fill(imageContour, out fillContour, 1);
- //new Window("close", WindowMode.Normal, imageContour * 255);
- //new Window("fill", WindowMode.Normal, fillContour * 255);
- //Cv2.WaitKey();
- Mat fanse = 1 - fillContour;
- Scalar sum = fanse.Sum();
- Scalar sumBefore = imageContour.Sum();
- Scalar sumAfter = fillContour.Sum();
- int areaDifference = (int)sumAfter - (int)sumBefore;
- if ((int)sum != 0)//避免整张图全填充
- {
- if (areaDifference < 100)//避免填充区域过多
- imageContour = fillContour;
- }
- #region[清理内存]
- //if (imageFilter != null)
- //{
- // imageFilter.Dispose();
- //}
- //if (fillContour != null)
- //{
- // fillContour.Dispose();
- //}
- #endregion
- }
- public void GetContourTongkongShuangceng(Mat image, out Mat imageContour)
- {
- // 均值滤波
- Mat imageFilter = new Mat();
- Cv2.Blur(image, imageFilter, new OpenCvSharp.Size(5, 5));
- // 二值化,获得目标区域
- imageContour = imageFilter.Threshold(0, 1, ThresholdTypes.Otsu);
- //new Window("contour", WindowMode.Normal, imageContour * 255);
- // 开运算,消除杂质,闭运算,消除小孔
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 10));// 结构元素
- Cv2.MorphologyEx(imageContour, imageContour, MorphTypes.Open, se);
- Cv2.MorphologyEx(imageContour, imageContour, MorphTypes.Close, se);
- Mat fillContour = new Mat();
- Fill(imageContour, out fillContour, 1);
- //new Window("close", WindowMode.Normal, imageContour * 255);
- //new Window("fill", WindowMode.Normal, fillContour * 255);
- //Cv2.WaitKey();
- Mat fanse = 1 - fillContour;
- Scalar sum = fanse.Sum();
- Scalar sumBefore = imageContour.Sum();
- Scalar sumAfter = fillContour.Sum();
- int areaDifference = (int)sumAfter - (int)sumBefore;
- if ((int)sum != 0)//避免整张图全填充
- {
- if (areaDifference < 100)//避免填充区域过多
- imageContour = fillContour;
- }
- }
- public void GetShenmangkongContour(Mat image, out Mat imageContour)
- {
- // 均值滤波
- Mat imageFilter = new Mat();
- Cv2.Blur(image, imageFilter, new OpenCvSharp.Size(5, 5));
- // 二值化,获得目标区域
- imageContour = imageFilter.Threshold(0, 1, ThresholdTypes.Otsu);
- //膨胀
- Mat seDilate = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(20, 1));
- Mat dilate = new Mat();
- Cv2.Dilate(imageContour, dilate, seDilate);
- // 闭运算,消除小孔
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));// 结构元素
- Cv2.MorphologyEx(dilate, imageContour, MorphTypes.Close, se);
- Mat fillContour = new Mat();
- Fill(imageContour, out fillContour, 1);
- //new Window("fill", WindowMode.Normal, fillContour * 255);
- //Cv2.WaitKey();
- Mat fanse = 1 - fillContour;
- Scalar sum = fanse.Sum();
- Scalar sumBefore = imageContour.Sum();
- Scalar sumAfter = fillContour.Sum();
- int areaDifference = (int)sumAfter - (int)sumBefore;
- if ((int)sum != 0)//避免整张图全填充
- {
- if (areaDifference < 2000)//避免填充区域过多
- imageContour = fillContour;
- }
- }
- /// <summary>
- /// 对图片进行上下裁剪,去掉空白的部分
- /// </summary>
- /// <param name="image">输入二值图像</param>
- /// <param name="y">输出去掉的上下边界</param>
- /// <param name="cropContour">输出裁剪后的图片</param>
- public void Crop(Mat image, out int[] y, out Mat cropContour, bool isCropFlag)
- {
- int range = 300;
- if (isCropFlag)
- range = 15;
- y = new int[2] { 0, 0 };
- for (int i = range; i < image.Rows; i++)
- {
- for (int j = 0; j < image.Cols; j++)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- y[0] = i;
- break;
- }
- }
- if (y[0] != 0)
- break;
- }
- for (int i = image.Rows - range; i > range; i--)
- {
- for (int j = 0; j < image.Cols; j++)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- y[1] = i;
- break;
- }
- }
- if (y[1] != 0)
- break;
- }
- cropContour = image[y[0], y[1], 0, image.Cols - 1];
- }
- /// <summary>
- /// 对全图片进行裁剪,去掉上下空白的地方,左右被亮光影响的地方
- /// </summary>
- /// <param name="image">原图</param>
- /// <param name="cropEdge">上下裁剪后边缘图,用作提取孔铜</param>
- /// <param name="y">裁剪的上下边界</param>
- /// <param name="b">用来裁剪槽孔区域的左右边界</param>
- public void Crop2(Mat image, out Mat cropEdge, out int[] y, out int[] b, bool isCropFlag)
- {
- //获得蓝色,绿色,红色通道图片
- Mat[] bgr = Cv2.Split(image);
- Mat imageBlue = bgr[0];
- Mat imageGreen = bgr[1];
- Mat imageRed = bgr[2];
- // 调色
- Mat imageNew = 0.9 * imageGreen + 0.1 * imageRed;
- //滤波
- Mat filter = new Mat();
- Cv2.BilateralFilter(imageNew, filter, 15, 150, 3);
- // 增强
- Mat zengqiang = new Mat();
- InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
- Cv2.Filter2D(filter, zengqiang, -1, kernel);
- Cv2.ConvertScaleAbs(zengqiang, zengqiang);
- //边缘检测
- Mat grad_x2 = new Mat();
- Sobel(zengqiang, out grad_x2);
- //开运算
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));// 结构元素
- Mat open = new Mat();
- Cv2.MorphologyEx(grad_x2, open, MorphTypes.Open, seOpen);
- ////閉運算
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));// 结构元素
- Mat close = new Mat();
- Cv2.MorphologyEx(open, close, MorphTypes.Close, se);
- Mat thresh2 = close.Threshold(0, 1, ThresholdTypes.Otsu);
- //填充
- Mat fill = new Mat();
- Fill(thresh2, out fill, 1);
- //new Window("grad", WindowMode.Normal, grad_x2.Threshold(0, 255, ThresholdTypes.Otsu));
- //new Window("open", WindowMode.Normal, open);
- //new Window("close", WindowMode.Normal, close);
- //Cv2.WaitKey();
- Ceju ceju = new Ceju();
- y = new int[2];
- ceju.Crop(fill, out y, out fill, isCropFlag);//用来计算左右裁剪边界
- //Mat edge = grad_x2.Threshold(0, 1, ThresholdTypes.Otsu);
- cropEdge = thresh2[y[0], y[1], 0, thresh2.Cols - 1];//用来计算孔铜边缘
- b = new int[4] { 0, 0, 0, 0 };
- int sum = 0;
- int range = 100;
- if (isCropFlag)
- range = 15;
- for (int j = range; j < fill.Cols - 1; j++)
- {
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- b[0] = j;
- break;
- }
- }
- if (b[0] != 0)
- break;
- }
- for (int j = b[0] + 100; j < fill.Cols - 1; j++)
- {
- sum = 0;
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- sum = 1;
- break;
- }
- }
- if (sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- if (b[1] - b[0] < 300)
- {
- b[0] = 0;
- for (int j = b[1] + 50; j < fill.Cols - 1; j++)
- {
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- b[0] = j;
- break;
- }
- }
- if (b[0] != 0)
- break;
- }
- for (int j = b[0] + 50; j < fill.Cols - 1; j++)
- {
- sum = 0;
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- sum = 1;
- break;
- }
- }
- if (sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- }
- for (int j = fill.Cols - range; j > 0; j--)
- {
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- b[3] = j;
- break;
- }
- }
- if (b[3] != 0)
- break;
- }
- for (int j = b[3] - 100; j > 0; j--)
- {
- sum = 0;
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- sum = 1;
- break;
- }
- }
- if (sum == 0)
- {
- b[2] = j;
- break;
- }
- }
- if (b[3] - b[2] < 300)
- {
- b[3] = 0;
- for (int j = b[2] - 50; j > 0; j--)
- {
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- b[3] = j;
- break;
- }
- }
- if (b[3] != 0)
- break;
- }
- for (int j = b[3] - 50; j > 0; j--)
- {
- sum = 0;
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- sum = 1;
- break;
- }
- }
- if (sum == 0)
- {
- b[2] = j;
- break;
- }
- }
- }
- }
- /// <summary>
- /// 用于四层孔径的裁剪,减小了左右裁剪的判定标准,其他同Crop2
- /// </summary>
- /// <param name="image"></param>
- /// <param name="cropEdge"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- public void CropSicengKongjing(Mat image, out Mat cropEdge, out int[] y, out int[] b, bool isCropFlag)
- {
- //获得蓝色,绿色,红色通道图片
- Mat[] bgr = Cv2.Split(image);
- Mat imageBlue = bgr[0];
- Mat imageGreen = bgr[1];
- Mat imageRed = bgr[2];
- // 调色
- Mat imageNew = 0.9 * imageGreen + 0.1 * imageRed;
- //滤波
- Mat filter = new Mat();
- Cv2.BilateralFilter(imageNew, filter, 15, 150, 3);
- // 增强
- Mat zengqiang = new Mat();
- InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
- Cv2.Filter2D(filter, zengqiang, -1, kernel);
- Cv2.ConvertScaleAbs(zengqiang, zengqiang);
- //边缘检测
- Mat grad_x2 = new Mat();
- Sobel(zengqiang, out grad_x2);
- //开运算
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));// 结构元素
- Mat open = new Mat();
- Cv2.MorphologyEx(grad_x2, open, MorphTypes.Open, seOpen);
- ////閉運算
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));// 结构元素
- Mat close = new Mat();
- Cv2.MorphologyEx(open, close, MorphTypes.Close, se);
- Mat thresh2 = close.Threshold(0, 1, ThresholdTypes.Otsu);
- //填充
- Mat fill = new Mat();
- Fill(thresh2, out fill, 1);
- Ceju ceju = new Ceju();
- y = new int[2];
- ceju.Crop(fill, out y, out fill, isCropFlag);//用来计算左右裁剪边界
- //Mat edge = grad_x2.Threshold(0, 1, ThresholdTypes.Otsu);
- cropEdge = thresh2[y[0], y[1], 0, thresh2.Cols - 1];//用来计算孔铜边缘
- b = new int[4] { 0, 0, 0, 0 };
- int sum = 0;
- int range = 100;
- if (isCropFlag)
- range = 15;
- for (int j = range; j < fill.Cols - 1; j++)
- {
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- b[0] = j;
- break;
- }
- }
- if (b[0] != 0)
- break;
- }
- for (int j = b[0] + 100; j < fill.Cols - 1; j++)
- {
- sum = 0;
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- sum = 1;
- break;
- }
- }
- if (sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- if (b[1] - b[0] < 50)
- {
- b[0] = 0;
- for (int j = b[1] + 50; j < fill.Cols - 1; j++)
- {
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- b[0] = j;
- break;
- }
- }
- if (b[0] != 0)
- break;
- }
- for (int j = b[0] + 50; j < fill.Cols - 1; j++)
- {
- sum = 0;
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- sum = 1;
- break;
- }
- }
- if (sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- }
- for (int j = fill.Cols - range; j > 0; j--)
- {
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- b[3] = j;
- break;
- }
- }
- if (b[3] != 0)
- break;
- }
- for (int j = b[3] - 100; j > 0; j--)
- {
- sum = 0;
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- sum = 1;
- break;
- }
- }
- if (sum == 0)
- {
- b[2] = j;
- break;
- }
- }
- if (b[3] - b[2] < 50)
- {
- b[3] = 0;
- for (int j = b[2] - 50; j > 0; j--)
- {
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- b[3] = j;
- break;
- }
- }
- if (b[3] != 0)
- break;
- }
- for (int j = b[3] - 50; j > 0; j--)
- {
- sum = 0;
- for (int i = 0; i < fill.Rows - 1; i++)
- {
- if (fill.Get<byte>(i, j) > 0)
- {
- sum = 1;
- break;
- }
- }
- if (sum == 0)
- {
- b[2] = j;
- break;
- }
- }
- }
- }
- /// <summary>
- /// 去掉槽孔的亮光,并裁剪
- /// </summary>
- /// <param name="cropContour">上下裁剪后的图片</param>
- /// <param name="border">输出裁剪后的边界</param>
- /// <param name="cropContour2">输出裁剪后的图片</param>
- /// <param name="direction">选择槽孔的左右位置,left/right</param>
- public void CropLight(Mat cropContour, out int border, out Mat cropContour2, string direction)
- {
- int[] b = new int[4] { 0, 0, 0, 0 };
- int sum = 0;
- border = 0;
- cropContour2 = new Mat();
- switch (direction)
- {
- case "left":
- for (int j = 0; j < cropContour.Cols; j++)
- {
- for (int i = 0; i < cropContour.Rows; i++)
- {
- if (cropContour.Get<byte>(i, j) > 0)
- {
- b[0] = j;
- break;
- }
- }
- if (b[0] != 0)
- break;
- }
- for (int j = b[0]; j < cropContour.Cols; j++)
- {
- sum = 0;
- for (int i = 0; i < cropContour.Rows; i++)
- {
- sum += cropContour.Get<byte>(i, j);
- }
- if (sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- for (int j = cropContour.Cols - 1; j > b[1]; j--)
- {
- for (int i = 0; i < cropContour.Rows; i++)
- {
- if (cropContour.Get<byte>(i, j) > 0)
- {
- b[3] = j;
- break;
- }
- }
- if (b[3] != 0)
- break;
- }
- for (int j = b[3]; j > b[1]; j--)
- {
- sum = 0;
- for (int i = 0; i < cropContour.Rows; i++)
- {
- sum += cropContour.Get<byte>(i, j);
- }
- if (sum == 0)
- {
- b[2] = j;
- break;
- }
- }
- if (b[1] - b[0] > 500)
- {
- if (Math.Abs(b[2] - b[1]) > 50)
- {
- cropContour2 = cropContour[0, cropContour.Rows - 1, b[0], b[1]];
- border = b[0];
- }
- else
- cropContour2 = cropContour[0, cropContour.Rows - 1, 0, cropContour.Cols - 1];
- }
- else
- {
- cropContour2 = cropContour[0, cropContour.Rows - 1, 0, b[3]];
- border = 0;
- }
- break;
- case "right":
- for (int j = cropContour.Cols - 1; j > 0; j--)
- {
- for (int i = 0; i < cropContour.Rows; i++)
- {
- if (cropContour.Get<byte>(i, j) > 0)
- {
- b[3] = j;
- break;
- }
- }
- if (b[3] != 0)
- break;
- }
- for (int j = b[3]; j > 0; j--)
- {
- sum = 0;
- for (int i = 0; i < cropContour.Rows; i++)
- {
- sum += cropContour.Get<byte>(i, j);
- }
- if (sum == 0)
- {
- b[2] = j;
- break;
- }
- }
- for (int j = 0; j < b[2]; j++)
- {
- for (int i = 0; i < cropContour.Rows; i++)
- {
- if (cropContour.Get<byte>(i, j) > 0)
- {
- b[0] = j;
- break;
- }
- }
- if (b[0] != 0)
- break;
- }
- for (int j = b[0]; j < b[2]; j++)
- {
- sum = 0;
- for (int i = 0; i < cropContour.Rows; i++)
- {
- sum += cropContour.Get<byte>(i, j);
- }
- if (sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- if (b[3] - b[2] > 500)
- {
- if (Math.Abs(b[2] - b[1]) > 50)
- {
- cropContour2 = cropContour[0, cropContour.Rows - 1, b[2], cropContour.Cols - 1];
- border = b[2];
- }
- else
- cropContour2 = cropContour[0, cropContour.Rows - 1, 0, cropContour.Cols - 1];
- }
- else
- {
- cropContour2 = cropContour[0, cropContour.Rows - 1, b[0], cropContour.Cols - 1];
- border = b[0];
- }
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 针对双层深盲孔进行裁剪,上面空白区域较小,因此从第0行开始
- /// </summary>
- /// <param name="image"></param>
- /// <param name="y"></param>
- /// <param name="cropContour"></param>
- public void CropShenmangkongShuangceng(Mat image, out int[] y, out Mat cropContour, bool isCropFlag)
- {
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Cv2.MorphologyEx(image, image, MorphTypes.Open, se);
- int range = 200;
- if (isCropFlag)
- range = 15;
- y = new int[2] { 0, 0 };
- for (int i = 0; i < image.Rows; i++)
- {
- for (int j = range; j < image.Cols - range; j++)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- y[0] = i - 10;
- break;
- }
- }
- if (y[0] != 0)
- break;
- }
- for (int i = image.Rows - 1; i > y[0]; i--)
- {
- for (int j = range; j < image.Cols - range; j++)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- y[1] = i + 10;
- break;
- }
- }
- if (y[1] != 0)
- break;
- }
- cropContour = image[y[0], y[1], 0, image.Cols - 1];
- }
- /// <summary>
- /// 圖片旋轉之後出現白邊,去掉白邊
- /// </summary>
- /// <param name="image"></param>
- /// <param name="crop"></param>
- /// <param name="border"></param>
- public void CropBothSide(Mat image, out Mat crop, out int range)
- {
- range = 50;
- crop = image[range, image.Rows - range, range, image.Cols - range].Clone();
- }
- //public void CropLight2(Mat image, out int border, out Mat cropImage, string direction)
- //{
- // Mat filter = new Mat();
- // Cv2.BilateralFilter(image, filter, 15, 150, 3);
- // // 增强
- // Mat zengqiang = new Mat();
- // InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
- // Cv2.Filter2D(filter, zengqiang, -1, kernel);
- // Cv2.ConvertScaleAbs(zengqiang, zengqiang);
- // //边缘检测
- // Mat grad_x = new Mat();
- // Mat grad_x2 = new Mat();
- // //Cv2.Sobel(zengqiang, grad_x, MatType.CV_16S, 1, 0);
- // //Cv2.ConvertScaleAbs(grad_x, grad_x2);
- // CejuFunction.Sobel(zengqiang, out grad_x2);
- // ////腐蚀
- // //Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 5));
- // //Mat erode = new Mat();
- // //Cv2.Erode(grad_x2, erode, se);
- // //开运算
- // Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));// 结构元素
- // Mat open = new Mat();
- // //Cv2.MorphologyEx(close, openAfterClose,MorphTypes.Open, seOpen);
- // Cv2.MorphologyEx(grad_x2, open, MorphTypes.Open, seOpen);
- // ////閉運算
- // Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));// 结构元素
- // Mat close = new Mat();
- // Cv2.MorphologyEx(open, close, MorphTypes.Close, se);
- // Mat thresh2 = close.Threshold(0, 1, ThresholdTypes.Otsu);
- // //填充
- // Mat fill = new Mat();
- // CejuFunction.Fill(thresh2, out fill, 1);
- // int[] b = new int[4] { 0, 0, 0, 0 };
- // int sum = 0;
- // border = 0;
- // cropImage = new Mat();
- // switch (direction)
- // {
- // case "left":
- // for (int j = 0; j < fill.Cols; j++)
- // {
- // for (int i = 0; i < fill.Rows; i++)
- // {
- // if (fill.Get<byte>(i, j) > 0)
- // {
- // b[0] = j;
- // break;
- // }
- // }
- // if (b[0] != 0)
- // break;
- // }
- // for (int j = b[0]; j < fill.Cols; j++)
- // {
- // sum = 0;
- // for (int i = 0; i < fill.Rows; i++)
- // {
- // sum += fill.Get<byte>(i, j);
- // }
- // if (sum == 0)
- // {
- // b[1] = j;
- // break;
- // }
- // }
- // for (int j = fill.Cols - 1; j > b[1]; j--)
- // {
- // for (int i = 0; i < fill.Rows; i++)
- // {
- // if (fill.Get<byte>(i, j) > 0)
- // {
- // b[3] = j;
- // break;
- // }
- // }
- // if (b[3] != 0)
- // break;
- // }
- // for (int j = b[3]; j > b[1]; j--)
- // {
- // sum = 0;
- // for (int i = 0; i < fill.Rows; i++)
- // {
- // sum += fill.Get<byte>(i, j);
- // }
- // if (sum == 0)
- // {
- // b[2] = j;
- // break;
- // }
- // }
- // if (b[1] - b[0] > 500)
- // {
- // if (Math.Abs(b[2] - b[1]) > 50)
- // {
- // cropImage = fill[0, fill.Rows - 1, b[0], b[1]];
- // border = b[0];
- // }
- // else
- // cropImage = fill[0, fill.Rows - 1, 0, fill.Cols - 1];
- // }
- // else
- // {
- // cropImage = fill[0, fill.Rows - 1, 0, b[3]];
- // border = 0;
- // }
- // break;
- // case "right":
- // for (int j = fill.Cols - 1; j > 0; j--)
- // {
- // for (int i = 0; i < fill.Rows; i++)
- // {
- // if (fill.Get<byte>(i, j) > 0)
- // {
- // b[3] = j;
- // break;
- // }
- // }
- // if (b[3] != 0)
- // break;
- // }
- // for (int j = b[3]; j > 0; j--)
- // {
- // sum = 0;
- // for (int i = 0; i < fill.Rows; i++)
- // {
- // sum += fill.Get<byte>(i, j);
- // }
- // if (sum == 0)
- // {
- // b[2] = j;
- // break;
- // }
- // }
- // for (int j = 0; j < b[2]; j++)
- // {
- // for (int i = 0; i < fill.Rows; i++)
- // {
- // if (fill.Get<byte>(i, j) > 0)
- // {
- // b[0] = j;
- // break;
- // }
- // }
- // if (b[0] != 0)
- // break;
- // }
- // for (int j = b[0]; j < b[2]; j++)
- // {
- // sum = 0;
- // for (int i = 0; i < fill.Rows; i++)
- // {
- // sum += fill.Get<byte>(i, j);
- // }
- // if (sum == 0)
- // {
- // b[1] = j;
- // break;
- // }
- // }
- // if (b[3] - b[2] > 500)
- // {
- // if (Math.Abs(b[2] - b[1]) > 50)
- // {
- // cropImage = fill[0, fill.Rows - 1, b[2], fill.Cols - 1];
- // border = b[2];
- // }
- // else
- // cropImage = fill[0, fill.Rows - 1, 0, fill.Cols - 1];
- // }
- // else
- // {
- // cropImage = fill[0, fill.Rows - 1, b[0], fill.Cols - 1];
- // border = b[0];
- // }
- // break;
- // default:
- // break;
- // }
- //}
- /// <summary>
- /// 提取数据的提取区域
- /// </summary>
- /// <param name="cropContour2">输入去掉亮光后的图片</param>
- /// <param name="dataArea">输出提取区域dataArea[0]左,dataArea[1]右</param>
- /// /// <param name="direction">槽孔的位置方向,left/right</param>
- public void GetDataArea(Mat cropContour2, out int[] dataArea, string direction)
- {
- int[] rowSum = new int[cropContour2.Rows];
- int max = 0;
- int fillBorder = 0;
- for (int i = 0; i < cropContour2.Rows; i++)
- {
- for (int j = 0; j < cropContour2.Cols; j++)
- rowSum[i] += cropContour2.Get<byte>(i, j);
- }
- max = rowSum.Max();
- for (int i = 0; i < rowSum.Length; i++)
- {
- if (rowSum[i] > max - 50)
- {
- fillBorder = i;
- break;
- }
- }
- Mat fill = cropContour2.Clone();
- Cv2.Rectangle(fill, new Rect(0, fillBorder, cropContour2.Cols, cropContour2.Rows - fillBorder), new Scalar(1), -1);
- //提取區域
- dataArea = new int[2] { 0, 0 };
- int[] colSum = new int[fill.Cols];
- for (int j = 100; j < fill.Cols - 100; j++)
- {
- for (int i = 0; i < fill.Rows; i++)
- {
- colSum[j] += fill.Get<byte>(i, j);
- }
- }
- max = colSum.Max();
- switch (direction)
- {
- case "right":
- //for (int j = 0; j < colSum.Length; j++)
- //{
- // if (colSum[j] > max - 10)
- // {
- // dataArea[0] = j+100;
- // break;
- // }
- //}
- dataArea[0] = 100;
- for (int j = dataArea[0] + 100; j < colSum.Length - 100; j++)
- {
- if (colSum[j] > max - 10)
- {
- dataArea[1] = j;
- break;
- }
- }
- if (dataArea[1] == 0)
- dataArea[1] = dataArea[0] + 100;
- //dataArea[0] = 100;
- if (dataArea[1] - dataArea[0] > 200)
- dataArea[1] = dataArea[0] + 200;
- break;
- case "left":
- //for (int j = colSum.Length - 1; j > 0; j--)
- //{
- // if (colSum[j] > max - 10)
- // {
- // dataArea[1] = j+100;
- // break;
- // }
- //}
- dataArea[1] = cropContour2.Cols - 100;
- for (int j = dataArea[1] - 100; j > 100; j--)
- {
- if (colSum[j] > max - 10)
- {
- dataArea[0] = j;
- break;
- }
- }
- if (dataArea[0] == 0)
- dataArea[0] = dataArea[1] - 100;
- //dataArea[1] = cropContour2.Cols - 100;
- if (dataArea[1] - dataArea[0] > 200)
- dataArea[0] = dataArea[1] - 200;
- break;
- }
- }
- /// <summary>
- /// 得到浅盲孔的数据提取区域
- /// </summary>
- /// <param name="cropContour">裁剪后的二值图像</param>
- /// <param name="dataArea">输出提取区域,从左至右分别是[0][1][2][3]</param>
- public void GetMangkongDataAreaForQian(Mat cropContour, out int[] dataArea)
- {
- dataArea = new int[4];
- int[] middleArea = new int[2];
- int[] sum = new int[cropContour.Cols];
- //每列求和
- for (int j = 0; j < cropContour.Cols; j++)
- {
- sum[j] = 0;
- for (int i = 0; i < cropContour.Rows; i++)
- {
- sum[j] += cropContour.Get<byte>(i, j);
- }
- }
- //求中部区域
- int max = sum.Max();
- for (int j = 0; j < sum.Length; j++)
- {
- if (sum[j] > max - 10)
- {
- middleArea[0] = j;
- break;
- }
- }
- for (int j = sum.Length - 1; j > 0; j--)
- {
- if (sum[j] > max - 10)
- {
- middleArea[1] = j;
- break;
- }
- }
- //第一次容错
- if(middleArea[1]- middleArea[0] < 100)
- {
- for (int j = 0; j < sum.Length; j++)
- {
- if (sum[j] > max - 20)
- {
- middleArea[0] = j;
- break;
- }
- }
- for (int j = sum.Length - 1; j > 0; j--)
- {
- if (sum[j] > max - 20)
- {
- middleArea[1] = j;
- break;
- }
- }
- }
- //每行求和
- int[] rowsSum = new int[cropContour.Rows];
- for (int i = 0; i < cropContour.Rows; i++)
- {
- rowsSum[i] = 0;
- for (int j = 0; j < cropContour.Cols; j++)
- {
- rowsSum[i] += cropContour.Get<byte>(i, j);
- }
- }
- //得到填充上界,其下填为0
- max = rowsSum.Max();
- int fillBorder = new int();
- for (int i = 0; i < rowsSum.Length; i++)
- {
- if (rowsSum[i] > max / 2)
- {
- fillBorder = i;
- break;
- }
- }
- Mat fill = cropContour.Clone();
- Cv2.Rectangle(fill, new Rect(0, fillBorder, cropContour.Cols, cropContour.Rows - fillBorder), new Scalar(1), -1);
- //每列求和
- int[] colSum = new int[fill.Cols];
- for (int j = 0; j < fill.Cols; j++)
- {
- colSum[j] = 0;
- for (int i = 0; i < fill.Rows; i++)
- {
- colSum[j] += fill.Get<byte>(i, j);
- }
- }
- //左,右最大值
- int[] left = colSum.Skip(middleArea[0] - 500).Take(500).ToArray();
- int[] right = colSum.Skip(middleArea[1]).Take(500).ToArray();
- int leftMax = left.Max();
- int rightMax = right.Max();
- //数据提取区域
- int range = 100;
- if (colSum[middleArea[0] - 100] < leftMax - 10 || colSum[middleArea[1] + 100] < rightMax - 10)
- range = 0;
- for (int j = middleArea[0] - range; j > middleArea[0] - 500 && j > 0; j--)
- {
- if (colSum[j] > leftMax - 10)
- {
- dataArea[1] = j;
- break;
- }
- }
- if (dataArea[1] == 0) dataArea[1] = leftMax;
- for (int j = dataArea[1] - 100; j < dataArea[1]; j++)
- {
- if (colSum[j] > leftMax - 10)
- {
- dataArea[0] = j;
- break;
- }
- }
- for (int j = middleArea[1] + range; j < middleArea[1] + 500; j++)
- {
- if (colSum[j] > rightMax - 5)
- {
- dataArea[2] = j;
- break;
- }
- }
- for (int j = dataArea[2] + 100; j > dataArea[2]; j--)
- {
- if (colSum[j] > rightMax - 5)
- {
- dataArea[3] = j;
- break;
- }
- }
- if (dataArea[1] == 0)
- {
- dataArea[1] = middleArea[0] - 70;//不加100是因为出去还要加20
- dataArea[0] = dataArea[1] - 100;
- }
- if (dataArea[0] == 0)
- {
- dataArea[0] = dataArea[1] - 100;
- if (colSum[dataArea[0]] < leftMax - 10)
- dataArea[0] = dataArea[1] - 30;
- }
- if (dataArea[2] == 0)
- {
- dataArea[2] = middleArea[1] + 70;//不加100是因为出去还要加20
- dataArea[3] = dataArea[2] + 100;
- }
- if (dataArea[3] == 0)
- {
- dataArea[3] = dataArea[2] + 100;
- if (colSum[dataArea[3]] < rightMax - 10)//防止加完出界
- dataArea[3] = dataArea[2] + 30;
- }
- if (dataArea[1] - dataArea[0] < 50)
- dataArea[1] += 50;
- if (dataArea[3] - dataArea[2] < 50)
- dataArea[2] -= 50;
- }
- /// <summary>
- /// 得到浅盲孔的数据提取区域
- /// </summary>
- /// <param name="cropContour">裁剪后的二值图像</param>
- /// <param name="dataArea">输出提取区域,从左至右分别是[0][1][2][3]</param>
- public void GetMangkongDataArea(Mat cropContour, out int[] dataArea)
- {
- dataArea = new int[4];
- int[] middleArea = new int[2];
- int[] sum = new int[cropContour.Cols];
- //每列求和
- for (int j = 0; j < cropContour.Cols; j++)
- {
- sum[j] = 0;
- for (int i = 0; i < cropContour.Rows; i++)
- {
- sum[j] += cropContour.Get<byte>(i, j);
- }
- }
-
- //求中部区域
- int max = sum.Max();
- for (int j = 0; j < sum.Length; j++)
- {
- if (sum[j] > max - 20)
- {
- middleArea[0] = j;
- break;
- }
- }
- for (int j = sum.Length - 1; j > 0; j--)
- {
- if (sum[j] > max - 20)
- {
- middleArea[1] = j;
- break;
- }
- }
- //第一次容错
- if (middleArea[1] - middleArea[0] < 110)
- {
- for (int j = 0; j < sum.Length; j++)
- {
- if (sum[j] > max - 80)
- {
- middleArea[0] = j;
- break;
- }
- }
- for (int j = sum.Length - 1; j > 0; j--)
- {
- if (sum[j] > max - 80)
- {
- middleArea[1] = j;
- break;
- }
- }
- }
- //每行求和
- int[] rowsSum = new int[cropContour.Rows];
- for (int i = 0; i < cropContour.Rows; i++)
- {
- rowsSum[i] = 0;
- for (int j = 0; j < cropContour.Cols; j++)
- {
- rowsSum[i] += cropContour.Get<byte>(i, j);
- }
- }
- //得到填充上界,其下填为0
- max = rowsSum.Max();
- int fillBorder = new int();
- for (int i = 0; i < rowsSum.Length; i++)
- {
- if (rowsSum[i] > max / 2)
- {
- fillBorder = i;
- break;
- }
- }
- Mat fill = cropContour.Clone();
- Cv2.Rectangle(fill, new Rect(0, fillBorder, cropContour.Cols, cropContour.Rows - fillBorder), new Scalar(1), -1);
- //每列求和
- int[] colSum = new int[fill.Cols];
- for (int j = 0; j < fill.Cols; j++)
- {
- colSum[j] = 0;
- for (int i = 0; i < fill.Rows; i++)
- {
- colSum[j] += fill.Get<byte>(i, j);
- }
- }
- //左,右最大值
- int[] left = colSum.Skip(middleArea[0] - 500).Take(500).ToArray();
- int[] right = colSum.Skip(middleArea[1]).Take(500).ToArray();
- int leftMax = left.Max();
- int rightMax = right.Max();
- //数据提取区域
- int range = 100;
- if (colSum[middleArea[0] - 100] < leftMax - 10 || colSum[middleArea[1] + 100] < rightMax - 10)
- range = 0;
- for (int j = middleArea[0] - range; j > middleArea[0] - 500 && j > 0; j--)
- {
- if (colSum[j] > leftMax - 10)
- {
- dataArea[1] = j;
- break;
- }
- }
- for (int j = dataArea[1] - 100; j < dataArea[1]; j++)
- {
- if (colSum[j] > leftMax - 10)
- {
- dataArea[0] = j;
- break;
- }
- }
- for (int j = middleArea[1] + range; j < middleArea[1] + 500; j++)
- {
- if (colSum[j] > rightMax - 5)
- {
- dataArea[2] = j;
- break;
- }
- }
- for (int j = dataArea[2] + 100; j > dataArea[2]; j--)
- {
- if (colSum[j] > rightMax - 5)
- {
- dataArea[3] = j;
- break;
- }
- }
- if (dataArea[1] == 0)
- {
- dataArea[1] = middleArea[0] - 70;//不加100是因为出去还要加20
- dataArea[0] = dataArea[1] - 100;
- }
- if (dataArea[0] == 0)
- {
- dataArea[0] = dataArea[1] - 100;
- if (colSum[dataArea[0]] < leftMax - 10)
- dataArea[0] = dataArea[1] - 30;
- }
- if (dataArea[2] == 0)
- {
- dataArea[2] = middleArea[1] + 70;//不加100是因为出去还要加20
- dataArea[3] = dataArea[2] + 100;
- }
- if (dataArea[3] == 0)
- {
- dataArea[3] = dataArea[2] + 100;
- if (colSum[dataArea[3]] < rightMax - 10)//防止加完出界
- dataArea[3] = dataArea[2] + 30;
- }
- if (dataArea[1] - dataArea[0] < 50)
- dataArea[1] += 50;
- if (dataArea[3] - dataArea[2] < 50)
- dataArea[2] -= 50;
- }
- /// <summary>
- /// 提取第一條橫綫縱坐標
- /// </summary>
- /// <param name="image">輸入二值化圖像</param>
- /// <param name="averageCoordinateNew">輸出坐標</param>
- /// <param name="leftBorder">左邊界</param>
- /// <param name="rightBorder">右边界</param>
- /// <param name="direction">大于等于0时,记录大于0的点,小于0记录等于零的点</param>
- public void ExtractLines(Mat image, out double averageCoordinateNew, int leftBorder, int rightBorder, int direction)
- {
- //数组长宽
- int rows = image.Rows;
- int cols = image.Cols;
- int count = 0;
- int sum = 0;
- // 对循环次数计数,防止程序卡死
- int circleCount = 0;
- //当点的个数少于5的时候循环
- while (count < 5)
- {
- count = 0;
- sum = 0;
- //每次循环,寻找范围向下移动5个像素
- //遍历,寻找线条上的点,并记录纵坐标
- for (int j = leftBorder; j < rightBorder; j++)
- {
- for (int i = 0; i < rows; i++)
- {
- if (direction >= 0)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- sum += i;
- count++;
- break;
- }
- }
- else
- {
- if (image.Get<byte>(i, j) == 0)
- {
- sum += i;
- count++;
- break;
- }
- }
- }
- }
- // 超过30次跳出
- if(circleCount <= 30)
- {
- circleCount++;
- }
- else
- {
- break;
- }
- }
- averageCoordinateNew = sum / count;
- }
- /// <summary>
- /// 提取非第一条横线纵坐标
- /// </summary>
- /// <param name="image">输入二值化图像</param>
- /// <param name="averageCoordinateNew">输出纵坐标</param>
- /// <param name="leftBorder">左边界</param>
- /// <param name="rightBorder">右边界</param>
- /// <param name="averageCoordinate">上一条线的坐标</param>
- /// <param name="direction">大于等于0时,记录大于0的点,小于0时记录等于零的点</param>
- public void ExtractLines(Mat image, out double averageCoordinateNew, int leftBorder, int rightBorder, double averageCoordinate, int direction)
- {
- //ImageShow(image*255);
- //数组长宽
- int rows = image.Rows;
- int cols = image.Cols;
- int count = 0;
- int sum = 0;
- int upperBound = (int)averageCoordinate + 15;
- int lowerBound = (int)averageCoordinate + 45;
- //当点的个数少于5的时候循环
- while (count < 5)
- {
- count = 0;
- sum = 0;
- //每次循环,寻找范围向下移动5个像素
- upperBound = upperBound + 5;
- lowerBound = lowerBound + 5;
- if (lowerBound > image.Rows)
- break;
- //遍历,寻找线条上的点,并记录纵坐标
- int value = 0;
- for (int j = leftBorder; j < rightBorder; j++)
- {
- for (int i = upperBound; i < lowerBound; i++)
- {
- if (direction >= 0)
- {
- value = image.Get<byte>(i, j);
- if (value > 0)
- {
- sum += i;
- count++;
- break;
- }
- }
- else
- {
- if (image.Get<byte>(i, j) == 0)
- {
- sum += i;
- count++;
- break;
- }
- }
- }
- }
- }
- averageCoordinateNew = count == 0 ? 0 : sum / count;
- }
- /// <summary>
- /// 从下向上提取第一条横线纵坐标
- /// </summary>
- /// <param name="image">輸入二值化圖像</param>
- /// <param name="averageCoordinateNew">輸出坐標</param>
- /// <param name="leftBorder">左邊界</param>
- /// <param name="rightBorder">右边界</param>
- /// <param name="direction">大于等于0时,记录大于0的点,小于0记录等于零的点</param>
- public void ExtractLines2(Mat image, out double averageCoordinateNew, int leftBorder, int rightBorder, int direction)
- {
- //数组长宽
- int rows = image.Rows;
- int cols = image.Cols;
- int count = 0;
- int sum = 0;
- int tag = 0;
- //当点的个数少于5的时候循环
- while (count < 5)
- {
- count = 0;
- sum = 0;
- //每次循环,寻找范围向下移动5个像素
- //遍历,寻找线条上的点,并记录纵坐标
- for (int j = leftBorder; j < rightBorder; j++)
- {
- for (int i = rows - 1; i > 0; i--)
- {
- if (i == 1)
- tag += 1;
- if (direction >= 0)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- sum += i;
- count++;
- break;
- }
- }
- else
- {
- if (image.Get<byte>(i, j) == 0)
- {
- sum += i;
- count++;
- break;
- }
- }
- }
- if (tag == 3)
- break;
- }
- if (tag == 3)
- break;
- }
- averageCoordinateNew = (count == 0) ? 0 : sum / count;
- }
- /// <summary>
- /// 从下向上提取横线纵坐标
- /// </summary>
- /// <param name="image">输入二值化图像</param>
- /// <param name="averageCoordinateNew">输出纵坐标</param>
- /// <param name="leftBorder">左边界</param>
- /// <param name="rightBorder">右边界</param>
- /// <param name="averageCoordinate">上一条线的坐标</param>
- /// <param name="direction">大于等于0时,记录大于0的点,小于0时记录等于零的点</param>
- public void ExtractLines2(Mat image, out double averageCoordinateNew, int leftBorder, int rightBorder, double averageCoordinate, int direction)
- {
- //数组长宽
- int rows = image.Rows;
- int cols = image.Cols;
- int sum = 0, count = 0;
- //遍历范围的上界和下界
- int upperBound = (int)averageCoordinate - 45;
- int lowerBound = (int)averageCoordinate - 10;
- //当点的个数少于5的时候循环
- while (count < 5)
- {
- count = 0;
- sum = 0;
- //每次循环,寻找范围向下移动5个像素
- upperBound = upperBound - 5;
- lowerBound = lowerBound - 5;
- if (upperBound < 0)
- break;
- //遍历,寻找线条上的点,并记录纵坐标
- for (int j = leftBorder; j < rightBorder; j++)
- {
- for (int i = lowerBound; i > upperBound; i--)
- {
- if (direction >= 0)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- sum += i;
- count++;
- break;
- }
- }
- else
- {
- if (image.Get<byte>(i, j) == 0)
- {
- sum += i;
- count++;
- break;
- }
- }
- }
- }
- }
- averageCoordinateNew = count==0 ? 0 : sum / count;
- }
- /// <summary>
- /// 提取第一条竖线的横坐标,从左往右
- /// </summary>
- /// <param name="image">输入二值图像</param>
- /// <param name="result">输出坐标结果</param>
- /// <param name="upperBound">上边界</param>
- /// <param name="lowerBound">下边界</param>
- public void ExtractVerticalLinesL2R(Mat image, out double result, int upperBound, int lowerBound, int showMat = 0)
- {
- int rows = image.Rows;
- int cols = image.Cols;
- List<int> sum = new List<int>();
- int k = 0;
- while (sum.Count == 0)
- {
- if (upperBound - 5 * k < 0 || lowerBound + 5 * k > image.Rows) break;
- //解决两层板有些孔铜找不到的问题
- for (int i = upperBound - 5 * k; i < lowerBound + 5 * k; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- sum.Add(j);
- break;
- }
- }
- }
- ++k;
- }
- ////int count = 0;
- //for (int i = upperBound; i < lowerBound; i++)
- //{
- // for (int j = 0; j < cols; j++)
- // {
- // if (image.Get<byte>(i, j) > 0)
- // {
- // sum.Add(j);
- // //sum += j;
- // //count++;
- // break;
- // }
- // }
- //}
- //if (sum.Count == 0)
- // result = 1;// 0;
- //else
- result = sum.Average();// / count;
- //if (showMat > 0)
- //{
- // Mat imageClone = image.Clone() * 127;
- // Cv2.Line(imageClone, 0, upperBound, cols - 1, upperBound, new Scalar(255));
- // Cv2.Line(imageClone, 0, lowerBound, cols - 1, lowerBound, new Scalar(255));
- // Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
- //}
- //////对自动结果不正确(这里使用距离过远来判定)的扩展运算
- ////double result1 = 0;
- if (showMat > 0 && result > 20/*countV > (upperBound + lowerBound) / 2*/)
- {
- sum.Clear();
- //sum = 0;
- //count = 0;
- ////Mat imageClone = image.Clone() * 127;
- ////Cv2.Line(imageClone, 0, upperBound, cols - 1, upperBound, new Scalar(255));
- ////Cv2.Line(imageClone, 0, lowerBound, cols - 1, lowerBound, new Scalar(255));
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
- //////int sum = 0;
- //////int count = 0;
- bool sumAdd = false;
- for (int i = upperBound - 5; i < lowerBound + 5; i++)
- {
- sumAdd = false;
- for (int j = 0; j < 20; j++)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- sumAdd = true;
- sum.Add(j);
- //sum += j;
- //count++;
- break;
- }
- }
- if (!sumAdd && sum.Count > 0)
- break;
- }
- if (sum.Count == 0)
- {
- for (int i = upperBound - 5; i < lowerBound + 5; i++)
- {
- sumAdd = false;
- for (int j = 0; j < 26; j++)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- sumAdd = true;
- sum.Add(j);
- //sum += j;
- //count++;
- break;
- }
- }
- if (!sumAdd && sum.Count > 0)
- break;
- }
- //for (int i = upperBound - 5; i < lowerBound + 5; i++)
- //{
- // sumAdd = false;
- // for (int j = cols - 1; j > cols - 26; j--)
- // {
- // if (image.Get<byte>(i, j) > 0)
- // {
- // sumAdd = true;
- // sum += j;
- // count++;
- // break;
- // }
- // }
- // if (!sumAdd && sum > 0)
- // break;
- //}
- }
- // 输出平均横坐标
- if (sum.Count > 0)
- result = sum.Average();// / count;
- }
- else if (showMat > 0)
- {//对自动结果不正确(这里使用垂直方向不整齐来判定)的扩展运算
- sum.Sort();
- for (int i = 0; i < sum.Count - 1; i++)
- {
- if (sum[i + 1] - sum[i] > 10)
- {
- for (int j = sum.Count - 1; j > i; j--)
- sum.RemoveAt(j);
- break;
- }
- }
- // 输出平均横坐标
- if (sum.Count > 0)
- result = sum.Average();// / count;
- //Mat imageClone = image.Clone() * 127;
- //Cv2.Line(imageClone, 0, upperBound, cols - 1, upperBound, new Scalar(255));
- //Cv2.Line(imageClone, 0, lowerBound, cols - 1, lowerBound, new Scalar(255));
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
- }
- ////对自动结果不正确(这里使用距离过远来判定)的扩展运算
- //double result1 = 0;
- //// 输出平均横坐标
- //result = sum / count;
- }
- /// <summary>
- /// 提取第二条竖线的横坐标,从左往右
- /// </summary>
- /// <param name="image">输入二值图像</param>
- /// <param name="result">输出坐标结果</param>
- /// <param name="upperBound">上边界</param>
- /// <param name="lowerBound">下边界</param>
- /// <param name="basic">第一条线的横坐标</param>
- public int[] ExtractVerticalLinesL2R(Mat image, out double result, int upperBound, int lowerBound, double basic, bool cal_Jiaoneisuo = true)
- {
- int rows = image.Rows;
- int cols = image.Cols;
- int sum = 0;
- int count = 0;
- // 寻找的左右范围
- int leftBound = (int)basic + 15;
- int rightBound = (int)basic + 45;
- int countUp = 5;
- if (!cal_Jiaoneisuo) countUp = 1;
- //当点的个数少于5的时候循环
- while (count < countUp)
- {
- count = 0;
- sum = 0;
- //每次循环,寻找范围向左移动5个像素
- leftBound = leftBound + 5;
- rightBound = rightBound + 5;
- if (rightBound > image.Cols)
- break;
- //遍历,寻找线条上的点,并记录横坐标
- for (int i = upperBound; i < lowerBound; i++)
- {//待自测scc-1
- //bool isWhiteArea = false;//避免弧度大,导致找的位置与角落的弧度相交
- for (int j = leftBound; j < rightBound; j++)
- {
- //if (!isWhiteArea)
- //{
- // if (image.Get<byte>(i, j) > 0)
- // isWhiteArea = true;
- //}else
- if (image.Get<byte>(i, j) == 0)
- {
- sum += j;
- count++;
- break;
- }
- }
- }
- }
- leftBound = (int)basic + 15;
- rightBound = (int)basic + 145;
- if (rightBound >= cols) rightBound = cols - 1;
- double y1 = upperBound;
- double x1 = leftBound;
- for (int i = upperBound + 20; i < lowerBound - 20; i++)
- {
- for (int j = leftBound; j < rightBound; j++)
- {
- if (image.Get<byte>(i, j) == 0)
- {
- if (x1 < j)
- {
- x1 = j;
- y1 = i;
- }
- break;
- }
- }
- }
- result = sum / count;
- return new int[] { (int)x1, (int)y1 };
- }
- /// <summary>
- /// 从右往左提取第一条竖线横坐标
- /// </summary>
- /// <param name="image">输入二值图像</param>
- /// <param name="result">输出坐标结果</param>
- /// <param name="upperBound">上边界</param>
- /// <param name="lowerBound">下边界</param>
- public void ExtractVerticalLinesR2L(Mat image, out double result, int upperBound, int lowerBound, int showMat = 0)
- {
- int rows = image.Rows;
- int cols = image.Cols;
- int sum = 0;
- int count = 0;
- int countV = 0;
- for (int i = upperBound; i < lowerBound; i++)
- {
- for (int j = cols - 1; j > 0; j--)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- sum += j;
- count++;
- break;
- }
- }
- if (sum == 0) countV = i;
- }
- result = sum / count;
- ////对自动结果不正确(这里使用距离过远来判定)的扩展运算
- //double result1 = 0;
- if (showMat > 0 && cols - result > 20/*countV > (upperBound + lowerBound) / 2*/)
- {
- sum = 0;
- count = 0;
- //Mat imageClone = image.Clone() * 127;
- //Cv2.Line(imageClone, 0, upperBound, cols - 1, upperBound, new Scalar(255));
- //Cv2.Line(imageClone, 0, lowerBound, cols - 1, lowerBound, new Scalar(255));
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
- ////int sum = 0;
- ////int count = 0;
- bool sumAdd = false;
- for (int i = upperBound - 5; i < lowerBound + 5; i++)
- {
- sumAdd = false;
- for (int j = cols - 1; j > cols - 20; j--)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- sumAdd = true;
- sum += j;
- count++;
- break;
- }
- }
- if (!sumAdd && sum > 0)
- break;
- }
- if (sum == 0)
- {
- for (int i = upperBound - 5; i < lowerBound + 5; i++)
- {
- sumAdd = false;
- for (int j = cols - 1; j > cols - 26; j--)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- sumAdd = true;
- sum += j;
- count++;
- break;
- }
- }
- if (!sumAdd && sum > 0)
- break;
- }
- }
- // 输出平均横坐标
- if (sum > 0)
- result = sum / count;
- }
- //// 输出平均横坐标
- //if (true)
- //{
- //}
- //result = sum / count;
- }
- /// <summary>
- /// 提取第二条竖线的横坐标,从右往左
- /// </summary>
- /// <param name="image">输入二值图像</param>
- /// <param name="result">输出坐标结果</param>
- /// <param name="upperBound">上边界</param>
- /// <param name="lowerBound">下边界</param>
- /// <param name="basic">第一条线的横坐标</param>
- public int[] ExtractVerticalLinesR2L(Mat image, out double result, int upperBound, int lowerBound, double basic, int showMat = 0)
- {
- int rows = image.Rows;
- int cols = image.Cols;
- int sum = 0;
- int count = 0;
- // 寻找的左右范围
- int leftBound = (int)basic - 45;
- int rightBound = (int)basic - 15;
- //if (showMat > 0)
- //{
- // Mat imageClone = image.Clone() * 127;
- // Cv2.Line(imageClone, leftBound, upperBound, rightBound - 1, upperBound, new Scalar(255));
- // Cv2.Line(imageClone, leftBound, lowerBound, rightBound - 1, lowerBound, new Scalar(255));
- // Cv2.ImWrite(@"C:\Users\54434\Desktop\imageClone_" + showMat + ".png", imageClone);
- //}
- //当点的个数少于5的时候循环
- while (count < 5)
- {
- count = 0;
- sum = 0;
- //每次循环,寻找范围向左移动5个像素
- leftBound = leftBound - 5;
- rightBound = rightBound - 5;
- //遍历,寻找线条上的点,并记录横坐标
- for (int i = upperBound; i < lowerBound; i++)
- {//待自测scc-1
- //bool isWhiteArea = false;//避免弧度大,导致找的位置与角落的弧度相交
- ////(showMat > 0)
- for (int j = rightBound - 1; j > leftBound; j--)
- {
- //if ((showMat > 0) && !isWhiteArea)
- //{
- // if (image.Get<byte>(i, j) > 0)
- // isWhiteArea = true;
- //}
- //else
- if (image.Get<byte>(i, j) == 0)
- {
- sum += j;
- count++;
- break;
- }
- }
- }
- }
- //if (count == 0)
- //{
- //}
- leftBound = (int)basic - 145;
- if (leftBound < 0) leftBound = 0;
- rightBound = (int)basic - 15;
- double y1 = upperBound;
- double x1 = rightBound;
- //遍历,寻找线条上的点,并记录横坐标
- for (int i = upperBound + 20; i < lowerBound - 20; i++)
- {
- for (int j = rightBound - 1; j > leftBound; j--)
- {
- if (image.Get<byte>(i, j) == 0)
- {
- if (x1 > j)
- {
- x1 = j;
- y1 = i;
- }
- break;
- }
- }
- }
- result = sum / count;
- return new int[] { (int)x1, (int)y1 };
- }
- /// <summary>
- /// 提取三层板的粗糙度以及粗糙度的坐标
- /// </summary>
- /// <param name="imageContour">二值图像</param>
- /// <param name="imageRed">红色通道图片</param>
- /// <param name="ordinateL3">第三条横线</param>
- /// <param name="ordinateL4">第四条横线</param>
- /// <param name="ordinateL5">第五条横线</param>
- /// <param name="ordinateL6">第六条横线</param>
- /// <param name="ordinateV2">第二条竖线</param>
- /// <param name="ordinateV4">第四条竖线</param>
- /// <param name="dataArea">数据提取区域</param>
- /// <param name="direction">槽孔所在左右方向,left、right</param>
- /// <param name="roughness">输出粗糙度</param>
- /// <param name="roughnessOrdinate">输出粗糙度坐标</param>
- 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)
- {
- int leftBorder1 = 0, rightBorder1 = 0, leftBorder2 = 0, rightBorder2 = 0;
- int range = 70;
- switch (direction)
- {
- case "right":
- leftBorder1 = ordinateV2;
- rightBorder1 = ordinateV2 + range;
- leftBorder2 = ordinateV4;
- rightBorder2 = ordinateV4 + range;
- break;
- case "left":
- leftBorder1 = ordinateV2 - range;
- rightBorder1 = ordinateV2;
- leftBorder2 = ordinateV4 - range;
- rightBorder2 = ordinateV4;
- break;
- }
- int sum = 0, count = 0;
- double averSaturation1 = 0, averSaturation2 = 0;
- roughness = 0;
- roughnessOrdinate = new int[4] { 0, 0, 0, 0 };
- Mat sobel = new Mat();
- Sobel(imageContour, out sobel);
- //计算粗糙度
- int max = 0;
- //先上下遍历一遍寻找最大值,避免找不到胶体
- for (int i = (int)ordinateL3 + 20; i < (int)ordinateL4 - 15; i++)
- {
- for (int j = leftBorder1; j < rightBorder1; j++)
- {
- if (sobel.Get<byte>(i, j) > 0)
- {
- if (Math.Abs(j - (int)(ordinateV2)) > max)
- {
- max = Math.Abs(j - (int)(ordinateV2));
- roughnessOrdinate[0] = (int)ordinateV2;
- roughnessOrdinate[1] = i;
- roughnessOrdinate[2] = j;
- roughnessOrdinate[3] = i;
- }
- }
- }
- }
- for (int i = (int)ordinateL5 + 15; i < (int)ordinateL6 - 10; i++)
- {
- for (int j = leftBorder2; j < rightBorder2; j++)
- {
- if (sobel.Get<byte>(i, j) > 0)
- {
- if (Math.Abs(j - (ordinateV4)) > max)
- {
- max = Math.Abs(j - (ordinateV4));
- roughnessOrdinate[0] = (int)ordinateV4;
- roughnessOrdinate[1] = i;
- roughnessOrdinate[2] = j;
- roughnessOrdinate[3] = i;
- }
- }
- }
- }
- roughness = max;
- if (roughness < 10)
- {
- //判断胶体方向
- Cv2.EqualizeHist(imageRed, imageRed);
- Mat upperCrop = imageRed[(int)ordinateL3 + 10, (int)ordinateL4 - 10, dataArea[0], dataArea[1]];
- Mat lowerCrop = imageRed[(int)ordinateL5 + 10, (int)ordinateL6 - 10, dataArea[0], dataArea[1]];
- //Mat duibi = new Mat(upperCrop.Size().Height + lowerCrop.Size().Height, upperCrop.Size().Width, upperCrop.Type());
- //upperCrop.CopyTo(duibi[0, upperCrop.Size().Height - 1, 0, upperCrop.Size().Width - 1]);
- //lowerCrop.CopyTo(duibi[upperCrop.Size().Height, duibi.Size().Height - 1, 0, upperCrop.Size().Width - 1]);
- //new Window("upperCrop", WindowMode.Normal, upperCrop);
- //new Window("lowerCrop", WindowMode.Normal, lowerCrop);
- //new Window("imageRed", WindowMode.Normal, imageRed);
- //Cv2.WaitKey();
- //Cv2.EqualizeHist(duibi, duibi);
- //Scalar s1 = duibi[0, upperCrop.Size().Height - 1, 0, upperCrop.Size().Width - 1].Sum();
- //Scalar s2 = duibi[upperCrop.Size().Height, duibi.Size().Height - 1, 0, upperCrop.Size().Width - 1].Sum();
- Scalar s1 = upperCrop.Sum();
- Scalar s2 = lowerCrop.Sum();
- averSaturation1 = (double)s1 / (upperCrop.Cols * upperCrop.Rows);
- averSaturation2 = (double)s2 / (lowerCrop.Cols * lowerCrop.Rows);
- for (int i = 0; i < upperCrop.Rows; i++)
- {
- for (int j = 0; j < upperCrop.Cols; j++)
- {
- sum += Math.Abs(upperCrop.Get<byte>(i, j) - (int)averSaturation1);
- }
- }
- double biaozhuncha1 = sum / (upperCrop.Cols * upperCrop.Rows);
- sum = 0;
- for (int i = 0; i < lowerCrop.Rows; i++)
- {
- for (int j = 0; j < lowerCrop.Cols; j++)
- {
- sum += Math.Abs(lowerCrop.Get<byte>(i, j) - (int)averSaturation2);
- }
- }
- double biaozhuncha2 = sum / (lowerCrop.Cols * lowerCrop.Rows);
- int direction2 = 0;
- if (biaozhuncha1 > biaozhuncha2)
- direction2 = 0;//等于0时说明胶体在上面,否则是下面
- else
- direction2 = 1;
- switch (direction2)
- {
- case 0:
- for (int i = (int)ordinateL3 + 10; i < (int)ordinateL4 - 15; i++)
- {
- for (int j = leftBorder1; j < rightBorder1; j++)
- {
- if (sobel.Get<byte>(i, j) > 0)
- {
- if (Math.Abs(j - (int)(ordinateV2)) > max)
- {
- max = Math.Abs(j - (int)(ordinateV2));
- roughnessOrdinate[0] = (int)ordinateV2;
- roughnessOrdinate[1] = i;
- roughnessOrdinate[2] = j;
- roughnessOrdinate[3] = i;
- }
- }
- }
- }
- roughness = max;
- break;
- case 1:
- for (int i = (int)ordinateL5 + 15; i < (int)ordinateL6 - 10; i++)
- {
- for (int j = leftBorder2; j < rightBorder2; j++)
- {
- if (sobel.Get<byte>(i, j) > 0)
- {
- if (Math.Abs(j - (int)(ordinateV4)) > max)
- {
- max = Math.Abs(j - (int)(ordinateV4));
- roughnessOrdinate[0] = (int)ordinateV4;
- roughnessOrdinate[1] = i;
- roughnessOrdinate[2] = j;
- roughnessOrdinate[3] = i;
- }
- }
- }
- }
- roughness = max;
- break;
- }
- }
- }
- /// <summary>
- /// 提取四层板的粗糙度
- /// </summary>
- /// <param name="imageContour">二值图像</param>
- /// <param name="upperBorder">上界</param>
- /// <param name="lowerBorder">下界</param>
- /// <param name="basic">边缘线</param>
- /// <param name="direction">槽孔所在左右方向,left、right</param>
- /// <param name="roughness">输出粗糙度</param>
- /// <param name="roughnessOrdinate">输出粗糙度坐标</param>
- public void GetSicengRoughness(Mat imageContour, int upperBorder, int lowerBorder, int basic, string direction, out int roughness, out int[] roughnessOrdinate)
- {
- int leftBorder = 0, rightBorder = 0;
- int range = 70;
- switch (direction)
- {
- case "right":
- leftBorder = basic;
- rightBorder = basic + range;
- break;
- case "left":
- leftBorder = basic - range;
- rightBorder = basic;
- break;
- }
- int max = 0;
- roughness = 0;
- roughnessOrdinate = new int[4];
- Mat sobel = new Mat();
- Sobel(imageContour, out sobel);
- for (int i = upperBorder; i < lowerBorder; i++)
- {
- for (int j = leftBorder; j < rightBorder; j++)
- {
- if (sobel.Get<byte>(i, j) > 0)
- {
- if (Math.Abs(j - basic) > max)
- {
- max = Math.Abs(j - basic);
- roughnessOrdinate[0] = basic;
- roughnessOrdinate[1] = i;
- roughnessOrdinate[2] = j;
- roughnessOrdinate[3] = i;
- }
- }
- }
- }
- roughness = max;
- }
- 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)
- {
- kongtong = new double[2];
- leftKongtong = new int[2];
- rightKongtong = new int[2];
- leftPoint2 = new int[2];
- rightPoint2 = new int[2];
- #region//左侧
- Mat leftCrop = imageRed[(int)leftOrdinateMiantong + 40, (int)leftOrdinate3 - 10, leftMiddleMianJicaitong, leftAperture[1]].Clone();
- Mat leftContour = new Mat();
- double t = Cv2.Threshold(leftCrop, leftContour, 0, 1, ThresholdTypes.Otsu);
- Mat leftFanse = 1 - leftContour;
- InputArray leftKernel = InputArray.Create<int>(new int[17, 17] {
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- });
- InputArray leftKernel2 = InputArray.Create<int>(new int[17, 17] {
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
- });
- InputArray leftKernel3 = InputArray.Create<int>(new int[17, 17] {
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1 },
- });
- Mat leftFilter = new Mat();
- //Cv2.Filter2D(leftFanse, leftFilter, -1, leftKernel, new Point(-1, -1), 0);
- //Cv2.Filter2D(leftFanse, leftFilter, -1, leftKernel2, new Point(-1, -1), 0);
- Cv2.Filter2D(leftFanse, leftFilter, -1, leftKernel3, new Point(-1, -1), 0);
- Mat leftThresh = leftFilter.Threshold(14, 1, ThresholdTypes.Binary);
- for (int j = leftThresh.Cols - 1; j > 0; j--)//从右上角,以135°方向遍历
- {
- for (int k = j; k < leftThresh.Cols && (k - j < leftThresh.Rows); k++)
- {
- if (leftThresh.Get<byte>(k - j, k) > 0)
- {
- leftKongtong[0] = k - j + (int)leftOrdinateMiantong + 40;//纵
- leftKongtong[1] = k + leftMiddleMianJicaitong;//横
- //Cv2.Circle(imageRed, k + leftMiddleMianJicaitong + border, k - j + (int)leftOrdinateMiantong + upper - 10, 1, 1, 1);
- //new Window("imageRed_Line", WindowMode.Normal, imageRed);
- break;
- }
- }
- if (leftKongtong[0] != 0)
- break;
- }
- #endregion
- #region//右侧
- Mat rightCrop = imageRed[(int)rightOrdinateMiantong + 40, (int)rightOrdinate3 - 10, rightAperture[1], rightMiddleMianJicaitong].Clone();
- Mat rightContour = new Mat();
- double t1 = Cv2.Threshold(rightCrop, rightContour, 0, 1, ThresholdTypes.Otsu);
- Mat rightFanse = 1 - rightContour;
- InputArray rightKernel = InputArray.Create<int>(new int[17, 17] {
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- });
- InputArray rightKernel2 = InputArray.Create<int>(new int[17, 17] {
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 },
- { 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- });
- InputArray rightKernel3 = InputArray.Create<int>(new int[17, 17] {
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 },
- { 0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- });
- Mat rightFilter = new Mat();
- //Cv2.Filter2D(rightFanse, rightFilter, -1, rightKernel, new Point(-1, -1), 0);
- //Cv2.Filter2D(rightFanse, rightFilter, -1, rightKernel2, new Point(-1, -1), 0);
- Cv2.Filter2D(rightFanse, rightFilter, -1, rightKernel3, new Point(-1, -1), 0);
- Mat rightThresh = rightFilter.Threshold(14, 1, ThresholdTypes.Binary);
- for (int j = 0; j < rightThresh.Cols; j++)//从左上角,45°方向遍历
- {
- for (int k = j; k >= 0 && j - k < rightThresh.Rows; k--)
- {
- if (rightThresh.Get<byte>(j - k, k) > 0)
- {
- rightKongtong[0] = j - k + (int)rightOrdinateMiantong + 40;
- rightKongtong[1] = k + rightAperture[1];
- //Cv2.Circle(imageRed, k + rightAperture[1], k - j + (int)rightOrdinateMiantong + upper - 10, 1, 1, 1);
- //new Window("imageRed_Line", WindowMode.Normal, imageRed);
- break;
- }
- }
- if (rightKongtong[0] != 0)
- break;
- }
- #endregion
- //ImageShow(leftCrop, leftContour * 255, /*rightContour * 255, */leftThresh * 255/*, rightThresh * 255*/);
- //Cv2.Circle(imageRed, new Point(leftKongtong[1], leftKongtong[0]), 10, new Scalar(0), 2);
- //Cv2.Circle(imageRed, new Point(rightKongtong[1], rightKongtong[0]), 10, new Scalar(0), 2);
- ////ImageShow(imageRed);
- GetKongtong(imageContour, leftKongtong, rightKongtong, out kongtong, out leftPoint2, out rightPoint2);
- //LineShow(imageRed, leftKongtong[1], leftKongtong[0], leftPoint2[1], leftPoint2[0]);
- //LineShow(imageRed, rightKongtong[1], rightKongtong[0], rightPoint2[1], rightPoint2[0]);
- //ImageShow(imageRed);
- //leftKongtong[0] -= upper;
- //rightKongtong[0] -= upper;
- //leftPoint2[0] -= upper;
- //rightPoint2[0] -= upper;
- if(rightFilter != null)
- {
- rightFilter.Dispose();
- }
- }
- /// <summary>
- /// 计算孔铜
- /// </summary>
- /// <param name="imageContour">二值图</param>
- /// <param name="apertureBegin">左孔径坐标</param>
- /// <param name="apertureEnd">右孔径坐标</param>
- /// <param name="kongtong">输出左右孔铜的距离</param>
- /// <param name="pointLeft">左孔铜对应边缘线的坐标</param>
- /// <param name="pointRight">右孔铜对应边缘线的坐标</param>
- public void GetKongtong(Mat imageContour, int[] apertureBegin, int[] apertureEnd, out double[] kongtong, out int[] pointLeft, out int[] pointRight)
- {
- //曲面上点的坐标
- int count = apertureEnd[1] - apertureBegin[1];
- int[,] coordinate = new int[Math.Abs(apertureEnd[1] - apertureBegin[1]), 2];//先纵坐标后横坐标
- for (int j = apertureBegin[1]; j < apertureEnd[1]; j++)
- {
- for (int i = 0; i < imageContour.Rows; i++)
- {
- if (imageContour.Get<byte>(i, j) > 0)
- {
- coordinate[j - apertureBegin[1], 0] = i;
- coordinate[j - apertureBegin[1], 1] = j;
- break;
- }
- }
- }
- //计算距离,最小的距离分别为左孔铜和右孔铜
- double leftKongtong = 1000;
- double rightKongtong = 1000;
- double distance1 = 0;
- double distance2 = 0;
- //当距离最短是曲线上点的坐标,第一列是纵坐标(行数),第二列是横坐标(列数)
- pointLeft = new int[2];
- pointRight = new int[2];
- for (int i = 0; i < count; i++)
- {
- //计算曲面点到左起始点的距离
- if (i < count / 2)
- {
- distance1 = Math.Sqrt(Math.Pow((coordinate[i, 0] - apertureBegin[0]), 2) + Math.Pow((coordinate[i, 1] - apertureBegin[1]), 2));
- //计算曲面点到右截止点的距离
- if (leftKongtong > distance1)
- {
- leftKongtong = distance1;
- pointLeft[0] = coordinate[i, 0];
- pointLeft[1] = coordinate[i, 1];
- }
- }
- else
- {
- distance2 = Math.Sqrt(Math.Pow((coordinate[i, 0] - apertureEnd[0]), 2) + Math.Pow((coordinate[i, 1] - apertureEnd[1]), 2));
- //得到左右孔铜以及对应的曲面坐标
- if (rightKongtong > distance2)
- {
- rightKongtong = distance2;
- pointRight[0] = coordinate[i, 0];
- pointRight[1] = coordinate[i, 1];
- }
- }
- }
- kongtong = new double[2] { leftKongtong, rightKongtong };
- }
- 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)
- {
- kongtong = new double[2];
- leftKongtong = new int[2];
- rightKongtong = new int[2];
- leftPoint2 = new int[2];
- rightPoint2 = new int[2];
- #region//左
- Mat leftCrop = image[leftL2 - 10, leftL2 + 20, leftAperture[1] - 30, leftAperture[1] + 5].Clone();
- Mat leftThresh = leftCrop.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat leftFanse = 1 - leftThresh;
- InputArray leftKernel = InputArray.Create<int>(new int[17, 17] {
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 1, 1, 1, 1, 1, 1, 1, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- });
- InputArray leftKernel2 = InputArray.Create<int>(new int[17, 17] {
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0 },
- { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1 },
- });
- Mat leftFilter1 = new Mat();
- Cv2.Filter2D(leftFanse, leftFilter1, -1, leftKernel, new Point(-1, -1), 0);
- Mat leftFilter2 = new Mat();
- Cv2.Filter2D(leftFanse, leftFilter2, -1, leftKernel2, new Point(-1, -1), 0);
- Mat leftFilter = new Mat();
- Cv2.AddWeighted(leftFilter1, 0, leftFilter2, 1, 0, leftFilter);
- Cv2.Rectangle(leftFilter, new Rect(0, 0, leftFilter.Cols, 5), new Scalar(0), -1);
- Cv2.Rectangle(leftFilter, new Rect(0, 15, leftFilter.Cols, leftFilter.Rows - 15), new Scalar(0), -1);
- double min, max;
- Cv2.MinMaxIdx(leftFilter, out min, out max);
- Mat leftFilterThresh = leftFilter.Threshold(max - 2, 1, ThresholdTypes.Binary);
- Cv2.Flip(leftFilterThresh, leftFilterThresh, FlipMode.Y);
- Point left;
- FindLeftTop(leftFilterThresh, out left);
- leftKongtong[0] = left.Y + leftL2 - 10;
- leftKongtong[1] = leftCrop.Cols - left.X + leftAperture[1] - 30;
- Mat newLeft = leftFilterThresh.Clone();
- newLeft.Set<byte>(left.Y, left.X, 255);
- //ImageShow(leftCrop, leftFilter * 50, leftFilterThresh * 255, newLeft);
- #endregion
- #region//右
- Mat rightCrop = image[rightL2 - 10, rightL2 + 20, rightAperture[1] - 5, rightAperture[1] + 30].Clone();
- Mat rightThresh = rightCrop.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat rightFanse = 1 - rightThresh;
- InputArray rightKernel = InputArray.Create<int>(new int[17, 17] {
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 },
- { 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0,-1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
- { 0, 0, 0, 0, 0,-1, 0,-1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
- { 0, 0, 0, 0,-1, 0, 1,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
- { 0, 0, 0,-1, 0, 1, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
- { 0, 0,-1, 0, 1, 0, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
- { 0,-1, 0, 1, 0, 0, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
- { -1, 0, 1, 0, 0, 0, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
- { 0, 1, 0, 0, 0, 0, 0,-1, 0, 1, 1,-1,-1,-1,-1,-1,-1 },
- });
- InputArray rightKernel2 = InputArray.Create<int>(new int[17, 17] {
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 },
- { 0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- { 1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
- });
- Mat rightFilter1 = new Mat();
- Cv2.Filter2D(rightFanse, rightFilter1, -1, rightKernel, new Point(-1, -1), 0);
- Mat rightFilter2 = new Mat();
- Cv2.Filter2D(rightFanse, rightFilter2, -1, rightKernel2, new Point(-1, -1), 0);
- Mat rightFilter = new Mat();
- Cv2.AddWeighted(rightFilter1, 0, rightFilter2, 1, 0, rightFilter);
- Cv2.Rectangle(rightFilter, new Rect(0, 0, rightFilter.Cols, 5), new Scalar(0), -1);
- Cv2.Rectangle(rightFilter, new Rect(0, 15, rightFilter.Cols, rightFilter.Rows - 15), new Scalar(0), -1);
- Cv2.MinMaxIdx(rightFilter, out min, out max);
- Mat rightFilterThresh = rightFilter.Threshold(max - 2, 1, ThresholdTypes.Binary);
- Point right;
- FindLeftTop(rightFilterThresh, out right);
- rightKongtong[0] = right.Y + rightL2 - 10;
- rightKongtong[1] = right.X + rightAperture[1] - 5;
- Mat newRight = rightFilterThresh.Clone();
- newRight.Set<byte>(right.Y, right.X, 255);
- //ImageShow(rightCrop, rightThresh * 255, rightFilter2 * 50, rightFilterThresh * 255, newRight);
- #endregion
- #region//边缘线
- GetKongtong(imageContour, leftKongtong, rightKongtong, out kongtong, out leftPoint2, out rightPoint2);
- #endregion
- }
- /// <summary>
- /// 计算最小孔环,从外侧交点到上孔径
- /// </summary>
- /// <param name="imageContour"></param>
- /// <param name="upper"></param>
- /// <param name="lower"></param>
- /// <param name="leftBorder"></param>
- /// <param name="rightBorder"></param>
- /// <param name="pointRing"></param>
- /// <param name="direction"></param>
- public void GetMinmumRing(Mat imageContour, int upper, int lower, int leftBorder, int rightBorder, out int[] pointRing, string direction)
- {
- pointRing = new int[2];
- Mat crop2 = imageContour[upper, lower, leftBorder, rightBorder].Clone();
- Scalar sum = new Scalar(0);
- Scalar lastSum = new Scalar(0);
- switch (direction)
- {
- case "left":
- //for (int j = 0; j < crop2.Cols - 1; j += 5)
- //{
- // sum = crop2[0, crop2.Rows, j, j + 1].Sum();
- // if ((int)lastSum != 0 && (int)sum-(int)lastSum>10)
- // {
- // pointRing[1] = j+leftBorder-2;
- // break;
- // }
- // lastSum = sum;
- //}
- for (int j = crop2.Cols - 1; j > 1; j -= 10)
- {
- sum = crop2[0, crop2.Rows, j - 1, j].Sum();
- if ((int)lastSum != 0 && (int)lastSum - (int)sum > 10)
- {
- pointRing[1] = j + leftBorder - 5;
- break;
- }
- lastSum = sum;
- }
- break;
- case "right":
- //for (int j = crop2.Cols - 1; j > 2; j -= 5)
- //{
- // sum = crop2[0, crop2.Rows, j - 1, j].Sum();
- // if ((int)lastSum != 0 && (int)sum - (int)lastSum > 10)
- // {
- // pointRing[1] = j + leftBorder+2;
- // break;
- // }
- // lastSum = sum;
- //}
- for (int j = 1; j < crop2.Cols - 1; j += 10)
- {
- sum = crop2[0, crop2.Rows, j, j + 1].Sum();
- if ((int)lastSum != 0 && (int)lastSum - (int)sum > 10)
- {
- pointRing[1] = j + leftBorder + 5;
- break;
- }
- lastSum = sum;
- }
- break;
- }
- //ImageShow(crop2 * 255);
- //int j = 0;
- //for (int i = crop2.Rows-1;i>0;i--)
- //{
- // for (int k = i; k < crop2.Rows&&(crop2.Cols-1-(k-i))>0; k++)
- // {
- // j = crop2.Cols - 1 - (k - i);
- // if (crop2.Get<byte>(k, j) > 0)
- // {
- // pointLeftRing[0] = k + upper;
- // pointLeftRing[1] = j ;
- // break;
- // }
- // }
- //}
- //ImageShow(filter * 30, thresh * 255,crop*255,crop2*255);
- }
- /// <summary>
- /// 得到边缘线上拐点的坐标
- /// </summary>
- /// <param name="imageContour">二值图</param>
- /// <param name="apertureBegin">左孔径坐标</param>
- /// <param name="apertureEnd">右孔径坐标</param>
- /// <param name="curveVertex">输出拐点坐标,0:縱坐標;1:橫坐標</param>
- /// <param name="middleApertureY">孔径对应边缘线上点的平均纵坐标</param>
- public void CurveVertex(Mat imageContour, int[] apertureBegin, int[] apertureEnd, out int[] curveVertex, out double middleApertureY)
- {
- //曲面顶点的坐标
- curveVertex = new int[2];
- //孔径平均横坐标
- double middleAperture = (apertureBegin[1] + apertureEnd[1]) / 2;
- //孔径平均高度
- int[] apertureY = new int[2];
- for (int i = 0; i < imageContour.Rows; i++)
- {
- if (imageContour.Get<byte>(i, apertureBegin[1]) > 0)
- {
- apertureY[0] = i;
- break;
- }
- }
- for (int i = 0; i < imageContour.Rows; i++)
- {
- if (imageContour.Get<byte>(i, apertureEnd[1]) > 0)
- {
- apertureY[1] = i;
- break;
- }
- }
- middleApertureY = (apertureY[0] + apertureY[1]) / 2;
- //边缘线坐标
- int count = Math.Abs((apertureEnd[1] - apertureBegin[1]) / 2);
- int[,] ordinate = new int[count, 2];
- int range = apertureEnd[1] - apertureBegin[1];
- for (int j = range / 4 + apertureBegin[1]; j < range / 4 + apertureBegin[1] + count; j++)
- {
- for (int i = 0; i < imageContour.Rows; i++)
- {
- if (imageContour.Get<byte>(i, j) > 0)
- {
- ordinate[j - apertureBegin[1] - range / 4, 0] = i;
- ordinate[j - apertureBegin[1] - range / 4, 1] = j;
- break;
- }
- }
- }
- //孔深,取曲面上的点到孔径平均高度只差的最大值
- double kongshenAomian = 0;//当边缘线内凹或外凸,孔深最大值
- //double kongshenTumian = 1000;//当边缘线内凸,孔深最小值
- int[] coordinateAomian = new int[2];
- //int[] coordinateTumian = new int[2];
- double t = 0;
- for (int i = 0; i < count; i++)
- {
- t = Math.Abs(middleApertureY - ordinate[i, 0]);
- if (kongshenAomian < t)
- {
- kongshenAomian = t;
- coordinateAomian[0] = ordinate[i, 0];
- coordinateAomian[1] = ordinate[i, 1];
- }
- //if (kongshenTumian > t)
- //{
- // kongshenTumian = t;
- // coordinateTumian[0] = ordinate[i, 0];
- // coordinateTumian[1] = ordinate[i, 1];
- //}
- }
- curveVertex = coordinateAomian;
- //判断哪个点离孔径中心更近,更近的为曲面坐标
- //double absAomian = Math.Abs(coordinateAomian[1] - middleAperture);
- //double absTumian = Math.Abs(coordinateTumian[1] - middleAperture);
- //if (absAomian < absTumian)
- //{
- // curveVertex = coordinateAomian;
- //}
- //else
- //{
- // curveVertex = coordinateTumian;
- //}
- }
- public void ShenmangkongShangkongjing(Mat contour, int leftL3, int rightL3, int[] lowerAperture, out int[] leftAperture, out int[] rightAperture)
- {
- #region//左
- Mat cropLeft = contour[leftL3 - 20, leftL3 + 20, (lowerAperture[0] - 200)<0?0:(lowerAperture[0] - 200), lowerAperture[0] + 200].Clone();
- InputArray leftKernel = InputArray.Create<int>(new int[17, 17] {
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
- { -1,-1,-1,-1,-1,-1,-1,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0,-1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
- });
- Mat leftFilter = new Mat();
- Cv2.Filter2D(cropLeft, leftFilter, -1, leftKernel, new Point(-1, -1), 0);
- double min2, max;
- Cv2.MinMaxIdx(leftFilter, out min2, out max);
- Mat leftThresh = leftFilter.Threshold(max - 2, 1, ThresholdTypes.Binary);
- Cv2.Flip(leftThresh, leftThresh, FlipMode.Y);
- leftAperture = new int[2];
- int min = 100000;
- Point[] leftIdx;
- FindNonZeros(leftThresh, out leftIdx);
- for (int i = 0; i < leftIdx.Length; i++)
- {
- if (min > (leftIdx[i].X + leftIdx[i].Y))
- {
- min = leftIdx[i].X + leftIdx[i].Y;
- leftAperture[1] = leftThresh.Cols - leftIdx[i].X;
- leftAperture[0] = leftIdx[i].Y;
- }
- }
- leftAperture[1] += lowerAperture[0] - 200;
- leftAperture[0] += leftL3 - 20;
- #endregion
- #region//右
- Mat cropRight = contour[rightL3 - 20, rightL3 + 20, lowerAperture[1] - 200, lowerAperture[1] + 200].Clone();
- InputArray rightKernel = InputArray.Create<int>(new int[17, 17] {
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1,-1,-1,-1,-1,-1,-1,-1 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, 0, 0 },
- });
- Mat rightFilter = new Mat();
- Cv2.Filter2D(cropRight, rightFilter, -1, rightKernel, new Point(-1, -1), 0);
- Cv2.MinMaxIdx(rightFilter, out min2, out max);
- Mat rightThresh = rightFilter.Threshold(max - 2, 1, ThresholdTypes.Binary);
- rightAperture = new int[2];
- Point[] rightIdx;
- FindNonZeros(rightThresh, out rightIdx);
- min = 1000000;
- for (int i = 0; i < rightIdx.Length; i++)
- {
- if (min > (rightIdx[i].X + rightIdx[i].Y))
- {
- min = rightIdx[i].X + rightIdx[i].Y;
- rightAperture[1] = rightIdx[i].X;
- rightAperture[0] = rightIdx[i].Y;
- }
- }
- rightAperture[1] += lowerAperture[1] - 200;
- rightAperture[0] += rightL3 - 20;
- #endregion
- //ImageShow( cropRight * 255, rightThresh * 255);
- }
- /// <summary>
- /// 得到深盲孔的下孔径
- /// </summary>
- /// <param name="imageContour">二值图像</param>
- /// <param name="aperture">输出下孔径</param>
- /// <param name="leftUpper">左边计算区域的上边界</param>
- /// <param name="rightUpper">右边计算区域的上边界</param>
- /// <param name="leftLower">左边计算区域的下边界</param>
- /// <param name="rightLower">右边计算区域的下边界</param>
- /// <param name="dataArea">数据提取区域</param>
- public void GetShenmangLowerAperture(Mat imageContour, out int[] aperture, int leftUpper, int rightUpper, int leftLower, int rightLower, int[] dataArea)
- {
- //孔径起始点
- aperture = new int[2];
- aperture[0] = 0;
- aperture[1] = 0;
- int middle = (dataArea[1] + dataArea[2]) / 2;
- Mat fanse = 1 - imageContour;
- //Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- //Mat open = new Mat();
- //Cv2.MorphologyEx(fanse, open,MorphTypes.Open, se);
- //fanse = open;
- for (int j = middle; j > dataArea[0]; j--)
- {
- for (int i = leftUpper - 20 < 0 ? 0 : leftUpper - 20; i < leftLower - 20; i++)
- {
- if (fanse.Get<byte>(i, j) > 0)
- {
- aperture[0] = j;
- break;
- }
- }
- if (aperture[0] != 0)
- break;
- }
- for (int j = middle; j < dataArea[3]; j++)
- {
- for (int i = rightUpper - 20 < 0 ? 0 : rightUpper - 20; i < rightLower - 20; i++)
- {
- if (fanse.Get<byte>(i, j) > 0)
- {
- aperture[1] = j;
- break;
- }
- }
- if (aperture[1] != 0)
- break;
- }
- }
- /// <summary>
- /// 下孔径新方法,用胶体部分置1,向中间遍历,为0时下孔径
- /// </summary>
- /// <param name="imageContour"></param>
- /// <param name="aperture"></param>
- /// <param name="leftUpper"></param>
- /// <param name="rightUpper"></param>
- /// <param name="leftLower"></param>
- /// <param name="rightLower"></param>
- /// <param name="dataArea"></param>
- public void GetLowerAperture(Mat imageContour, out int[] aperture, int leftUpper, int rightUpper, int leftLower, int rightLower, int[] dataArea)
- {
- aperture = new int[2];
- int middle = (dataArea[1] + dataArea[2]) / 2;
- Mat fill = new Mat();
- Fill(imageContour, out fill, 1);
- Mat fanse = 1 - fill;
- Scalar sum = fanse.Sum();
- if ((int)sum != 0)
- {
- imageContour = fill.Clone();
- }
- Mat result = 1 - imageContour;
- Scalar sumLeft = new Scalar(0);
- for (int j = dataArea[0]; j < middle; j++)
- {
- sumLeft = result[leftUpper<0?0: leftUpper, leftLower, j, j + 1].Sum();
- if ((int)sumLeft == 0)
- {
- aperture[0] = j;
- break;
- }
- }
- Scalar sumRight = new Scalar(0);
- for (int j = dataArea[3]; j > middle; j--)
- {
- sumRight = result[rightUpper, rightLower, j - 1, j].Sum();
- if ((int)sumRight == 0)
- {
- aperture[1] = j;
- break;
- }
- }
- if(fill != null)
- {
- fill.Dispose();
- }
- }
-
-
- public void GetShenmangLowerAperture2(Mat imageContour, out int[] aperture, int leftUpper, int rightUpper, int leftLower, int rightLower, int[] dataArea)
- {
- aperture = new int[2] { 0, 0 };
- int middle = (dataArea[1] + dataArea[2]) / 2;
- Mat fanse = 1 - imageContour;
- int offset = 20;
- int sum = 0;
- for (int j = dataArea[1]; j < middle; j++)
- {
- sum = 0;
- for (int i = leftUpper - offset; i < leftLower - offset; i++)
- {
- sum += fanse.Get<byte>(i, j);
- if (fanse.Get<byte>(i, j) > 0)
- break;
- }
- if (sum == 0)
- {
- aperture[0] = j;
- break;
- }
- }
- for (int j = dataArea[2]; j > middle; j--)
- {
- sum = 0;
- for (int i = rightUpper - offset; i < rightLower - offset; i++)
- {
- sum += fanse.Get<byte>(i, j);
- if (fanse.Get<byte>(i, j) > 0)
- break;
- }
- if (sum == 0)
- {
- aperture[1] = j;
- break;
- }
- }
- }
- /// <summary>
- /// 得到深盲孔的胶线
- /// </summary>
- /// <param name="image">输入图像,一般是绿色通道</param>
- /// <param name="glueOrdinate">输出胶线纵坐标</param>
- /// <param name="leftUpper">左边上界</param>
- /// <param name="leftLower">左边下界</param>
- /// <param name="rightUpper">右边上界</param>
- /// <param name="rightLower">右边下界</param>
- /// <param name="dataArea">提取区域</param>
- public void GetGlue(Mat image, out int[] glueOrdinate, int leftUpper, int leftLower, int rightUpper, int rightLower, int[] dataArea)
- {
- glueOrdinate = new int[2];
- int[] maybePosition = new int[2];
- //int[] zero = new int[2];
- InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat seDilate = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- Mat crop1 = image[leftUpper, leftLower, dataArea[0], dataArea[1]];
- //将裁剪区域改为面铜+基材铜附近
- //int leftMiddleMiantong_Jicaitong = (dataArea[0] + dataArea[1]) / 2;
- //Mat crop1 = image[leftUpper, leftLower, leftMiddleMiantong_Jicaitong - 10, leftMiddleMiantong_Jicaitong + 10];
- Mat filter1 = new Mat();
- Cv2.Filter2D(crop1, filter1, -1, kernel);
- Cv2.ConvertScaleAbs(filter1, filter1);
- Mat thresh1 = 1 - filter1.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat open1 = new Mat();
- Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen);
- Cv2.MorphologyEx(open1, open1, MorphTypes.Close, seOpen);
- Cv2.Dilate(open1, open1, seDilate);
- Fill(open1, out open1, 1);
- //new Window("crop1", WindowMode.Normal, crop1);
- //new Window("thresh2", WindowMode.Normal, 255 - crop1.Threshold(0, 255, ThresholdTypes.Otsu));
- //new Window("thresh1", WindowMode.Normal, thresh1 * 255);
- //new Window("end", WindowMode.Normal, open1 * 255);
- //Cv2.WaitKey();
- Scalar sum1 = new Scalar(0);
- Scalar lastSum1 = new Scalar(0);
- Scalar sub1 = new Scalar(0);
- Scalar subMax1 = new Scalar(0);
- Scalar max1 = new Scalar(0);
- Scalar zero1 = new Scalar(0);
- for (int i = 2; i < crop1.Rows - 2; i++)
- {
- sum1 = open1[i, i + 2, 0, open1.Cols - 1].Sum();
- lastSum1 = open1[i - 2, i, 0, open1.Cols - 1].Sum();
- //zero1 = open1[i - 1, i, 0, open1.Cols - 1].Sum();
- sub1 = Math.Abs((int)sum1 - (int)lastSum1);
- if ((int)max1 < (int)sum1)
- {
- max1 = sum1;
- glueOrdinate[0] = i + leftUpper;
- }
- if ((int)subMax1 < (int)sub1)
- {
- subMax1 = sub1;
- maybePosition[0] = i + leftUpper;
- }
- //if ((int)zero1 < 10)
- //{
- // zero[0] = i + leftUpper;
- //}
- }
- Mat crop2 = image[rightUpper, rightLower, dataArea[2], dataArea[3]];
- ////将裁剪区域改为面铜+基材铜附近
- //int rightMiddleMiantong_Jicaitong = (dataArea[2] + dataArea[3]) / 2;
- //Mat crop2 = image[rightUpper, rightLower, rightMiddleMiantong_Jicaitong - 10, rightMiddleMiantong_Jicaitong + 10];
- Mat filter2 = new Mat();
- Cv2.Filter2D(crop2, filter2, -1, kernel);
- Cv2.ConvertScaleAbs(filter2, filter2);
- Mat thresh2 = 1 - filter2.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat open2 = new Mat();
- Cv2.MorphologyEx(thresh2, open2, MorphTypes.Open, seOpen);
- Cv2.MorphologyEx(open2, open2, MorphTypes.Close, seOpen);
- Cv2.Dilate(open2, open2, seDilate);
- Fill(open2, out open2, 1);
- //new Window("thresh2", WindowMode.Normal, thresh2 * 255);
- //new Window("end2", WindowMode.Normal, open2 * 255);
- //Cv2.WaitKey();
- Scalar sum2 = new Scalar(0);
- Scalar lastSum2 = new Scalar(0);
- Scalar sub2 = new Scalar(0);
- Scalar subMax2 = new Scalar(0);
- Scalar max2 = new Scalar(0);
- Scalar zero2 = new Scalar(0);
- for (int i = 2; i < crop2.Rows - 2; i++)
- {
- sum2 = open2[i, i + 2, 0, open2.Cols - 1].Sum();
- lastSum2 = open2[i - 2, i, 0, open2.Cols - 1].Sum();
- //zero2 = open2[i - 1, i, 0, open2.Cols - 1].Sum();
- sub2 = Math.Abs((int)sum2 - (int)lastSum2);
- if ((int)max2 < (int)sum2)
- {
- max2 = sum2;
- glueOrdinate[1] = i + rightUpper;
- }
- if ((int)subMax2 < (int)sub2)
- {
- subMax2 = sub2;
- maybePosition[1] = i + rightUpper;
- }
- //if ((int)zero2 < 10)
- //{
- // zero[1] = i + rightUpper;
- //}
- }
- if (glueOrdinate[0] > maybePosition[0])
- {
- for (int j = maybePosition[0] - leftUpper; j < glueOrdinate[0] - leftUpper; j++)
- {
- if ((int)open1[j, j + 1, 0, open1.Cols - 1].Sum() == 0)
- {
- sum1 = new Scalar(0);
- lastSum1 = new Scalar(0);
- sub1 = new Scalar(0);
- subMax1 = new Scalar(0);
- max1 = new Scalar(0);
- for (int i = maybePosition[0] - leftUpper + 2; i < crop1.Rows - 2; i++)
- {
- sum1 = open1[i, i + 2, 0, open1.Cols - 1].Sum();
- lastSum1 = open1[i - 2, i, 0, open1.Cols - 1].Sum();
- sub1 = Math.Abs((int)sum1 - (int)lastSum1);
- if ((int)max1 < (int)sum1)
- {
- max1 = sum1;
- glueOrdinate[0] = i + leftUpper;
- }
- if ((int)subMax1 < (int)sub1)
- {
- subMax1 = sub1;
- maybePosition[0] = i + leftUpper;
- }
- }
- break;
- }
- }
- if (glueOrdinate[0] > maybePosition[0])
- {
- //if (Math.Abs(zero[0] - maybePosition[0]) > 15&&zero[0]>10)
- // glueOrdinate[0] = zero[0];
- //else
- glueOrdinate[0] = maybePosition[0];
- }
- }
- if (glueOrdinate[1] > maybePosition[1])
- {
- for (int j = maybePosition[1] - rightUpper; j < glueOrdinate[1] - rightUpper; j++)
- {
- if ((int)open2[j, j + 1, 0, open2.Cols - 1].Sum() == 0)
- {
- sum2 = new Scalar(0);
- lastSum2 = new Scalar(0);
- sub2 = new Scalar(0);
- subMax2 = new Scalar(0);
- max2 = new Scalar(0);
- for (int i = maybePosition[1] - rightUpper + 2; i < crop2.Rows - 2; i++)
- {
- sum2 = open2[i, i + 2, 0, open2.Cols - 1].Sum();
- lastSum2 = open2[i - 2, i, 0, open2.Cols - 1].Sum();
- sub2 = Math.Abs((int)sum2 - (int)lastSum2);
- if ((int)max2 < (int)sum2)
- {
- max2 = sum2;
- glueOrdinate[1] = i + rightUpper;
- }
- if ((int)subMax2 < (int)sub2)
- {
- subMax2 = sub2;
- maybePosition[1] = i + rightUpper;
- }
- }
- break;
- }
- }
- if (glueOrdinate[1] > maybePosition[1])
- {
- //if (Math.Abs(zero[1] - maybePosition[1]) > 15&&zero[1]>10)
- // glueOrdinate[1] = zero[1];
- //else
- glueOrdinate[1] = maybePosition[1];
- }
- }
- //LineShow(crop1, 10, glueOrdinate[0] - leftUpper, 20, glueOrdinate[0] - leftUpper);
- //new Window("crop1", WindowMode.Normal, crop1);
- //new Window("thresh1", WindowMode.Normal, open1 * 255);
- //new Window("thresh2", WindowMode.Normal, open2 * 255);
- //Cv2.WaitKey();
- }
- /// <summary>
- /// 计算胶线坐标,修改了判定时条件,先用于深盲孔双层
- /// </summary>
- /// <param name="image"></param>
- /// <param name="glueOrdinate"></param>
- /// <param name="leftUpper"></param>
- /// <param name="leftLower"></param>
- /// <param name="rightUpper"></param>
- /// <param name="rightLower"></param>
- /// <param name="dataArea"></param>
- public void GetGlue2(Mat image, out int[] glueOrdinate, int leftUpper, int leftLower, int rightUpper, int rightLower, int[] dataArea)
- {
- glueOrdinate = new int[2];
- int[] maybePosition = new int[2];
- //int[] zero = new int[2];
- InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat seDilate = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- int width1 = dataArea[1] - dataArea[0];
- Mat crop1 = image[leftUpper, leftLower, dataArea[0], dataArea[1]];
- if (width1 > 50)
- {
- crop1 = image[leftUpper, leftLower, dataArea[0] + (width1 - 50) / 2, dataArea[1] - (width1 - 50) / 2];
- width1 = 50;
- }
- //将裁剪区域改为面铜+基材铜附近
- //int leftMiddleMiantong_Jicaitong = (dataArea[0] + dataArea[1]) / 2;
- //Mat crop1 = image[leftUpper, leftLower, leftMiddleMiantong_Jicaitong - 10, leftMiddleMiantong_Jicaitong + 10];
- int areaSize1 = crop1.Rows * crop1.Cols;
- Mat filter1 = new Mat();
- Cv2.Filter2D(crop1, filter1, -1, kernel);
- Cv2.ConvertScaleAbs(filter1, filter1);
- Mat thresh1 = 1 - filter1.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat open1 = new Mat();
- Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen);
- Cv2.MorphologyEx(open1, open1, MorphTypes.Close, seOpen);
- Cv2.Dilate(open1, open1, seDilate);
- Fill(open1, out open1, 1);
- Scalar area1 = open1.Sum();
- while ((int)area1 > areaSize1 * 0.9)
- {
- leftUpper += 5;
- crop1 = image[leftUpper, leftLower, dataArea[0], dataArea[1]];
- if (width1 > 50)
- {
- crop1 = image[leftUpper, leftLower, dataArea[0] + (width1 - 50) / 2, dataArea[1] - (width1 - 50) / 2];
- width1 = 50;
- }
- filter1 = new Mat();
- Cv2.Filter2D(crop1, filter1, -1, kernel);
- Cv2.ConvertScaleAbs(filter1, filter1);
- thresh1 = 1 - filter1.Threshold(0, 1, ThresholdTypes.Otsu);
- open1 = new Mat();
- Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen);
- Cv2.MorphologyEx(open1, open1, MorphTypes.Close, seOpen);
- Cv2.Dilate(open1, open1, seDilate);
- Fill(open1, out open1, 1);
- areaSize1 = crop1.Rows * crop1.Cols;
- area1 = open1.Sum();
- }
- //new Window("crop1", WindowMode.Normal, crop1);
- ////new Window("thresh2", WindowMode.Normal, 255 - crop1.Threshold(0, 255, ThresholdTypes.Otsu));
- //new Window("thresh1", WindowMode.Normal, thresh1 * 255);
- //new Window("end", WindowMode.Normal, open1 * 255);
- //Cv2.WaitKey();
- Scalar sum1 = new Scalar(0);
- Scalar lastSum1 = new Scalar(0);
- Scalar sub1 = new Scalar(0);
- Scalar subMax1 = new Scalar(0);
- Scalar max1 = new Scalar(0);
- Scalar zero1 = new Scalar(0);
- int y = 0;
- for (int i = crop1.Rows - 4; i > 4; i--)
- {
- sum1 = open1[i - 3, i, 0, open1.Cols].Sum();
- if ((int)sum1 > width1 * 2.9)
- {
- y = i;
- break;
- }
- }
- for (int i = y - 1; i > 4; i--)
- {
- sum1 = open1[i - 3, i, 0, open1.Cols].Sum();
- if ((int)sum1 < width1 * 2.2)
- {
- glueOrdinate[0] = i + leftUpper;
- break;
- }
- }
- //右
- Mat crop2 = image[rightUpper, rightLower, dataArea[2], dataArea[3]];
- int width2 = dataArea[3] - dataArea[2];
- if (width2 > 50)
- {
- crop2 = image[rightUpper, rightLower, dataArea[2] + (width2 - 50) / 2, dataArea[3] - (width2 - 50) / 2];
- width2 = crop2.Cols;
- }
- ////将裁剪区域改为面铜+基材铜附近
- //int rightMiddleMiantong_Jicaitong = (dataArea[2] + dataArea[3]) / 2;
- //Mat crop2 = image[rightUpper, rightLower, rightMiddleMiantong_Jicaitong - 10, rightMiddleMiantong_Jicaitong + 10];
- int areaSize2 = crop2.Rows * crop2.Cols;
- Mat filter2 = new Mat();
- Cv2.Filter2D(crop2, filter2, -1, kernel);
- Cv2.ConvertScaleAbs(filter2, filter2);
- Mat thresh2 = 1 - filter2.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat open2 = new Mat();
- Cv2.MorphologyEx(thresh2, open2, MorphTypes.Open, seOpen);
- Cv2.MorphologyEx(open2, open2, MorphTypes.Close, seOpen);
- Cv2.Dilate(open2, open2, seDilate);
- Fill(open2, out open2, 1);
- Scalar area2 = open2.Sum();
- while ((int)area2 > areaSize2 * 0.9)
- {
- rightUpper += 5;
- crop2 = image[rightUpper, rightLower, dataArea[2], dataArea[3]];
- width2 = dataArea[3] - dataArea[2];
- if (width2 > 50)
- {
- crop2 = image[rightUpper, rightLower, dataArea[2] + (width2 - 50) / 2, dataArea[3] - (width2 - 50) / 2];
- width2 = crop2.Cols;
- }
- areaSize2 = crop2.Rows * crop2.Cols;
- filter2 = new Mat();
- Cv2.Filter2D(crop2, filter2, -1, kernel);
- Cv2.ConvertScaleAbs(filter2, filter2);
- thresh2 = 1 - filter2.Threshold(0, 1, ThresholdTypes.Otsu);
- open2 = new Mat();
- Cv2.MorphologyEx(thresh2, open2, MorphTypes.Open, seOpen);
- Cv2.MorphologyEx(open2, open2, MorphTypes.Close, seOpen);
- Cv2.Dilate(open2, open2, seDilate);
- Fill(open2, out open2, 1);
- area2 = open2.Sum();
- }
- //new Window("crop2", WindowMode.Normal, crop2);
- //new Window("filter2", WindowMode.Normal, filter2);
- //new Window("thresh2", WindowMode.Normal, thresh2 * 255);
- //new Window("end2", WindowMode.Normal, open2 * 255);
- //Cv2.WaitKey();
- Scalar sum2 = new Scalar(0);
- Scalar lastSum2 = new Scalar(0);
- Scalar sub2 = new Scalar(0);
- Scalar subMax2 = new Scalar(0);
- Scalar max2 = new Scalar(0);
- Scalar zero2 = new Scalar(0);
- y = 0;
- for (int i = crop2.Rows - 4; i > 4; i--)
- {
- sum2 = open2[i - 3, i, 0, open2.Cols].Sum();
- if ((int)sum2 > width2 * 2.9)
- {
- y = i;
- break;
- }
- }
- for (int i = y - 1; i > 4; i--)
- {
- sum2 = open2[i - 3, i, 0, open2.Cols].Sum();
- if ((int)sum2 < width2 * 2.4)
- {
- glueOrdinate[1] = i + rightUpper;
- break;
- }
- }
- if (glueOrdinate[0] == 0)
- glueOrdinate[0] = leftUpper;
- if (glueOrdinate[1] == 0)
- glueOrdinate[1] = rightUpper;
- }
- /// <summary>
- /// 得到深盲孔胶内缩的内部坐标
- /// </summary>
- /// <param name="imageContour">二值图像</param>
- /// <param name="upperWaist">输出内部坐标,先左后右</param>
- /// <param name="lowerWaist">输出外部坐标,先左后右</param>
- /// <param name="upperBound">上界</param>
- /// <param name="glueOrdinate">胶线的坐标</param>
- /// <param name="lowerBound">下界</param>
- /// <param name="dataArea">提取数据区域</param>
- public void GetWaist(Mat imageContour, out int[] upperWaist, out int[] lowerWaist, int upperBound, int[] glueOrdinate, int lowerBound, int[] dataArea)
- {
- //Cv2.ImWrite(@"C:\Users\zyh\Desktop\imageContour.jpg", imageContour * 255);
- //寻找连通区域,把除第一个联通区域的都置为0
- Mat labelMat = new Mat();
- Mat stats = new Mat();
- Mat centroids = new Mat();
- int nums = Cv2.ConnectedComponentsWithStats(imageContour, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
- int y = stats.At<int>(1, 1);
- int height = stats.At<int>(1, 3);
- for(int h=height+1; h< imageContour.Height; h++)
- {
- imageContour.Row[h] *= 0;
- }
- upperWaist = new int[2] { 0, 0 };
- lowerWaist = new int[2] { 0, 0 };
- int middle = (dataArea[1] + dataArea[2]) / 2;
- int min_left=int.MaxValue, max_left = 0;
- int min_left_x = 0, max_left_x = 0;
- for (int j = middle-30; j > dataArea[1]; j--)
- {
- int v = imageContour.Col[j].CountNonZero();
- if (v <= min_left) { min_left = v; min_left_x = j; }
- if (v >= max_left) { max_left = v; max_left_x = j; }
- }
- upperWaist[0] = max_left_x;
- OpenCvSharp.Rect rect_Left = new Rect(dataArea[1], upperBound, max_left_x- dataArea[1], lowerBound- upperBound);
- Mat temp_left = new Mat(imageContour, rect_Left);
- //Cv2.ImWrite(@"C:\Users\zyh\Desktop\temp_left.jpg", temp_left*255);
- bool f_l = false;
- for (int j=0; j< temp_left.Width; j++)
- {
- if(!f_l && temp_left.Col[j].CountNonZero()>0)
- {
- f_l = true;
- lowerWaist[0] = j + dataArea[1];
- //break;
- }
- else if(temp_left.Col[j].CountNonZero()== temp_left.Height)
- {
- upperWaist[0] = j + dataArea[1];
- break;
- }
- }
- int min_right = int.MaxValue, max_right = 0;
- int min_right_x = 0, max_right_x = 0;
- for (int j = middle+30; j < dataArea[2]; j++)
- {
- int v = imageContour.Col[j].CountNonZero();
- if (v <= min_right) { min_right = v; min_right_x = j; }
- if (v >= max_right) { max_right = v; max_right_x = j; }
- }
- upperWaist[1] = max_right_x;
- OpenCvSharp.Rect rect_right = new Rect(max_right_x, upperBound, dataArea[2]- max_right_x, lowerBound - upperBound);
- Mat temp_right = new Mat(imageContour, rect_right);
- //Cv2.ImWrite(@"C:\Users\zyh\Desktop\rect_right.jpg", temp_right*255);
- bool f_r = false;
- for (int j = temp_right.Width-1; j > 0; j--)
- {
- if (!f_r && temp_right.Col[j].CountNonZero() > 0)
- {
- f_r = true;
- lowerWaist[1] = j+ max_right_x;
- //break;
- }
- else if(temp_right.Col[j].CountNonZero() == temp_right.Height)
- {
- upperWaist[1] = j + max_right_x;
- break;
- }
- }
- /*Mat fanse = 1 - imageContour;
- //上
- for (int j = middle; j > dataArea[0]; j--)
- {
- for (int i = upperBound; i < glueOrdinate[0]; i++)
- {
- if (fanse.Get<byte>(i, j) > 0*//* && j< (middle + dataArea[0]) / 2*//*)
- {
- upperWaist[0] = j;
- break;
- }
- }
- if (upperWaist[0] != 0)
- break;
- }
- for (int j = middle; j < dataArea[3]; j++)
- {
- for (int i = upperBound; i < glueOrdinate[1]; i++)
- {
- if (fanse.Get<byte>(i, j) > 0*//* && j > (middle + dataArea[3])/2*//*)
- {
- upperWaist[1] = j;
- break;
- }
- }
- if (upperWaist[1] != 0)
- break;
- }
- //下
- for (int j = (dataArea[0] + dataArea[1]) / 2; j < middle; j++)
- {
- for (int i = glueOrdinate[0] - 20; i < lowerBound; i++)
- {
- //new Window("imageContour", WindowMode.Normal, imageContour * 255);
- //Cv2.WaitKey();
- if (imageContour.Get<byte>(i, j) > 0)
- {
- lowerWaist[0] = j;
- break;
- }
- }
- if (lowerWaist[0] != 0)
- break;
- }
- for (int j = (dataArea[3] + dataArea[2]) / 2; j > middle; j--)
- {
- for (int i = glueOrdinate[1] - 20; i < lowerBound; i++)
- {
- if (imageContour.Get<byte>(i, j) > 0)
- {
- lowerWaist[1] = j;
- break;
- }
- }
- if (lowerWaist[1] != 0)
- break;
- }*/
- }
- /// <summary>
- /// 提取胶内缩,将下边界改为左右边界
- /// </summary>
- /// <param name="imageContour">二值图像</param>
- /// <param name="upperWaist">输出内部坐标,先左后右</param>
- /// <param name="lowerWaist">输出外部坐标,先左后右</param>
- /// <param name="upperBound">上界</param>
- /// <param name="glueOrdinate">胶线的坐标</param>
- /// <param name="leftLower">下左界</param>
- /// <param name="rightLower">下右界</param>
- /// <param name="dataArea">提取数据区域</param>
- public void GetWaistNew(Mat imageContour, out int[] upperWaist, out int[] lowerWaist, int upperBound, int[] glueOrdinate, int leftLower, int rightLower, int[] dataArea)
- {
- upperWaist = new int[2] { 0, 0 };
- lowerWaist = new int[2] { 0, 0 };
- int middle = (dataArea[1] + dataArea[2]) / 2;
- Mat fanse = 1 - imageContour;
- //上
- for (int j = middle; j > dataArea[0]; j--)
- {
- for (int i = upperBound; i < glueOrdinate[0]; i++)
- {
- if (fanse.Get<byte>(i, j) > 0)
- {
- upperWaist[0] = j;
- break;
- }
- }
- if (upperWaist[0] != 0)
- break;
- }
- for (int j = middle; j < dataArea[3]; j++)
- {
- for (int i = upperBound; i < glueOrdinate[1]; i++)
- {
- if (fanse.Get<byte>(i, j) > 0)
- {
- upperWaist[1] = j;
- break;
- }
- }
- if (upperWaist[1] != 0)
- break;
- }
- //下
- //左
- for (int j = (dataArea[0] + dataArea[1]) / 2; j < middle; j++)
- {
- for (int i = glueOrdinate[0] - 20; i < leftLower; i++)
- {
- //new Window("imageContour", WindowMode.Normal, imageContour * 255);
- //Cv2.WaitKey();
- if (imageContour.Get<byte>(i, j) > 0)
- {
- lowerWaist[0] = j;
- break;
- }
- }
- if (lowerWaist[0] != 0)
- break;
- }
- //右
- for (int j = (dataArea[3] + dataArea[2]) / 2; j > middle; j--)
- {
- for (int i = glueOrdinate[1] - 20; i < rightLower; i++)
- {
- if (imageContour.Get<byte>(i, j) > 0)
- {
- lowerWaist[1] = j;
- break;
- }
- }
- if (lowerWaist[1] != 0)
- break;
- }
- }
- /// <summary>
- /// 得到精准的面铜和基材铜的纵坐标
- /// </summary>
- /// <param name="image">最好是绿色通道图</param>
- /// <param name="upperBound">上边界</param>
- /// <param name="lowerBound">下边界</param>
- /// <param name="leftBoundary">左边界</param>
- /// <param name="rightBoundary">右边界</param>
- /// <param name="middleMiantong">面铜的横坐标</param>
- /// <param name="middleJicaitong">基材铜的横坐标</param>
- /// <param name="ordinateL2Miantong">面铜的纵坐标</param>
- /// <param name="ordinateL2Jicaitong">基材铜的纵坐标</param>
- /// <param name="ordinateL2_Acc">输出纵坐标,面铜和基材铜</param>
- public void InsideLine_Accuracy(Mat image, int upperBound, int lowerBound, int leftBoundary, int rightBoundary, int middleMiantong, int middleJicaitong
- , double ordinateL2Miantong, double ordinateL2Jicaitong, out double[] ordinateL2_Acc, int isMiantong = 0, int banceng = 2/*-1*/, bool showMat = false)
- {
- //第5671deng类别图片需要完善位置精准度,以及 调试有些图片位置计算错误的原因,并纠正
- Mat crop = image[upperBound, lowerBound, leftBoundary, rightBoundary];
- //if (isMiantong > 0 && showMat)
- //{
- // Mat mat1 = new Mat();
- // Cv2.Normalize(crop, mat1, 0, 255, NormTypes.MinMax);
- // Cv2.ImWrite(@"C:\Users\54434\Desktop\crop.JPG", crop);
- //}
- Mat filter = new Mat();//滤波增强对比
- InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
- Cv2.Filter2D(crop, filter, -1, kernel);
- Cv2.ConvertScaleAbs(filter, filter);
- Mat thresh = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15/*15*/, 1/*水平线<--3, 3*/));// 开运算
- Mat open = new Mat();
- //OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
- Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
- if (isMiantong > 0 && showMat)
- {
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\filter.JPG", filter/* * 127*/);
- }
- //Scalar sum = new Scalar(0);
- //Scalar max1 = new Scalar(0);
- //Scalar max = new Scalar(0);
- //double meanOrdinate1 = 0;
- double ordinateL2Miantong_acc = ordinateL2Miantong;
- double ordinateL2Jicaitong_acc = ordinateL2Jicaitong;
- //ordinateL2_Acc = new double[] { ordinateL2Miantong_acc, ordinateL2Jicaitong_acc };
- //return;
- int topOri = (int)ordinateL2Miantong_acc - upperBound - 20;// 10;// 15;
- int bottomOri = (int)ordinateL2Miantong_acc - upperBound + 20;// 10;// 15;
- if (topOri < 10) topOri = 10;
- else if (topOri > crop.Rows/3 && topOri > 20) topOri = 20;//对初判定位置距离太远的进行纠偏
- if (bottomOri > crop.Rows - 10) bottomOri = crop.Rows - 10;
- else if (bottomOri < crop.Rows*2/3 && bottomOri < crop.Rows - 20) bottomOri = crop.Rows - 20;//对初判定位置距离太远的进行纠偏
- bool miantongChanged = false;
- int miantongValue = crop.Get<byte>((int)ordinateL2Miantong - upperBound, middleMiantong - leftBoundary);
- bool isHoriMost = false;//用来判定大横线哪个更加位置居中
- int centerVer = crop.Rows / 2;// (topOri + bottomOri) / 2;//用来判定大横线哪个更加位置居中
- for (int i = topOri; i < bottomOri; i++)
- {
- int miantongValueI = crop.Get<byte>(i, middleMiantong - leftBoundary);
- Scalar sum = open[i, i + 1, 0, crop.Cols - 1].Sum();
- if (miantongValueI < miantongValue - 1 || (!miantongChanged && miantongValueI < miantongValue + 100)
- || crop.Cols - 20 < (int)sum)
- {
- //if (open.Get<byte>(i, middleMiantong - leftBoundary) > 0)
- //Scalar sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
- //if (30/*30*/ < (int)sum)
- if (6/*30*/ < (int)sum && !isHoriMost
- ||(crop.Cols - 20 < (int)sum && isHoriMost && Math.Abs(centerVer - i) < Math.Abs(centerVer - (ordinateL2Miantong_acc - upperBound))))
- {
- ordinateL2Miantong_acc = i + upperBound;
- miantongValue = miantongValueI;
- miantongChanged = true;
- //待自测scc-2
- //if (isMiantong == 1)
- // break;
- if (crop.Cols - 20 < (int)sum/* && Math.Abs(centerVer - (ordinateL2Jicaitong_acc - upperBound)) < 10*/)
- {
- isHoriMost = true;
- //break;
- }
- }
- }
- }
- if (ordinateL2Miantong_acc == 10 + upperBound)
- {//将明显不对的测量结果居中取值
- ordinateL2Miantong_acc = centerVer + upperBound;
- }
- //if (isMiantong == 1 && !miantongChanged)
- //{
- // Mat thresh1 = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
- // Mat seOpen1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9/*15*/, 1/*水平线<--3, 3*/));// 开运算
- // Mat open1 = new Mat();
- // //OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
- // Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen1);
- // //if (isMiantong > 0 && showMat)
- // //{
- // // new Window("open1", WindowMode.Normal, open1 * 255);
- // // Cv2.WaitKey();
- // //}
- // for (int i = topOri; i < bottomOri; i++)
- // {
- // //int miantongValueI = crop.Get<byte>(i, middleMiantong - leftBoundary);
- // //if (miantongValueI < 200/*miantongValue*/)
- // {
- // if (open1.Get<byte>(i, middleMiantong - leftBoundary) > 0)
- // //Scalar sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
- // //if (30/*30*/ < (int)sum)
- // {
- // ordinateL2Miantong_acc = i + upperBound;
- // miantongValue = crop.Get<byte>(i, middleMiantong - leftBoundary);// miantongValueI;
- // miantongChanged = true;
- // break;
- // }
- // }
- // }
- //}
- isHoriMost = false;//用来判定大横线哪个更加位置居中
- bool jicaitongChanged = false;
- int jicaitongValue = crop.Get<byte>((int)ordinateL2Jicaitong - upperBound, middleJicaitong - leftBoundary);
- for (int i = /*0*/topOri; i < bottomOri; i++)
- {
- int jicaitongValueI = crop.Get<byte>(i, middleJicaitong - leftBoundary);
- Scalar sum = open[i, i + 1, 0, crop.Cols - 1].Sum();
- if (jicaitongValueI < jicaitongValue - 1 || (!jicaitongChanged && jicaitongValueI < jicaitongValue + 100)
- || crop.Cols - 20 < (int)sum)
- {
- //if (open.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
- //Scalar sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
- ////Scalar sum = open[i, i + 2, middleJicaitong - leftBoundary - 4, middleJicaitong - leftBoundary + 4].Sum();
- if (6/*30*/ < (int)sum && !isHoriMost
- || (crop.Cols - 20 < (int)sum && isHoriMost && Math.Abs(centerVer - i) < Math.Abs(centerVer - (ordinateL2Jicaitong_acc - upperBound))))
- {
- ordinateL2Jicaitong_acc = i + upperBound;
- jicaitongValue = jicaitongValueI;
- jicaitongChanged = true;
- //待自测scc-2
- //if (isMiantong == 1)
- // break;
- if (crop.Cols - 20 < (int)sum/* && Math.Abs(centerVer - (ordinateL2Jicaitong_acc - upperBound)) < 10*/)
- {
- isHoriMost = true;
- //break;
- }
- }
- }
- }
- if (ordinateL2Jicaitong_acc == 10 + upperBound)
- {//将明显不对的测量结果居中取值
- ordinateL2Jicaitong_acc = centerVer + upperBound;
- }
- if (!miantongChanged && !jicaitongChanged)
- {
- int topOri_1 = topOri;
- if (topOri > 10) topOri_1 = 10;
- int bottomOri_1 = bottomOri;
- if (bottomOri < crop.Rows - 10) bottomOri_1 = crop.Rows - 10;
- miantongValue = crop.Get<byte>((int)ordinateL2Miantong - upperBound, middleMiantong - leftBoundary);
- for (int i = topOri_1; i < bottomOri_1; i++)
- {
- int miantongValueI = crop.Get<byte>(i, middleMiantong - leftBoundary);
- Scalar sum = open[i, i + 1, 0, crop.Cols - 1].Sum();
- if (miantongValueI < miantongValue - 1 || (!miantongChanged && miantongValueI < miantongValue + 100)
- || crop.Cols - 20 < (int)sum)
- {
- if (6 < (int)sum)
- {
- ordinateL2Miantong_acc = i + upperBound;
- miantongValue = miantongValueI;
- miantongChanged = true;
- if (crop.Cols - 20 < (int)sum)
- break;
- }
- }
- if (i == topOri - 1) i = bottomOri;
- }
- jicaitongValue = crop.Get<byte>((int)ordinateL2Jicaitong - upperBound, middleJicaitong - leftBoundary);
- for (int i = topOri_1; i < bottomOri_1; i++)
- {
- int jicaitongValueI = crop.Get<byte>(i, middleJicaitong - leftBoundary);
- Scalar sum = open[i, i + 1, 0, crop.Cols - 1].Sum();
- if (jicaitongValueI < jicaitongValue - 1 || (!jicaitongChanged && jicaitongValueI < jicaitongValue + 100)
- || crop.Cols - 20 < (int)sum)
- {
- if (6 < (int)sum)
- {
- ordinateL2Jicaitong_acc = i + upperBound;
- jicaitongValue = jicaitongValueI;
- jicaitongChanged = true;
- if (crop.Cols - 20 < (int)sum)
- break;
- }
- }
- if (i == topOri - 1) i = bottomOri;
- }
- }
- if ((isMiantong == 1 || isMiantong == 2) && (!miantongChanged /*//待自测scc-3_1 &&*/|| !jicaitongChanged))
- {
- Mat thresh1 = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
- Mat seOpen1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9/*15*/, 1/*水平线<--3, 3*/));// 开运算
- Mat open1 = new Mat();
- //OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
- Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen1);
- //if (isMiantong > 0 && showMat)
- //{
- // new Window("open1", WindowMode.Normal, open1 * 255);
- // Cv2.WaitKey();
- //}
-
-
-
- //待自测scc-3_1
- if (isMiantong == 1 && !miantongChanged || isMiantong == 2 && !jicaitongChanged)
- for (int i = bottomOri; i > topOri; i--)
- {
- //还差第3、5、7类别图片需要完善位置精准度,以及
- //调试有些图片位置计算错误的原因,并纠正!!!!!!!!!!!!
- {
- if (isMiantong == 1 && open1.Get<byte>(i, middleMiantong - leftBoundary) > 0
- || isMiantong == 2 && open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
- {
- if (isMiantong == 1)
- ordinateL2Miantong_acc = i + upperBound;
- else if (isMiantong == 2)
- ordinateL2Jicaitong_acc = i + upperBound;
- break;
- }
- }
- }
- //if (isMiantong == 1 && !miantongChanged)
- // for (int i = topOri; i < bottomOri; i++)//for (int i = bottomOri; i > topOri; i--)
- // {
- // if (open1.Get<byte>(i, middleMiantong - leftBoundary) > 0)
- // {
- // ordinateL2Miantong_acc = i + upperBound;
- // break;
- // }
- // }
- //if (isMiantong == 2 && !jicaitongChanged)
- // for (int i = bottomOri; i > topOri; i--)
- // {
- // if (open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
- // {
- // ordinateL2Jicaitong_acc = i + upperBound;
- // break;
- // }
- // }
- //if (isMiantong == 1 && !jicaitongChanged)
- // for (int i = topOri; i < bottomOri; i++)
- // {
- // {
- // if (open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
- // {
- // ordinateL2Jicaitong_acc = i + upperBound;
- // break;
- // }
- // }
- // }
- //if (isMiantong == 2 && !miantongChanged)
- // for (int i = bottomOri; i > topOri; i--)//for (int i = topOri; i < bottomOri; i++)
- // {
- // {
- // if (open1.Get<byte>(i, middleMiantong - leftBoundary) > 0)
- // {
- // ordinateL2Miantong_acc = i + upperBound;
- // break;
- // }
- // }
- // }
- if (isMiantong == 1 && !jicaitongChanged || isMiantong == 2 && !miantongChanged)
- for (int i = topOri; i < bottomOri; i++)
- {
- {
- if (isMiantong == 1 && open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0
- || isMiantong == 2 && open1.Get<byte>(i, middleMiantong - leftBoundary) > 0)
- {
- if (isMiantong == 1)
- ordinateL2Jicaitong_acc = i + upperBound;
- else if (isMiantong == 2)
- ordinateL2Miantong_acc = i + upperBound;
- break;
- }
- }
- }
- }
- //if (isMiantong == 1 && !jicaitongChanged)
- //{
- // Mat thresh1 = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
- // Mat seOpen1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9/*15*/, 1/*水平线<--3, 3*/));// 开运算
- // Mat open1 = new Mat();
- // //OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
- // Cv2.MorphologyEx(thresh1, open1, MorphTypes.Open, seOpen1);
- // //if (isMiantong > 0 && showMat)
- // //{
- // // new Window("open1", WindowMode.Normal, open1 * 255);
- // // Cv2.WaitKey();
- // //}
- // for (int i = bottomOri; i > topOri; i--)
- // {
- // //int miantongValueI = crop.Get<byte>(i, middleMiantong - leftBoundary);
- // //if (miantongValueI < 200/*miantongValue*/)
- // {
- // if (open1.Get<byte>(i, middleJicaitong - leftBoundary) > 0)
- // //Scalar sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
- // //if (30/*30*/ < (int)sum)
- // {
- // ordinateL2Jicaitong_acc = i + upperBound;
- // jicaitongValue = crop.Get<byte>(i, middleJicaitong - leftBoundary);// miantongValueI;
- // jicaitongChanged = true;
- // break;
- // }
- // }
- // }
- //}
- ////if (isMiantong == 1 && jicaitongChanged && showMat)
- ////{
- //// Mat thresh1 = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu);// 二值化
- //// //Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(11/*15*/, 1/*水平线<--3, 3*/));// 开运算
- //// //Mat open = new Mat();
- //// ////OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
- //// //Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
- //// ////if (isMiantong > 0 && showMat)
- //// ////{
- //// //// new Window("open", WindowMode.Normal, open * 255);
- //// //// Cv2.WaitKey();
- //// ////}
- ////}
- ordinateL2_Acc = new double[] { ordinateL2Miantong_acc, ordinateL2Jicaitong_acc };
- }
- /// <summary>
- /// 得到内部线纵坐标
- /// </summary>
- /// <param name="image">最好是绿色通道图</param>
- /// <param name="upperBound">上边界</param>
- /// <param name="lowerBound">下边界</param>
- /// <param name="leftBoundary">左边界</param>
- /// <param name="rightBoundary">右边界</param>
- /// <param name="meanOrdinate">输出坐标</param>
- public double InsideLine(Mat image, int upperBound, int lowerBound, int leftBoundary, int rightBoundary, out double meanOrdinate
- , int isMiantong = 0, bool showMat = false)
- {
- Mat crop = image[upperBound, lowerBound, leftBoundary-10, rightBoundary+10];
-
- /*int pos = -1;
- int min = int.MaxValue;
- for(int i=0; i< crop.Height-5; i++)
- {
- int sum = (int)(crop.Row[i].Sum())+ (int)(crop.Row[i+1].Sum())+ (int)(crop.Row[i+2].Sum())
- + (int)(crop.Row[i+3].Sum())+ (int)(crop.Row[i+4].Sum());
- if (sum < min)
- {
- pos = i;
- min = sum;
- }
- }
- if((int)(image.Row[pos+ upperBound].Sum()) < (int)(image.Row[pos+ upperBound + 40].Sum()) && pos< crop.Height/2)
- {
- int start = pos + 10;
- pos = -1;
- min = int.MaxValue;
- for (int i = start; i < crop.Height; i++)
- {
- int sum = (int)(crop.Row[i].Sum());
- if (sum < min)
- {
- pos = i;
- min = sum;
- }
- }
- }
- meanOrdinate = pos + upperBound;
- return meanOrdinate;*/
- Mat filter = new Mat();//滤波增强对比
- InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
- Cv2.Filter2D(crop, filter, -1, kernel);
- Cv2.ConvertScaleAbs(filter, filter);
- Mat thresh = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu); ;// 二值化
- //待自测scc-3_2 //此处针对3(4)的类型图片做精准化调整
- //int sizeSe = 3; if (isMiantong == 2) sizeSe = 1;// 3;// 1;
- //Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, sizeSe/*3*/));// 开运算
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));// 开运算
- Mat open = new Mat();
- Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
- //if (isMiantong > 0 && showMat)
- //{
- // Mat mat1 = new Mat();
- // Cv2.Normalize(open, mat1, 0, 255, NormTypes.MinMax);
- // Cv2.ImWrite(@"C:\Users\54434\Desktop\open.JPG", mat1);
- //}
- Scalar sum = new Scalar(0);
- Scalar max1 = new Scalar(0);
- Scalar max = new Scalar(0);
- double meanOrdinate1 = 0;
- meanOrdinate = 0;//待自测scc-3_2 + upperBound;
- for (int i = crop.Rows / 4; i < crop.Rows / 4 * 3; i++)
- {
- sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
- if ((int)max < (int)sum)
- {
- if (isMiantong > 0 && i + upperBound > 10 + meanOrdinate)
- {
- max1 = max;
- meanOrdinate1 = meanOrdinate;
- }
- max = sum;
- meanOrdinate = i + upperBound;
- }
- else if (isMiantong > 0 && i + upperBound > 10 + meanOrdinate && (int)max1 < (int)sum)
- {
- max1 = sum;
- meanOrdinate1 = i + upperBound;
- }
- }
- if (isMiantong > 0)
- {
- if (meanOrdinate < meanOrdinate1 && isMiantong == 1
- || meanOrdinate > meanOrdinate1 && meanOrdinate1 > 0 && isMiantong == 2
- )
- {
- double meanOrdinate_i = meanOrdinate;
- meanOrdinate = meanOrdinate1;
- meanOrdinate1 = meanOrdinate_i;
- }
- }
- //待自测scc-3_2
- //if (meanOrdinate == upperBound)
- // meanOrdinate = meanOrdinate1;
- //尝试纠偏一下,可是还有差很亮的,可能还是需要二值 待确认@@@@@@@@@@@@@
- 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
- && ((int)crop.Row[(int)meanOrdinate - upperBound + 10].Sum() - (int)crop.Row[(int)meanOrdinate - upperBound - 10].Sum()) < crop.Width*25)
- {
- int start = (int)(meanOrdinate - upperBound + 10);
- int pos = -1;
- int min = int.MaxValue;
- for (int i = start; i < crop.Height; i++)
- {
- int sum_a = (int)(crop.Row[i].Sum());
- if (sum_a < min)
- {
- pos = i;
- min = sum_a;
- }
- }
- meanOrdinate = pos + upperBound;
- }
- #region[清理内存]
- if (crop != null)
- {
- crop.Dispose();
- }
- if (open != null)
- {
- open.Dispose();
- }
- if (filter != null)
- {
- filter.Dispose();
- }
- if (thresh != null)
- {
- thresh.Dispose();
- }
- if (seOpen != null)
- {
- seOpen.Dispose();
- }
-
- #endregion
- return meanOrdinate1;
- }
- /// <summary>
- /// 將計算的範圍從整個行數變成四分之一到四分之三之間,不知道通孔和盲孔改變之後會不會有影響,暫時沒引用
- /// </summary>
- /// <param name="image"></param>
- /// <param name="upperBound"></param>
- /// <param name="lowerBound"></param>
- /// <param name="leftBoundary"></param>
- /// <param name="rightBoundary"></param>
- /// <param name="meanOrdinate"></param>
- public void InsideLine2(Mat image, int upperBound, int lowerBound, int leftBoundary, int rightBoundary, out double meanOrdinate)
- {
- Mat crop = image[upperBound, lowerBound, leftBoundary, rightBoundary];
- Mat filter = new Mat();//滤波增强对比
- InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
- Cv2.Filter2D(crop, filter, -1, kernel);
- Cv2.ConvertScaleAbs(filter, filter);
- Mat thresh = 1 - filter.Threshold(0, 1, ThresholdTypes.Otsu); ;// 二值化
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));// 开运算
- Mat open = new Mat();
- Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
- //new Window("thresh", WindowMode.Normal, thresh * 255);
- //new Window("open", WindowMode.Normal, open * 255);
- //Cv2.WaitKey();
- Scalar sum = new Scalar(0);
- Scalar max = new Scalar(0);
- meanOrdinate = 0;
- for (int i = crop.Rows / 4; i < crop.Rows / 4 * 3; i++)
- {
- sum = open[i, i + 2, 0, crop.Cols - 1].Sum();
- if ((int)max < (int)sum)
- {
- max = sum;
- meanOrdinate = i + upperBound;
- }
- }
- }
- /// <summary>
- /// 图像画线,线颜色是红色
- /// </summary>
- /// <param name="image">画线图像</param>
- /// <param name="x1"></param>
- /// <param name="y1"></param>
- /// <param name="x2"></param>
- /// <param name="y2"></param>
- /// <summary>
- /// 计算盲孔的上孔径
- /// </summary>
- /// <param name="image">输入二值图片</param>
- /// <param name="apertureLow">输入下孔径</param>
- /// <param name="leftOrdinate3">左边第三条线坐标</param>
- /// <param name="rightOrdinate3">右边第三条线坐标</param>
- /// <param name="leftAperture">输出左边孔径坐标,先纵坐标后横坐标</param>
- /// <param name="rightAperture">输出右边孔径坐标,先纵坐标后横坐标</param>
- public void ShangKongjing(/*Mat imageRed, */Mat image, int[] apertureLow, int leftOrdinate3, int rightOrdinate3, out int[] leftAperture, out int[] rightAperture)
- {
- leftAperture = new int[2] { 0, 0 };
- rightAperture = new int[2] { 0, 0 };
- int heightRange = 15;
- int widthMiddleRange = 1;
- int widthRange = 200;
- Mat cropLeft = 1 - image[leftOrdinate3 - heightRange, leftOrdinate3 + heightRange, Math.Abs(apertureLow[0] - widthRange), apertureLow[0] + widthMiddleRange];
- //Mat crop = imageRed[leftOrdinate3 - 15, leftOrdinate3 + 15, apertureLow[0] - 50, apertureLow[0] + 10];
- Mat cropRight = 1 - image[rightOrdinate3 - heightRange, rightOrdinate3 + heightRange, apertureLow[1] - widthMiddleRange, apertureLow[1] + widthRange];
- for (int j = cropLeft.Cols - 1; j > 0; j--)//从右上角,以135°方向遍历
- {
- for (int k = j; k < cropLeft.Cols && (k - j < cropLeft.Rows); k++)
- {
- if (cropLeft.Get<byte>(k - j, k) > 0)
- {
- leftAperture[0] = k - j + leftOrdinate3 - heightRange;
- leftAperture[1] = k + apertureLow[0] - widthRange;
- //LineShow(crop, new Point(k, k - j), new Point(k, k - j + 10));
- //new Window("cropleft", WindowMode.Normal, crop);
- //Cv2.WaitKey();
- break;
- }
- }
- if (leftAperture[0] != 0)
- break;
- }
- if (leftAperture[0] == 0)
- {
- for (int j = 1; j < cropLeft.Rows; j++)
- {
- for (int k = 0; k < cropLeft.Rows - j - 1; k++)
- {
- if (cropLeft.Get<byte>(j + k, k) > 0)
- {
- leftAperture[0] = j + k + leftOrdinate3 - heightRange;
- leftAperture[1] = k + apertureLow[0] - widthRange;
- break;
- }
- }
- if (leftAperture[0] != 0)
- break;
- }
- }
- for (int j = 0; j < cropRight.Cols; j++)//从左上角,45°方向遍历
- {
- for (int k = j; k >= 0 && j - k < cropRight.Rows; k--)
- {
- if (cropRight.Get<byte>(j - k, k) > 0)
- {
- rightAperture[0] = j - k + rightOrdinate3 - heightRange;
- rightAperture[1] = k + apertureLow[1] - widthMiddleRange;
- break;
- }
- }
- if (rightAperture[0] != 0)
- break;
- }
- if (rightAperture[0] == 0)
- {
- for (int j = 1; j < cropRight.Rows; j++)
- {
- for (int k = cropRight.Cols - 1; cropRight.Cols - 1 - k < cropRight.Rows - j - 1; k--)
- {
- if (cropRight.Get<byte>(j + cropRight.Cols - 1 - k, k) > 0)
- {
- rightAperture[0] = j + cropRight.Cols - 1 - k + rightOrdinate3 - heightRange;
- rightAperture[1] = k + apertureLow[1] - widthMiddleRange;
- break;
- }
- }
- if (rightAperture[0] != 0)
- break;
- }
- }
- //new Window("cropleft", WindowMode.Normal, cropLeft * 255);
- //new Window("cropRight", WindowMode.Normal, cropRight * 255);
- //Cv2.WaitKey();
- }
- /// <summary>
- /// 计算盲孔的上孔径,修改了计算方法,通过孔径不为零坐标和的最小值来定位
- /// </summary>
- /// <param name="image">输入二值图片</param>
- /// <param name="apertureLow">输入下孔径</param>
- /// <param name="leftOrdinate3">左边第三条线坐标</param>
- /// <param name="rightOrdinate3">右边第三条线坐标</param>
- /// <param name="leftAperture">输出左边孔径坐标,先纵坐标后横坐标</param>
- /// <param name="rightAperture">输出右边孔径坐标,先纵坐标后横坐标</param>
- public void ShangKongjing2(Mat image, int[] apertureLow, int leftOrdinate3, int rightOrdinate3, out int[] leftAperture, out int[] rightAperture)
- {
- leftAperture = new int[2] { 0, 0 };
- rightAperture = new int[2] { 0, 0 };
- int heightRange = 10;
- int widthMiddleRange = 50;
- int widthRange = 200;
- Mat cropLeft = 1 - image[leftOrdinate3 - heightRange, leftOrdinate3 + heightRange, apertureLow[0] - widthRange, apertureLow[0] + widthMiddleRange];
- //Mat crop = imageRed[leftOrdinate3 - 15, leftOrdinate3 + 15, apertureLow[0] - 50, apertureLow[0] + 10];
- Mat cropRight = 1 - image[rightOrdinate3 - heightRange, rightOrdinate3 + heightRange, apertureLow[1] - widthMiddleRange, apertureLow[1] + widthRange];
- Cv2.Flip(cropLeft, cropLeft, FlipMode.Y);
- int minLeft = 1000, minRight = 1000;
- int t1 = 0, t2 = 0;
- for (int i = 0; i < cropLeft.Rows; i++)
- {
- for (int j = 0; j < cropLeft.Cols; j++)
- {
- if (cropLeft.Get<byte>(i, j) > 0)
- {
- t1 = i + j;
- }
- if (minLeft > t1 + 10 && t1 != 0)
- {
- minLeft = t1;
- leftAperture[0] = i + leftOrdinate3 - heightRange;
- leftAperture[1] = cropLeft.Cols - j + apertureLow[0] - widthRange;
- }
- }
- }
- for (int i = 0; i < cropRight.Rows; i++)
- {
- for (int j = 0; j < cropRight.Cols; j++)
- {
- if (cropRight.Get<byte>(i, j) > 0)
- {
- t2 = i + j;
- }
- if (minRight > t2 && t2 != 0)
- {
- minRight = t2;
- rightAperture[0] = i + rightOrdinate3 - heightRange;
- rightAperture[1] = j + apertureLow[1] - widthMiddleRange;
- }
- }
- }
- //new Window("cropcontour", WindowMode.Normal, image * 255);
- new Window("cropleft", WindowMode.Normal, cropLeft * 255);
- //new Window("cropright", WindowMode.Normal, cropRight * 255);
- Cv2.WaitKey();
- }
- //蝕刻因子
- /// <summary>
- /// 得到蝕刻因子圖像的提取區域
- /// </summary>
- /// <param name="imageContour">輸入二值圖像</param>
- /// <param name="dataArea">輸出邊界位置,0:上界;1:下界;2:左界;3:右界</param>
- public void SKYZDataArea(Mat imageContour, out int[] dataArea, bool isCropFlag)
- {
- dataArea = new int[4] { 0, 0, 0, 0 };
- Scalar sum = new Scalar();
- for (int i = 100; i < imageContour.Rows; i++)
- {
- sum = imageContour[i, i + 1, 0, imageContour.Cols].Sum();
- if ((int)sum > 0)
- {
- dataArea[0] = i;//上界
- break;
- }
- }
- for (int i = dataArea[0]; i < imageContour.Rows; i++)
- {
- sum = imageContour[i, i + 1, 0, imageContour.Cols].Sum();
- if ((int)sum == 0)
- {
- dataArea[1] = i;//下界
- break;
- }
- }
- if (isCropFlag)
- {
- for (int j = 0; j < imageContour.Cols - 1; j++)
- {
- sum = imageContour[dataArea[0], dataArea[1], j, j + 1].Sum();
- if ((int)sum > 0)
- {
- dataArea[2] = j;
- break;
- }
- }
- for (int j = imageContour.Cols - 1; j > 1; j--)
- {
- sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
- {
- if ((int)sum > 0)
- {
- dataArea[3] = j;
- break;
- }
- }
- }
- }
- else
- {
- int t = 0;
- for (int j = imageContour.Cols - 100; j > 0; j--)
- {
- sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
- if ((int)sum > 0)
- {
- t = j;//最右邊的開始
- break;
- }
- }
- for (int j = t - 1; j > 0; j--)
- {
- sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
- if ((int)sum == 0)
- {
- t = j;//最右邊的結束
- break;
- }
- }
- for (int j = t - 1; j > 0; j--)
- {
- sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
- if ((int)sum > 0)
- {
- t = j;//中間開始
- dataArea[3] = j;
- break;
- }
- }
- for (int j = t - 1; j > 0; j--)
- {
- sum = imageContour[dataArea[0], dataArea[1], j - 1, j].Sum();
- if ((int)sum == 0)
- {
- t = j;//中間結束
- dataArea[2] = j;
- break;
- }
- }
- }
- }
- /// <summary>
- /// 提取上幅的值及坐標
- /// </summary>
- /// <param name="imageContour">輸入二值圖像</param>
- /// <param name="upperLineValue">上幅的大小</param>
- /// <param name="upperLineOrdinate">上幅的坐標,0:縱坐標;1:左橫坐標;2:右橫坐標</param>
- public void UpperLine(Mat imageContour, out int upperLineValue, out int[] upperLineOrdinate)
- {
- upperLineValue = 0;
- upperLineOrdinate = new int[3] { 0, 0, 0 };
- int cols = imageContour.Cols;
- int rows = imageContour.Rows;
- Scalar sum = new Scalar();
- for (int i = 0; i < rows - 150; i++)
- {
- sum = imageContour[i, i + 1, 0, cols].Sum();
- if ((int)sum > cols * 5 / 6)//儅每行點數大於寬度的五分之四時,判爲上幅
- {
- upperLineOrdinate[0] = i;
- break;
- }
- }
- if (upperLineOrdinate[0] == 0)//如果没有检测到
- {
- for (int i = 0; i < rows / 4; i++)
- {
- sum = imageContour[i, i + 1, 0, cols].Sum();
- if ((int)sum > cols * 4 / 5)//儅每行點數大於寬度的五分之三時,判爲上幅
- {
- upperLineOrdinate[0] = i;
- break;
- }
- }
- }
- int max = 0;
- int t = upperLineOrdinate[0];
- //繼續向下10(5)個像素尋找最大行,并認爲是上幅
- for (int i = t; i < t + 10; i++)
- {
- sum = imageContour[i, i + 1, 0, cols].Sum();
- if ((int)sum > max)
- {
- max = (int)sum;
- upperLineOrdinate[0] = i;
- }
- }
- //左邊點坐標
- for (int j = 0; j < cols; j++)
- {
- if (imageContour.Get<byte>(upperLineOrdinate[0], j) > 0)
- {
- upperLineOrdinate[1] = j;
- break;
- }
- }
- //右邊點坐標
- for (int j = cols - 1; j > 0; j--)
- {
- if (imageContour.Get<byte>(upperLineOrdinate[0], j) > 0)
- {
- upperLineOrdinate[2] = j;
- break;
- }
- }
- upperLineValue = upperLineOrdinate[2] - upperLineOrdinate[1];
- }
- public void UpperLine2(Mat imageContour, out int upperLineValue, out int[] upperLineOrdinate)
- {
- upperLineValue = 0;
- upperLineOrdinate = new int[3] { 0, 0, 0 };
- int cols = imageContour.Cols;
- int rows = imageContour.Rows;
- Scalar sum = new Scalar();
- for (int i = 0; i < rows - 150; i++)
- {
- sum = imageContour[i, i + 1, 0, cols].Sum();
- if ((int)sum > cols * 5 / 6)//儅每行點數大於寬度的五分之四時,判爲上幅
- {
- upperLineOrdinate[0] = i;
- break;
- }
- }
- if (upperLineOrdinate[0] == 0)//如果没有检测到
- {
- for (int i = 0; i < rows / 4; i++)
- {
- sum = imageContour[i, i + 1, 0, cols].Sum();
- if ((int)sum > cols * 4 / 5)//儅每行點數大於寬度的五分之三時,判爲上幅
- {
- upperLineOrdinate[0] = i;
- break;
- }
- }
- }
- if (upperLineOrdinate[0] == 0)//如果没有检测到
- {
- for (int i = 0; i < rows / 3; i++)
- {
- sum = imageContour[i, i + 1, 0, cols].Sum();
- if ((int)sum > cols * 3 / 5)//儅每行點數大於寬度的五分之三時,判爲上幅
- {
- upperLineOrdinate[0] = i;
- break;
- }
- }
- }
- if (upperLineOrdinate[0] == 0)//如果没有检测到
- {
- for (int i = 0; i < rows / 3; i++)
- {
- sum = imageContour[i, i + 1, 0, cols].Sum();
- if ((int)sum > cols * 2 / 5)//儅每行點數大於寬度的五分之三時,判爲上幅
- {
- upperLineOrdinate[0] = i;
- break;
- }
- }
- }
- int max = 0;
- int t = upperLineOrdinate[0];
- //繼續向下10(5)個像素尋找最大行,并認爲是上幅
- for (int i = t; i < t + 10; i++)
- {
- sum = imageContour[i, i + 1, 0, cols].Sum();
- if ((int)sum > max)
- {
- max = (int)sum;
- upperLineOrdinate[0] = i;
- }
- }
- //左邊點坐標
- for (int j = 0; j < cols; j++)
- {
- if (imageContour.Get<byte>(upperLineOrdinate[0], j) > 0)
- {
- upperLineOrdinate[1] = j;
- break;
- }
- }
- //右邊點坐標
- for (int j = cols - 1; j > 0; j--)
- {
- if (imageContour.Get<byte>(upperLineOrdinate[0], j) > 0)
- {
- upperLineOrdinate[2] = j;
- break;
- }
- }
- upperLineValue = upperLineOrdinate[2] - upperLineOrdinate[1];
- }
- /// <summary>
- /// 計算蝕刻因子上下的總後的值和坐標(是單層的銅厚)
- /// </summary>
- /// <param name="imageContour">二值圖像</param>
- /// <param name="tonghouValue">輸出銅厚的值</param>
- /// <param name="tonghouOrdinate">輸出銅厚的坐標,0:橫坐標;1:上縱坐標;2:下縱坐標</param>
- public void Zonghou(Mat imageContour, out int zonghouValue, out int[] zonghouOrdinate)
- {
- zonghouValue = 0;
- zonghouOrdinate = new int[3];
- int rows = imageContour.Rows;
- int cols = imageContour.Cols;
- int middle = cols / 2;
- zonghouOrdinate[0] = middle;
- for (int i = 0; i < rows; i++)
- {
- if (imageContour.Get<byte>(i, middle) > 0)
- {
- zonghouOrdinate[1] = i;
- break;
- }
- }
- for (int i = rows - 1; i > 0; i--)
- {
- if (imageContour.Get<byte>(i, middle) > 0)
- {
- zonghouOrdinate[2] = i;
- break;
- }
- }
- zonghouValue = zonghouOrdinate[2] - zonghouOrdinate[1];
- }
- /// <summary>
- /// 計算銅厚以及銅厚坐標
- /// </summary>
- /// <param name="imageGreen">綠色通道圖像</param>
- /// <param name="imageContour">二值圖像</param>
- /// <param name="tonghouValue">銅厚大小</param>
- /// <param name="tonghouOrdinate">銅厚坐標,0:橫坐標;1:上縱坐標;2:下縱坐標</param>
- public void Tonghou(Mat imageGreen, Mat imageContour, out int tonghouValue, out int[] tonghouOrdinate)
- {
- tonghouValue = 0;
- tonghouOrdinate = new int[3];
- int rows = imageGreen.Rows;
- int cols = imageGreen.Cols;
- int middle = cols / 2;
- tonghouOrdinate[0] = (middle + cols) / 2;
- double insideLine;
- InsideLine2(imageGreen, 30, rows - 50, 20, cols - 20, out insideLine);
- tonghouOrdinate[1] = (int)insideLine;
- for (int i = rows - 1; i > 0; i--)
- {
- if (imageContour.Get<byte>(i, tonghouOrdinate[0]) > 0)
- {
- tonghouOrdinate[2] = i;
- break;
- }
- }
- tonghouValue = tonghouOrdinate[2] - tonghouOrdinate[1];
- }
- #region 防焊
- #region 没开口
- /// <summary>
- /// 防焊 有开口 铜厚
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- public void FanghanTonghouForMeiKaiKou(Mat gray, out int[] tonghouY, out int[] y, out int[] b)
- {
- y = new int[2];
- b = new int[4];
- tonghouY = new int[2];
- Mat contour = new Mat();
- double T = 0;
- double t = Cv2.Threshold(gray, contour, 0, 255, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
- Mat close = new Mat();
- Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
- Mat result = close.Clone();
- result = result / 255;
- //ImageShow(result * 255);
- //计算边界
- Scalar sum = new Scalar(0);
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum > 200)
- {
- y[0] = i - 20;
- break;
- }
- }
- for (int i = y[0] + 50; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum == 0)
- {
- y[1] = i;
- break;
- }
- }
- for (int j = 0; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[0] = j;
- break;
- }
- }
- for (int j = b[0] + 200; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- for (int j = b[1] + 10; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[2] = j;
- break;
- }
- }
- if (b[2] - b[1] < 300)
- {
- for (int j = b[2] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- for (int j = b[1] + 10; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[2] = j;
- break;
- }
- }
- }
- for (int j = b[2] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[3] = j;
- break;
- }
- }
- if (b[3] == 0)
- b[3] = contour.Cols - 1;
- //计算铜厚
- int tonghouX = b[1] - 150;
- Mat thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- for (int i = y[0]; i < y[1]; i++)
- {
- sum = thresh[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
- if ((int)sum > 30)
- {
- tonghouY[0] = i;
- break;
- }
- }
- contour = contour / 255;
- for (int i = tonghouY[0] + 30; i < contour.Rows; i++)
- {
- sum = contour[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
- if ((int)sum == 0)
- {
- tonghouY[1] = i;
- break;
- }
- }
- }
- /// <summary>
- /// 防焊 有开口 厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- public void FanghanhouduForMeiKaiKou_2(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, bool isLeft)
- {
- int fanghanhouduY_0 = tonghouY[0];
- int fanghanX1 = Math.Max(0, fanghanhouduX - 150);
- int fanghanX2 = Math.Min(gray.Cols - 1, fanghanhouduX + 150);
- int fanghanTop = fanghanhouduY_0 - 150/*100*/;
- int marginTop = 150;
- if (fanghanTop < 0)
- {
- fanghanTop = 0;
- marginTop = fanghanhouduY_0 - 1;
- }
- Mat grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
- FanghanhouduForMeiKaiKou(grayRect, y, marginTop, tonghouY, out fanghanhouduY1, out minGray);
- int fanghanhouduY1__2 = fanghanhouduY1;
- int minGray__2 = minGray;
- fanghanX1 += 140;// 145;
- fanghanX2 -= 140;// 145;
- grayRect = gray[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
- int fanghanhouduY1__2_bottom;
- FanghanhouduForYouKaiKou_ACC(grayRect, y, marginTop, fanghanhouduY1 - (isLeft ? 8/*5*/ : 1), out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2);
- if (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 16/*11*//*<-10*//*20*//*10*/
- || (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/))
- fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
- //else
- //{
- // fanghanX1 -= 69;
- // fanghanX2 += 69;
- // grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
- // FanghanhouduForMeiKaiKou(grayRect, y, marginTop, tonghouY, out fanghanhouduY1__2, out minGray__2);
- //}
- //if (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 10)
- // fanghanhouduY1 = fanghanhouduY1__2 + fanghanTop;
- else
- fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
- }
- /// <summary>
- /// 防焊 有开口 厚度 精确计算 统一到有开口的方法精确定位 避免个别因素产生大的偏差
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- 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)
- {
- minGray = 300 * 255;
- int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- fanghanhouduY1Bottom = 0;
- for (int i = Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + 30/*25*/, gray.Rows) - 5; i++)
- {
- curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Add(curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- fanghanhouduY1Bottom = i;
- minGray = curGray;
- }
- }
- for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i+=2)
- {
- if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
- {
- minRowIndex += 1;
- fanghanhouduY1Bottom += 2;
- }
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- fanghanhouduY1 = minRowIndex;// 84;// 72;// minRowIndex;
- }
- /// <summary>
- /// 防焊 有开口 厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- private void FanghanhouduForMeiKaiKou(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, int a = 0)
- {
- ///*int fanghanhouduX = 150; */
- //fanghanhouduY1 = -1;
- //Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", thresh);
- //for (int i = 0; i < Math.Min(100, thresh.Rows); i++)
- //{
- // if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
- // {
- // //fanghanhouduY1 = i;
- // int sumTop = Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255));
- // int searchTimes = 15;
- // while (searchTimes-- > 0 && i+2 < thresh.Rows && sumTop == Cv2.FloodFill(thresh, new Point(fanghanhouduX, ++i), new Scalar(255)))
- // {
- // //fanghanhouduY1 = i;
- // }
- // fanghanhouduY1 = i;
- // break;
- // }
- //}
- minGray = 300 * 255;
- //if (fanghanhouduY1 == -1)
- // FanghanhouduForMeiKaiKou(gray, y, fanghanhouduX, tonghouY, out fanghanhouduY1, out minGray, ++a);
- //else
- {//计算数值的地方
- //Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
- /*int minGray = 300*255; */
- int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray;
- for (int i = 6/*1*/; i < Math.Min(100, gray.Rows) - 5; i++)
- {
- curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- if (curGray < minGray)
- {
- minRowIndex = i;
- minGray = curGray;
- }
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- fanghanhouduY1 = minRowIndex;
- }
- }
- 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)
- {
- //int fanghanhouduY_1 = fanghanhouduY_center - fanghanhouduY_radius;
- //int fanghanhouduY_2 = fanghanhouduY_center + fanghanhouduY_radius;
- int vmax_2 = 0;
- int vres_y = 1000;// 3000;
- List<int> vmax_x = new List<int>();
- for (int i = scanX_start; i < scanX_end; i++)
- {
- if (thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0 > vres_y)
- {
- vmax_2 = (int)thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0;
- vmax_x.Add(i); ;
- }
- }
- List<int> vmax_x_temp = new List<int>();
- vmax_x_temp.AddRange(vmax_x);
- while (vmax_x.Count > 10 && vmax_x_temp.Count > 1)
- {
- vres_y += 500;// 1000;
- vmax_x_temp.Clear();
- for (int i = scanX_start; i < scanX_end; i++)
- {
- if (thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0 > vres_y)
- {
- vmax_2 = (int)thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0;
- vmax_x_temp.Add(i); ;
- }
- }
- if (vmax_x_temp.Count > 1)
- {
- vmax_x.Clear();
- vmax_x.AddRange(vmax_x_temp);
- }
- }
- if (vmax_x.Count == 0)
- {
- vmax_x.Add(0);
- for (int i = scanX_start; i < scanX_end; i++)
- {
- if (thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0 > vmax_2)
- {
- vmax_2 = (int)thres_2[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0;
- vmax_x[0] = i;
- }
- }
- }
- else if (vmax_x.Count > 3)
- {
- bool isround = false;//去掉球上的点
- int lastmax_x = vmax_x[vmax_x.Count - 1];
- int endIndex = vmax_x.Count - 4;
- for (int i = vmax_x.Count - 2; i > 0; i--)
- {
- if (vmax_x[i] - 20 > lastmax_x)
- {
- endIndex = i;
- isround = true;
- break;
- }
- lastmax_x = vmax_x[i];
- }
- if (isround)
- {
- int max_y_res = 100;
- for (int i = vmax_x.Count - 1; i > endIndex; i--)
- {
- if (vmax_x[Math.Max(0, i - 2)] > max_y_res)
- {
- break;
- }
- vmax_x.RemoveAt(i);
- }
- }
- }
- fanghanhouduX_0 = (vmax_x.Count > 1 ? (vmax_x[vmax_x.Count - 2] + vmax_x[vmax_x.Count - 1]) / 2 : vmax_x[0]);
- }
- 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)
- {
- //int fanghanhouduY_1 = fanghanhouduY_center - fanghanhouduY_radius;
- //int fanghanhouduY_2 = fanghanhouduY_center + fanghanhouduY_radius;
- List<int> vmax_x_scanStart = new List<int>();
- for (int i = scanX_start; i < scanX_end; i++)
- vmax_x_scanStart.Add((int)thres_2_0[fanghanhouduY_1, fanghanhouduY_2, i - 1, i].Sum().Val0);
- int vmax_2 = 0;
- int vres_y = 1000;// 3000;
- List<int> vmax_x = new List<int>();
- for (int i = scanX_start; i < scanX_end; i++)
- {
- if (vmax_x_scanStart[i - scanX_start] > vres_y)
- {
- vmax_2 = vmax_x_scanStart[i - scanX_start];
- vmax_x.Add(i); ;
- }
- }
- List<int> vmax_x_temp = new List<int>();
- vmax_x_temp.AddRange(vmax_x);
- int temp_x_left = vmax_x.Count > 0 ? vmax_x[0] : scanX_start;
- //bool fanghanhouduX_0_Found = false; fanghanhouduX_0 = 300;
- while (vmax_x.Count > 10 && vmax_x_temp.Count > 1/* && !fanghanhouduX_0_Found*/)
- {
- vres_y += 500;// 500;// 1000;
- vmax_x_temp.Clear();
- for (int i = scanX_start; i < scanX_end; i++)
- {
- if (vmax_x_scanStart[i - scanX_start] > vres_y)
- {
- vmax_2 = vmax_x_scanStart[i - scanX_start];
- vmax_x_temp.Add(i); ;
- }
- }
- if (vmax_x_temp.Count > 19)
- {
- vmax_x.Clear();
- vmax_x.AddRange(vmax_x_temp);
- Console.WriteLine("temp_x_left:" + vmax_x[0] + ";vmax_x.Count:" + vmax_x.Count);
- ////根据数值是否断开距离超过20判定offset左端点,其中60、370、20为超参参数
- //if (vmax_x_temp.Count < 60)
- // for (int i = 1; i < vmax_x.Count; i++)
- //{
- // if (vmax_x[i-1] < 370) continue;
- // if (vmax_x[i] > vmax_x[i - 1] + 20)
- // {
- // fanghanhouduX_0 = vmax_x[i-1];// 384;// prev_x_v / prev_x_c;
- // fanghanhouduX_0_Found = true;
- // break;
- // }
- //}
- }
- }
- //if (fanghanhouduX_0_Found)
- //{
- // return;
- //}
- if (vmax_x.Count == 0)
- {
- vmax_x.Add(0);
- for (int i = scanX_start; i < scanX_end; i++)
- {
- if (vmax_x_scanStart[i - scanX_start] > vmax_2)
- {
- vmax_2 = vmax_x_scanStart[i - scanX_start];
- vmax_x[0] = i;
- }
- }
- }
- else if (vmax_x.Count > 3)
- {
- bool isround = false;//去掉球上的点
- int lastmax_x = vmax_x[vmax_x.Count - 1];
- int endIndex = vmax_x.Count - 4;
- for (int i = vmax_x.Count - 2; i > 0; i--)
- {
- if (vmax_x[i] - 20 > lastmax_x)
- {
- endIndex = i;
- isround = true;
- break;
- }
- lastmax_x = vmax_x[i];
- }
- if (isround)
- {
- int max_y_res = 100;
- for (int i = vmax_x.Count - 1; i > endIndex; i--)
- {
- if (vmax_x[Math.Max(0, i - 2)] > max_y_res)
- {
- break;
- }
- vmax_x.RemoveAt(i);
- }
- }
- }
- if (vmax_x.Count > 2)
- {
- int vmax_x_v = vmax_x[0];
- int vmax_x_c = 1;
- int prev_x_v = vmax_x_v;
- int prev_x_c = vmax_x_c;
- for (int i = 1; i < vmax_x.Count; i++)
- {
- if (vmax_x[i] < vmax_x[i - 1] + 2)
- {
- vmax_x_v += vmax_x[i];
- vmax_x_c += 1;
- }
- else
- {
- if (prev_x_c <= vmax_x_c)
- {
- prev_x_v = vmax_x_v;
- prev_x_c = vmax_x_c;
- }
- i++;
- vmax_x_v = vmax_x[i];
- vmax_x_c = 1;
- }
- }
- if (prev_x_c <= vmax_x_c+1)
- {
- prev_x_v = vmax_x_v;
- prev_x_c = vmax_x_c;
- }
- fanghanhouduX_0 = prev_x_v / prev_x_c;
- if (fanghanhouduX_0 > thres_2_0.Cols-30)
- {
- fanghanhouduX_0 = 382 - 100;
- Console.WriteLine("temp_x_left: OverLoad 1");
- }
- else if (vmax_x.Count > 20)
- {
- Console.WriteLine("temp_x_left: OverLoad 2");
- }
- }
- else
- fanghanhouduX_0 = (vmax_x.Count > 1 ? (vmax_x[vmax_x.Count - 2] + vmax_x[vmax_x.Count - 1]) / 2 : vmax_x[0]);
- }
- /// <summary>
- /// 防焊 没有开口 厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- public void FanghanhouduForMeiKaiKou_2(Mat thres_2, int fanghanhouduX_center, int fanghanhouduX_radius, out int fanghanhouduY_0, bool showMat = false)
- {
- int fanghanhouduX_1 = fanghanhouduX_center - fanghanhouduX_radius;
- int fanghanhouduX_2 = fanghanhouduX_center + fanghanhouduX_radius;
- int vmax_2 = 0;
- int vres_y = 3000;
- List<int> vmax_y = new List<int>();
- //int vmax_y = crop2.Rows - 2;
- for (int i = thres_2.Rows - 2; i > 0; i--)
- {
- if (thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0 >
- vres_y)
- //|| Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
- {
- vmax_2 = (int)thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0;
- vmax_y.Add(i); ;
- }
- }
- List<int> vmax_y_temp = new List<int>();
- vmax_y_temp.AddRange(vmax_y);
- while (vmax_y.Count > 10 && vmax_y_temp.Count > 1)
- {
- vres_y += 1000;
- //int vmax_y = crop2.Rows - 2;
- vmax_y_temp.Clear();
- for (int i = thres_2.Rows - 2; i > 0; i--)
- {
- if (thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0 >
- vres_y)
- //|| Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
- {
- vmax_2 = (int)thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0;
- vmax_y_temp.Add(i); ;
- }
- }
- if (vmax_y_temp.Count > 1)
- {
- vmax_y.Clear();
- vmax_y.AddRange(vmax_y_temp);
- }
- }
- if (vmax_y.Count == 0)
- {
- vmax_y.Add(0);
- for (int i = thres_2.Rows - 2; i > 0; i--)
- {
- if (thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0 >
- vmax_2)
- //|| Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
- {
- vmax_2 = (int)thres_2[i - 1, i, fanghanhouduX_1, fanghanhouduX_2].Sum().Val0;
- vmax_y[0] = i;
- }
- }
- }
- else if (vmax_y.Count > 3)
- {
- bool isround = false;//去掉球上的点
- int lastmax_y = vmax_y[vmax_y.Count - 1];
- int endIndex = vmax_y.Count - 4;
- for (int i = vmax_y.Count - 2; i > 0; i--)
- {
- if (vmax_y[i] - 20 > lastmax_y)
- {
- endIndex = i;
- isround = true;
- break;
- }
- lastmax_y = vmax_y[i];
- }
- if (isround)
- {
- int max_y_res = 100;
- for (int i = vmax_y.Count - 1; i > endIndex; i--)
- {
- if (vmax_y[Math.Max(0, i - 2)] > max_y_res)
- {
- break;
- }
- vmax_y.RemoveAt(i);
- }
- }
- }
- fanghanhouduY_0 = (vmax_y.Count > 1 ? (vmax_y[vmax_y.Count - 2] + vmax_y[vmax_y.Count - 1]) / 2 : vmax_y[0]);
- }
- ///// <summary>
- ///// 防焊 没有开口 銅厚
- ///// </summary>
- ///// <param name="imageContour">二值圖像</param>
- ///// <param name="tonghouY">輸出銅厚的上下縱坐標</param>
- ///// <param name="y">截取第一層銅的區域</param>
- ///// <param name="b">b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
- //public void FanghanTonghouForMeiKaiKou(Mat gray, out int[] tonghouY, out int[] y, out int[] b)
- //{
- // y = new int[2];
- // b = new int[4];
- // int[] bAdd = new int[5];
- // tonghouY = new int[2];
- // Mat contour = gray.Threshold(0, 255, ThresholdTypes.Otsu);
- // //去掉小颗粒
- // contour = BinaryTools.DebrisRemoval_New(contour.CvtColor(ColorConversionCodes.GRAY2BGRA), 1000).CvtColor(ColorConversionCodes.BGRA2GRAY);
- // //分析的区域切图的不对[2]to do //
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\contour.JPG", contour);
- // Mat result = contour.Clone() / 255;
- // //计算边界
- // Scalar sum = new Scalar(0);
- // for (int i = 0; i < result.Rows; i++)
- // {
- // sum = result[i, i + 1, 0, result.Cols].Sum();
- // if ((int)sum > 200)
- // {
- // y[0] = i - 20;
- // break;
- // }
- // }
- // ////区分突然骤减的情况也说明已经到了铜厚的底部
- // //List<int> listSum = new List<int>();
- // //listSum.Add((int)sum);
- // //int halfOfSumTime = 0;
- // for (int i = y[0] + 50; i < result.Rows; i++)
- // {
- // sum = result[i, i + 1, 0, result.Cols].Sum();
- // if ((int)sum == 0)
- // {
- // y[1] = i;
- // break;
- // }
- // //if ((int)sum * 2 < listSum.Average())
- // //{
- // // if (++halfOfSumTime > 3)
- // // {
- // // y[1] = i;
- // // //break;
- // // }
- // //}
- // //else
- // //{
- // // halfOfSumTime = 0;
- // // listSum.Add((int)sum);
- // //}
- // }
- // if (y[0] <= 0 || y[1] <= 0)
- // {
- // contour = gray.Threshold(BinaryTools.CalcSuitableValueForMax(gray), 255, ThresholdTypes.Binary);
- // result = contour.Clone() / 255;
- // for (int i = 0; i < result.Rows; i++)
- // {
- // sum = result[i, i + 1, 0, result.Cols].Sum();
- // if ((int)sum > 200)
- // {
- // y[0] = i - 20;
- // break;
- // }
- // }
- // for (int i = y[0] + 50; i < result.Rows; i++)
- // {
- // sum = result[i, i + 1, 0, result.Cols].Sum();
- // if ((int)sum == 0)
- // {
- // y[1] = i;
- // break;
- // }
- // }
- // for (int j = 0; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum > 0)
- // {
- // b[0] = j;
- // bAdd[0] = j;
- // break;
- // }
- // }
- // for (int j = b[0] + 200; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum == 0)
- // {
- // b[1] = j;
- // bAdd[1] = j;
- // break;
- // }
- // }
- // }
- // for (int j = 0; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum > 0)
- // {
- // b[0] = j;
- // bAdd[0] = j;
- // break;
- // }
- // }
- // for (int j = b[0] + 50; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum == 0)
- // {
- // b[1] = j;
- // bAdd[1] = j;
- // break;
- // }
- // }
- // //暂时这么一写
- // if (b[1] <= 0)
- // {
- // contour = gray.Threshold(BinaryTools.CalcSuitableValueForMax(gray), 255, ThresholdTypes.Binary);
- // //去掉小颗粒
- // contour = BinaryTools.DebrisRemoval_New(contour.CvtColor(ColorConversionCodes.GRAY2BGRA), 1000).CvtColor(ColorConversionCodes.BGRA2GRAY);
- // result = contour.Clone() / 255;
- // for (int i = 0; i < result.Rows; i++)
- // {
- // sum = result[i, i + 1, 0, result.Cols].Sum();
- // if ((int)sum > 200)
- // {
- // y[0] = i - 20;
- // break;
- // }
- // }
- // for (int i = y[0] + 50; i < result.Rows; i++)
- // {
- // sum = result[i, i + 1, 0, result.Cols].Sum();
- // if ((int)sum == 0)
- // {
- // y[1] = i;
- // break;
- // }
- // }
- // if (y[0] < 0 || y[1] < 0) return;
- // for (int j = 0; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum > 0)
- // {
- // b[0] = j;
- // bAdd[0] = j;
- // break;
- // }
- // }
- // for (int j = b[0] + 50; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum == 0)
- // {
- // b[1] = j;
- // bAdd[1] = j;
- // break;
- // }
- // }
- // }
- // for (int j = b[1] + 10; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum > 0)
- // {
- // b[2] = j;
- // bAdd[2] = j;
- // break;
- // }
- // }
- // if (b[2] - b[1] < 300)
- // {
- // for (int j = b[2] + 50; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum == 0)
- // {
- // b[1] = j;
- // bAdd[1] = j;
- // break;
- // }
- // }
- // for (int j = b[1] + 10; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum > 0)
- // {
- // b[2] = j;
- // bAdd[2] = j;
- // break;
- // }
- // }
- // }
- // for (int j = bAdd[2] + 50; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum == 0)
- // {
- // bAdd[3] = j - 1;
- // break;
- // }
- // }
- // if (bAdd[3] == 0)
- // bAdd[3] = contour.Cols - 1;
- // for (int j = bAdd[3] + 10; j < result.Cols; j++)
- // {
- // sum = result[y[0], y[1], j, j + 1].Sum();
- // if ((int)sum > 0)
- // {
- // bAdd[4] = j;
- // break;
- // }
- // }
- // for (int j = result.Cols - 1; j > b[2]; j--)
- // {
- // sum = result[y[0], y[1], j - 1, j].Sum();
- // if ((int)sum > 0)
- // {
- // b[3] = j;
- // break;
- // }
- // }
- // if (b[3] == 0)
- // b[3] = contour.Cols - 1;
- // //bAdd[5] = b[3];
- // 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))/*防止过拟合*/)//测量区域在右边
- // {
- // b[2] = bAdd[4];
- // b[1] = bAdd[3];
- // b[0] = bAdd[2];
- // }
- // //计算铜厚
- // int tonghouX = b[1] - 50;
- // if (tonghouX <= 0) return;
- // Mat thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- // for (int i = y[0]; i < y[1]; i++)
- // {
- // sum = thresh[i, i + 1, (tonghouX - 50) < 0 ? 0 : (tonghouX - 50), tonghouX + 50].Sum();
- // if ((int)sum > 20)
- // {
- // tonghouY[0] = i;
- // break;
- // }
- // }
- // contour = contour / 255;
- // for (int i = tonghouY[0] + 30; i < contour.Rows; i++)
- // {
- // sum = contour[i, i + 1, (tonghouX - 50) < 0 ? 0 : (tonghouX - 50), tonghouX + 50].Sum();
- // if ((int)sum == 0)
- // {
- // tonghouY[1] = i;
- // break;
- // }
- // }
- //}
- ///// <summary>
- ///// 防焊 没有开口 厚度
- ///// </summary>
- ///// <param name="gray"></param>
- ///// <param name="y"></param>
- ///// <param name="b"></param>
- ///// <param name="tonghouY"></param>
- ///// <param name="fanghanhouduY"></param>
- ///// <param name="a"></param>
- //public Mat FanghanhouduForMeiKaiKou(Mat gray, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY, int a = 0, bool showMat = false)
- //{
- // fanghanhouduY = new int[2];
- // int fanghanhouduX = b[1] - 100;
- // Mat maskRes = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
- // Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- // fanghanhouduY[0] = tonghouY[0];
- // for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
- // {
- // Mat mask1 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
- // if (thresh.At<byte>(i, fanghanhouduX) == 0 &&
- // Cv2.FloodFill(thresh, mask1, new Point(fanghanhouduX, i), new Scalar(127/*255*/)) > 1000/*150*//*300*/)
- // {
- // maskRes = mask1.Clone();
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask1.png", mask);
- // fanghanhouduY[1] = i;
- // break;
- // }
- // }
- // if (fanghanhouduY[1] == 0)
- // {
- // if (showMat)
- // maskRes = FanghanhouduForMeiKaiKou(gray, y, b, tonghouY, out fanghanhouduY, ++a, true);
- // else
- // maskRes = FanghanhouduForMeiKaiKou(gray, y, b, tonghouY, out fanghanhouduY, ++a);
- // }
- // //if (LPIHouduY[1] == 0)
- // //{
- // // if (showMat)
- // // {
- // // FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a, true);
- // // }
- // // else
- // // FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
- // //}
- // else if (showMat)
- // {
- // int fanghanhouduY_1 = fanghanhouduY[1];//防止圆球的影响
- // float result1Value = BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5);// + 5;
- // int valueLoop = 0;// fanghanhouduY[0] - 50
- // while (++valueLoop < 12 && fanghanhouduY_1 == fanghanhouduY[1])
- // {
- // valueLoop += 1;
- // Mat mask2 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
- // Mat thresh2 = gray.Threshold(result1Value + valueLoop, 255, ThresholdTypes.Binary);
- // for (int i = fanghanhouduY_1 + 25/*15*/; i < fanghanhouduY_1 + 100/*50*/; i++)
- // //for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
- // {
- // if (thresh2.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh2, mask2, new Point(fanghanhouduX, i), new Scalar(127/*255*/)) > 1000/*150*//*300*/)
- // {
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh2.png", thresh);
- // maskRes = mask2.Clone();
- // fanghanhouduY[1] = i;
- // break;
- // }
- // }
- // //result1 = gray.Threshold(result1Value + valueLoop, 255, ThresholdTypes.Binary);
- // //for (int i = fanghanhouduY_1 + 25/*15*/; i < fanghanhouduY_1 + 100/*50*/; i++)
- // //{
- // // if (i > 0 && result1.At<byte>(i, LPIHouduX) == 0)
- // // {
- // // Mat temp = ~result1;
- // // if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
- // // {
- // // Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI2.png", result1);
- // // fanghanhouduY[1] = i;
- // // break;
- // // }
- // // }
- // //}
- // }
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh3.png", thresh);
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI3.png", result1);
- // }
- // return maskRes;
- //}
- /// <summary>
- /// 防焊 没有开口 offset
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- /// <param name="i"></param>
- public void FanghanOffsetForMeiKaiKou_Acc(Mat gray0, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, int offsetX0_0, out int offsetX0)
- {
- int offsetY1 = Math.Max(0, fanghanhouY[1] - 50);
- int offsetY2 = Math.Min(gray0.Rows - 1, tonghouY[1] + 50);
- int offsetLeft = b[1] + 5;
- Mat gray = gray0[offsetY1, offsetY2, offsetLeft, b[2] - 5];
- int colStart = offsetX0_0 - 10 - offsetLeft;
- int colEnd = offsetX0_0 + 20/*10*/ - offsetLeft;
- int minGray = 300 * 255; int minColIndex = 0; int rowStart = offsetY - 10 - offsetY1; int rowEnd = offsetY + 10 - offsetY1;
- List<int> curgrayList = new List<int>();
- List<int> minareaList_k = new List<int>();
- List<int> minareaList_v = new List<int>();
- for (int i = colStart; i < colEnd; i++)
- {
- curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
- if (curgrayList[i - colStart] < minGray)
- {
- minColIndex = i;
- minGray = curgrayList[i - colStart];
- }
- }
- //for (int i = colStart; i < (gray.Cols + colStart) / 2; i++)
- //{
- // if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
- // {
- // bool isAreamin = true;
- // for (int k = i - colStart - 10; k < i - colStart + 10; k++)
- // {
- // if (curgrayList[i - colStart] >= curgrayList[k])
- // {
- // isAreamin = false;
- // break;
- // }
- // if (k == i - colStart - 1) k++;
- // }
- // if (isAreamin)
- // {
- // minareaList_k.Add(i);
- // minareaList_v.Add(curgrayList[i - colStart]);
- // }
- // }
- //}
- ////Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- //////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- ////下面为计算极小值,从计算极小值中的序列中选取,替换极大值
- ////minareaList.Add(minColIndex, minGray);
- ////offsetX0 = 47 + colStart;// minColIndex;
- for (int i = 0; i < minareaList_k.Count; i++)
- {
- if (Math.Abs(minGray - minareaList_v[i]) < 120)
- {
- minColIndex = minareaList_k[i];
- break;
- }
- }
- offsetX0 = minColIndex;
- offsetX0 = offsetX0 + offsetLeft;
- }
- /// <summary>
- /// 防焊 没有开口 udercut
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- /// <param name="i"></param>
- public void FanghanUndercutForMeiKaiKou_Acc(Mat gray0, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, int offsetX0_0, out int offsetX0)
- {
- int offsetY1 = Math.Max(0, fanghanhouY[1] - 50);
- int offsetY2 = Math.Min(gray0.Rows - 1, tonghouY[1] + 50);
- int offsetLeft = b[1] + 5;
- Mat gray = gray0[offsetY1, offsetY2, offsetLeft, b[2] - 5];
- int colStart = offsetX0_0 - 30/*10*/ - offsetLeft;
- int colEnd = offsetX0_0 + 20/*10*/ - offsetLeft;
- int minGray = 300 * 255; int minColIndex = 0; int rowStart = offsetY - 10 - offsetY1; int rowEnd = offsetY + 10 - offsetY1;
- List<int> curgrayList = new List<int>();
- List<int> minareaList_k = new List<int>();
- List<int> minareaList_v = new List<int>();
- for (int i = colStart; i < colEnd; i++)
- {
- curgrayList.Add(this.FanghanOffsetForNewMeiKaiKouUndercut0_areaMin(gray, i, rowStart, rowEnd));
- if (curgrayList[i - colStart] < minGray)
- {
- minColIndex = i;
- minGray = curgrayList[i - colStart];
- }
- }
- //for (int i = colStart; i < (gray.Cols + colStart) / 2; i++)
- //{
- // if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
- // {
- // bool isAreamin = true;
- // for (int k = i - colStart - 10; k < i - colStart + 10; k++)
- // {
- // if (curgrayList[i - colStart] >= curgrayList[k])
- // {
- // isAreamin = false;
- // break;
- // }
- // if (k == i - colStart - 1) k++;
- // }
- // if (isAreamin)
- // {
- // minareaList_k.Add(i);
- // minareaList_v.Add(curgrayList[i - colStart]);
- // }
- // }
- //}
- ////Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- //////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- ////下面为计算极小值,从计算极小值中的序列中选取,替换极大值
- ////minareaList.Add(minColIndex, minGray);
- ////offsetX0 = 47 + colStart;// minColIndex;
- for (int i = 0; i < minareaList_k.Count; i++)
- {
- if (Math.Abs(minGray - minareaList_v[i]) < 120)
- {
- minColIndex = minareaList_k[i];
- break;
- }
- }
- offsetX0 = minColIndex;
- offsetX0 = offsetX0 + offsetLeft;
- }
- //获取当前行附件最暗的总和
- private int FanghanOffsetForNewMeiKaiKouUndercut0_areaMin(Mat gray, int colIndex, int rowStart, int rowEnd)
- {
- int areaMin = 0;
- for (int j = rowStart; j < rowEnd; j++)
- {
- int colMin = 255;
- for (int i = colIndex - 5; i < colIndex + 5; i++)
- if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
- areaMin += colMin;
- }
- return areaMin;
- }
- /// <summary>
- /// 防焊 没开口 offset
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- /// <param name="i"></param>
- public void FanghanOffsetForMeiKaiKouThroughErzhi(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, out int offsetX0, out bool changetoMax, int i = 0)
- {
- //int offsetY = tonghouY[0] - 50;
- offsetY = tonghouY[0] - 50 + 25;
- changetoMax = false;
- int offsetY1 = Math.Max(0, fanghanhouY[1]/*offsetY*/ - 50);
- int offsetY2 = Math.Min(gray.Rows - 1, tonghouY[1] + 50);
- int offsetLeft = b[1] + 5;
- //Mat grayRect = gray[offsetY1, offsetY2, offsetLeft, b[2] - 5];
- //this.FanghanOffsetForNewMeiKaiKouOffset0(grayRect, colStart, out offsetX0);
- //offsetX0 = offsetX0 + offsetLeft;
- offsetX0 = -1;///*offsetX0 + */offsetLeft;
- //66~~71
- //二值化
- Mat threshEdge = gray.Threshold(71/*66*//*71*//*BinaryTools.CalcSuitableValue(gray) - 10*/ + (i * 5), 255, ThresholdTypes.BinaryInv);
- Cv2.ImWrite(@"C:\Users\win10SSD\Desktop\threshEdge_0.png", threshEdge);
- //去碎屑
- threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
- Cv2.ImWrite(@"C:\Users\win10SSD\Desktop\threshEdge_1.png", threshEdge);
- int sumBloodLast = -1;
- List<int> offsetX0_list = new List<int>();
- for (int j = b[2] - 100/*55*/; j > 0; j--)
- {
- if (threshEdge.At<byte>(offsetY + 20/*45*/, j) == 0)
- {
- Mat temp = ~threshEdge;
- int sumBlood = Cv2.FloodFill(temp, new Point(j, offsetY + 20/*45*/), new Scalar(255));
- if (sumBlood > 1000/*2000*/)
- {
- if (sumBlood >= sumBloodLast)
- {
- int chazhi = sumBloodLast - sumBlood;
- sumBloodLast = sumBlood;
- int offsetX0_temp = Tools.GetLeftOrRightPoint(new Point(j, offsetY + 20/*45*/), threshEdge, 2).X;
- if (/*offsetX0_temp-j< 5 && */(offsetX0 == -1 || offsetX0_temp < offsetX0) && Math.Abs(b[2]/*offsetX_1*/ - offsetX0_temp) <= 800)
- {
- if (chazhi > 0) changetoMax = true;
- offsetX0 = offsetX0_temp;
- }
- if(offsetX0_temp - j < 10) offsetX0_list.Add(offsetX0_temp);
- else offsetX0_list.Add(j);
- }
- else
- offsetX0_list.Add(j);
- //break;
- }
- }
- }
- if (offsetX0_list.Count > 0 && offsetX0 > 0)
- {
- Console.WriteLine("list to sort ...");
- if (offsetX0_list.Max() - offsetX0 > 0 && changetoMax ||
- (offsetX0_list.Max() - offsetX0 > 200/*201*/))
- {
- changetoMax = true;
- offsetX0 = offsetX0_list.Max();// 1082;// offsetX0_list.Max();
- }
- }
- if (i < 20 && (offsetX0 <= 0 || Math.Abs(b[2]/*offsetX_1*/ - offsetX0) > 800))
- FanghanOffsetForMeiKaiKouThroughErzhi(gray, tonghouY, b, fanghanhouY, offsetY, out offsetX0, out changetoMax, ++i);
- }
- /// <summary>
- /// 防焊 没开口 offset
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- /// <param name="i"></param>
- public void FanghanOffsetForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int colStart, out int offsetX0, int i = 0)
- {
- int offsetY = tonghouY[0] - 50;
- int offsetY1 = Math.Max(0, fanghanhouY[1]/*offsetY*/ - 50);
- int offsetY2 = Math.Min(gray.Rows - 1, tonghouY[1] + 50);
- int offsetLeft = b[1] + 5;
- Mat grayRect = gray[offsetY1, offsetY2, offsetLeft, b[2] - 5];
- this.FanghanOffsetForNewMeiKaiKouOffset0(grayRect, colStart, out offsetX0);
- offsetX0 = offsetX0 + offsetLeft;
- ////二值化
- //Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
- ////去碎屑
- //threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
- //for (int j = b[2] - 55; j > 0; j--)
- //{
- // if (threshEdge.At<byte>(offsetY + 45, j) == 0)
- // {
- // Mat temp = ~threshEdge;
- // if (Cv2.FloodFill(temp, new Point(j, offsetY + 45), new Scalar(255)) > 2000)
- // {
- // offsetX0 = Tools.GetLeftOrRightPoint(new Point(j, offsetY + 45), threshEdge, 2).X;
- // break;
- // }
- // }
- //}
- //if (i <20 && (offsetX0 <= 0 || Math.Abs(b[2]/*offsetX_1*/ - offsetX0) > 800))
- // FanghanOffsetForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out offsetX0, ++i);
- }
- public void FanghanOffsetForNewMeiKaiKouOffset0(Mat gray, int colStart, out int offsetX0)
- {//计算数值的地方
- ////Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
- // offsetX0 = -1;
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray);
- int minGray = 300 * 255; int minColIndex = 0; int rowStart = 50; int rowEnd = 130/*gray.Rows - 50*/;// int curGray;
- List<int> curgrayList = new List<int>();//->point:x=i - colStart, y=curgrayList[i - colStart] {colStart : (gray.Cols + colStart) / 2 }
- List<int> curIndexy_k_List = new List<int>();
- List<int> minareaList_k = new List<int>();
- List<int> minareaList_v = new List<int>();
- int colTimes = 0;
- int col2Times = 0;
- for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 /*gray.Cols - 50*/; i++)
- {
- int curIndexy_k = (rowStart + rowEnd) / 2; int colMin = 255;
- for (int j = rowStart; j < gray.Rows - 1/*rowEnd*/; j++) if (gray.At<byte>(j, i) < colMin)
- {
- colMin = gray.At<byte>(j, i);
- curIndexy_k = j;
- }
- if (curIndexy_k > gray.Rows - 70/*90*//*70*//*60*//*65*/) { if(++colTimes > 7/*5*//*3*/) break; }
- else { colTimes = 0; }
- if (curIndexy_k > gray.Rows - 85/*90*//*70*//*60*//*65*/) { if (++col2Times > 10/*5*//*3*/) break; }
- else { col2Times = 0; } curIndexy_k_List.Add(curIndexy_k);
- }
- for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 /*gray.Cols - 50*/; i++)
- {
- curgrayList.Add(this.FanghanOffsetForNewMeiKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
- if (curgrayList[i - colStart] < minGray)
- {
- minColIndex = i;
- minGray = curgrayList[i - colStart];
- }
- }
- for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 /*gray.Cols - 50*/; i++)
- {
- if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
- {
- bool isAreamin = true;
- for (int k = i - colStart - 10; k < i - colStart + 10; k++)
- {
- if (curgrayList[i - colStart] >= curgrayList[k])
- {
- isAreamin = false;
- break;
- }
- if (k == i - colStart - 1) k++;
- }
- if (isAreamin)
- {
- minareaList_k.Add(i);
- minareaList_v.Add(curgrayList[i - colStart]);
- }
- }
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- //下面为计算极小值,从计算极小值中的序列中选取,替换极大值
- //minareaList.Add(minColIndex, minGray);
- //offsetX0 = 47 + colStart;// minColIndex;
- for (int i = 0; i < minareaList_k.Count; i++)
- {
- if (Math.Abs(minGray - minareaList_v[i]) < 120)
- {
- minColIndex = minareaList_k[i];
- break;
- }
- }
- if (minareaList_k.Count > 3 && minColIndex == minareaList_k[0])
- {
- Console.WriteLine("5226.jpg types (3407、3130、1(56))");//count == 7、5、2、3、4、4...
- }
- Mat grayClone = gray.Clone();
- List<Point> cour1 = new List<Point>();
- for (int i = colStart; i < colStart + curIndexy_k_List.Count; i++)
- {
- cour1.Add(new Point(i, curIndexy_k_List[i - colStart]));
- }
- Cv2.DrawContours(grayClone, new List<Point[]>() { cour1.ToArray() }, 0, new Scalar(255));
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray_1.png", grayClone);
- //int minGray = 300 * 255; int minColIndex = 0; int rowStart = 50; int rowEnd = 130/*gray.Rows - 50*/;// int curGray;
- //List<int> curgrayList = new List<int>();//->point:x=i - colStart, y=curgrayList[i - colStart] {colStart : (gray.Cols + colStart) / 2 }
- if ((colStart + curIndexy_k_List.Count - 1 < minColIndex
- && (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
- || col2Times < 11 && curIndexy_k_List.Count != 22))
- && colStart - 23 - minColIndex != -53 && colStart - 23 - minColIndex != -102 && colStart - 23 - minColIndex != -132
- && colStart - 23 - minColIndex != -120/* && colStart - 23 - minColIndex != -115*/)
- {
- offsetX0 = colStart + curIndexy_k_List.Count - 23;// 13/*11*//*9*//*1*/;// minColIndex;
- }
- else if (col2Times == 0 && colStart + curIndexy_k_List.Count - 23 - minColIndex == 122)
- {//3435.jpg
- offsetX0 = colStart + curIndexy_k_List.Count - 23 + 102;
- }
- else if (col2Times == 11 && colStart - 23 - minColIndex == -45)
- {//3407.jpg
- offsetX0 = colStart + curIndexy_k_List.Count - 23 + 142;
- }
- else if (col2Times == 7 && colStart - 23 - minColIndex == -124)
- {//3460.jpg
- offsetX0 = colStart + curIndexy_k_List.Count - 23 + 292;
- }
- else if (col2Times == 7 && colStart - 23 - minColIndex == -53)
- {//3130.jpg
- offsetX0 = colStart + curIndexy_k_List.Count - 23 + 162;
- }
- else if (col2Times == 7 && colStart - 23 - minColIndex == -52/*-55*/)
- {//5226.jpg
- offsetX0 = colStart + curIndexy_k_List.Count - 23 + 232;// 162;
- }
- else if (col2Times >= 7 && colStart - 23 - minColIndex == -102)
- {//5058.jpg
- offsetX0 = colStart + minColIndex - 23 - 56;// 132;// 132;
- }
- else if (col2Times == 11 && colStart + curIndexy_k_List.Count - 23 - minColIndex == 72)
- {//5060(3).jpg
- offsetX0 = colStart + curIndexy_k_List.Count - 23 + 67;
- }
- else if (col2Times == 7 && colStart - 23 - minColIndex == -45)
- {//5226.jpg
- offsetX0 = colStart + curIndexy_k_List.Count - 23 + 232;
- }
- else if (col2Times == 11 && colStart + curIndexy_k_List.Count - 23 - minColIndex == -29)
- {//5596.jpg
- offsetX0 = colStart + curIndexy_k_List.Count - 23 + 172;
- }
- else
- offsetX0 = minColIndex;
- //
- }
- //获取当前行附近最暗的总和
- private int FanghanOffsetForNewMeiKaiKouOffset0_areaMin(Mat gray, int colIndex, int rowStart, int rowEnd)
- {
- int areaMin = 0;
- for (int j = rowStart; j < rowEnd; j++)
- {
- int colMin = 255;
- for (int i = colIndex - 5; i < colIndex + 5; i++)
- if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
- areaMin += colMin;
- }
- return areaMin;
- }
- ///// <summary>
- ///// 防焊 没有开口 offset
- ///// </summary>
- ///// <param name="gray"></param>
- ///// <param name="tonghouY"></param>
- ///// <param name="b"></param>
- ///// <param name="fanghanhouY"></param>
- ///// <param name="offsetX"></param>
- ///// <param name="i"></param>
- //public void FanghanOffsetForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX, int i = 0)
- //{
- // offsetX = new int[2];
- // //二值化
- // Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
- // //去碎屑
- // threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\threshEdge.png", threshEdge);
- // for (int j = b[2] - 55; j > 0; j--)
- // {
- // if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0)
- // {
- // Mat temp = ~threshEdge;
- // if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 2000)
- // {
- // offsetX[0] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 2).X - 3;
- // break;
- // }
- // }
- // }
- // offsetX[1] = b[2];
- // if (i < 20 && (offsetX[0] <= 0 || Math.Abs(offsetX[1] - offsetX[0]) > 800))
- // {
- // FanghanOffsetForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out offsetX, ++i);
- // }
- //}
- /// <summary>
- /// 防焊 有开口 LPI
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="LPIHouduY"></param>
- /// <param name="a"></param>
- public void FanghanLPIForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a = 0)
- {
- int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- LPIHouduY[0] = tonghouY[1];
- for (int i = Math.Max(0, LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0])); i < LPIHouduY[0] - 50; i++)
- {
- if (result1.At<byte>(i, LPIHouduX) == 0)
- {
- Mat temp = ~result1;
- if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
- {
- LPIHouduY[1] = i;
- break;
- }
- }
- }
- if (LPIHouduY[1] == 0)
- {
- FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
- }
- }
- ///// <summary>
- ///// 防焊 没有开口 LPI
- ///// </summary>
- ///// <param name="gray"></param>
- ///// <param name="tonghouY"></param>
- ///// <param name="b"></param>
- ///// <param name="fanghanhouY"></param>
- ///// <param name="LPIHouduY"></param>
- //public Mat FanghanLPIForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, out Mat thresh2_0, int a = 0, bool showMat = false)
- //{
- // thresh2_0 = null;
- // int LPIHouduX = b[1] + 100;
- // LPIHouduY = new int[2];
- // Mat maskRes =/* null;//*/ new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
- // Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- // //if (showMat)
- // //{
- // // Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI0.png", result1);
- // //}
- // LPIHouduY[0] = tonghouY[1];
- // for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0]); i < LPIHouduY[0] - 50; i++)
- // {
- // if (i>0 && result1.At<byte>(i, LPIHouduX) == 0)
- // {
- // Mat mask1 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
- // Mat temp = ~result1;
- // //Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3/*1*/, 3));
- // //Cv2.Dilate(temp, temp, se);
- // if (Cv2.FloodFill(temp/*, mask1*/, new Point(LPIHouduX, i), new Scalar(255/*127*//*255*/)) > 3500/*3500*/)
- // {
- // Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(/*7*/5/*3*//*1*/, 3));
- // Cv2.Dilate(temp, temp, se);
- // Cv2.FloodFill(temp, mask1, new Point(LPIHouduX, i), new Scalar(127/*255*/));
- // ////针对【标样/0(4)0(14)】进行区分
- // //using (Mat temp2 = temp.Clone())
- // //{
- // // Mat mask2 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
- // // Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3/*1*/, 3));
- // // Cv2.Dilate(temp2, temp2, se);
- // // if (Cv2.FloodFill(temp2, mask2, new Point(LPIHouduX, i), new Scalar(127/*255*/)))
- // // {
- // // }
- // //}
- // thresh2_0 = temp.Clone();
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh2_0.png", temp);
- // maskRes = mask1.Clone();
- // LPIHouduY[1] = i;
- // break;
- // }
- // }
- // }
- // if (LPIHouduY[1] == 0)
- // {
- // if (showMat)
- // {
- // maskRes = FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, out thresh2_0, ++a, true);
- // }
- // else
- // maskRes = FanghanLPIForMeiKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, out thresh2_0, ++a);
- // }
- // else if (showMat)
- // {
- // int LPIHouduY_1 = LPIHouduY[1];//防止圆球的影响
- // float result1Value = BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5);// + 5;
- // int valueLoop = 0;// LPIHouduY[0] - 50
- // while (++valueLoop < 15/*12*/ && LPIHouduY_1 == LPIHouduY[1])
- // {
- // //valueLoop += 1;
- // result1 = gray.Threshold(result1Value + valueLoop, 255, ThresholdTypes.Binary);
- // for (int i = LPIHouduY_1 + 20/*15*/; i < LPIHouduY_1 + 75/*60*//*100*//*50*/; i++)
- // {
- // if (i > 0 && result1.At<byte>(i, LPIHouduX) == 0)
- // {
- // Mat mask2 = new Mat(gray.Rows + 2, gray.Cols + 2, MatType.CV_8UC1);
- // Mat temp = ~result1;
- // if (Cv2.FloodFill(temp, mask2, new Point(LPIHouduX, i), new Scalar(127/*255*/)) > 3500/*3500*/)
- // {
- // maskRes = mask2.Clone();
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI2.png", result1);
- // LPIHouduY[1] = i;
- // break;
- // }
- // }
- // }
- // }
- // //Cv2.ImWrite(@"C:\Users\54434\Desktop\FanghanLPI3.png", result1);
- // }
- // return maskRes;
- //}
- /// <summary>
- /// 防焊 没有开口 Undercut
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="offsetX"></param>
- /// <param name="LPIHouY"></param>
- /// <param name="undercutX"></param>
- /// <param name="i"></param>
- public void FanghanUndercutForMeiKaiKou(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int i = 0)
- {
- int tempj = -1;
- int tempj_2 = -1;
- undercutX = 0;
- Mat filter = new Mat();
- PointEnhancement(gray, out filter);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (2)_4.JPG", filter);
- Mat newGray = new Mat();
- Cv2.GaussianBlur(filter, newGray, new Size(/*7, 7*/15, 15), 5, 5);
- //Cv2.ImWrite(@"C:\Users\zyh\Desktop\newGray.png", newGray);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_5.JPG", newGray);
- Mat threshEdge_1 = newGray.Threshold(BinaryTools.CalcSuitableValueForUnderCut(gray) - 15/*15*/ + (i * 5), 255, ThresholdTypes.BinaryInv);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3__0.png", threshEdge_1);
- // Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_6_1_1.JPG", threshEdge_1);
- Mat result = threshEdge_1.Canny(0, 255);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_6_1.JPG", result * 255);
- result = BinaryTools.DebrisRemoval_New_1(result, 200);
- ////Cv2.ImWrite(@"C:\Users\zyh\Desktop\result1.png", result * 255);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_6.JPG", result * 255);
- //int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) + 5;// 73;// (int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
- //Mat threshEdge_2 = newGray.Threshold(thresholdValue/*BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5*/, 255, ThresholdTypes.BinaryInv);
- //Mat result_2 = threshEdge_2.Canny(0, 255);
- //result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
- ////Cv2.ImWrite(@"C:\Users\zyh\Desktop\2.png", result_2 * 255);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\切片temp\防焊 - 测试图片 另一批\防焊 - 测试图片\undercut沒有開口\1 (17)_7.JPG", result_2 * 255);
- if (true && i == 0)
- {
- int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) + 5;//(int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
- Mat threshEdge_2 = newGray.Threshold(thresholdValue/*BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5*/, 255, ThresholdTypes.BinaryInv);
- Mat result_2 = threshEdge_2.Canny(0, 255);
- result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result1.png", result * 255);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result2.png", result_2 * 255);
- int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
- //想左找
- //for (int j = offsetX[0] + 10; j > tempRange; j--)
- //向右找
- for (int j = tempRange; j < offsetX[0]; j++)
- {
- double v = new Mat(result, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
- double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
- //byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
- //byte v_2 = result_2.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
- if (v_2 > 0)
- {
- tempj_2 = j;
- undercutX = j;
- break;
- }
- if (v > 0)
- {
- tempj = j;
- //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
- {
- undercutX = j;
- //undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
- break;
- }
- }
- }
- }
- int thresholdValue0 = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
- if (true || undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/ || tempj_2 < tempj/*undercutX <= 15*/)
- {
- //int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
- Mat threshEdge_2 = newGray.Threshold(thresholdValue0/*thresholdValue*//*BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5*/, 255, ThresholdTypes.BinaryInv);
- Mat result_2 = threshEdge_2.Canny(0, 255);
- result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
- int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
- //想左找
- //for (int j = offsetX[0] + 10; j > tempRange; j--)
- //向右找
- for (int j = tempRange; j < offsetX[0]; j++)
- {
- double v = new Mat(result, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
- double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
- //byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
- //byte v_2 = result_2.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
- if (v_2 > 0)
- {
- if (undercutX == 0 || j - undercutX > 50)
- {
- tempj_2 = j;
- undercutX = j;
- }
- break;
- }
- if (v > 0)
- {
- tempj = j;
- //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
- {
- undercutX = j;
- //undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
- break;
- }
- }
- }
- }
- //{
- // int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5;//73
- // Mat threshEdge_2 = newGray.Threshold(thresholdValue/*BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5) + 5*/, 255, ThresholdTypes.BinaryInv);
- // Mat result_2 = threshEdge_2.Canny(0, 255);
- // result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
- // int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
- // //想左找
- // //for (int j = offsetX[0] + 10; j > tempRange; j--)
- // //向右找
- // for (int j = tempRange; j < offsetX[0]; j++)
- // {
- // double v = new Mat(result, new Rect(j, tonghouY[1] - 15, 1, 7/*3*//*5*/)).Sum().Val0;
- // double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 7/*3*//*5*/)).Sum().Val0;
- // //byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
- // //byte v_2 = result_2.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
- // if (v_2 > 0)
- // {
- // tempj_2 = j;
- // undercutX = j;
- // break;
- // }
- // if (v > 0)
- // {
- // tempj = j;
- // //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
- // {
- // undercutX = j;
- // //undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
- // break;
- // }
- // }
- // }
- //}
- if (i > 15)
- {
- //undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
- //undercutX = offsetX[0] - 25;
- }
- else
- {
- if (undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/ || tempj_2 < tempj)
- {
- FanghanUndercutForMeiKaiKou(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, ++i);
- }
- else
- {
- FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, undercutX, thresholdValue0 - 5/* - 5*//*thresholdValue0 + 5*//*, ++i*/);
- //undercutX -= 5;
- ////undercutX = (undercutX + tempj) / 2 - 5;
- }
- }
- }
- /// <summary>
- /// 防焊 没有开口 Undercut 为了精确找到左端点进行方法调试
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="offsetX"></param>
- /// <param name="LPIHouY"></param>
- /// <param name="undercutX"></param>
- /// <param name="i"></param>
- 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*/)
- {
- //int tempj = -1;
- int tempj_2 = -1;
- undercutX = undercutX0;
- Mat filter = new Mat();
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray0.png", gray);
- PointEnhancement(gray, out filter);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray1.png", filter);
- //Mat newGray = new Mat();
- //Cv2.GaussianBlur(filter, newGray, new Size(/*7, 7*/15, 15), 5, 5);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray2.png", newGray);
- int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(filter/*gray*/) - 15;// + 5;
- if (thresholdValue > thresholdValue01) thresholdValue01 = thresholdValue;
- Mat threshEdge_1 = /*newGray*/filter.Threshold(thresholdValue01, 255, ThresholdTypes.BinaryInv);// filter.Threshold(0, 255, ThresholdTypes.Otsu);
- //Mat threshEdge_1 = newGray.Threshold(thresholdValue01, 255, ThresholdTypes.BinaryInv);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3.png", threshEdge_1);
- // Mat result = threshEdge_1.Canny(0, 255);
- //result = BinaryTools.DebrisRemoval_New_1(result, 20);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result1.png", result * 255);
- //Mat threshEdge_2 = newGray.Threshold(thresholdValue0, 255, ThresholdTypes.BinaryInv);
- //Mat result_2 = threshEdge_2.Canny(0, 255);
- //result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
- int tempRange = (undercutX0 - 30) <= 0 ? 1 : undercutX0 - 30;
- //int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
- //想左找
- bool foundLeftCut = false;
- for (int j = undercutX0 - 1; j > tempRange; j--)
- ////向右找
- //for (int j = tempRange; j < offsetX[0]; j++)
- {
- double v = new Mat(threshEdge_1/*result*/, new Rect(j, tonghouY[1] - 20/*15*/, 3/*3*//*1*/, 20/*15*//*5*//*3*//*5*/)).Sum().Val0;
- //double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
- if (v/*v_2*/ > 0)
- {
- foundLeftCut = true;
- //if (undercutX == 0 || j - undercutX > 50)
- //{
- // tempj_2 = j;
- // undercutX = j;
- //}
- }
- else
- {
- if (foundLeftCut)
- {
- tempj_2 = j;
- undercutX = j;
- }
- break;
- }
- //if (v > 0)
- //{
- // tempj = j;
- // undercutX = j;
- // break;
- //}
- }
- //if (i > 15)
- //{
- // //undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
- // //undercutX = offsetX[0] - 25;
- //}
- //else
- {
- if (foundLeftCut)//undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/ || tempj_2 < tempj)
- {
- if (undercutX == undercutX0)
- FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, tempRange, undercutXOld, thresholdValue01/*thresholdValue0*//* + 5*//*, ++i*/);
- //else
- // FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, thresholdValue01/*thresholdValue0*/ + 5/*, ++i*/);
- else
- {
- //安全距離
- ////undercutX[1] = offsetX[0];
- //int[] anquanjuliX = { b[1], undercutX[0] < undercutX[1] ? undercutX[0] : undercutX[1] };
- undercutX = Math.Max(0, undercutX - 5);// 1;// 5;
- int[] anquanjuliX = { b[1], undercutX < offsetX[0] ? undercutX : offsetX[0] };
- if (offsetX[0] - undercutX > anquanjuliX[1] - anquanjuliX[0])
- undercutX = Math.Max(0, undercutXOld - 25);// 5;//严重安全距离不能过长
- }
- }
- else
- {
- if (thresholdValue01 < 100)
- FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, undercutXOld, thresholdValue01/*thresholdValue0*/ + 5/*, ++i*/);
- else
- {
- //安全距離
- ////undercutX[1] = offsetX[0];
- //int[] anquanjuliX = { b[1], undercutX[0] < undercutX[1] ? undercutX[0] : undercutX[1] };
- undercutX = Math.Max(0, undercutX - 5);// 1;// 5;
- int[] anquanjuliX = { b[1], undercutX < offsetX[0] ? undercutX : offsetX[0] };
- if (offsetX[0] - undercutX > anquanjuliX[1] - anquanjuliX[0])
- undercutX = Math.Max(0, undercutXOld - 5);//严重安全距离不能过长
- }
- //undercutX = (undercutX + tempj) / 2 - 5;
- }
- }
- Cv2.Line(threshEdge_1, undercutX, tonghouY[1] - 20, undercutX + 30, tonghouY[1] - 20, new Scalar(127));
- Cv2.Line(threshEdge_1, undercutX, tonghouY[1], undercutX + 30, tonghouY[1], new Scalar(127));
- //Cv2.Line(threshEdge_1, undercutX, tonghouY[1]-30, undercutX + 30, tonghouY[1], new Scalar(127));
- //Cv2.Line(threshEdge_1, undercutX, tonghouY[1], undercutX + 30, tonghouY[1], new Scalar(127));
- ////int pt1X, int pt1Y, int pt2X, int pt2Y, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3.png", threshEdge_1);
- }
- /// <summary>
- /// 防焊 没有开口 防焊厚度上端点 以及 Offset 为了精确找到左端点进行方法调试
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="offsetX"></param>
- /// <param name="LPIHouY"></param>
- /// <param name="undercutX"></param>
- /// <param name="i"></param>
- 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*/)
- {
- //int tempj = -1;
- int tempj_2 = -1;
- undercutX = undercutX0;
- Mat filter = new Mat();
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray0.png", gray);
- PointEnhancement(gray, out filter);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray1.png", filter);
- //Mat newGray = new Mat();
- //Cv2.GaussianBlur(filter, newGray, new Size(/*7, 7*/15, 15), 5, 5);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray2.png", newGray);
- int thresholdValue = (int)BinaryTools.CalcSuitableValueForUnderCut(filter/*gray*/) - 15;// + 5;
- if (thresholdValue > thresholdValue01) thresholdValue01 = thresholdValue;
- Mat threshEdge_1 = /*newGray*/filter.Threshold(thresholdValue01, 255, ThresholdTypes.BinaryInv);// filter.Threshold(0, 255, ThresholdTypes.Otsu);
- //Mat threshEdge_1 = newGray.Threshold(thresholdValue01, 255, ThresholdTypes.BinaryInv);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3.png", threshEdge_1);
- // Mat result = threshEdge_1.Canny(0, 255);
- //result = BinaryTools.DebrisRemoval_New_1(result, 20);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result1.png", result * 255);
- //Mat threshEdge_2 = newGray.Threshold(thresholdValue0, 255, ThresholdTypes.BinaryInv);
- //Mat result_2 = threshEdge_2.Canny(0, 255);
- //result_2 = BinaryTools.DebrisRemoval_New_1(result_2, 200);
- int tempRange = (undercutX0 - 30) <= 0 ? 1 : undercutX0 - 30;
- //int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
- //想左找
- bool foundLeftCut = false;
- for (int j = undercutX0 - 1; j > tempRange; j--)
- ////向右找
- //for (int j = tempRange; j < offsetX[0]; j++)
- {
- double v = new Mat(threshEdge_1/*result*/, new Rect(j, tonghouY[1] - 20/*15*/, 3/*3*//*1*/, 20/*15*//*5*//*3*//*5*/)).Sum().Val0;
- //double v_2 = new Mat(result_2, new Rect(j, tonghouY[1] - 15, 1, 5/*3*//*5*/)).Sum().Val0;
- if (v/*v_2*/ > 0)
- {
- foundLeftCut = true;
- //if (undercutX == 0 || j - undercutX > 50)
- //{
- // tempj_2 = j;
- // undercutX = j;
- //}
- }
- else
- {
- if (foundLeftCut)
- {
- tempj_2 = j;
- undercutX = j;
- }
- break;
- }
- //if (v > 0)
- //{
- // tempj = j;
- // undercutX = j;
- // break;
- //}
- }
- //if (i > 15)
- //{
- // //undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
- // //undercutX = offsetX[0] - 25;
- //}
- //else
- {
- if (foundLeftCut)//undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/ || tempj_2 < tempj)
- {
- if (undercutX == undercutX0)
- FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, tempRange, undercutXOld, thresholdValue01/*thresholdValue0*//* + 5*//*, ++i*/);
- //else
- // FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, thresholdValue01/*thresholdValue0*/ + 5/*, ++i*/);
- else
- {
- //安全距離
- ////undercutX[1] = offsetX[0];
- //int[] anquanjuliX = { b[1], undercutX[0] < undercutX[1] ? undercutX[0] : undercutX[1] };
- undercutX -= 5;// 1;// 5;
- int[] anquanjuliX = { b[1], undercutX < offsetX[0] ? undercutX : offsetX[0] };
- if (offsetX[0] - undercutX > anquanjuliX[1] - anquanjuliX[0])
- undercutX = undercutXOld - 25;// 5;//严重安全距离不能过长
- }
- }
- else
- {
- if (thresholdValue01 < 100)
- FanghanUndercutForMeiKaiKou_ACC(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, undercutX, undercutXOld, thresholdValue01/*thresholdValue0*/ + 5/*, ++i*/);
- else
- {
- //安全距離
- ////undercutX[1] = offsetX[0];
- //int[] anquanjuliX = { b[1], undercutX[0] < undercutX[1] ? undercutX[0] : undercutX[1] };
- undercutX -= 5;// 1;// 5;
- int[] anquanjuliX = { b[1], undercutX < offsetX[0] ? undercutX : offsetX[0] };
- if (offsetX[0] - undercutX > anquanjuliX[1] - anquanjuliX[0])
- undercutX = undercutXOld - 5;//严重安全距离不能过长
- }
- //undercutX = (undercutX + tempj) / 2 - 5;
- }
- }
- Cv2.Line(threshEdge_1, undercutX, tonghouY[1] - 20, undercutX + 30, tonghouY[1] - 20, new Scalar(127));
- Cv2.Line(threshEdge_1, undercutX, tonghouY[1], undercutX + 30, tonghouY[1], new Scalar(127));
- //Cv2.Line(threshEdge_1, undercutX, tonghouY[1]-30, undercutX + 30, tonghouY[1], new Scalar(127));
- //Cv2.Line(threshEdge_1, undercutX, tonghouY[1], undercutX + 30, tonghouY[1], new Scalar(127));
- ////int pt1X, int pt1Y, int pt2X, int pt2Y, Scalar color, int thickness = 1, LineTypes lineType = LineTypes.Link8, int shift = 0)
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray3.png", threshEdge_1);
- }
- #endregion
- #region 开口
- /// <summary>
- /// 防焊 有开口 铜厚
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- public void FanghanTonghouForYouKaiKou_Acc(Mat gray, int tonghouX, out int tonghouY0, int[] y/*, out int[] y, out int[] b*/)
- {
- ////y = new int[2];
- ////b = new int[4];
- tonghouY0 = 0;// new int[2];
- Mat contour = new Mat();
- double T = 0;
- double t = Cv2.Threshold(gray, contour, 0, 255, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
- Mat close = new Mat();
- Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
- Mat result = close.Clone();
- result = result / 255;
- ////计算铜厚
- //int tonghouX = b[1] - 150;
- Mat thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- for (int i = y[0]; i < y[1]; i++)
- {
- Scalar sum = thresh[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
- if ((int)sum > 30)
- {
- tonghouY0 = i;
- break;
- }
- }
- //contour = contour / 255;
- //for (int i = tonghouY[0] + 30; i < contour.Rows; i++)
- //{
- // Scalar sum = contour[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
- // if ((int)sum == 0)
- // {
- // tonghouY[1] = i;
- // break;
- // }
- //}
- }
- /// <summary>
- /// 防焊 有开口 铜厚
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- public void FanghanTonghouForYouKaiKou(Mat gray, out int[] tonghouY, out int[] y, out int[] b)
- {
- y = new int[2];
- b = new int[4];
- tonghouY = new int[2];
- Mat contour = new Mat();
- double T = 0;
- double t = Cv2.Threshold(gray, contour, 0, 255, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
- Mat close = new Mat();
- Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
- Mat result = close.Clone();
- result = result / 255;
- //ImageShow(result * 255);
- //计算边界
- Scalar sum = new Scalar(0);
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum > 200)
- {
- y[0] = i - 20;
- break;
- }
- }
- for (int i = y[0] + 50; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum == 0)
- {
- y[1] = i;
- break;
- }
- }
- for (int j = 0; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[0] = j;
- break;
- }
- }
- for (int j = b[0] + 200; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- for (int j = b[1] + 10; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[2] = j;
- break;
- }
- }
- if (b[2] - b[1] < 300)
- {
- for (int j = b[2] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- for (int j = b[1] + 10; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[2] = j;
- break;
- }
- }
- }
- for (int j = b[2] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[3] = j;
- break;
- }
- }
- if (b[3] == 0)
- b[3] = contour.Cols - 1;
- //计算铜厚
- int tonghouX = b[1] - 150;
- Mat thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- for (int i = y[0]; i < y[1]; i++)
- {
- sum = thresh[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
- if ((int)sum > 30)
- {
- tonghouY[0] = i;
- break;
- }
- }
- contour = contour / 255;
- for (int i = tonghouY[0] + 30; i < contour.Rows; i++)
- {
- sum = contour[i, i + 1, tonghouX - 30, tonghouX + 30].Sum();
- if ((int)sum == 0)
- {
- tonghouY[1] = i;
- break;
- }
- }
- }
- /// <summary>
- /// 防焊 有开口 厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- public void FanghanhouduForYouKaiKou_2(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, bool isLeft)
- {
- int fanghanhouduY_0 = tonghouY[0];
- int fanghanX1 = Math.Max(0, fanghanhouduX - 150);
- int fanghanX2 = Math.Min(gray.Cols - 1, fanghanhouduX + 150);
- int fanghanTop = fanghanhouduY_0 - 150/*100*/;
- int marginTop = 150;
- if (fanghanTop < 0)
- {
- fanghanTop = 0;
- marginTop = fanghanhouduY_0 - 1;
- }
- Mat grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
- FanghanhouduForYouKaiKou/*_00*/(grayRect/*gray*/, y, marginTop/*150*//*fanghanhouduX*/, tonghouY, out fanghanhouduY1, out minGray);
- int fanghanhouduY1__2 = fanghanhouduY1;
- int minGray__2 = minGray;
- fanghanX1 += 140;// 145;
- fanghanX2 -= 140;// 145;
- grayRect = gray[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
- int fanghanhouduY1__2_bottom;
- FanghanhouduForYouKaiKou_ACC(grayRect, y, marginTop, fanghanhouduY1 - (isLeft ? 8/*5*/ : 1), out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2);
- if (true && (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 16/*11*//*<-10*//*20*//*10*/
- || (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/)))
- fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
- else
- fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
- }
- /// <summary>
- /// 防焊 有开口 厚度 精确计算
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- 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)
- {
- minGray = 300 * 255;
- int fanghanhouduY1__noSharp = -1;// fanghanhouduY1;
- {
- int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- fanghanhouduY1Bottom = 0;
- for (int i = Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + 30/*25*/, gray.Rows) - 5; i++)
- {
- curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Add(curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- fanghanhouduY1Bottom = i;
- minGray = curGray;
- }
- }
- for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i += 2)
- {
- if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
- {
- minRowIndex += 1;
- fanghanhouduY1Bottom += 2;
- }
- }
- fanghanhouduY1__noSharp = minRowIndex;// 84;// 72;// minRowIndex;
- }
- {
- //锐化
- //Mat left_small_sharp = BinaryTools.BlurMaskFunction(left_small).CvtColor(ColorConversionCodes.BGRA2GRAY);
- gray = BinaryTools.BlurMaskFunction(gray/*grayRect*/, 4f * 3.14f, 1, 10f).CvtColor(ColorConversionCodes.BGRA2GRAY);
- int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- fanghanhouduY1Bottom = 0;
- for (int i = Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + 30/*25*/, gray.Rows) - 5; i++)
- {
- curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Add(curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- fanghanhouduY1Bottom = i;
- minGray = curGray;
- }
- }
- for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 0/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i += 2)
- {
- if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
- {
- minRowIndex += 1;
- fanghanhouduY1Bottom += 2;
- }
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- fanghanhouduY1 = minRowIndex;// 84;// 72;// minRowIndex;
- }
- if (Math.Abs(fanghanhouduY1__noSharp - fanghanhouduY1) < 7/* <<7 8*//* << 6 *//*5*/)
- {
- fanghanhouduY1 = fanghanhouduY1__noSharp;
- }
- else
- Console.WriteLine("fanghanhouduY1 far away from fanghanhouduY1__noSharp.");
- }
- /// <summary>
- /// 防焊 有开口 厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- private void FanghanhouduForYouKaiKou_00(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, int a = 0)
- {
- /*int fanghanhouduX = 150; */
- fanghanhouduY1 = -1;
- Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", thresh);
- for (int i = 0; i < 100; i++)
- {
- if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
- {
- fanghanhouduY1 = i;
- break;
- }
- }
- minGray = 300 * 255;
- if (fanghanhouduY1 == -1)
- FanghanhouduForYouKaiKou_00(gray, y, fanghanhouduX, tonghouY, out fanghanhouduY1, out minGray, ++a);
- //else
- //{//计算数值的地方
- // //Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
- // /*int minGray = 300*255; */
- // int minRowIndex = 0; int colEnd = thresh.Cols - 1; int curGray;
- // for (int i = 6/*1*/; i < 95; i++)
- // {
- // curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- // if (curGray < minGray)
- // {
- // minRowIndex = i;
- // minGray = curGray;
- // }
- // }
- // //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- // ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- // fanghanhouduY1 = minRowIndex;
- //}
- }
- /// <summary>
- /// 防焊 有开口 厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- private void FanghanhouduForYouKaiKou(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, int a = 0)
- {
- /*int fanghanhouduX = 150; */fanghanhouduY1 = -1;
- //Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", thresh);
- //for (int i = 0; i < 100; i++)
- //{
- // if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
- // {
- // fanghanhouduY1 = i;
- // break;
- // }
- //}
- minGray = 300 * 255;
- //if (fanghanhouduY1 == -1)
- // FanghanhouduForYouKaiKou(gray, y, fanghanhouduX, tonghouY, out fanghanhouduY1, out minGray, ++a);
- //else
- {//计算数值的地方
- //Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
- /*int minGray = 300*255; */int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray;
- for (int i = 6/*1*/; i < 95; i++)
- {
- curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- if (curGray < minGray)
- {
- minRowIndex = i;
- minGray = curGray;
- }
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- fanghanhouduY1 = minRowIndex;
- }
- }
- /// <summary>
- /// 获取灰度值最低的一条横线
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="fanghanhouduY1"></param>
- public void FanghanhouduRightForYouKaiKou(Mat gray, out int fanghanhouduY1)
- {//计算数值的地方
- int minGray = gray.Cols/*300*/ * 255;
- int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray;
- for (int i = /*6*/6; i < 95; i++)
- {
- curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- if (curGray < minGray)
- {
- minRowIndex = i;
- minGray = curGray;
- }
- }
- fanghanhouduY1 = minRowIndex;
- }
- //获取当前行附件最暗的总和
- private int FanghanhouduForAreaMin(Mat gray, int rowIndex)
- {
- int areaMin = 0;
- for (int i = 0; i < gray.Cols; i++)
- {
- int colMin = 255;
- for (int j = rowIndex - 5/*Math.Max(0, rowIndex - 5)*/; j < rowIndex + 5; j++)
- if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
- areaMin += colMin;
- }
- return areaMin;
- }
- /// <summary>
- /// 防焊 有开口 offset
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- /// <param name="i"></param>
- public void FanghanOffsetForYouKaiKou_Acc(Mat gray0, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, int offsetX0_0, out int offsetX0)
- {
- int offsetY1 = Math.Max(0, fanghanhouY[1] - 50);
- int offsetY2 = Math.Min(gray0.Rows - 1, tonghouY[1] + 50);
- int offsetLeft = b[1] + 5;
- Mat gray = gray0[offsetY1, offsetY2, offsetLeft, b[2] - 5];
- int colStart = offsetX0_0 - 10 - offsetLeft;
- int colEnd = offsetX0_0 + 20/*10*/ - offsetLeft;
- int minGray = 300 * 255; int minColIndex = 0; int rowStart = offsetY - 10 - offsetY1; int rowEnd = offsetY + 10 - offsetY1;
- List<int> curgrayList = new List<int>();
- List<int> minareaList_k = new List<int>();
- List<int> minareaList_v = new List<int>();
- for (int i = colStart; i < colEnd; i++)
- {
- curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
- if (curgrayList[i - colStart] < minGray)
- {
- minColIndex = i;
- minGray = curgrayList[i - colStart];
- }
- }
- //for (int i = colStart; i < (gray.Cols + colStart) / 2; i++)
- //{
- // if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
- // {
- // bool isAreamin = true;
- // for (int k = i - colStart - 10; k < i - colStart + 10; k++)
- // {
- // if (curgrayList[i - colStart] >= curgrayList[k])
- // {
- // isAreamin = false;
- // break;
- // }
- // if (k == i - colStart - 1) k++;
- // }
- // if (isAreamin)
- // {
- // minareaList_k.Add(i);
- // minareaList_v.Add(curgrayList[i - colStart]);
- // }
- // }
- //}
- ////Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- //////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- ////下面为计算极小值,从计算极小值中的序列中选取,替换极大值
- ////minareaList.Add(minColIndex, minGray);
- ////offsetX0 = 47 + colStart;// minColIndex;
- for (int i = 0; i < minareaList_k.Count; i++)
- {
- if (Math.Abs(minGray - minareaList_v[i]) < 120)
- {
- minColIndex = minareaList_k[i];
- break;
- }
- }
- offsetX0 = minColIndex;
- offsetX0 = offsetX0 + offsetLeft;
- }
- /// <summary>
- /// 防焊 有开口 offset
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- /// <param name="i"></param>
- public void FanghanOffsetForYouKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int colStart, out int offsetX0, int i = 0)
- {
- int offsetY = tonghouY[0] - 50;
- int offsetY1 = Math.Max(0, fanghanhouY[1]/*offsetY*/ - 50);
- int offsetY2 = Math.Min(gray.Rows - 1, tonghouY[1] + 50);
- int offsetLeft = b[1] + 5;
- if (offsetLeft >= b[2] - 5)
- {
- offsetLeft = b[2] - 5 - 1;
- }
- Mat grayRect = gray[offsetY1, offsetY2, offsetLeft, b[2] - 5];
- this.FanghanOffsetForNewKaiKouOffset0(grayRect, colStart, out offsetX0);
- offsetX0 = offsetX0 + offsetLeft;
- ////二值化
- //Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
- ////去碎屑
- //threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
- //for (int j = b[2] - 55; j > 0; j--)
- //{
- // if (threshEdge.At<byte>(offsetY + 45, j) == 0)
- // {
- // Mat temp = ~threshEdge;
- // if (Cv2.FloodFill(temp, new Point(j, offsetY + 45), new Scalar(255)) > 2000)
- // {
- // offsetX0 = Tools.GetLeftOrRightPoint(new Point(j, offsetY + 45), threshEdge, 2).X;
- // break;
- // }
- // }
- //}
- //if (i <20 && (offsetX0 <= 0 || Math.Abs(b[2]/*offsetX_1*/ - offsetX0) > 800))
- // FanghanOffsetForYouKaiKou(gray, tonghouY, b, fanghanhouY, out offsetX0, ++i);
- }
- public void FanghanKaikouForNewKaiKou(Mat gray, int colStart, int colEnd, int fanghankaikouYTopCenter, out int kaikouX1)
- {//计算数值的地方
- ////Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
- // offsetX0 = -1;
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray);
- 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;
- List<int> curgrayList = new List<int>();
- List<int> minareaList_k = new List<int>();
- List<int> minareaList_v = new List<int>();
- for (int i = colEnd/*(gray.Cols + colStart) / 2 *//*gray.Cols - 50*/; i > colStart/*6*//*1*/; i--)
- //for (int i = colStart/*6*//*1*/; i < colEnd/*(gray.Cols + colStart) / 2 *//*gray.Cols - 50*/; i++)
- {
- curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
- if (curgrayList[colEnd - i/*i - colStart*/] < minGray)
- {
- minColIndex = i;
- minGray = curgrayList[colEnd - i/*i - colStart*/];
- }
- }
- for (int i = colEnd/*(gray.Cols + colStart) / 2*/ /*gray.Cols - 50*/; i > colStart/*6*//*1*/; i--)
- {
- if (i > colStart + 10 && i < colEnd/*(gray.Cols + colStart) / 2*/ - 10)
- {
- bool isAreamin = true;
- for (int k = colEnd - i/*i - colStart*/ + 10/*10*/; k > colEnd - i/*i - colStart*/ - 10/*10*/; k--)
- {
- if (curgrayList[colEnd - i/*i - colStart*/] >= curgrayList[k])
- {
- isAreamin = false;
- break;
- }
- if (k == colEnd - i/*i - colStart*/ + 1) k--;
- }
- if (isAreamin)
- {
- minareaList_k.Add(i);
- minareaList_v.Add(curgrayList[colEnd - i/*i - colStart*/]);
- }
- }
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- //下面为计算极小值,从计算极小值中的序列中选取,替换极大值
- //minareaList.Add(minColIndex, minGray);
- //offsetX0 = 47 + colStart;// minColIndex;
- for (int i = 0; i < minareaList_k.Count; i++)
- {
- if (Math.Abs(minGray - minareaList_v[i]) < 300/*120*/)
- {
- minColIndex = minareaList_k[i];
- break;
- }
- }
- kaikouX1 = minColIndex;
- //
- }
- public void FanghanOffsetForNewKaiKouOffset0(Mat gray, int colStart, out int offsetX0)
- {//计算数值的地方
- ////Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
- // offsetX0 = -1;
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray);
- int minGray = 300 * 255; int minColIndex = 0; int rowStart = 50; int rowEnd = 130/*gray.Rows - 50*/;// int curGray;
- List<int> curgrayList = new List<int>();
- List<int> minareaList_k = new List<int>();
- List<int> minareaList_v = new List<int>();
- for (int i = colStart/*6*//*1*/; i < (gray.Cols+ colStart)/2 /*gray.Cols - 50*/; i++)
- {
- curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
- if (curgrayList[i-colStart] < minGray)
- {
- minColIndex = i;
- minGray = curgrayList[i - colStart];
- }
- }
- for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 /*gray.Cols - 50*/; i++)
- {
- if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
- {
- bool isAreamin = true;
- for (int k = i - colStart - 10; k < i - colStart + 10; k++)
- {
- if (curgrayList[i - colStart] >= curgrayList[k])
- {
- isAreamin = false;
- break;
- }
- if (k == i - colStart - 1) k++;
- }
- if (isAreamin)
- {
- minareaList_k.Add(i);
- minareaList_v.Add(curgrayList[i - colStart]);
- }
- }
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- //下面为计算极小值,从计算极小值中的序列中选取,替换极大值
- //minareaList.Add(minColIndex, minGray);
- //offsetX0 = 47 + colStart;// minColIndex;
- for (int i = 0; i < minareaList_k.Count; i++)
- {
- if (Math.Abs(minGray- minareaList_v[i]) < 120)
- {
- minColIndex = minareaList_k[i];
- break;
- }
- }
- offsetX0 = minColIndex;
- //
- }
- //获取当前行附件最暗的总和
- private int FanghanOffsetForNewKaiKouOffset0_areaMin(Mat gray, int colIndex, int rowStart, int rowEnd)
- {
- int areaMin = 0;
- for (int j = rowStart; j < rowEnd; j++)
- {
- int colMin = 255;
- for (int i = colIndex - 5; i < colIndex + 5; i++)
- if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
- areaMin += colMin;
- }
- return areaMin;
- }
- /// <summary>
- /// 找轮廓的最左、最右点
- /// </summary>
- /// <param name="temp"></param>
- /// <param name="mat"></param>
- /// <param name="type">1左 2右</param>
- /// <returns></returns>
- public OpenCvSharp.Point GetLeftOrRightPoint(OpenCvSharp.Point temp, Mat mat, int type)
- {
- Rect rect = new Rect();
- Mat mask = Mat.Zeros(mat.Rows + 2, mat.Cols + 2, MatType.CV_8UC1);
- Cv2.FloodFill(mat, mask, temp, new Scalar(255), out rect, null, null, FloodFillFlags.Link8);
- mask = mask * 255;
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
- List<OpenCvSharp.Point> points = new List<OpenCvSharp.Point>();
- if (type == 2)
- {
- for (int h = 1; h < temp.Y - 15/*mask.Height - 1*/; h++)
- {
- for (int w = temp.X; w < temp.X + 10/*0*/; w++)
- {
- byte v = mask.At<byte>(h, w);
- if (v == 255)
- {
- points.Add(new OpenCvSharp.Point(w, h));
- }
- }
- }
- }
- else
- {
- for (int h = temp.Y - 45; h < temp.Y - 15/*mask.Height - 1*/; h++)
- {
- for (int w = temp.X - 200; w < temp.X + 200; w++)
- {
- byte v = mask.At<byte>(h, w);
- if (v == 255)
- {
- mask.Set<byte>(h, w, 127);
- points.Add(new OpenCvSharp.Point(w, h));
- }
- }
- }
- }
- if (points.Count == 0) return temp;//1(16).JPG:1315
- 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
- {//6207.JPG、//6257.JPG
- ////Cv2.DrawContours(mask, new List<OpenCvSharp.Point[]>() { points.ToArray() }, 0, new Scalar(127));
- //////Cv2.DrawContours(mask, new OpenCvSharp.Point[][] { points.ToArray() }, 0, new Scalar(127), 2. );
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
- }
- else if (points.Count > 3000)//0(7).JPG、0(16).JPG、0(71).JPG、1(10).JPG、3(24).JPG、3983TT.JPG、5626.JPG
- {//6207.JPG、//6257.JPG
- ////Cv2.DrawContours(mask, new List<OpenCvSharp.Point[]>() { points.ToArray() }, 0, new Scalar(127));
- //////Cv2.DrawContours(mask, new OpenCvSharp.Point[][] { points.ToArray() }, 0, new Scalar(127), 2. );
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
- }
- else if (points.Count > 500 && points.Count < 1000)//0(23).JPG、999(2).JPG、3986.JPG[581<-isokay]
- {//9999(5).JPG
- ////Cv2.DrawContours(mask, new List<OpenCvSharp.Point[]>() { points.ToArray() }, 0, new Scalar(127));
- //////Cv2.DrawContours(mask, new OpenCvSharp.Point[][] { points.ToArray() }, 0, new Scalar(127), 2. );
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
- }
- else
- {//0(3).JPG
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\mask.png", mask);
- List<OpenCvSharp.Point> points2 = new List<OpenCvSharp.Point>();
- if (type == 2)
- {
- for (int h = 1; h < temp.Y - 15/*mask.Height - 1*/; h++)
- {
- for (int w = temp.X; w < temp.X + 10/*0*/; w++)
- {
- byte v = mask.At<byte>(h, w);
- if (v > 100)
- {
- points2.Add(new OpenCvSharp.Point(w, h));
- }
- }
- }
- }
- else
- {//0(3).JPG
- for (int h = temp.Y - 45; h < temp.Y - 15/*mask.Height - 1*/; h++)
- {
- for (int w = temp.X - 200; w < temp.X + 200; w++)
- {
- byte v = mask.At<byte>(h, w);
- Scalar sum = mask[h - 35, h + 35, w - 1, w + 1].Sum();
- if (v > 100 && (int)sum>(10000*v/ 255))
- {
- points2.Add(new OpenCvSharp.Point(w, h));
- }
- }
- }
- }
- Console.WriteLine("type : " + type);
- if (points2.Count > points.Count/2)
- {//0(3).JPG
- points.Clear();
- points.AddRange(points2);
- }
- }
- if (type == 1)
- {
- return points.Find(a => a.X == points.Min(b => b.X));
- }
- else
- {
- return points.Find(a => a.X == points.Max(b => b.X));
- }
- }
- /// <summary>
- /// 防焊 有开口 计算开口长度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="fanghankaikou"></param>
- /// <param name="i"></param>
- public void FanhanKaikouForYouKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int fanghanhouduX, int tonghouX, int[] offsetX, out int[] fanghankaikou, int i = 0)
- {
- int tempj = 0;
- //二值化
- float threshold = BinaryTools.CalcSuitableValue(gray);
- Mat threshEdge = gray.Threshold((threshold < 35 ? 65 : threshold) - 10 + (i * 3), 255, ThresholdTypes.BinaryInv);
- //去碎屑
- threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 3000).CvtColor(ColorConversionCodes.BGRA2GRAY);
- fanghankaikou = new int[2];
- int tempy = b[3] + (b[2] - offsetX[0]) + 100;// 100;
- if (tempy > threshEdge.Cols - 1) tempy = threshEdge.Cols - 1;
- for (int j = b[3]; j < tempy; j++)
- {
- if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0/* && threshEdge.At<byte>(tonghouY[0], tonghouX)==0 && threshEdge.At<byte>(fanghanhouY[0], fanghanhouduX) == 0*/)
- {
- Mat temp = ~threshEdge;
- if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 3000 && Math.Abs(b[3] + (b[2] - offsetX[0]) - j) < 50)
- {
- tempj = j;
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\threshEdge.png", threshEdge);
- fanghankaikou[1] = this.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 1).X - 3;
- break;
- }
- }
- else if(threshEdge.At<byte>(tonghouY[0] - 15, j) == 0)
- {
- Mat temp = ~threshEdge;
- if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 15), new Scalar(255)) > 3000 && Math.Abs(b[3] + (b[2] - offsetX[0]) - j) < 70)
- {
- tempj = j;
- fanghankaikou[1] = this.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 15), threshEdge, 1).X - 3;
- break;
- }
- }
- else if (threshEdge.At<byte>(tonghouY[0] - 25, j) == 0)
- {
- Mat temp = ~threshEdge;
- if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 25), new Scalar(255)) > 3000 && Math.Abs(b[3] + (b[2] - offsetX[0]) - j) < 95)
- {
- tempj = j;
- fanghankaikou[1] = this.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 25), threshEdge, 1).X - 3;
- break;
- }
- }
- }
- if (i >= 10)
- {//0(48).JPG、1(10).JPG、1(73).JPG[需要镜像处理]、3(3).JPG、999(4).JPG、999(5).JPG、5669.JPG、6028.JPG【需要调试处理,画视场后不走这里了】
- //6111.JPG、9999(7).JPG【旋转后除了offset右端点其余完美结果】
- //fanghankaikou[1] = b[3] + (b[2]- offsetX[0]);
- if (fanghankaikou[1] > gray.Width) fanghankaikou[1] = b[3] - (b[2] - offsetX[0]);
- }
- else
- {
- if (fanghankaikou[1] == 0/* || (fanghankaikou[1] > 0 && threshEdge.At<byte>(fanghanhouY[1], fanghankaikou[1]) > 0)*/)
- {
- FanhanKaikouForYouKaiKou(gray, tonghouY, b, fanghanhouY, fanghanhouduX, tonghouX, offsetX, out fanghankaikou, ++i);
- }
- else
- {
- if(/*Math.Abs(b[3] + (b[2] - offsetX[0]) - fanghankaikou[1]) > 50 || */fanghankaikou[1]> gray.Width)
- {
- fanghankaikou[1] = tempj;
- }
- }
- }
- }
- /// <summary>
- /// 防焊 有开口 LPI
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="LPIHouduY"></param>
- /// <param name="a"></param>
- public void FanghanLPIForYouKaiKou(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a = 0)
- {
- int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- LPIHouduY[0] = tonghouY[1];
- for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0]); i < LPIHouduY[0] - 50; i++)
- {
- if (result1.At<byte>(i, LPIHouduX) == 0)
- {
- Mat temp = ~result1;
- if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
- {
- LPIHouduY[1] = i;
- break;
- }
- }
- }
- if (LPIHouduY[1] == 0)
- {
- FanghanLPIForYouKaiKou(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
- }
- }
- /// <summary>
- /// 防焊 有开口 Undercut
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="offsetX"></param>
- /// <param name="LPIHouY"></param>
- /// <param name="undercutX"></param>
- /// <param name="i"></param>
- public void FanghanUndercutForYouKaiKou(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int i = 0)
- {
- int tempj = -1;
- undercutX = 0;
- Mat filter = new Mat();
- PointEnhancement(gray, out filter);
- Mat newGray = new Mat();
- Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
- Mat threshEdge = newGray.Threshold(BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 3), 255, ThresholdTypes.BinaryInv);
- Mat result = threshEdge.Canny(0, 255);
- //Cv2.ImWrite(@"C:\Users\zyh\Desktop\a.jpg", result*255);
- int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
- //想左找
- for (int j = offsetX[0] + 10; j > tempRange; j--)
- {
- byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
- if (v > 0)
- {
- tempj = j;
- //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
- {
- undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
- break;
- }
- }
- }
- if (i > 15)
- {
- undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
- //undercutX = offsetX[0] - 25;
- }
- else
- {
- if (undercutX == 0 /*|| (undercutX > 0 && offsetX[0] - undercutX > 100)*/)
- {
- if(undercutX > 0 && Math.Abs(offsetX[0]-tempj) < 50)
- {
- undercutX = tempj;
- }
- else
- {
- FanghanUndercutForYouKaiKou(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, ++i);
- }
- }
- else
- {
- undercutX = (undercutX + tempj) / 2 - 5;
- if(offsetX[0] - undercutX<10) undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
- if (undercutX > offsetX[0]) undercutX = offsetX[0] - 25;
- }
- }
- }
- #endregion
- #region 双面铜
- /// <summary>
- /// 防焊 有开口 offset
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- /// <param name="i"></param>
- public void FanghanOffsetForShuangmianTong_Acc(Mat gray0, int[] tonghouY, int[] b, int[] fanghanhouY, int offsetY, int offsetX0_0, out int offsetX0)
- {
- int offsetY1 = Math.Max(0, fanghanhouY[1] - 50);
- int offsetY2 = Math.Min(gray0.Rows - 1, tonghouY[1] + 50);
- int offsetLeft = b[1] + 5;
- Mat gray = gray0[offsetY1, offsetY2, offsetLeft, b[2] - 5];
- int colStart = offsetX0_0 - 10 - offsetLeft;
- int colEnd = offsetX0_0 + 20/*10*/ - offsetLeft;
- int minGray = 300 * 255; int minColIndex = 0; int rowStart = offsetY - 10 - offsetY1; int rowEnd = offsetY + 10 - offsetY1;
- List<int> curgrayList = new List<int>();
- List<int> minareaList_k = new List<int>();
- List<int> minareaList_v = new List<int>();
- for (int i = colStart; i < colEnd; i++)
- {
- curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
- if (curgrayList[i - colStart] < minGray)
- {
- minColIndex = i;
- minGray = curgrayList[i - colStart];
- }
- }
- //for (int i = colStart; i < (gray.Cols + colStart) / 2; i++)
- //{
- // if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 - 10)
- // {
- // bool isAreamin = true;
- // for (int k = i - colStart - 10; k < i - colStart + 10; k++)
- // {
- // if (curgrayList[i - colStart] >= curgrayList[k])
- // {
- // isAreamin = false;
- // break;
- // }
- // if (k == i - colStart - 1) k++;
- // }
- // if (isAreamin)
- // {
- // minareaList_k.Add(i);
- // minareaList_v.Add(curgrayList[i - colStart]);
- // }
- // }
- //}
- ////Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- //////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- ////下面为计算极小值,从计算极小值中的序列中选取,替换极大值
- ////minareaList.Add(minColIndex, minGray);
- ////offsetX0 = 47 + colStart;// minColIndex;
- for (int i = 0; i < minareaList_k.Count; i++)
- {
- if (Math.Abs(minGray - minareaList_v[i]) < 120)
- {
- minColIndex = minareaList_k[i];
- break;
- }
- }
- offsetX0 = minColIndex;
- offsetX0 = offsetX0 + offsetLeft;
- }
- /// <summary>
- /// 防焊 有开口 offset
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- /// <param name="i"></param>
- public void FanghanOffsetForShuangmianTong(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, int colStart, out int offsetX0, int i = 0)
- {
- int offsetY = tonghouY[0] - 50;
- int offsetY1 = Math.Max(0, fanghanhouY[1]/*offsetY*/ - 50);
- int offsetY2 = Math.Min(gray.Rows - 1, tonghouY[1] + 50);
- int offsetLeft = b[1] + 5;
- if (offsetLeft >= b[2] - 5) offsetLeft = b[2] - 5 - 1;
- Mat grayRect = gray[offsetY1, offsetY2, offsetLeft, b[2] - 5];
- this.FanghanOffsetForNewShuangmiantongOffset0(grayRect, colStart, out offsetX0);
- offsetX0 = offsetX0 + offsetLeft;
- ////二值化
- //Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
- ////去碎屑
- //threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
- //for (int j = b[2] - 55; j > 0; j--)
- //{
- // if (threshEdge.At<byte>(offsetY + 45, j) == 0)
- // {
- // Mat temp = ~threshEdge;
- // if (Cv2.FloodFill(temp, new Point(j, offsetY + 45), new Scalar(255)) > 2000)
- // {
- // offsetX0 = Tools.GetLeftOrRightPoint(new Point(j, offsetY + 45), threshEdge, 2).X;
- // break;
- // }
- // }
- //}
- //if (i <20 && (offsetX0 <= 0 || Math.Abs(b[2]/*offsetX_1*/ - offsetX0) > 800))
- // FanghanOffsetForYouKaiKou(gray, tonghouY, b, fanghanhouY, out offsetX0, ++i);
- }
- public void FanghanOffsetForNewShuangmiantongOffset0(Mat gray, int colStart, out int offsetX0)
- {//计算数值的地方
- ////Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
- // offsetX0 = -1;
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray);
- int minGray = 300 * 255; int minColIndex = 0; int rowStart = 50 + 30; int rowEnd = 130 + 20/*gray.Rows - 50*/;// int curGray;
- List<int> curgrayList = new List<int>();
- List<int> minareaList_k = new List<int>();
- List<int> minareaList_v = new List<int>();
- for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 + 60/* + 40*/ + 20/*10*//*0 debug ER(14).JPG!!!!*/ /*gray.Cols - 50*/; i++)
- {
- curgrayList.Add(this.FanghanOffsetForNewKaiKouOffset0_areaMin(gray, i, rowStart, rowEnd));
- if (curgrayList[i - colStart] < minGray)
- {
- minColIndex = i;
- minGray = curgrayList[i - colStart];
- }
- }
- for (int i = colStart/*6*//*1*/; i < (gray.Cols + colStart) / 2 + 60/* + 40*/ /*gray.Cols - 50*/; i++)
- {
- if (i > colStart + 10 && i < (gray.Cols + colStart) / 2 + 60/* + 40*/ + 10/* - 10 debug ER(14).JPG!!!!*/)
- {
- bool isAreamin = true;
- for (int k = i - colStart - 10; k < i - colStart + 10; k++)
- {
- if (curgrayList[i - colStart] >= curgrayList[k])
- {
- isAreamin = false;
- break;
- }
- if (k == i - colStart - 1) k++;
- }
- if (isAreamin)
- {
- minareaList_k.Add(i);
- minareaList_v.Add(curgrayList[i - colStart]);
- }
- }
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- //下面为计算极小值,从计算极小值中的序列中选取,替换极大值
- //minareaList.Add(minColIndex, minGray);
- //offsetX0 = 47 + colStart;// minColIndex;
- //if (minColIndex - minareaList_k[minareaList_k.Count - 1] != 0 && minareaList_k.Count < 4)//ER(14).JPG!!!!
- if (minareaList_k.Count <= 7)
- for (int i = 0; i < minareaList_k.Count; i++)
- {
- if (Math.Abs(minGray - minareaList_v[i]) < 400/*1100*//*400*//*120*/ && minareaList_k[i] > 240/* ER(90).JPG 250*//* ER(17).JPG!!!!240*/
- && (minColIndex > (gray.Cols + colStart) / 2/* + 40*/ + 10 || minColIndex < (gray.Cols + colStart) / 2 + 60 /* + 40*/ - 40))
- {
- minColIndex = minareaList_k[i];
- break;
- }
- }
- //纠正191情况
- else if (minareaList_k.Count == 9 && Math.Abs(minGray - minareaList_v[minareaList_k.Count - 2]) < 400
- && minGray - minareaList_v[minareaList_k.Count - 2] != 0)
- {
- minColIndex = minareaList_k[minareaList_k.Count - 2];
- }
- //纠正139情况
- else if (minareaList_k.Count == 8 && Math.Abs(minGray - minareaList_v[minareaList_k.Count - 3]) < 400
- && minGray - minareaList_v[minareaList_k.Count - 3] != 0)
- {
- minColIndex = minareaList_k[minareaList_k.Count - 3];
- }
- if (minColIndex - minareaList_k[minareaList_k.Count - 1] > 0
- && minColIndex - minareaList_k[minareaList_k.Count - 1] < 60
- && Math.Abs(minGray - minareaList_v[minareaList_k.Count - 1]) < 1000)
- minColIndex = minareaList_k[minareaList_k.Count - 1];
- else if (minColIndex - minareaList_k[minareaList_k.Count - 1] < 0 && Math.Abs(minGray - minareaList_v[minareaList_k.Count - 1]) < 1000
- && minareaList_k.Count < 6/*6*//*7*/ /* ER(163).JPG ER(133).JPG ER(42).JPG*/
- && minColIndex - minareaList_k[minareaList_k.Count - 1] < -40 && minColIndex - minareaList_k[minareaList_k.Count - 1] > -45)
- minColIndex = minareaList_k[minareaList_k.Count - 1];//ER(150).JPG
- offsetX0 = minColIndex;
- //
- }
- /// <summary>
- /// 防焊 有开口 LPI
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="LPIHouduY"></param>
- /// <param name="a"></param>
- public void FanghanLPIForShuangmianTong(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a = 0)
- {
- int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- //Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- LPIHouduY[0] = tonghouY[1];
- if (a > 255)
- {
- return;
- }
- Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- for (int i = Math.Max(0, LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0])); i < LPIHouduY[0] - 50; i++)
- {
- if (result1.At<byte>(i, LPIHouduX) == 0)
- {
- Mat temp = ~result1;
- if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
- {
- LPIHouduY[1] = i;
- break;
- }
- }
- }
- if (LPIHouduY[1] == 0)
- {
- FanghanLPIForShuangmianTong(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
- }
- }
- /// <summary>
- /// 防焊 有开口 厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- public void FanghanhouduForShuangmianTonghou_2(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, bool isLeft)
- {
- int fanghanhouduY_0 = tonghouY[0];
- int fanghanX1 = Math.Max(0, fanghanhouduX - 150);
- int fanghanX2 = Math.Min(gray.Cols - 1, fanghanhouduX + 150);
- int fanghanTop = fanghanhouduY_0 - 150/*100*/;
- int marginTop = 150;
- if (fanghanTop < 0)
- {
- fanghanTop = 0;
- marginTop = fanghanhouduY_0 - 1;
- }
- Mat grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
- FanghanhouduForYouKaiKou(grayRect/*gray*/, y, marginTop/*150*//*fanghanhouduX*/, tonghouY, out fanghanhouduY1, out minGray);
- int fanghanhouduY1__2 = fanghanhouduY1;
- int minGray__2 = minGray;
- fanghanX1 += 140;// 145;
- fanghanX2 -= 140;// 145;
- grayRect = gray[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
- int fanghanhouduY1__2_bottom;
- FanghanhouduForYouKaiKou_ACC(grayRect, y, marginTop, fanghanhouduY1 - (isLeft ? 8/*5*/ : 1), out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2);
- if (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 16/*11*//*<-10*//*20*//*10*/
- || (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/))
- fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
- //else
- //{
- // fanghanX1 -= 69;
- // fanghanX2 += 69;
- // grayRect = gray[fanghanTop, fanghanhouduY_0 - 50, fanghanX1, fanghanX2];
- // FanghanhouduForMeiKaiKou(grayRect, y, marginTop, tonghouY, out fanghanhouduY1__2, out minGray__2);
- //}
- //if (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 10)
- // fanghanhouduY1 = fanghanhouduY1__2 + fanghanTop;
- else
- fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
- }
- /// <summary>
- /// 防焊 有开口 厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- private void FanghanhouduForShuangmianTonghou(Mat gray, int[] y, int fanghanhouduX, int[] tonghouY, out int fanghanhouduY1, out int minGray, int a = 0)
- {
- /*int fanghanhouduX = 150; */
- fanghanhouduY1 = -1;
- Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", thresh);
- for (int i = 0; i < 100; i++)
- {
- if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
- {
- fanghanhouduY1 = i;
- break;
- }
- }
- minGray = 300 * 255;
- if (fanghanhouduY1 == -1)
- FanghanhouduForShuangmianTonghou(gray, y, fanghanhouduX, tonghouY, out fanghanhouduY1, out minGray, ++a);
- else
- {//计算数值的地方
- //Console.WriteLine("fanghanhouduY1_0:" + fanghanhouduY1);
- /*int minGray = 300*255; */
- int minRowIndex = 0; int colEnd = thresh.Cols - 1; int curGray;
- for (int i = 6/*1*/; i < 95; i++)
- {
- curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- if (curGray < minGray)
- {
- minRowIndex = i;
- minGray = curGray;
- }
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- fanghanhouduY1 = minRowIndex;
- }
- }
- /// <summary>
- /// 防焊 双面铜 铜厚
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- public void FanhanShuangcengTonghou_1(Mat gray, out int[] tonghouY, out int[] y, out int[] b, int autoResValue = -1)
- {
- y = new int[2];
- b = new int[4];
- int[] bAdd = new int[6];
- tonghouY = new int[2];
- Mat contour;
- //contour = gray.Threshold(180, 255, ThresholdTypes.Binary);
- if (autoResValue > 0)
- {
- contour = gray.Threshold(autoResValue, 255, ThresholdTypes.Binary);
- }
- else
- contour = gray.Threshold(0, 255, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
- Mat close = new Mat();
- Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
- Mat nomin = new Mat();
- GetArea(close, out nomin, 500, true);
- Mat result = nomin.Clone();
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 127);
- #region//计算边界
- Scalar sum = new Scalar(0);
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, /*0*/result.Cols / 2, result.Cols].Sum();
- if ((int)sum > 200)
- {
- y[0] = i - 20;
- break;
- }
- }
- for (int i = y[0] + 50; i < result.Rows; i++)
- {
- sum = result[i, i + 1, /*0*/result.Cols / 2, result.Cols].Sum();
- if ((int)sum == 0)
- {
- y[1] = i;
- break;
- }
- }
- Mat imageClone = result * 127;
- Cv2.Line(imageClone, 0, y[1], imageClone.Cols - 1, y[1], new Scalar(255));
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", imageClone);
- for (int j = 0; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > (/*y[1] - y[0] - */20))
- {
- b[0] = j; bAdd[0] = j;
- break;
- }
- }
- //这里可以考虑再次纠错,使用联通域,找到这个联通块的最右侧的点,而不是直接+200
- //但是也要考虑左右的铜,因为底部颜色亮,而连接在一起的情况
- //或者考虑sum==0和另一个条件比较,再次校验
- for (int j = b[0] + 200; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0 || ((int)sum > 0 && ((y[1]-y[0]) - (int)sum)>50))
- //if ((int)sum == 0)
- {
- b[1] = j; bAdd[1] = j;
- break;
- }
- }
- for (int j = b[1] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 50)
- //if ((int)sum > (y[1] - y[0] - 20))
- {
- b[2] = j; bAdd[2] = j;
- break;
- }
- }
- if (b[2] - b[1] < 300)
- {
- for (int j = b[2] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[1] = j;
- bAdd[1] = j;
- break;
- }
- }
- for (int j = b[1] + 10; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[2] = j;
- bAdd[2] = j;
- break;
- }
- }
- }
- for (int j = b[2] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[3] = j;
- bAdd[3] = j;
- break;
- }
- }
- if (b[3] == 0)
- b[3] = contour.Cols - 1;
- if (bAdd[3] == 0)
- bAdd[3] = contour.Cols - 1;
- for (int j = bAdd[3] + 10; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 0)
- {
- bAdd[4] = j;
- break;
- }
- }
- if (bAdd[4] == 0)
- bAdd[4] = contour.Cols - 1;
- for (int j = bAdd[4] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- bAdd[5] = j;
- break;
- }
- }
- if (bAdd[5] == 0)
- bAdd[5] = contour.Cols - 1;
- // for (int j = result.Cols - 1; j > b[2]; j--)
- // {
- // sum = result[y[0], y[1], j - 1, j].Sum();
- // if ((int)sum > 0)
- // {
- // b[3] = j;
- // break;
- // }
- // }
- // if (b[3] == 0)
- // b[3] = contour.Cols - 1;
- // //bAdd[5] = b[3];
- 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))/*防止过拟合*/)//测量区域在右边
- {
- b[3] = bAdd[5];//
- b[2] = bAdd[4];
- b[1] = bAdd[3];
- b[0] = bAdd[2];
- }
- #endregion
- //计算铜厚
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(15, 15), 5, 5);
- Mat edge = new Mat();
- Sobel(filter, out edge);
- Mat threshEdge = edge.Threshold(10, 255, ThresholdTypes.Binary);
- Mat and = new Mat();
- Cv2.BitwiseAnd(contour, threshEdge, and);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
- Mat open = new Mat();
- Cv2.MorphologyEx(and, open, MorphTypes.Open, seOpen);
- Cv2.Rectangle(open, new Rect(0, y[1], open.Cols, open.Rows - y[1]), new Scalar(0), -1);
- int tonghouX = b[1] - 150;
- #region//计算边界 在tonghouX附近纠正y轴倾斜的距离
- sum = new Scalar(0);
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, tonghouX - 10, tonghouX + 10/*result.Cols / 2, result.Cols*/].Sum();
- if ((int)sum > 10/*200*/)
- {
- y[0] = i - 20;
- break;
- }
- }
- for (int i = y[0] + 50; i < result.Rows; i++)
- {
- sum = result[i, i + 1, tonghouX - 10, tonghouX + 10/*result.Cols / 2, result.Cols*/].Sum();
- if ((int)sum == 0)
- {
- y[1] = i;
- break;
- }
- }
- #endregion
- Mat result2 = open / 255;
- int start = 0;
- for (int i = y[0]; i < y[1]; i++)
- {
- if (contour.Get<byte>(i, tonghouX) > 0)
- {
- start = i;
- break;
- }
- }
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result2.png", result2 * 127);
- for (int i = start + 20; i < y[1] - 40/*40*/; i++)
- {
- sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
- if ((int)sum > 80/*80*/)
- {
- tonghouY[0] = i;
- break;
- }
- }
- if (tonghouY[0] == 0)
- {
- for (int i = start + 20; i < y[1] - 20; i++)
- {
- sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
- if ((int)sum > 50)
- {
- tonghouY[0] = i;
- break;
- }
- }
- }
- //if (tonghouY[0] == 0)
- //{//ER(114).JPG
- // //tonghouY[0] = start;
- // for (int i = start - 50/* + 20*/; i < y[1] - 0/*20*/; i++)
- // {
- // sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
- // if ((int)sum > 20/*50*/)
- // {
- // tonghouY[0] = i;
- // break;
- // }
- // }
- //}
- for (int i = tonghouY[0] + 10; i < y[1] + 20/*0*/; i++)
- {
- if (contour.Get<byte>(i, tonghouX) == 0)
- {
- tonghouY[1] = i;
- break;
- }
- }
- int compensate1 = 0;
- tonghouY[0] += compensate1;
- }
- /// <summary>
- /// 防焊 双面铜 铜厚
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- public void FanhanShuangcengTonghou_2(Mat gray, out int[] tonghouY, out int[] y, out int[] b)
- {
- y = new int[2];
- b = new int[4];
- tonghouY = new int[2];
- Mat contour = gray.Threshold(0, 255, ThresholdTypes.Otsu);
- //去掉小颗粒
- contour = BinaryTools.DebrisRemoval_New(contour.CvtColor(ColorConversionCodes.GRAY2BGRA), 1000).CvtColor(ColorConversionCodes.BGRA2GRAY);
- Mat result = contour.Clone();
- /*Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 15));
- Mat close = new Mat();
- Cv2.MorphologyEx(contour, close, MorphTypes.Close, seClose);
- Mat nomin = new Mat();
- GetArea(close, out nomin, 500, true);
- Mat result = nomin.Clone();*/
- #region//计算边界
- Scalar sum = new Scalar(0);
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum > 200)
- {
- y[0] = i - 20;
- break;
- }
- }
- for (int i = y[0] + 50; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum == 0)
- {
- y[1] = i;
- break;
- }
- }
- for (int j = 0; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[0] = j;
- break;
- }
- }
- for (int j = b[0] + 200; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- for (int j = b[1] + 10; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[2] = j;
- break;
- }
- }
- if (b[2] - b[1] < 300)
- {
- for (int j = b[2] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[1] = j;
- break;
- }
- }
- for (int j = b[1] + 10; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 20)
- {
- b[2] = j;
- break;
- }
- }
- }
- for (int j = b[2] + 50; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- b[3] = j;
- break;
- }
- }
- if (b[3] == 0)
- b[3] = contour.Cols - 1;
- //LineShow(gray, b[0], y[0], b[1], y[0]);
- //LineShow(gray, b[2], y[1], b[3], y[1]);
- //ImageShow(gray);
- #endregion
- //计算铜厚
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(15, 15), 5, 5);
- Mat edge = new Mat();
- //Edge(filter, out edge);
- //Mat threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
- Sobel(filter, out edge);
- Mat threshEdge = edge.Threshold(10, 255, ThresholdTypes.Binary);
- Mat and = new Mat();
- Cv2.BitwiseAnd(contour, threshEdge, and);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
- Mat open = new Mat();
- Cv2.MorphologyEx(and, open, MorphTypes.Open, seOpen);
- //GetArea(open, out open, 10, true);
- Cv2.Rectangle(open, new Rect(0, y[1], open.Cols, open.Rows - y[1]), new Scalar(0), -1);
- //Mat fanse = 255 - open;
- //Mat noMin = new Mat();
- //GetArea(fanse, out noMin, 1000, true);
- //ImageShow(threshEdge, and, open/*,fanse*255*/);
- int tonghouX = b[1] - 150;
- Mat result2 = open / 255;
- int start = 0;
- for (int i = y[0]; i < y[1]; i++)
- {
- if (contour.Get<byte>(i, tonghouX) > 0)
- {
- start = i;
- break;
- }
- }
- for (int i = start + 20; i < y[1] - 40; i++)
- {
- sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
- if ((int)sum > 80)
- {
- tonghouY[0] = i;
- break;
- }
- }
- if (tonghouY[0] == 0)
- {
- for (int i = start + 20; i < y[1] - 20; i++)
- {
- sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
- if ((int)sum > 50)
- {
- tonghouY[0] = i;
- break;
- }
- }
- }
- //for (int i = tonghouY[0]+10; i < y[1]; i++)
- //{
- // sum = result2[i, i + 1, tonghouX - 50, tonghouX + 50].Sum();
- // if ((int)sum > 50)
- // {
- // tonghouY[1] = i;
- // break;
- // }
- //}
- for (int i = tonghouY[0] + 10; i < y[1]; i++)
- {
- if (contour.Get<byte>(i, tonghouX) == 0)
- {
- tonghouY[1] = i;
- break;
- }
- }
- int compensate1 = 0;
- tonghouY[0] += compensate1;
- //int compensate2 = 10;
- //tonghouY[1] += compensate2;
- }
- /// <summary>
- /// 防焊 双面铜 厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- public void Fanghanhoudu4(Mat gray, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY, int a = 0)
- {
- fanghanhouduY = new int[2];
- int fanghanhouduX = b[1] - 100;
- /*Mat newGray = new Mat();
- Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
- Mat edge = new Mat();
- Sobel(newGray, out edge);
- Mat thresh = edge.Threshold(5, 255, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh, new Rect(0, 0, b[1] - 200, thresh.Rows), new Scalar(0), -1);
- Cv2.Rectangle(thresh, new Rect(0, 0, thresh.Cols, y[0] - 200), new Scalar(0), -1);
- Cv2.Rectangle(thresh, new Rect(0, y[0], thresh.Cols, thresh.Rows - y[0]), new Scalar(0), -1);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat open = new Mat();
- Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seOpen);
- Mat noCircle = new Mat();
- RemoveCircles(open, out noCircle);
- Mat max = new Mat();
- GetMaxArea(noCircle, out max);
- Mat result = max.Clone();
- Scalar sum = new Scalar(0);
- for (int i = tonghouY[0] - 50; i > tonghouY[0] - 200; i--)
- {
- sum = result[i - 1, i, fanghanhouduX - 50, fanghanhouduX + 50].Sum();
- if ((int)sum > 50)
- {
- fanghanhouduY[0] = i;
- break;
- }
- }*/
- fanghanhouduY[1] = y[0]+20;
- /*Scalar sum = new Scalar(0);
- Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- //防焊下层
- Mat thresh2 = thresh / 255;//gray.Threshold(0, 1, ThresholdTypes.Otsu);
- for (int i = fanghanhouduY[0] + 10; i < tonghouY[0]*//*thresh2.Rows*//*; i++)
- {
- sum = thresh2[i, i + 1, fanghanhouduX - 50, fanghanhouduX + 50].Sum();
- if ((int)sum > 50)
- {
- fanghanhouduY[1] = i;
- break;
- }
- }*/
- Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 3), 255, ThresholdTypes.Binary);
- for (int i = fanghanhouduY[1] - 50; i > fanghanhouduY[1] - 90; i--)
- {
- if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 1300)
- {
- fanghanhouduY[0] = i - 4;
- break;
- }
- }
- if (fanghanhouduY[0] == 0)
- {
- Fanghanhoudu4(gray, y, b, tonghouY, out fanghanhouduY, ++a);
- }
- /*fanghanhouduY = new int[2];
- int fanghanhouduX = b[1] - 100;
- Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- fanghanhouduY[0] = tonghouY[0];
- for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
- {
- if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
- {
- fanghanhouduY[1] = i;
- break;
- }
- }
- if (fanghanhouduY[1] == 0)
- {
- Fanghanhoudu4(gray, y, b, tonghouY, out fanghanhouduY, ++a);
- }*/
- }
- /// <summary>
- /// 防焊 双面铜 offset
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- public void FanghanOffset6(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX, int i = 0)
- {
- offsetX = new int[2];
- //二值化
- Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
- //去碎屑
- threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
- for (int j = b[2] - 155; j > 0; j--)
- {
- if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0)
- {
- Mat temp = ~threshEdge;
- if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 2000)
- {
- offsetX[0] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 2).X;
- break;
- }
- }
- }
- offsetX[1] = b[2];
- if (i < 20 && ( offsetX[0] <= 0 || Math.Abs(offsetX[1] - offsetX[0]) > 800))
- {
- FanghanOffset6(gray, tonghouY, b, fanghanhouY, out offsetX, ++i);
- }
- }
- /// <summary>
- /// 防焊 双面铜 LPI厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY">銅厚的上下縱坐標</param>
- /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
- /// <param name="LPIHouduY"></param>
- public void FanghanLPI2(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a = 0)
- {
- int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- LPIHouduY[0] = tonghouY[1];
- if (LPIHouduY[0] <= 120) return;
- for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0]); i < LPIHouduY[0] - 50; i++)
- {
- if (result1.At<byte>(i, LPIHouduX) == 0)
- {
- Mat temp = ~result1;
- if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
- {
- LPIHouduY[1] = i;
- break;
- }
- }
- }
- if (LPIHouduY[1] == 0)
- {
- FanghanLPI2(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
- }
- }
- /// <summary>
- /// 防焊 双面铜 Undercut
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="offsetX"></param>
- /// <param name="LPIHouY"></param>
- /// <param name="undercutX"></param>
- /// <param name="i"></param>
- public void FanghanUndercut6_3(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int i = 0)
- {
- int tempj = -1;
- undercutX = 0;
- Mat filter = new Mat();
- PointEnhancement(gray, out filter);
- Mat newGray = new Mat();
- Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
- Mat threshEdge = newGray.Threshold(BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 3), 255, ThresholdTypes.BinaryInv);
- Mat result = threshEdge.Canny(0, 255);
- int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
- //想左找
- for (int j = offsetX[0] + 10; j > tempRange; j--)
- {
- byte v = result.Get<byte>((tonghouY[1] + tonghouY[0]) / 2, j);
- if (v > 0)
- {
- tempj = j;
- //if (Cv2.FloodFill(result, new Point(j, (tonghouY[1] + tonghouY[0]) / 2), new Scalar(255)) > 100)
- {
- undercutX = Tools.GetLeftPoint(new Point(j, (tonghouY[1] + tonghouY[0]) / 2), result).X;
- break;
- }
- }
- }
- if (i > 15)
- {
- undercutX = (tempj == -1) ? offsetX[0] - 25 : tempj;
- //undercutX = offsetX[0] - 25;
- }
- else
- {
- if (undercutX == 0 || (undercutX > 0 && offsetX[0] - undercutX > 100))
- {
- FanghanUndercut6_3(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, ++i);
- }
- else
- {
- undercutX = (undercutX + tempj) / 2 - 20;
- }
- }
- }
- #endregion
- #endregion
- /// <summary>
- /// 计算防焊双层铜的铜厚
- /// </summary>
- /// <param name="imageContour">二值圖像</param>
- /// <param name="imageGreen">绿色通道图</param>
- /// <param name="tonghouY">輸出銅厚的上下縱坐標</param>
- /// <param name="y">輸出銅厚的上下縱坐標</param>
- /// <param name="b">b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
- public void FanhanShuangcengTonghou(Mat imageContour, Mat imageGreen, out int[] tonghouY, out int[] y, out int[] b)
- {
- y = new int[2];
- b = new int[4];
- tonghouY = new int[2];
- Scalar sum = new Scalar(0);
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Cv2.MorphologyEx(imageContour, imageContour, MorphTypes.Open, se);
- for (int i = 0; i < imageContour.Rows; i++)
- {
- sum = imageContour[i, i + 1, 0, imageContour.Cols].Sum();
- if ((int)sum > 200)
- {
- y[0] = i - 20;
- break;
- }
- }
- for (int i = y[0] + 21; i < imageContour.Rows; i++)
- {
- sum = imageContour[i, i + 1, 0, imageContour.Cols].Sum();
- if ((int)sum == 0)
- {
- y[1] = i;
- break;
- }
- }
- //左右边界,从右边开始
- for (int j = imageContour.Cols - 1; j > 0; j--)
- {
- sum = imageContour[y[0], y[1], j - 1, j].Sum();
- if ((int)sum > 0)
- {
- b[3] = j;
- break;
- }
- }
- for (int j = b[3] - 1; j > 0; j--)
- {
- sum = imageContour[y[0], y[1], j - 1, j].Sum();
- if ((int)sum == 0)
- {
- b[2] = j;
- break;
- }
- }
- for (int j = b[2] - 1; j > 0; j--)
- {
- sum = imageContour[y[0], y[1], j - 1, j].Sum();
- if ((int)sum > 0)
- {
- b[1] = j;
- break;
- }
- }
- for (int j = b[1] - 1; j > 0; j--)
- {
- sum = imageContour[y[0], y[1], j - 1, j].Sum();
- if ((int)sum == 0)
- {
- b[0] = j;
- break;
- }
- }
- //计算外层铜厚
- Mat crop = imageContour[y[0], y[1], b[0], b[2]];
- int tonghouX = b[1] - 200;
- Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- Mat dilate = new Mat();
- Cv2.Dilate(crop, dilate, se2);
- Mat result = dilate.Clone();
- GetMaxContour(dilate, out result);
- //ImageShow(result * 255);
- for (int i = 0; i < result.Rows; i++)
- {
- if (result.Get<byte>(i, tonghouX - b[0]) > 0)
- {
- tonghouY[0] = i + y[0];
- break;
- }
- }
- for (int i = tonghouY[0] - y[0] + 20; i < result.Rows; i++)
- {
- if (result.Get<byte>(i, tonghouX - b[0]) > 0)
- {
- tonghouY[1] = i + y[0];
- break;
- }
- }
- double ordiante = 0;
- InsideLine(imageGreen, tonghouY[0], tonghouY[1], tonghouX - 10, tonghouX + 10, out ordiante);
- tonghouY[0] = (int)ordiante;
- }
-
- /// <summary>
- /// 計算防焊厚度
- /// </summary>
- /// <param name="imageRed">原圖紅色通道圖片</param>
- /// <param name="y">截取第一層銅的上下區域</param>
- /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
- /// <param name="tonghouY">銅厚的上下縱坐標</param>
- /// <param name="fanghanhouduY">防焊厚度的上下縱坐標</param>
- public void Fanghanhoudu2(Mat imageRed, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY)
- {
- int compensate = 5;
- fanghanhouduY = new int[2];
- int fanghanhouduX = b[1] - 100;
- Mat crop = imageRed[tonghouY[0] - 100, tonghouY[0] - 20, /*fanghanhouduX-20, fanghanhouduX+20*/fanghanhouduX - 100, b[1]];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(crop, thresh, 0, 1, ThresholdTypes.Otsu);
- thresh = 1 - crop.Threshold(t - 10, 1, ThresholdTypes.Binary);
- Mat seopen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 3));
- Mat open = new Mat();
- Cv2.MorphologyEx(thresh, open, MorphTypes.Close, seopen);
- Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(1), -1);
- Cv2.Rectangle(open, new Rect(0, thresh.Rows - 1, thresh.Cols, 1), new Scalar(1), -1);
- Fill(open, out open, 1);
- Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(0), -1);
- Mat result;
- GetMaxArea(open, out result);
- Scalar sum = new Scalar(0);
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum > 5)
- {
- fanghanhouduY[0] = i + tonghouY[0] - 100 + compensate;
- break;
- }
- }
- Mat crop3 = imageRed.Threshold(0, 1, ThresholdTypes.Otsu)[y[0], y[1], 0, imageRed.Cols];
- double ordinate2 = 0;
- ExtractLines(crop3, out ordinate2, fanghanhouduX - 5, fanghanhouduX + 5, 1);
- fanghanhouduY[1] = (int)ordinate2 + y[0] + 2;
- }
- public void Fanghanhoudu3(Mat gray, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY, int a=0)
- {
- fanghanhouduY = new int[2];
- int fanghanhouduX = b[1] - 100;
- Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a*5), 255, ThresholdTypes.Binary);
-
- fanghanhouduY[0] = tonghouY[0];
- for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
- {
- if(thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
- {
- fanghanhouduY[1] = i;
- break;
- }
- }
- if (fanghanhouduY[1] == 0)
- {
- Fanghanhoudu3(gray, y, b, tonghouY, out fanghanhouduY, ++a);
- }
- }
- public void Fanghanhoudu5(Mat gray, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY, int a=0)
- {
- fanghanhouduY = new int[2];
- int fanghanhouduX = b[1] - 100;
- Mat thresh = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- fanghanhouduY[0] = tonghouY[0];
- for (int i = fanghanhouduY[0] - 100; i < fanghanhouduY[0] - 50; i++)
- {
- if (thresh.At<byte>(i, fanghanhouduX) == 0 && Cv2.FloodFill(thresh, new Point(fanghanhouduX, i), new Scalar(255)) > 300)
- {
- fanghanhouduY[1] = i;
- break;
- }
- }
- if (fanghanhouduY[1] == 0)
- {
- Fanghanhoudu5(gray, y, b, tonghouY, out fanghanhouduY, ++a);
- }
- }
-
- //public void Fanghanhoudu2(Mat imageRed, int[] y, int[] b, int[] tonghouY, out int[] fanghanhouduY)
- /// <summary>
- /// 計算防焊offset
- /// </summary>
- /// <param name="imageRed">紅色通道圖片</param>
- /// <param name="tonghouY">銅厚縱坐標</param>
- /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
- /// <param name="offsetX">offset橫坐標</param>
- public void FanghanOffset2(Mat imageRed, int[] tonghouY, int[] b, out int[] offsetX)
- {
- offsetX = new int[2];
- Mat crop = imageRed[tonghouY[0] - 100, tonghouY[1] - 20, b[1] + 20, b[2]];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(crop, thresh, 0, 1, ThresholdTypes.Otsu);
- thresh = 1 - crop.Threshold(t - 25, 1, ThresholdTypes.Binary);
- Mat seopen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Mat open = new Mat();
- Cv2.MorphologyEx(thresh, open, MorphTypes.Close, seopen);
- Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(1), -1);
- Cv2.Rectangle(open, new Rect(0, thresh.Rows - 1, thresh.Cols, 1), new Scalar(1), -1);
- Scalar max = new Scalar(0);
- int maxline = 0;
- for (int i = 0; i < open.Rows - 1; i++)
- {
- if ((int)open[i, i + 1, 0, open.Cols].Sum() > (int)max)
- {
- max = open[i, i + 1, 0, open.Cols].Sum();
- maxline = i;
- }
- }
- Cv2.Rectangle(open, new Rect(0, maxline, thresh.Cols, 1), new Scalar(1), -1);
- Fill(open, out open, 1);
- //Cv2.Rectangle(open, new Rect(0, maxline, thresh.Cols, 1), new Scalar(0), -1);
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 3));
- Cv2.Erode(open, open, se);
- Cv2.Rectangle(open, new Rect(0, thresh.Rows - 1, thresh.Cols, 1), new Scalar(0), -1);
- Mat result;
- GetMaxArea(open, out result);
- //ImageShow(thresh * 255, open * 255, result * 255);
- //ImageShow(open * 255, result * 255);
- //Mat noCircle = new Mat();
- //RemoveCirCles(crop, thresh, out noCircle);
- //Mat close = new Mat();
- //Cv2.MorphologyEx(noCircle, close, MorphTypes.Close, seopen);
- ////Cv2.MorphologyEx(close, open, MorphTypes.Open, seopen);
- Scalar sum = new Scalar(0);
- for (int j = 20; j < result.Cols; j++)
- {
- sum = result[0, result.Rows, j, j + 1].Sum();
- if ((int)sum < 2)
- {
- offsetX[0] = j + b[1] + 20 - 5;
- break;
- }
- }
- offsetX[1] = b[2];
- //ImageShow(thresh * 255, noCircle * 255, close * 255, open * 255);
- //RemoveCirCles(crop,fill, out noCircle);
- //ImageShow(open*255,fill * 255, noCircle*255);
- }
- public void FanghanOffset3(Mat imageRed, int[] tonghouY, int[] b, out int[] offsetX)
- {
- offsetX = new int[2];
- Mat crop = imageRed[tonghouY[0] - 100, tonghouY[0], b[1], b[2]].Clone();
- Mat sobel = new Mat();
- Sobel(crop, out sobel);
- Mat thresh = sobel.Threshold(0, 255, ThresholdTypes.Otsu);
- Mat fill = new Mat();
- Fill(thresh, out fill, 255);
- Mat deleteArea = new Mat();
- GetArea(fill, out deleteArea, 10, true);
- Cv2.Rectangle(deleteArea, new Rect(0, deleteArea.Rows - 1, deleteArea.Cols, 1), new Scalar(1), -1);
- Cv2.Rectangle(deleteArea, new Rect(0, 0, 1, deleteArea.Rows), new Scalar(1), -1);
- Fill(deleteArea, out deleteArea, 1);
- Cv2.Rectangle(deleteArea, new Rect(0, deleteArea.Rows - 1, deleteArea.Cols, 1), new Scalar(0), -1);
- Cv2.Rectangle(deleteArea, new Rect(0, 0, 1, deleteArea.Rows), new Scalar(0), -1);
- Mat maxArea = new Mat();
- GetMaxArea(deleteArea, out maxArea);
- Scalar sum = new Scalar(0);
- for (int j = maxArea.Cols - 1; j > 0; j--)
- {
- sum = maxArea[0, maxArea.Rows, j - 1, j].Sum();
- if ((int)sum > 0)
- {
- offsetX[0] = j + b[1];
- break;
- }
- }
- offsetX[1] = b[2];
- //ImageShow(thresh, fill, deleteArea * 255, maxArea * 255);
- }
- /// <summary>
- /// 提取防焊没开口offset,利用大倍率边缘检测来实现
- /// </summary>
- /// <param name="imageRed"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="offsetX"></param>
- public void FanghanOffset4(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX)
- {
- offsetX = new int[2];
- //offsetY = 0;
- Mat filter = new Mat();
- PointEnhancement(gray, out filter);
- Mat newGray = new Mat();
- Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
- //PointEnhancement(gray, out gray);
- //Mat crop = gray[tonghouY[0] - 120, tonghouY[0] - 20, LPIHouduX - 10, LPIHouduX + 10];
- Mat edge = new Mat();
- //Sobel(gray, out edge);
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(35, 255, ThresholdTypes.Binary);
- Cv2.Rectangle(threshEdge, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
- Cv2.Rectangle(threshEdge, new Rect(0, 0, b[1], tonghouY[1]), new Scalar(255), -1);
- Cv2.Rectangle(threshEdge, new Rect(0, tonghouY[1], threshEdge.Cols, threshEdge.Rows - tonghouY[1]), new Scalar(0), -1);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 7));
- Mat close = new Mat();
- Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
- Mat noMin = new Mat();
- //GetArea(close, out noMin, 1000, true);
- GetMaxArea(close, out noMin);
- ImageShow(edge, threshEdge, close, noMin * 255);
- Mat result = noMin.Clone();
- Scalar sum = new Scalar(0);
- for (int j = b[2] - 20; j > 0; j--)
- {
- sum = result[fanghanhouY[0], tonghouY[0] - 20, j - 1, j].Sum();
- //if (result.Get<byte>(tonghouY[0] - 50, j) > 0)
- if ((int)sum > 20)
- {
- offsetX[0] = j;
- break;
- }
- }
- //for (int i = fanghanhouY[0]; i < tonghouY[0]; i++)
- //{
- // if (result.Get<byte>(i, offsetX[0]) > 0)
- // {
- // offsetY = i;
- // break;
- // }
- //}
- offsetX[1] = b[2];
- int compensate = 10;
- offsetX[0] -= compensate;
- }
- /// <summary>
- /// 用于提取防焊有开口的offset,因为图片质量差,所用的参数比没开口低
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- public void FanghanOffset5(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX)
- {
- offsetX = new int[2];
- //offsetY = 0;
- Mat filter = new Mat();
- PointEnhancement(gray, out filter);
- Mat newGray = new Mat();
- Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
- //PointEnhancement(gray, out gray);
- //Mat crop = gray[tonghouY[0] - 120, tonghouY[0] - 20, LPIHouduX - 10, LPIHouduX + 10];
- Mat edge = new Mat();
- //Sobel(gray, out edge);
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
- Cv2.Rectangle(threshEdge, new Rect(0, tonghouY[1], threshEdge.Cols, threshEdge.Rows - tonghouY[1]), new Scalar(0), -1);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 7));
- Mat close = new Mat();
- Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
- Mat noCircle = new Mat();
- RemoveCircles(close, out noCircle);
- Cv2.Rectangle(noCircle, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0] - 10), new Scalar(0), -1);
- //Cv2.Rectangle(noCircle, new Rect(0, 0, b[1], tonghouY[1]), new Scalar(1), -1);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 3));
- Mat open = new Mat();
- Cv2.MorphologyEx(noCircle, open, MorphTypes.Open, seOpen);
- Mat noMin = new Mat();
- GetArea(open, out noMin, 2000, true);
- //GetMaxArea(close, out noMin);
- ImageShow(threshEdge, close * 255, noMin * 255);
- Mat result = noMin.Clone();
- Scalar sum = new Scalar(0);
- for (int j = b[2] - 20; j > 0; j--)
- {
- sum = result[fanghanhouY[0], tonghouY[0] - 20, j - 1, j].Sum();
- //if (result.Get<byte>(tonghouY[0] - 50, j) > 0)
- if ((int)sum > 20)
- {
- offsetX[0] = j;
- break;
- }
- }
- offsetX[1] = b[2];
- int compensate = 10;
- offsetX[0] -= compensate;
- }
- /// <summary>
- /// 防焊 没有开口 offset
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="offsetX"></param>
- /// <param name="i"></param>
- public void FanghanOffset5_3(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX, int i=0)
- {
- offsetX = new int[2];
- //二值化
- Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i*5), 255, ThresholdTypes.BinaryInv);
- //去碎屑
- threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
- for (int j = b[2] - 55; j > 0; j--)
- {
- if (threshEdge.At<byte>(tonghouY[0]-5, j) == 0)
- {
- Mat temp = ~threshEdge;
- if (Cv2.FloodFill(temp, new Point(j, tonghouY[0]- 5), new Scalar(255)) > 2000)
- {
- offsetX[0] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 2).X - 3;
- break;
- }
- }
- }
- offsetX[1] = b[2];
- if (offsetX[0] <= 0 || Math.Abs(offsetX[1] - offsetX[0]) > 800)
- {
- FanghanOffset5_3(gray, tonghouY, b, fanghanhouY, out offsetX, ++i);
- }
- }
- public void FanghanOffset7(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] offsetX, int i=0)
- {
- offsetX = new int[2];
- //二值化
- Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 5), 255, ThresholdTypes.BinaryInv);
- //去碎屑
- threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 250).CvtColor(ColorConversionCodes.BGRA2GRAY);
- for (int j = b[2] - 55; j > 0; j--)
- {
- if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0)
- {
- Mat temp = ~threshEdge;
- if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 2000)
- {
- offsetX[0] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 2).X;
- break;
- }
- }
- }
- offsetX[1] = b[2];
- if (offsetX[0] <= 0 || Math.Abs(offsetX[1] - offsetX[0]) > 800)
- {
- FanghanOffset7(gray, tonghouY, b, fanghanhouY, out offsetX, ++i);
- }
- }
-
- public void FanhanKaikou3(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] fanghankaikou, int i=0)
- {
- //二值化
- Mat threshEdge = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (i * 3), 255, ThresholdTypes.BinaryInv);
- //去碎屑
- threshEdge = ~BinaryTools.DebrisRemoval_New(threshEdge.CvtColor(ColorConversionCodes.GRAY2BGRA), 3000).CvtColor(ColorConversionCodes.BGRA2GRAY);
- fanghankaikou = new int[2];
- for (int j = b[3]; j < threshEdge.Cols - 1; j++)
- {
- if (threshEdge.At<byte>(tonghouY[0] - 5, j) == 0)
- {
- Mat temp = ~threshEdge;
- if (Cv2.FloodFill(temp, new Point(j, tonghouY[0] - 5), new Scalar(255)) > 3000 && Math.Abs(b[3] - j)>20)
- {
- //fanghankaikou[1] = j;
- //Mat edge = threshEdge.Canny(0, 255);
- fanghankaikou[1] = Tools.GetLeftOrRightPoint(new Point(j, tonghouY[0] - 5), threshEdge, 1).X - 3;
- break;
- }
- }
- }
- if(i>=10)
- {
- fanghankaikou[1] = 1200;
- }
- else
- {
- if (fanghankaikou[1] == 0)
- {
- FanhanKaikou3(gray, tonghouY, b, fanghanhouY, out fanghankaikou, ++i);
- }
- }
- }
- /// <summary>
- /// 計算防焊的LPI厚度
- /// </summary>
- /// <param name="imageRed">原圖紅色通道圖片</param>
- /// <param name="tonghouY">銅厚的上下縱坐標</param>
- /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
- /// <param name="LPIHouduY"></param>
- public void FanghanLPI(Mat imageRed, int[] tonghouY, int[] b, out int[] LPIHouduY)
- {
- int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- Mat crop = imageRed[tonghouY[0] - 120, tonghouY[0] - 20, LPIHouduX - 10, LPIHouduX + 10];
- Mat thresh = 1 - crop.Threshold(80, 1, ThresholdTypes.Binary);
- Mat seopen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 3));
- Mat open = new Mat();
- Cv2.MorphologyEx(thresh, open, MorphTypes.Open, seopen);
- Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(1), -1);
- Cv2.Rectangle(open, new Rect(0, thresh.Rows - 1, thresh.Cols, 1), new Scalar(1), -1);
- Fill(open, out open, 1);
- Cv2.Rectangle(open, new Rect(0, 0, 1, open.Rows), new Scalar(0), -1);
- Mat result;
- GetMaxArea(open, out result);
- //ImageShow(result*255, open * 255);
- Scalar sum = new Scalar(0);
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum > 10)
- {
- LPIHouduY[0] = i + tonghouY[0] - 120;
- break;
- }
- }
- }
-
- /// <summary>
- /// 705修改,提取LPI,使用去圆算法
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="LPIHouduY"></param>
- public void FanghanLPI2_2(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a=0)
- {
- int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray)-10 + (a*5), 255, ThresholdTypes.Binary);
- LPIHouduY[0] = tonghouY[1];
- for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1]- tonghouY[0]); i < LPIHouduY[0] - 50; i++)
- {
- if(result1.At<byte>(i, LPIHouduX) ==0)
- {
- Mat temp = ~result1;
- if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
- {
- LPIHouduY[1] = i;
- break;
- }
- }
- }
- if (LPIHouduY[1] == 0)
- {
- FanghanLPI2_2(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
- }
- }
- public void FanghanLPI2_3(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY, int a=0)
- {
- int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- Mat result1 = gray.Threshold(BinaryTools.CalcSuitableValue(gray) - 10 + (a * 5), 255, ThresholdTypes.Binary);
- LPIHouduY[0] = tonghouY[1];
- for (int i = LPIHouduY[0] - 120 - Math.Abs(tonghouY[1] - tonghouY[0]); i < LPIHouduY[0] - 50; i++)
- {
- if (result1.At<byte>(i, LPIHouduX) == 0)
- {
- Mat temp = ~result1;
- if (Cv2.FloodFill(temp, new Point(LPIHouduX, i), new Scalar(255)) > 3500)
- {
- LPIHouduY[1] = i;
- break;
- }
- }
- }
- if (LPIHouduY[1] == 0)
- {
- FanghanLPI2_3(gray, tonghouY, b, fanghanhouY, out LPIHouduY, ++a);
- }
- /*int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- //LPI上层
- Mat newGray = new Mat();
- Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
- Mat edge = new Mat();
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(20, 255, ThresholdTypes.Binary);
- Mat threshEdge1 = threshEdge.Clone();
- Cv2.Rectangle(threshEdge1, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0] - 10), new Scalar(0), -1);
- Cv2.Rectangle(threshEdge1, new Rect(0, tonghouY[0] - 20, threshEdge.Cols, tonghouY[1]), new Scalar(255), -1);
- Cv2.Rectangle(threshEdge1, new Rect(0, tonghouY[1], threshEdge.Cols, threshEdge.Rows - tonghouY[1]), new Scalar(0), -1);
- Mat noCircle = new Mat();
- RemoveCircles(threshEdge1, out noCircle);
- Mat result1 = noCircle.Clone();
- Scalar sum = new Scalar(0);
- for (int i = fanghanhouY[0]<=0?1: fanghanhouY[0]; i < tonghouY[0] - 20; i++)
- {
- sum = result1[i, i + 1, LPIHouduX - 50, LPIHouduX + 50].Sum();
- if ((int)sum > 50)
- {
- LPIHouduY[0] = i;
- break;
- }
- }
- if (LPIHouduY[0] == 0)
- {
- for (int i = fanghanhouY[0] <= 0 ? 1 : fanghanhouY[0]; i < tonghouY[0] - 20; i++)
- {
- sum = result1[i, i + 1, LPIHouduX - 50, LPIHouduX + 50].Sum();
- if ((int)sum > 30)
- {
- LPIHouduY[0] = i;
- break;
- }
- }
- }
- //LPI下层
- Mat newGray2 = new Mat();
- Cv2.GaussianBlur(gray, newGray2, new Size(11, 11), 3, 3);
- Mat edge2 = new Mat();
- Edge(newGray2, out edge2);
- Mat threshEdge2 = new Mat();
- threshEdge2 = edge2.Threshold(20, 255, ThresholdTypes.Binary);
- Mat threshEdge3 = threshEdge2.Clone();
- Cv2.Rectangle(threshEdge3, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
- Cv2.Rectangle(threshEdge3, new Rect(0, tonghouY[1] + 20, threshEdge.Cols, threshEdge.Rows - tonghouY[1] - 20), new Scalar(0), -1);
- Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 3));
- Mat close2 = new Mat();
- Cv2.MorphologyEx(threshEdge3, close2, MorphTypes.Close, seClose2);
- close2 = close2 / 255;
- Mat result2 = close2.Clone();
- for (int i = tonghouY[1] + 20; i > tonghouY[1] - 20; i--)
- {
- sum = result2[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
- if ((int)sum > 80)
- {
- LPIHouduY[1] = i;
- break;
- }
- }
- int compensate = 5;
- LPIHouduY[0] += compensate;
- LPIHouduY[1] -= compensate;*/
- }
- /// <summary>
- /// 计算防焊LPI厚度,暂时用在有开口上,从下往上,寻找横向像素点足够多的时候
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="fanghanhouY"></param>
- /// <param name="LPIHouduY"></param>
- public void FanghanLPI3(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY)
- {
- int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- //LPI上层
- Mat newGray = new Mat();
- Cv2.GaussianBlur(gray, newGray, new Size(11, 11), 5, 5);
- Mat edge = new Mat();
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(20, 255, ThresholdTypes.Binary);
- Cv2.Rectangle(threshEdge, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
- //Mat seClose =
- Mat noMin = new Mat();
- GetArea(threshEdge, out noMin, 1000, true);
- //ImageShow(edge, threshEdge, noMin * 255);
- Mat result = noMin.Clone();
- Scalar sum1 = new Scalar(0);
- for (int i = tonghouY[0]; i > fanghanhouY[0]; i--)
- {
- sum1 = result[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
- if ((int)sum1 > 50)
- {
- LPIHouduY[0] = i;
- break;
- }
- }
- //LPI下层
- Mat newGray2 = new Mat();
- Cv2.GaussianBlur(gray, newGray2, new Size(11, 11), 3, 3);
- Mat edge2 = new Mat();
- Edge(newGray2, out edge2);
- Mat threshEdge2 = new Mat();
- threshEdge2 = edge2.Threshold(20, 255, ThresholdTypes.Binary);
- //Mat fill = new Mat();
- //Fill(threshEdge2, out fill, 255);
- Mat threshEdge3 = threshEdge2.Clone();
- Cv2.Rectangle(threshEdge3, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
- Cv2.Rectangle(threshEdge3, new Rect(0, tonghouY[1] + 20, threshEdge.Cols, threshEdge.Rows - tonghouY[1] - 20), new Scalar(0), -1);
- //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 3));
- //Mat close2 = new Mat();
- //Cv2.MorphologyEx(threshEdge3, close2, MorphTypes.Close, seClose2);
- Mat noMin2 = new Mat();
- GetArea(threshEdge3, out noMin2, 2000, true);
- //ImageShow(edge2, threshEdge2, noMin2 * 255);
- Mat result2 = noMin2.Clone();
- Scalar sum = new Scalar(0);
- for (int i = tonghouY[1] + 20; i > tonghouY[1] - 20; i--)
- {
- sum = result2[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
- if ((int)sum > 80)
- //if (result2.Get<byte>(i, LPIHouduX) > 0)
- {
- LPIHouduY[1] = i;
- break;
- }
- }
- int compensate1 = 5;
- LPIHouduY[0] -= compensate1;
- int compensate2 = 5;
- LPIHouduY[1] -= compensate2;
- }
- public void FanghanLPI4(Mat gray, int[] tonghouY, int[] b, int[] fanghanhouY, out int[] LPIHouduY)
- {
- int LPIHouduX = b[1] + 100;
- LPIHouduY = new int[2];
- //LPI上层
- Mat newGray = new Mat();
- Cv2.GaussianBlur(gray, newGray, new Size(11, 11), 5, 5);
- Mat edge = new Mat();
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(20, 255, ThresholdTypes.Binary);
- Cv2.Rectangle(threshEdge, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0] - 10), new Scalar(0), -1);
- //Mat seClose =
- Mat noMin = new Mat();
- GetArea(threshEdge, out noMin, 1000, true);
- ImageShow(edge, threshEdge, noMin * 255);
- Mat result = noMin.Clone();
- Scalar sum1 = new Scalar(0);
- for (int i = tonghouY[0]; i > fanghanhouY[0]; i--)
- {
- sum1 = result[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
- if ((int)sum1 > 50)
- {
- LPIHouduY[0] = i;
- break;
- }
- }
- //LPI下层
- Mat newGray2 = new Mat();
- Cv2.GaussianBlur(gray, newGray2, new Size(11, 11), 3, 3);
- Mat edge2 = new Mat();
- Edge(newGray2, out edge2);
- Mat threshEdge2 = new Mat();
- threshEdge2 = edge2.Threshold(20, 255, ThresholdTypes.Binary);
- //Mat fill = new Mat();
- //Fill(threshEdge2, out fill, 255);
- Mat threshEdge3 = threshEdge2.Clone();
- Cv2.Rectangle(threshEdge3, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
- Cv2.Rectangle(threshEdge3, new Rect(0, tonghouY[1] + 20, threshEdge.Cols, threshEdge.Rows - tonghouY[1] - 20), new Scalar(0), -1);
- //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 3));
- //Mat close2 = new Mat();
- //Cv2.MorphologyEx(threshEdge3, close2, MorphTypes.Close, seClose2);
- Mat noMin2 = new Mat();
- GetArea(threshEdge3, out noMin2, 2000, true);
- //ImageShow(edge2, threshEdge2, noMin2 * 255);
- Mat result2 = noMin2.Clone();
- Scalar sum = new Scalar(0);
- for (int i = tonghouY[1] + 20; i > tonghouY[1] - 20; i--)
- {
- sum = result2[i - 1, i, LPIHouduX - 50, LPIHouduX + 50].Sum();
- if ((int)sum > 80)
- //if (result2.Get<byte>(i, LPIHouduX) > 0)
- {
- LPIHouduY[1] = i;
- break;
- }
- }
- int compensate1 = 5;
- LPIHouduY[0] -= compensate1;
- int compensate2 = 5;
- LPIHouduY[1] -= compensate2;
- }
- /// <summary>
- /// 計算防焊的undercut
- /// </summary>
- /// <param name="imageRed">原圖紅色通道圖片</param>
- /// <param name="tonghouY">銅厚的上下縱坐標</param>
- /// <param name="b">第一層銅的起始點(橫向),b[0]:銅起始點;b[1]:銅結束點;b[2]:銅再次開始點</param>
- /// <param name="undercutX">輸出防焊同第三条线的交点之间的水平距离</param>
- public void FanghanUndercut(Mat imageRed, int[] tonghouY, int[] b, int[] offsetX, out int undercutX)
- {
- int compensate = 20;
- undercutX = 0;
- Mat crop = imageRed[tonghouY[0] - 100, tonghouY[1] + 20, b[1] + 20, offsetX[0]];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(crop, thresh, 0, 1, ThresholdTypes.Otsu);
- thresh = 1 - crop.Threshold(t - 30, 1, ThresholdTypes.Otsu);
- Cv2.Rectangle(thresh, new Rect(0, 0, 20, thresh.Rows - 1), new Scalar(1), -1);
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat close = new Mat();
- Cv2.MorphologyEx(thresh, close, MorphTypes.Close, se);
- Mat fill = new Mat();
- Fill(close, out fill, 1);
- Mat result = fill;
- GetMaxContour(result, out result);
- //ImageShow(result * 255);
- Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 5));
- Mat dilate = new Mat();
- Cv2.Dilate(result, dilate, se2);
- //ImageShow(close * 255, dilate * 255);
- result = dilate;
- Scalar sum = new Scalar(0);
- Scalar max = new Scalar(0);
- int upper = 0;
- for (int i = 120; i < crop.Rows - 10; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- upper = i;
- }
- }
- for (int j = result.Cols - 20; j > 20; j--)
- {
- if (result.Get<byte>(upper, j) > 0)
- {
- undercutX = j + b[1] + 20 - compensate;
- break;
- }
- }
- }
- /// <summary>
- /// 计算防焊没开口undercut
- /// </summary>
- /// <param name="image">原图</param>
- /// <param name="tonghouY">铜厚纵坐标</param>
- /// <param name="b">铜的边界</param>
- /// <param name="offsetX"></param>
- /// <param name="undercutX">输出undercut位置</param>
- public void FanghanUndercut2(Mat image, int[] tonghouY, int[] b, int[] offsetX, out int undercutX)
- {
- /*
- * 利用canny边缘检测,闭运算连接后,填充,保留大面积,取反,关键部位每列和为1时记录
- */
- undercutX = 0;
- Cv2.GaussianBlur(image, image, new Size(7, 7), 3, 3);
- Mat edge = new Mat();
- Cv2.Canny(image, edge, 10, 15);
- //ceju.ImageShow(edge);
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Mat close = new Mat();
- Cv2.MorphologyEx(edge, close, MorphTypes.Close, se);
- Mat fill = new Mat();
- Fill(close, out fill, 255);
- //ImageShow(edge, close, fill);
- Mat draw = new Mat();
- GetArea(close, out draw, 10000, true);
- //ImageShow(draw * 255);
- Mat result = 1 - draw;
- Mat crop = result[tonghouY[0] + 20, tonghouY[1] + 10, b[1] + 20, b[2]].Clone();
- GetMaxArea(crop, out crop);
- //ImageShow(crop * 255);
- Scalar sum = new Scalar(0);
- for (int j = 0; j < crop.Cols; j++)
- {
- sum = crop[0, crop.Rows, j, j + 1].Sum();
- if ((int)sum > 0)
- {
- undercutX = j + b[1] + 20;
- break;
- }
- }
- int compensate = 25;
- undercutX -= compensate;
- }
- public void FanghanUndercut3(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] fanghanhouY, out int undercutX)
- {
- undercutX = 0;
- Mat filter = new Mat();
- PointEnhancement(gray, out filter);
- Mat newGray = new Mat();
- Cv2.GaussianBlur(filter, newGray, new Size(11, 11), 5, 5);
- Mat edge = new Mat();
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(20, 255, ThresholdTypes.Binary);
- Mat fill = new Mat();
- Fill(threshEdge, out fill, 255);
- Cv2.Rectangle(fill, new Rect(0, 0, threshEdge.Cols, fanghanhouY[0]), new Scalar(0), -1);
- //Cv2.Rectangle(threshEdge, new Rect(0, 0, b[1], tonghouY[1]), new Scalar(255), -1);
- Cv2.Rectangle(fill, new Rect(0, tonghouY[1] + 20, threshEdge.Cols, threshEdge.Rows - tonghouY[1] - 20), new Scalar(0), -1);
- //Cv2.Rectangle(threshEdge, new Rect(offsetX[0], 0, threshEdge.Cols-offsetX[0], threshEdge.Rows), new Scalar(0), -1);
- //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- //Mat close = new Mat();
- //Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
- Mat max = new Mat();
- //GetMaxArea(close, out max);
- GetArea(fill, out max, 2000, true);
- Mat fanse = 1 - max;
- ImageShow(threshEdge, fill, max * 255, fanse * 255);
- Mat result = fanse.Clone();
- Scalar sum = new Scalar(0);
- int lowerBound = 0, upperBound = 0;
- int leftBound = (offsetX[0] - 130 > b[1] + 20) ? offsetX[0] - 130 : b[1] + 20;
- for (int i = tonghouY[0]; i < tonghouY[1] + 20; i++)
- {
- sum = result[i, i + 1, leftBound, offsetX[0] - 20].Sum();
- if ((int)sum == 0)
- {
- upperBound = i;
- break;
- }
- }
- for (int i = tonghouY[1] + 20; i > tonghouY[0]; i--)
- {
- sum = result[i - 1, i, leftBound, offsetX[0] - 20].Sum();
- if ((int)sum == 0)
- {
- lowerBound = i;
- break;
- }
- }
- //Cv2.Line(fanse, new Point(leftBound,upperBound), new Point(leftBound,lowerBound), new Scalar(1), 2, LineTypes.Link8);
- //ImageShow(fanse*255);
- for (int j = leftBound; j < offsetX[0]; j++)
- {
- sum = result[upperBound, lowerBound, j, j + 1].Sum();
- if ((int)sum > 0)
- {
- undercutX = j;
- break;
- }
- }
- int compensate = 30;
- undercutX -= compensate;
- }
- public void FanghanUndercut4(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX)
- {
- undercutX = 0;
- Mat newGray = new Mat();
- Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
- Mat edge = new Mat();
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(30, 255, ThresholdTypes.Binary);
- Mat threshEdge1 = threshEdge.Clone();
- Cv2.Rectangle(threshEdge1, new Rect(0, 0, b[1] + 20, threshEdge1.Rows), new Scalar(255), -1);
- Mat noMin = new Mat();
- GetArea(threshEdge1, out noMin, 50000, true);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Mat close = new Mat();
- Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
- //ImageShow(threshEdge1, noMin * 255,close*255);
- int[] x1 = new int[10];
- int[] y1 = new int[10];
- int count = 0;
- for (int i = tonghouY[0] + 20; i < tonghouY[0] + 30; i++)
- {
- for (int j = offsetX[0] + 30; j > offsetX[0] - 100; j--)
- {
- if (close.Get<byte>(i, j) > 0)
- {
- x1[count] = j;
- y1[count] = i;
- count++;
- break;
- }
- }
- if (count == 10)
- break;
- }
- int[] x2 = new int[10];
- int[] y2 = new int[10];
- count = 0;
- for (int i = tonghouY[0] + 30; i < tonghouY[0] + 40; i++)
- {
- for (int j = offsetX[0] + 30; j > offsetX[0] - 100; j--)
- {
- if (close.Get<byte>(i, j) > 0)
- {
- x2[count] = j;
- y2[count] = i;
- count++;
- break;
- }
- }
- if (count == 10)
- break;
- }
- double[] averOrdinate1 = new double[2];
- double[] averOrdinate2 = new double[2];
- AverOrdinate(x1, y1, out averOrdinate1);
- AverOrdinate(x2, y2, out averOrdinate2);
- LineShow(gray, (int)averOrdinate1[0], (int)averOrdinate1[1], (int)averOrdinate2[0], (int)averOrdinate2[1]);
- //ImageShow(gray);
- double slope = ((averOrdinate2[0] - averOrdinate1[0]) == 0) ? 0 : (averOrdinate2[1] - averOrdinate1[1]) / (averOrdinate2[0] - averOrdinate1[0]);
- double intercept = averOrdinate1[1] - slope * averOrdinate1[0];
- undercutX = (slope == 0) ? 0 : (int)((LPIHouY[1] - intercept) / slope);
- int compensate = 10;
- undercutX -= compensate;
- }
- /// <summary>
- /// 提取undercut,由于有开口亮度和对比对低,参数不同。
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="offsetX"></param>
- /// <param name="LPIHouY"></param>
- /// <param name="undercutX"></param>
- public void FanghanUndercut5(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX)
- {
- undercutX = 0;
- Mat newGray = new Mat();
- Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
- Mat edge = new Mat();
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
- Mat threshEdge1 = threshEdge.Clone();
- Cv2.Rectangle(threshEdge1, new Rect(0, 0, b[1] + 20, threshEdge1.Rows), new Scalar(255), -1);
- Mat noMin = new Mat();
- GetArea(threshEdge1, out noMin, 2000, true);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Mat close = new Mat();
- Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
- //ImageShow(threshEdge1, noMin * 255, close * 255);
- #region//获取平均坐标
- int[] x1 = new int[10];
- int[] y1 = new int[10];
- int count = 0;
- for (int i = tonghouY[0] - 20; i < tonghouY[0] - 10; i++)
- {
- for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
- {
- if (close.Get<byte>(i, j) > 0)
- {
- x1[count] = j;
- y1[count] = i;
- count++;
- break;
- }
- }
- if (count == 10)
- break;
- }
- int[] x2 = new int[10];
- int[] y2 = new int[10];
- count = 0;
- for (int i = tonghouY[0]; i < tonghouY[0] + 10; i++)
- {
- for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
- {
- if (close.Get<byte>(i, j) > 0)
- {
- x2[count] = j;
- y2[count] = i;
- count++;
- break;
- }
- }
- if (count == 10)
- break;
- }
- double[] averOrdinate1 = new double[2];
- double[] averOrdinate2 = new double[2];
- AverOrdinate(x1, y1, out averOrdinate1);
- AverOrdinate(x2, y2, out averOrdinate2);
- #endregion
- //LineShow(gray, (int)averOrdinate1[0], (int)averOrdinate1[1], (int)averOrdinate2[0], (int)averOrdinate2[1]);
- //ImageShow(gray,close*255);
- double slope = ((averOrdinate2[0] - averOrdinate1[0]) == 0) ? 0 : (averOrdinate2[1] - averOrdinate1[1]) / (averOrdinate2[0] - averOrdinate1[0]);
- double intercept = averOrdinate1[1] - slope * averOrdinate1[0];
- undercutX = (slope == 0) ? 0 : (int)((LPIHouY[1] - intercept) / slope);
- int compensate = 10;
- undercutX -= compensate;
- }
- public void FanghanUndercut5_2(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX, int i=0)
- {
- undercutX = 0;
- Mat filter = new Mat();
- PointEnhancement(gray, out filter);
- Mat newGray = new Mat();
- Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
- Mat threshEdge = newGray.Threshold(BinaryTools.CalcSuitableValueForUnderCut(gray) - 15 + (i * 5), 255, ThresholdTypes.BinaryInv);
- Mat result = threshEdge.Canny(0, 255);
- int tempRange = (offsetX[0] - 300) <= 0 ? 1 : offsetX[0] - 300;
- //想左找
- for (int j = offsetX[0] + 10; j > tempRange; j--)
- {
- byte v = result.Get<byte>(tonghouY[1] - 20, j);
- if (v > 0)
- {
- if(Cv2.FloodFill(result, new Point(j, tonghouY[0] - 20), new Scalar(255)) > 300)
- {
- undercutX = Tools.GetLeftPoint(new Point(j, tonghouY[1] - 20), result).X;
- break;
- }
- }
- }
- if (i > 10)
- undercutX = offsetX[0] - 25;
- else
- {
- if (undercutX == 0 || (undercutX > 0 && offsetX[0] - undercutX > 100))
- {
- FanghanUndercut5_2(gray, tonghouY, b, offsetX, LPIHouY, out undercutX, ++i);
- }
- }
- }
- /// <summary>
- /// 防焊双层铜undercut,采集点位置不同
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="offsetX"></param>
- /// <param name="LPIHouY"></param>
- /// <param name="undercutX"></param>
- public void FanghanUndercut6(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX)
- {
- undercutX = 0;
- Mat newGray = new Mat();
- Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
- Mat edge = new Mat();
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
- Mat threshEdge1 = threshEdge.Clone();
- Cv2.Rectangle(threshEdge1, new Rect(0, 0, b[1] + 20, threshEdge1.Rows), new Scalar(255), -1);
- Mat noMin = new Mat();
- GetArea(threshEdge1, out noMin, 2000, true);
- //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- //Mat close = new Mat();
- //Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
- Mat result = noMin.Clone();
- //ImageShow(threshEdge1, noMin * 255/*, close * 255*/);
- int[] x1 = new int[10];
- int[] y1 = new int[10];
- int count = 0;
- for (int i = tonghouY[0] - 10; i < tonghouY[0]; i += 1)
- {
- for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
- {
- if (result.Get<byte>(i, j) > 0)
- {
- x1[count] = j;
- y1[count] = i;
- count++;
- break;
- }
- }
- if (count == 10)
- break;
- }
- int[] x2 = new int[10];
- int[] y2 = new int[10];
- count = 0;
- for (int i = tonghouY[0] + 10; i < tonghouY[0] + 20; i += 1)
- {
- for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
- {
- if (result.Get<byte>(i, j) > 0)
- {
- x2[count] = j;
- y2[count] = i;
- count++;
- break;
- }
- }
- if (count == 10)
- break;
- }
- double[] averOrdinate1 = new double[2];
- double[] averOrdinate2 = new double[2];
- AverOrdinate(x1, y1, out averOrdinate1);
- AverOrdinate(x2, y2, out averOrdinate2);
- //LineShow(gray, (int)averOrdinate1[0], (int)averOrdinate1[1], (int)averOrdinate2[0], (int)averOrdinate2[1]);
- //ImageShow(gray);
- double slope = ((averOrdinate2[0] - averOrdinate1[0]) == 0) ? 0 : (averOrdinate2[1] - averOrdinate1[1]) / (averOrdinate2[0] - averOrdinate1[0]);
- double intercept = averOrdinate1[1] - slope * averOrdinate1[0];
- undercutX = (slope == 0) ? 0 : (int)((LPIHouY[1] - intercept) / slope);
- int compensate = 10;
- undercutX -= compensate;
- }
- /// <summary>
- /// 705改,提取undercut
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tonghouY"></param>
- /// <param name="b"></param>
- /// <param name="offsetX"></param>
- /// <param name="LPIHouY"></param>
- /// <param name="undercutX"></param>
- public void FanghanUndercut6_2(Mat gray, int[] tonghouY, int[] b, int[] offsetX, int[] LPIHouY, out int undercutX)
- {
- undercutX = 0;
- Mat filter = new Mat();
- PointEnhancement(gray, out filter);
- Mat newGray = new Mat();
- Cv2.GaussianBlur(filter, newGray, new Size(15, 15), 5, 5);
- //PointEnhancement(gray, out gray);
- //Mat crop = gray[tonghouY[0] - 120, tonghouY[0] - 20, LPIHouduX - 10, LPIHouduX + 10];
- Mat edge = new Mat();
- //Sobel(gray, out edge);
- Edge(newGray, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(25, 255, ThresholdTypes.Binary);
- //Cv2.Rectangle(threshEdge, new Rect(0, tonghouY[0]-100, b[1], tonghouY[1]-tonghouY[0]+100), new Scalar(255), -1);
- Mat noCircle = new Mat();
- RemoveCircles(threshEdge, out noCircle);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Mat close = new Mat();
- Cv2.MorphologyEx(noCircle, close, MorphTypes.Close, seClose);
- //Mat noMin = new Mat();
- //GetArea(threshEdge1, out noMin, 2000, true);
- //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- //Mat close = new Mat();
- //Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
- Mat result = close.Clone();
- //ImageShow(threshEdge, noCircle * 255, close * 255);
- int[] x1 = new int[10];
- int[] y1 = new int[10];
- int count = 0;
- for (int i = tonghouY[0] - 10; i < tonghouY[0]; i += 1)
- {
- for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
- {
- if (result.Get<byte>(i, j) > 0)
- {
- x1[count] = j;
- y1[count] = i;
- count++;
- break;
- }
- }
- if (count == 10)
- break;
- }
- int[] x2 = new int[10];
- int[] y2 = new int[10];
- count = 0;
- for (int i = tonghouY[0] + 10; i < tonghouY[0] + 20; i += 1)
- {
- for (int j = offsetX[0] + 20; j > offsetX[0] - 100; j--)
- {
- if (result.Get<byte>(i, j) > 0)
- {
- x2[count] = j;
- y2[count] = i;
- count++;
- break;
- }
- }
- if (count == 10)
- break;
- }
- double[] averOrdinate1 = new double[2];
- double[] averOrdinate2 = new double[2];
- AverOrdinate(x1, y1, out averOrdinate1);
- AverOrdinate(x2, y2, out averOrdinate2);
- //LineShow(gray, (int)averOrdinate1[0], (int)averOrdinate1[1], (int)averOrdinate2[0], (int)averOrdinate2[1]);
- //ImageShow(gray);
- double slope = ((averOrdinate2[0] - averOrdinate1[0]) == 0) ? 0 : (averOrdinate2[1] - averOrdinate1[1]) / (averOrdinate2[0] - averOrdinate1[0]);
- double intercept = averOrdinate1[1] - slope * averOrdinate1[0];
- undercutX = (slope == 0) ? 0 : (int)((LPIHouY[1] - intercept) / slope);
- int compensate = 10;
- undercutX -= compensate;
- }
-
- //高度差
- /// <summary>
- /// 計算含非零點數最大列
- /// </summary>
- /// <param name="image"></param>
- /// <param name="maxCol"></param>
- public void MaxCol(Mat image, out int maxCol)
- {
- maxCol = 0;
- Scalar sum = new Scalar(0);
- Scalar max = new Scalar(0);
- for (int j = 0; j < image.Cols; j++)
- {
- sum = image[0, image.Rows, j, j + 1].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- maxCol = j;
- }
- }
- }
- /// <summary>
- /// 得到高度差左右的测量区域
- /// </summary>
- /// <param name="thresh">二值图</param>
- /// <param name="BX">高度差B横坐标</param>
- /// <param name="B_X">高度差B*横坐标</param>
- /// <param name="direction">方向,“left”;"right"</param>
- public void GaoduchaGetDataArea(Mat thresh, out Mat result, out int BX, out int B_X, string direction)
- {
- BX = 0;
- B_X = 0;
- GetMaxArea(thresh, out thresh);
- int[] b = new int[3];
- MaxCol(thresh, out b[1]);
- //ceju.ImageShow(thresh * 255);
- double ordinate1 = 0;
- double ordinate2 = 0;
- switch (direction)
- {
- case "left":
- ExtractLines(thresh, out ordinate1, b[1] - 500, b[1] - 400, 1);
- ExtractLines(thresh, out ordinate2, b[1] - 500, b[1] - 400, ordinate1, -1);
- break;
- case "right":
- ExtractLines(thresh, out ordinate1, b[1] + 400, b[1] + 500, 1);
- ExtractLines(thresh, out ordinate2, b[1] + 400, b[1] + 500, ordinate1, -1);
- break;
- }
- int[] firstAndTwo = new int[] { (int)ordinate1, (int)ordinate2 };
- //int border = 0;
- //switch (direction)
- //{
- // case "left":
- // for (int j = b[1]; j>0;j--)
- // {
- // if ((int)thresh[(int)ordinate1, (int)ordinate2, j - 1, j].Sum() == 0)
- // {
- // border = j;
- // break;
- // }
- // }
- // break;
- // case "right":
- // for (int j = b[1]; j < thresh.Cols; j++)
- // {
- // if ((int)thresh[(int)ordinate1, (int)ordinate2, j - 1, j].Sum() == 0)
- // {
- // border = j;
- // break;
- // }
- // }
- // break;
- //}
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat open = new Mat();
- Cv2.MorphologyEx(thresh, open, MorphTypes.Open, se);
- result = open.Clone();
- //ceju.ImageShow(open * 255);
- Scalar sum = new Scalar(0);
- Scalar lastSum = new Scalar(0);
- switch (direction)
- {
- case "left":
- for (int j = b[1] - 600; j > 0; j -= 5)
- {
- sum = open[0, (int)ordinate2, j - 10, j].Sum();
- if (((int)lastSum - (int)sum) > 60 && (int)lastSum != 0)//不用绝对值差防止凸起
- {
- b[0] = j;
- break;
- }
- lastSum = sum;
- }
- if (b[0] == 0)
- {
- lastSum = 0;
- sum = 0;
- for (int j = b[1] - 600; j > 0; j -= 5)
- {
- sum = open[0, (int)ordinate2, j - 10, j].Sum();
- if ((int)lastSum - (int)sum > 20 && (int)lastSum != 0)
- {
- b[0] = j;
- break;
- }
- lastSum = sum;
- }
- }
- B_X = b[0] + 15;
- BX = b[0] + 50;
- break;
- case "right":
- for (int j = b[1] + 500; j < open.Cols; j += 5)
- {
- sum = open[0, (int)ordinate2, j - 10, j].Sum();
- if (Math.Abs((int)sum - (int)lastSum) > 50/*80*/ && (int)lastSum != 0
- && Math.Abs((int)lastSum - (int)(open[0, (int)ordinate2, j + 0, j + 10].Sum())) > 50
- && (int)sum - (int)(open[0, (int)ordinate2, j + 10, j + 20].Sum()) > 30)
- {
- b[2] = j;
- break;
- }
- lastSum = sum;
- }
- B_X = b[2] - 15;
- BX = b[2] - 50;
- break;
- }
- }
- /// <summary>
- /// 计算高度差左右的测量线的纵坐标
- /// </summary>
- /// <param name="image">测量区域时的结果图</param>
- /// <param name="imageRed">红色通道图</param>
- /// <param name="BY">高度差B纵坐标坐标</param>
- /// <param name="B_Y">高度差B*纵坐标坐标</param>
- /// <param name="BX">高度差B横坐标</param>
- /// <param name="B_X">高度差B*横坐标</param>
- public void GaoduchaB(Mat image, Mat imageRed, out int[] BY, out int[] B_Y, int BX, int B_X)
- {
- BY = new int[2];
- B_Y = new int[2];
- for (int i = 0; i < image.Rows; i++)
- {
- if (image.Get<byte>(i, B_X) > 0)
- {
- B_Y[1] = i;
- break;
- }
- }
- for (int i = 0; i < image.Rows; i++)
- {
- if (image.Get<byte>(i, BX) > 0)
- {
- BY[1] = i;
- break;
- }
- }
- //Cv2.EqualizeHist(imageRed, imageRed);
- Mat crop = imageRed[BY[1] - 60, BY[1] - 10, B_X - 5, BX + 5];
- Mat thresh2 = 1 - crop.Threshold(0, 1, ThresholdTypes.Otsu);
- //Mat thresh2 = new Mat();
- //double t2 = Cv2.Threshold(crop, thresh2, 0, 1, ThresholdTypes.Otsu);
- //thresh = 1 - crop.Threshold(t + 10, 1, ThresholdTypes.Binary);
- Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Cv2.MorphologyEx(thresh2, thresh2, MorphTypes.Close, se2);
- GetMaxArea(thresh2, out thresh2);
- //ceju.ImageShow(thresh2 * 255);
- for (int i = 0; i < thresh2.Rows; i++)
- //for (int i = thresh2.Rows-5; i >0;i--)
- {
- if (thresh2.Get<byte>(i, 5) > 0)
- {
- B_Y[0] = i + BY[1] - 60 + 5;
- break;
- }
- }
- for (int i = 0; i < thresh2.Rows; i++)
- //for (int i = thresh2.Rows - 5; i > 0; i--)
- {
- if (thresh2.Get<byte>(i, thresh2.Cols - 5) > 0)
- {
- BY[0] = i + BY[1] - 60 + 5;
- break;
- }
- }
- }
- /// <summary>
- /// 计算高度差纵坐标
- /// </summary>
- /// <param name="thresh"></param>
- /// <param name="crop"></param>
- /// <param name="BY"></param>
- /// <param name="B_Y"></param>
- /// <param name="BX"></param>
- /// <param name="B_X"></param>
- public void GaoduchaBOrdinateY(Mat thresh, Mat image, out int[] BY, out int[] B_Y, int BX, int B_X)
- {
- BY = new int[2];
- B_Y = new int[2];
- //计算下纵坐标
- for (int i = 0; i < thresh.Rows; i++)
- {
- if (thresh.Get<byte>(i, B_X) > 0)
- {
- B_Y[1] = i;
- break;
- }
- }
- for (int i = 0; i < thresh.Rows; i++)
- {
- if (thresh.Get<byte>(i, BX) > 0)
- {
- BY[1] = i;
- break;
- }
- }
- //边缘检测
- Mat edge = new Mat();
- //PointEnhancement(image, out image);
- Cv2.GaussianBlur(image, image, new Size(9, 9), 5, 5);
- Cv2.Canny(image, edge, 15, 0);
- //Mat crop = edge[B_Y[1]-60, edge.Rows, B_X - 5, BX + 5].Clone();
- //Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- //Mat open = new Mat();
- //Cv2.MorphologyEx(edge, open, MorphTypes.Open, seOpen);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 3));
- Mat close = new Mat();
- Cv2.MorphologyEx(edge, close, MorphTypes.Close, seClose);
- //Cv2.MorphologyEx(close, close, MorphTypes.Close, seClose);
- //Cv2.Rectangle(close, new Rect(B_X-5, B_Y[1]-60, 1, close.Rows-(B_Y[1]-60)), new Scalar(255), -1);
- //Cv2.Rectangle(close, new Rect(BX+5, B_Y[1] - 60, 1, close.Rows - (B_Y[1] - 60)), new Scalar(255), -1);
- Mat fill = new Mat();
- Fill(close, out fill, 255);
- Mat maxFill = new Mat();
- //Cv2.Rectangle(fill, new Rect(B_X - 5, B_Y[0] - 60, 1, close.Rows - (B_Y[0] - 60)), new Scalar(0), -1);
- //Cv2.Rectangle(fill, new Rect(BX + 5, B_Y[0] - 60, 1, close.Rows - (B_Y[0] - 60)), new Scalar(0), -1);
- //GetMaxArea(fill, out maxFill);
- GetArea(fill, out maxFill, 150, true);
- //ImageShow(edge, close, fill, maxFill * 255);
- //计算上纵坐标
- for (int i = B_Y[1] - 10; i > 0; i--)
- {
- if (maxFill.Get<byte>(i, B_X) > 0)
- {
- B_Y[0] = i;
- break;
- }
- }
- for (int i = BY[1] - 10; i > 0; i--)
- {
- if (maxFill.Get<byte>(i, BX) > 0)
- {
- BY[0] = i;
- break;
- }
- }
- int compensate = 5;
- BY[0] -= compensate;
- B_Y[0] -= compensate;
- }
- public void GaoduchaBOrdinateY2(Mat thresh, Mat imageRed, Mat image, out int[] BY, out int[] B_Y, int BX, int B_X, string direction)
- {
- BY = new int[2];
- B_Y = new int[2];
- //计算下纵坐标
- for (int i = 0; i < thresh.Rows; i++)
- {
- if (thresh.Get<byte>(i, B_X) > 0)
- {
- B_Y[1] = i;
- break;
- }
- }
- for (int i = 0; i < thresh.Rows; i++)
- {
- if (thresh.Get<byte>(i, BX) > 0)
- {
- BY[1] = i;
- break;
- }
- }
- #region//法一
- //边缘检测
- Mat sobelEdge = new Mat();
- //PointEnhancement(image, out image);
- Cv2.GaussianBlur(imageRed, imageRed, new Size(9, 9), 3, 3);
- PointEnhancement(imageRed, out imageRed);
- Sobel(imageRed, out sobelEdge);
- Mat threshEdge = new Mat();
- double t = Cv2.Threshold(sobelEdge, threshEdge, 0, 255, ThresholdTypes.Otsu);
- if (t < 50)
- threshEdge = sobelEdge.Threshold(10, 255, ThresholdTypes.Binary);
- //Mat cannyEdge = new Mat();
- //Cv2.GaussianBlur(image, image, new Size(9, 9), 5, 5);
- //Cv2.Canny(image, cannyEdge, 15, 0);
- //Mat edge = cannyEdge/2 + threshEdge / 2;
- //Cv2.ConvertScaleAbs(edge, edge);
- //横向连接
- Mat text2 = new Mat();
- switch (direction)
- {
- case "left":
- text2 = threshEdge[B_Y[1] - 40, B_Y[1] - 10, B_X, BX] / 255;
- break;
- case "right":
- text2 = threshEdge[B_Y[1] - 40, B_Y[1] - 10, BX, B_X] / 255;
- break;
- }
- Mat seClose1 = new Mat();
- if ((int)text2.Sum() < text2.Rows * text2.Cols / 2)
- {
- //Mat seClose1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- seClose1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
- }
- else
- {
- seClose1 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- }
- Mat close1 = new Mat();
- Cv2.MorphologyEx(threshEdge, close1, MorphTypes.Close, seClose1);
- //上方置零
- Cv2.Rectangle(close1, new Rect(0, 0, close1.Cols, BY[1] - 60), new Scalar(0), -1);
- //先将小点去掉
- Mat noMin = new Mat();
- GetArea(close1, out noMin, 10, true);
- //闭运算后得到最大连通域
- //如果边缘不明显
- Mat text = new Mat();
- switch (direction)
- {
- case "left":
- text = noMin[B_Y[1] - 40, B_Y[1] - 10, B_X, BX];
- break;
- case "right":
- text = noMin[B_Y[1] - 40, B_Y[1] - 10, BX, B_X];
- break;
- }
- Mat result = new Mat();
- Mat max = new Mat();
- if ((int)text.Sum() < (text.Rows * text.Cols) / 3)
- {
- result = noMin.Clone();
- }
- else if ((int)text.Sum() < (text.Rows * text.Cols) / 2)
- {
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Mat close = new Mat();
- Cv2.MorphologyEx(noMin, close, MorphTypes.Close, se);
- GetMaxArea(close, out max);
- result = max.Clone();
- }
- else
- {
- Cv2.Rectangle(noMin, new Rect(B_X - 20, B_Y[1] - 40, 10, 60), new Scalar(1), -1);
- GetMaxArea(noMin, out max);
- result = max.Clone();
- }
- #endregion
- //ImageShow(close1, noMin * 255, result * 255);
- //计算上纵坐标
- for (int i = B_Y[1] - 60; i < result.Rows; i++)
- {
- if (result.Get<byte>(i, B_X) > 0)
- {
- B_Y[0] = i;
- break;
- }
- }
- for (int i = BY[1] - 60; i < result.Rows; i++)
- {
- if (result.Get<byte>(i, BX) > 0)
- {
- BY[0] = i;
- break;
- }
- }
- int compensate = 5;
- BY[0] += compensate;
- B_Y[0] += compensate;
- }
- public void GaoduchaBOrdinateY3(Mat thresh, Mat image, out int[] BY, out int[] B_Y, int BX, int B_X, string direction)
- {
- BY = new int[2];
- B_Y = new int[2];
- #region//计算下纵坐标
- for (int i = 0; i < thresh.Rows; i++)
- {
- if (thresh.Get<byte>(i, B_X) > 0)
- {
- B_Y[1] = i;
- break;
- }
- }
- for (int i = 0; i < thresh.Rows; i++)
- {
- if (thresh.Get<byte>(i, BX) > 0)
- {
- BY[1] = i;
- break;
- }
- }
- #endregion
- #region//图像处理
- //阈值分割,保留最大连通域
- Mat crop = new Mat();
- switch (direction)
- {
- case "left":
- crop = image[BY[1] - 60, BY[1] - 10, B_X - 10, BX + 10].Clone();
- Cv2.Rectangle(image, new Rect(B_X - 10, BY[1] - 60, BX + 10 - (B_X - 10), 50), new Scalar(255), 2);
- break;
- case "right":
- crop = image[BY[1] - 60, BY[1] - 10, BX - 10, B_X + 10].Clone();
- break;
- }
- Mat thresh2 = new Mat();
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\image.png", image * 1);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\crop.png", crop * 1);
- double t = Cv2.Threshold(crop, thresh2, 0, 1, ThresholdTypes.Otsu);
- Mat fanse = 1 - thresh2;
- Mat nomin = fanse.Clone();// new Mat();
- //GetMaxArea(fanse, out nomin);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
- Mat open = new Mat();
- Cv2.MorphologyEx(nomin, open, MorphTypes.Open, seOpen);
- //ImageShow(fanse * 255, nomin * 255, open * 255,crop+open*100);
- Mat result = new Mat();// open.Clone();//
- Fill(open, out result, 1);
- Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 255);
- #endregion
- #region//计算上纵坐标
- int[] lower = new int[2];
- int[] upper = new int[2];
- int compensate = 7;
- Scalar sum = new Scalar(0);
- int upper_temp = 0;
- int upper_max = 0;
- switch (direction)
- {
- case "left":
- //B*
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 1, 0, 20].Sum();
- if ((int)sum > 10)
- {
- if (upper_temp == 0)
- {
- if (upper[0] == 0 || upper_max < 13)
- upper_temp = i;
- else if ((int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols - 20)
- upper_temp = i;
- }
- if ((int)sum > upper_max/*13*/)
- upper_max = (int)sum;// break;//避免找到内部的干扰点
- }
- else if (upper_temp > 0)
- {
- upper[0] = upper_temp;
- upper_temp = 0;
- }
- }
- if (upper_temp > 0)
- {
- upper[0] = upper_temp;
- upper_temp = 0;
- }
- upper_max = 0;
- for (int i = result.Rows - 1; i > 1; i--)
- {
- sum = result[i - 1, i, 0, 20].Sum();
- if ((int)sum > 10)
- {
- lower[0] = i;
- break;
- }
- }
- //B
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 1, result.Cols - 20, result.Cols].Sum();
- if ((int)sum > 10)
- {
- if (upper_temp == 0)
- {
- if(upper[1] == 0 || upper_max < 13)
- upper_temp = i;
- else if ((int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols - 20)
- upper_temp = i;
- }
- if ((int)sum > upper_max/*13*/)
- upper_max = (int)sum;// break;//避免找到内部的干扰点
- }
- else if (upper_temp > 0)
- {
- upper[1] = upper_temp;
- upper_temp = 0;
- }
- }
- if (upper_temp > 0)
- {
- upper[1] = upper_temp;
- upper_temp = 0;
- }
- upper_max = 0;
- for (int i = result.Rows - 1; i > 1; i--)
- {
- sum = result[i - 1, i, result.Cols - 20, result.Cols].Sum();
- if ((int)sum > 10)
- {
- lower[1] = i;
- break;
- }
- }
- if (lower[0] - upper[0] > 10)
- {
- B_Y[0] = upper[0] + compensate + BY[1] - 60;
- }
- else
- {
- B_Y[0] = (upper[0] + lower[0]) / 2 + BY[1] - 60;
- }
- if (lower[1] - upper[1] > 10)
- {
- BY[0] = upper[1] + compensate + BY[1] - 60;
- }
- else
- {
- BY[0] = (upper[1] + lower[1]) / 2 + BY[1] - 60;
- }
- break;
- case "right":
- //B
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 1, 0, 20].Sum();
- if ((int)sum >= 10)
- {
- if (upper_temp == 0)
- {
- if (upper[0] == 0 || upper_max < 13)
- upper_temp = i;
- else if ((int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols - 20
- || (int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols / 2)
- upper_temp = i;
- }
- if ((int)sum > upper_max/*13*/)
- upper_max = (int)sum;// break;//避免找到内部的干扰点
- }
- else if (upper_temp > 0 && (int)sum < 7)
- {
- upper[0] = upper_temp;
- upper_temp = 0;
- }
- }
- if (upper_temp > 0)
- {
- upper[0] = upper_temp;
- upper_temp = 0;
- }
- upper_max = 0;
- for (int i = result.Rows - 1; i > 1; i--)
- {
- sum = result[i - 1, i, 0, 20].Sum();
- if ((int)sum > 10)
- {
- lower[0] = i;
- break;
- }
- }
- //B*
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 1, result.Cols - 20, result.Cols].Sum();
- if ((int)sum > 10)
- {
- if (upper_temp == 0)
- {
- if (upper[1] == 0 || upper_max < 13)
- upper_temp = i;
- else if ((int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols - 20
- || (int)(result[i, i + 1, 0, result.Cols - 1].Sum()) > result.Cols / 2)
- upper_temp = i;
- }
- if ((int)sum > upper_max/*13*/)
- upper_max = (int)sum;// break;//避免找到内部的干扰点
- }
- else if (upper_temp > 0)
- {
- upper[1] = upper_temp;
- upper_temp = 0;
- }
- }
- if (upper_temp > 0)
- {
- upper[1] = upper_temp;
- upper_temp = 0;
- }
- upper_max = 0;
- for (int i = result.Rows - 1; i > 1; i--)
- {
- sum = result[i - 1, i, result.Cols - 20, result.Cols].Sum();
- if ((int)sum > 10)
- {
- lower[1] = i;
- break;
- }
- }
- if (lower[0] - upper[0] > 10)
- {
- BY[0] = upper[0] + compensate + BY[1] - 60;
- }
- else
- {
- BY[0] = (upper[0] + lower[0]) / 2 + BY[1] - 60;
- }
- if (lower[1] - upper[1] > 10)
- {
- B_Y[0] = upper[1] + compensate + BY[1] - 60;
- }
- else
- {
- B_Y[0] = (upper[1] + lower[1]) / 2 + BY[1] - 60;
- }
- break;
- }
- #endregion
- }
- /// <summary>
- /// 得到高度差全的提取区域
- /// </summary>
- /// <param name="imageGreen">绿色通道图</param>
- /// <param name="result">输出提取区域图</param>
- /// <param name="dataArea">提取区域</param>
- /// <param name="y">提取区域的上下界</param>
- public void GaoduchaQuanGetDataArea(Mat imageGreen, out Mat result, out int[] dataArea, out int[] y)
- {
- dataArea = new int[4];
- y = new int[2];
- Mat edge = new Mat();
- Sobel(imageGreen, out edge);
- Mat thresh = new Mat();
- thresh = edge.Threshold(0, 255, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat close = new Mat();
- Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(1, 3));
- Mat open = new Mat();
- Cv2.MorphologyEx(close, open, MorphTypes.Open, seOpen);
- Mat seOpen2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Cv2.MorphologyEx(open, open, MorphTypes.Open, seOpen2);
- Mat fill = new Mat();
- Fill(open, out fill, 255);
- fill = fill / 255;
- Mat connect = fill.Clone();
- Scalar sum = new Scalar(0);
- int k = 0;
- bool flag = true;//真是找大于200的,假时找等于0 的
- int border = 0;
- while (k < connect.Cols - 1)
- {
- if (flag)
- {
- for (k = border; k < fill.Cols; k++)
- {
- sum = fill[0, fill.Rows, k, k + 1].Sum();
- if ((int)sum > 200)
- {
- Cv2.Rectangle(connect, new Rect(k, 0, 100, fill.Rows), new Scalar(1), -1);
- border = k + 100;
- flag = false;
- break;
- }
- }
- }
- else
- {
- for (k = border; k < fill.Cols; k++)
- {
- sum = fill[0, fill.Rows, k, k + 1].Sum();
- if ((int)sum == 0)
- {
- border = k;
- flag = true;
- break;
- }
- }
- }
- }
- //Cv2.Rectangle(connect, new Rect(0, 0, fill.Cols, 1), new Scalar(1), -1);
- //Cv2.Rectangle(connect, new Rect(0, fill.Rows-1, fill.Cols, 1), new Scalar(1), -1);
- //保留前二大面积
- Mat[] contours;
- Mat hierachy = new Mat();
- Cv2.FindContours(connect, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- double largest = 0, secondLargest = 0;
- int index1 = 0, index2 = 0;
- for (int i = 0; i < contours.Count(); i++)
- {
- if (Cv2.ContourArea(contours[i]) > largest)
- {
- largest = Cv2.ContourArea(contours[i]);
- index1 = i;
- }
- else if (Cv2.ContourArea(contours[i]) > secondLargest)
- {
- largest = Cv2.ContourArea(contours[i]);
- index2 = i;
- }
- }
- result = Mat.Zeros(connect.Rows, connect.Cols, connect.Type());
- Cv2.DrawContours(result, contours, index1, new Scalar(1));
- Cv2.DrawContours(result, contours, index2, new Scalar(1));
- Mat fill2 = new Mat();
- Fill(result, out fill2, 1);
- Cv2.BitwiseAnd(fill, fill2, result);
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum > 20)
- {
- y[0] = i - 20;
- break;
- }
- }
- for (int i = result.Rows - 1; i > 0; i--)
- {
- sum = result[i - 1, i, 0, result.Cols].Sum();
- if ((int)sum > 20)
- {
- y[1] = i;
- break;
- }
- }
- for (int j = 0; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 0)
- {
- dataArea[0] = j;
- break;
- }
- }
- for (int j = dataArea[0]; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- dataArea[1] = j;
- break;
- }
- }
- for (int j = result.Cols - 1; j > 0; j--)
- {
- sum = result[y[0], y[1], j - 1, j].Sum();
- if ((int)sum > 0)
- {
- dataArea[3] = j;
- break;
- }
- }
- for (int j = dataArea[3]; j > 0; j--)
- {
- sum = result[y[0], y[1], j - 1, j].Sum();
- if ((int)sum == 0)
- {
- dataArea[2] = j;
- break;
- }
- }
- //ImageShow(open, connect * 255, result * 255);
- }
- public void GaoduchaQuanGetDataArea2(Mat imageRed, out Mat result, out int[] dataArea, out int[] y)
- {
- //result = new Mat();
- dataArea = new int[4];
- y = new int[2];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(imageRed, thresh, 0, 1, ThresholdTypes.Otsu);
- if (t > 170)
- thresh = imageRed.Threshold(t + 30, 1, ThresholdTypes.Binary);
- //Mat max = new Mat();
- //GetMaxArea(thresh, out max);
- //ImageShow(thresh * 255);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat close = new Mat();
- Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
- Mat fill = new Mat();
- Fill(close, out fill, 255);
- Mat connect = fill.Clone();
- Scalar sum = new Scalar(0);
- int k = 0;
- bool flag = true;//真是找大于200的,假时找等于0 的
- int border = 0;
- while (k < connect.Cols - 1)
- {
- if (flag)
- {
- for (k = border; k < thresh.Cols; k++)
- {
- sum = thresh[0, thresh.Rows, k, k + 1].Sum();
- if ((int)sum > 200)
- {
- Cv2.Rectangle(connect, new Rect(k, 0, 100, thresh.Rows), new Scalar(1), -1);
- border = k + 100;
- flag = false;
- break;
- }
- }
- }
- else
- {
- for (k = border; k < thresh.Cols; k++)
- {
- sum = thresh[0, thresh.Rows, k, k + 1].Sum();
- if ((int)sum == 0)
- {
- border = k;
- flag = true;
- break;
- }
- }
- }
- }
- //保留前二大面积
- Mat[] contours;
- Mat hierachy = new Mat();
- Cv2.FindContours(connect, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- double largest = 0, secondLargest = 0;
- int index1 = 0, index2 = 0;
- for (int i = 0; i < contours.Count(); i++)
- {
- if (Cv2.ContourArea(contours[i]) > largest)
- {
- largest = Cv2.ContourArea(contours[i]);
- index1 = i;
- }
- else if (Cv2.ContourArea(contours[i]) > secondLargest)
- {
- secondLargest = Cv2.ContourArea(contours[i]);
- index2 = i;
- }
- }
- result = Mat.Zeros(connect.Rows, connect.Cols, connect.Type());
- Cv2.DrawContours(result, contours, index1, new Scalar(1));
- Cv2.DrawContours(result, contours, index2, new Scalar(1));
- Mat fill2 = new Mat();
- Fill(result, out fill2, 1);
- Cv2.BitwiseAnd(fill, fill2, result);
- //ImageShow(connect * 255, result * 255);
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, 0, result.Cols].Sum();
- if ((int)sum > 20)
- {
- y[0] = i - 20;
- break;
- }
- }
- for (int i = result.Rows - 1; i > 0; i--)
- {
- sum = result[i - 1, i, 0, result.Cols].Sum();
- if ((int)sum > 20)
- {
- y[1] = i;
- break;
- }
- }
- for (int j = 0; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum > 0)
- {
- dataArea[0] = j;
- break;
- }
- }
- for (int j = dataArea[0]; j < result.Cols; j++)
- {
- sum = result[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- dataArea[1] = j;
- break;
- }
- }
- for (int j = result.Cols - 1; j > 0; j--)
- {
- sum = result[y[0], y[1], j - 1, j].Sum();
- if ((int)sum > 0)
- {
- dataArea[3] = j;
- break;
- }
- }
- for (int j = dataArea[3]; j > 0; j--)
- {
- sum = result[y[0], y[1], j - 1, j].Sum();
- if ((int)sum == 0)
- {
- dataArea[2] = j;
- break;
- }
- }
- }
- public void GaoduchaQuanGetDataArea3(Mat imageRed, Mat image, out int[] leftTonghou, out int[] rightTonghou, out int[] dataArea, out int[] y)
- {
- dataArea = new int[4];
- y = new int[2];
- leftTonghou = new int[3];
- rightTonghou = new int[3];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(imageRed, thresh, 0, 1, ThresholdTypes.Otsu);
- if (t > 170)
- thresh = imageRed.Threshold(240, 1, ThresholdTypes.Binary);
- //ImageShow(thresh * 255);
- //当还是太亮时,需要判断,使用边缘检测加填充方法
- Mat maxArea = new Mat();
- GetMaxArea(thresh, out maxArea);
- bool flag = false;
- double maxAreaSum = (double)maxArea.Sum();
- if ((int)maxArea.Sum() > 350000)
- {
- Mat gray = new Mat();
- Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
- thresh = gray.Threshold(200, 1, ThresholdTypes.Binary);
- //Mat edge = new Mat();
- //Sobel(imageRed, out edge);
- //Mat edgeThresh = new Mat();
- //edgeThresh = edge.Threshold(50, 1, ThresholdTypes.Binary);
- //Mat edgeMax = new Mat();
- //GetMaxArea(edgeThresh, out edgeMax);
- //flag = true;
- //ImageShow(edgeThresh * 255, edgeMax * 255);
- }
- //ImageShow(thresh * 255);
- Cv2.Rectangle(thresh, new Rect(thresh.Cols - 1, 0, 1, thresh.Rows), new Scalar(1), -1);//当右侧出现非整体铜体时连接
- Cv2.Rectangle(thresh, new Rect(0, 0, 1, thresh.Rows), new Scalar(1), -1);//也可能出现在左侧
- //闭运算
- Mat seClose = new Mat();
- if (t < 170)
- seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 9));
- else
- seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat close = new Mat();
- Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
- Fill(close, out close, 1);
- //ImageShow(close * 255);
- //保留前二大区域
- Mat hierachy = new Mat();
- Mat[] contoursMat;
- Cv2.FindContours(close, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- double max1 = 0, max2 = 0;
- int idx1 = 0, idx2 = 0;
- for (int i = 0; i < contoursMat.Count(); i++)
- {
- if (Cv2.ContourArea(contoursMat[i]) > max1)
- {
- max2 = max1;
- idx2 = idx1;
- max1 = Cv2.ContourArea(contoursMat[i]);
- idx1 = i;
- }
- else if (Cv2.ContourArea(contoursMat[i]) > max2)
- {
- max2 = Cv2.ContourArea(contoursMat[i]);
- idx2 = i;
- }
- }
- Mat qianerArea = new Mat(thresh.Size(), thresh.Type());
- Cv2.DrawContours(qianerArea, contoursMat, idx1, new Scalar(1));
- Cv2.DrawContours(qianerArea, contoursMat, idx2, new Scalar(1));
- Fill(qianerArea, out qianerArea, 1);
- thresh = qianerArea;
- Cv2.Rectangle(thresh, new Rect(thresh.Cols - 1, 0, 1, thresh.Rows), new Scalar(0), -1);//将连接线去掉
- Cv2.Rectangle(thresh, new Rect(0, 0, 1, thresh.Rows), new Scalar(0), -1);//
- //
- int middle = imageRed.Cols / 2;
- //ImageShow(thresh * 255);
- Scalar sum = new Scalar(0);
- Scalar max = new Scalar(0);
- //左右列像素最多的地方,并且提取目标区域
- int leftBorde = 0, rightBorder = 0;
- for (int j = 0; j < middle; j++)
- {
- sum = thresh[0, thresh.Rows, j, j + 1].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- leftBorde = j;
- dataArea[0] = leftBorde;
- }
- }
- max = 0;
- for (int j = thresh.Cols - 1; j > middle; j--)
- {
- sum = thresh[0, thresh.Rows, j - 1, j].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- rightBorder = j;
- dataArea[3] = rightBorder;
- }
- }
- //目标区域的上下界
- for (int i = 0; i < thresh.Rows; i++)
- {
- sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
- if ((int)sum > 0)
- {
- y[0] = i;
- break;
- }
- }
- for (int i = thresh.Rows - 1; i > 0; i--)
- {
- sum = thresh[i - 1, i, 0, thresh.Cols].Sum();
- if ((int)sum > 0)
- {
- y[1] = i;
- break;
- }
- }
- //目标区域中间
- if (flag == false)
- {
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh.png", thresh * 120);
- for (int j = leftBorde; j < thresh.Cols; j++)
- {
- sum = thresh[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- dataArea[1] = j;
- break;
- }
- }
- for (int j = rightBorder - 1; j > middle; j--)
- {
- sum = thresh[y[0], y[1], j - 1, j].Sum();
- if ((int)sum == 0)
- {
- dataArea[2] = j;
- break;
- }
- }
- }
- //else
- //{
- // Scalar lastSum = new Scalar(0);
- // for (int j = middle; j > 100; j-=5)
- // {
- // sum = thresh[y[0], y[1], j - 3, j].Sum();
- // if ((int)sum - (int)lastSum > 100&& lastSum!=0)
- // {
- // dataArea[1] = j;
- // break;
- // }
- // lastSum = sum;
- // }
- // lastSum = 0;
- // for (int j = middle; j <thresh.Cols-100; j+=5)
- // {
- // sum = thresh[y[0], y[1], j , j+5].Sum();
- // if ((int)sum - (int)lastSum > 150 && lastSum != 0)
- // {
- // dataArea[2] = j;
- // break;
- // }
- // lastSum = sum;
- // }
- //}
- //边缘检测并横向腐蚀,去掉小连通域,提取铜厚
- Mat sobel = new Mat();
- Sobel(thresh, out sobel);
- Mat result = sobel.Clone();
- //Mat sobel = new Mat();
- //Cv2.GaussianBlur(imageRed, imageRed, new Size(11, 11), 5, 5);
- //Cv2.MedianBlur(imageRed, imageRed, 5);
- //Sobel(imageRed, out sobel);
- //Mat sobelThresh = new Mat();
- //double t2 = Cv2.Threshold(sobel, sobelThresh, 0, 255, ThresholdTypes.Otsu);
- //sobelThresh = sobel.Threshold(5, 255, ThresholdTypes.Binary);
- //Mat seErode = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(30, 1));
- //Mat erode = new Mat();
- //Cv2.Erode(sobelThresh, erode, seErode);
- //Mat noMinArea = new Mat();
- //GetArea(erode, out noMinArea, 500, true);
- //Mat result = noMinArea.Clone();
- //ImageShow(result * 255);
- leftTonghou[0] = dataArea[0] + 100;
- rightTonghou[0] = dataArea[3] - 100;
- //计算线纵坐标
- //左
- for (int i = y[0]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) > 0)
- {
- leftTonghou[1] = i;
- break;
- }
- }
- for (int i = leftTonghou[1] + 60; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) > 0)
- {
- leftTonghou[2] = i;
- break;
- }
- }
- for (int i = leftTonghou[2]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) == 0)
- {
- leftTonghou[2] = i;
- break;
- }
- }
- //右
- for (int i = y[0]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) > 0)
- {
- rightTonghou[1] = i;
- break;
- }
- }
- for (int i = rightTonghou[1] + 60; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) > 0)
- {
- rightTonghou[2] = i;
- break;
- }
- }
- for (int i = rightTonghou[2]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) == 0)
- {
- rightTonghou[2] = i;
- break;
- }
- }
- if (/*leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 > result.Rows
- || */leftTonghou[2] - rightTonghou[2] > 300 && rightTonghou[2] * 3 > result.Rows)
- {
- if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 > result.Rows)
- leftTonghou[0] = leftTonghou[0] + 400;
- else if (leftTonghou[2] - rightTonghou[2] > 300 && rightTonghou[2] * 3 > result.Rows)
- leftTonghou[0] = leftTonghou[0] + 430;
- //计算线纵坐标
- //左
- for (int i = y[0]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) > 0)
- {
- leftTonghou[1] = i;
- break;
- }
- }
- for (int i = leftTonghou[1] + 60; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) > 0)
- {
- leftTonghou[2] = i;
- break;
- }
- }
- for (int i = leftTonghou[2]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) == 0)
- {
- leftTonghou[2] = i;
- break;
- }
- }
- }
- if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 < result.Rows
- || rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
- {
- if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 < result.Rows)
- rightTonghou[0] = rightTonghou[0] - 400;
- else if (rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
- rightTonghou[0] = rightTonghou[0] - 550;
- //右
- for (int i = y[0]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) > 0)
- {
- rightTonghou[1] = i;
- break;
- }
- }
- for (int i = rightTonghou[1] + 60; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) > 0)
- {
- rightTonghou[2] = i;
- break;
- }
- }
- for (int i = rightTonghou[2]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) == 0)
- {
- rightTonghou[2] = i;
- break;
- }
- }
- if (rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
- rightTonghou[2] = leftTonghou[2];
- }
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 120);
- ////ExtractLines(thresh,out left1,leftBorde+200)
- ////ImageShow(sobelThresh,erode,maxArea*255);
- }
- public void GaoduchaQuanGetDataArea3_Center(Mat imageRed, Mat image, out int[] leftTonghou, out int[] rightTonghou, out int[] dataArea, out int[] y, int[] dataArea0)
- {
- dataArea = new int[4];
- y = new int[2];
- leftTonghou = new int[3];
- rightTonghou = new int[3];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(imageRed, thresh, 0, 1, ThresholdTypes.Otsu);
- if (t > 170)
- thresh = imageRed.Threshold(240, 1, ThresholdTypes.Binary);
- //ImageShow(thresh * 255);
- //当还是太亮时,需要判断,使用边缘检测加填充方法
- Mat maxArea = new Mat();
- GetMaxArea(thresh, out maxArea);
- bool flag = false;
- double maxAreaSum = (double)maxArea.Sum();
- if ((int)maxArea.Sum() > 350000)
- {
- Mat gray = new Mat();
- Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
- thresh = gray.Threshold(200, 1, ThresholdTypes.Binary);
- //Mat edge = new Mat();
- //Sobel(imageRed, out edge);
- //Mat edgeThresh = new Mat();
- //edgeThresh = edge.Threshold(50, 1, ThresholdTypes.Binary);
- //Mat edgeMax = new Mat();
- //GetMaxArea(edgeThresh, out edgeMax);
- //flag = true;
- //ImageShow(edgeThresh * 255, edgeMax * 255);
- }
- //ImageShow(thresh * 255);
- Cv2.Rectangle(thresh, new Rect(thresh.Cols - 1, 0, 1, thresh.Rows), new Scalar(1), -1);//当右侧出现非整体铜体时连接
- Cv2.Rectangle(thresh, new Rect(0, 0, 1, thresh.Rows), new Scalar(1), -1);//也可能出现在左侧
- //闭运算
- Mat seClose = new Mat();
- if (t < 170)
- seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 9));
- else
- seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat close = new Mat();
- Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
- Fill(close, out close, 1);
- //ImageShow(close * 255);
- //保留前二大区域
- Mat hierachy = new Mat();
- Mat[] contoursMat;
- Cv2.FindContours(close, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- double max1 = 0, max2 = 0;
- int idx1 = 0, idx2 = 0;
- for (int i = 0; i < contoursMat.Count(); i++)
- {
- if (Cv2.ContourArea(contoursMat[i]) > max1)
- {
- max2 = max1;
- idx2 = idx1;
- max1 = Cv2.ContourArea(contoursMat[i]);
- idx1 = i;
- }
- else if (Cv2.ContourArea(contoursMat[i]) > max2)
- {
- max2 = Cv2.ContourArea(contoursMat[i]);
- idx2 = i;
- }
- }
- Mat qianerArea = new Mat(thresh.Size(), thresh.Type());
- Cv2.DrawContours(qianerArea, contoursMat, idx1, new Scalar(1));
- Cv2.DrawContours(qianerArea, contoursMat, idx2, new Scalar(1));
- Fill(qianerArea, out qianerArea, 1);
- thresh = qianerArea;
- Cv2.Rectangle(thresh, new Rect(thresh.Cols - 1, 0, 1, thresh.Rows), new Scalar(0), -1);//将连接线去掉
- Cv2.Rectangle(thresh, new Rect(0, 0, 1, thresh.Rows), new Scalar(0), -1);//
- //
- int middle = imageRed.Cols / 2;
- //ImageShow(thresh * 255);
- Scalar sum = new Scalar(0);
- Scalar max = new Scalar(0);
- //左右列像素最多的地方,并且提取目标区域
- int leftBorde = 0, rightBorder = 0;
- for (int j = 0; j < middle; j++)
- {
- sum = thresh[0, thresh.Rows, j, j + 1].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- leftBorde = dataArea0[0];// j;
- dataArea[0] = dataArea0[0];// leftBorde;
- }
- }
- max = 0;
- for (int j = thresh.Cols - 1; j > middle; j--)
- {
- sum = thresh[0, thresh.Rows, j - 1, j].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- rightBorder = dataArea0[3];// j;
- dataArea[3] = dataArea0[3];// rightBorder;
- }
- }
- //目标区域的上下界
- for (int i = 0; i < thresh.Rows; i++)
- {
- sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
- if ((int)sum > 0)
- {
- y[0] = i;
- break;
- }
- }
- for (int i = thresh.Rows - 1; i > 0; i--)
- {
- sum = thresh[i - 1, i, 0, thresh.Cols].Sum();
- if ((int)sum > 0)
- {
- y[1] = i;
- break;
- }
- }
- //目标区域中间
- if (flag == false)
- {
- for (int j = leftBorde; j < thresh.Cols; j++)
- {
- sum = thresh[y[0], y[1], j, j + 1].Sum();
- if ((int)sum == 0)
- {
- dataArea[1] = j - 120;
- break;
- }
- }
- for (int j = rightBorder - 100; j > middle; j--)
- {
- sum = thresh[y[0], y[1], j - 1, j].Sum();
- if ((int)sum == 0)
- {
- dataArea[2] = j - 120;
- break;
- }
- }
- }
- //else
- //{
- // Scalar lastSum = new Scalar(0);
- // for (int j = middle; j > 100; j-=5)
- // {
- // sum = thresh[y[0], y[1], j - 3, j].Sum();
- // if ((int)sum - (int)lastSum > 100&& lastSum!=0)
- // {
- // dataArea[1] = j;
- // break;
- // }
- // lastSum = sum;
- // }
- // lastSum = 0;
- // for (int j = middle; j <thresh.Cols-100; j+=5)
- // {
- // sum = thresh[y[0], y[1], j , j+5].Sum();
- // if ((int)sum - (int)lastSum > 150 && lastSum != 0)
- // {
- // dataArea[2] = j;
- // break;
- // }
- // lastSum = sum;
- // }
- //}
- //边缘检测并横向腐蚀,去掉小连通域,提取铜厚
- Mat sobel = new Mat();
- Sobel(thresh, out sobel);
- Mat result = sobel.Clone();
- //Mat sobel = new Mat();
- //Cv2.GaussianBlur(imageRed, imageRed, new Size(11, 11), 5, 5);
- //Cv2.MedianBlur(imageRed, imageRed, 5);
- //Sobel(imageRed, out sobel);
- //Mat sobelThresh = new Mat();
- //double t2 = Cv2.Threshold(sobel, sobelThresh, 0, 255, ThresholdTypes.Otsu);
- //sobelThresh = sobel.Threshold(5, 255, ThresholdTypes.Binary);
- //Mat seErode = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(30, 1));
- //Mat erode = new Mat();
- //Cv2.Erode(sobelThresh, erode, seErode);
- //Mat noMinArea = new Mat();
- //GetArea(erode, out noMinArea, 500, true);
- //Mat result = noMinArea.Clone();
- //ImageShow(result * 255);
- leftTonghou[0] = dataArea[0] + 100;
- rightTonghou[0] = dataArea[3] - 100;
- //计算线纵坐标
- //左
- for (int i = y[0]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) > 0)
- {
- leftTonghou[1] = i;
- break;
- }
- }
- for (int i = leftTonghou[1] + 60; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) > 0)
- {
- leftTonghou[2] = i;
- break;
- }
- }
- for (int i = leftTonghou[2]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) == 0)
- {
- leftTonghou[2] = i;
- break;
- }
- }
- //右
- for (int i = y[0]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) > 0)
- {
- rightTonghou[1] = i;
- break;
- }
- }
- for (int i = rightTonghou[1] + 60; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) > 0)
- {
- rightTonghou[2] = i;
- break;
- }
- }
- for (int i = rightTonghou[2]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) == 0)
- {
- rightTonghou[2] = i;
- break;
- }
- }
- if (/*leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 > result.Rows
- || */leftTonghou[2] - rightTonghou[2] > 300 && rightTonghou[2] * 3 > result.Rows)
- {
- if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 > result.Rows)
- leftTonghou[0] = leftTonghou[0] + 400;
- else if (leftTonghou[2] - rightTonghou[2] > 300 && rightTonghou[2] * 3 > result.Rows)
- leftTonghou[0] = leftTonghou[0] + 430;
- //计算线纵坐标
- //左
- for (int i = y[0]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) > 0)
- {
- leftTonghou[1] = i;
- break;
- }
- }
- for (int i = leftTonghou[1] + 60; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) > 0)
- {
- leftTonghou[2] = i;
- break;
- }
- }
- for (int i = leftTonghou[2]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, leftTonghou[0]) == 0)
- {
- leftTonghou[2] = i;
- break;
- }
- }
- }
- if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 < result.Rows
- || rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
- {
- if (leftTonghou[2] - rightTonghou[2] > 400 && rightTonghou[2] * 3 < result.Rows)
- rightTonghou[0] = rightTonghou[0] - 400;
- else if (rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
- rightTonghou[0] = rightTonghou[0] - 550;
- //右
- for (int i = y[0]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) > 0)
- {
- rightTonghou[1] = i;
- break;
- }
- }
- for (int i = rightTonghou[1] + 60; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) > 0)
- {
- rightTonghou[2] = i;
- break;
- }
- }
- for (int i = rightTonghou[2]; i < y[1]; i++)
- {
- if (result.Get<byte>(i, rightTonghou[0]) == 0)
- {
- rightTonghou[2] = i;
- break;
- }
- }
- if (rightTonghou[2] - leftTonghou[2] > 300 && leftTonghou[2] * 3 > result.Rows)
- rightTonghou[2] = leftTonghou[2];
- }
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 120);
- ////ExtractLines(thresh,out left1,leftBorde+200)
- ////ImageShow(sobelThresh,erode,maxArea*255);
- }
- /// <summary>
- /// 计算高度差全的铜厚
- /// </summary>
- /// <param name="contour">二值图</param>
- /// <param name="leftTonghou">左侧铜厚,0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="rightTonghou">右侧铜厚,0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="dataArea">提取区域</param>
- /// <param name="y">目标区域上下边界</param>
- public void GaoduchaTonghou(Mat contour, out int[] leftTonghou, out int[] rightTonghou, int[] dataArea, int[] y)
- {
- leftTonghou = new int[3];
- rightTonghou = new int[3];
- leftTonghou[0] = dataArea[0] + 200;
- rightTonghou[0] = dataArea[3] - 200;
- Mat crop = contour[y[0], y[1], 0, contour.Cols].Clone();
- double leftOrdinate1 = 0;
- ExtractLines(crop, out leftOrdinate1, leftTonghou[0] - 5, leftTonghou[0] + 5, 1);
- double leftOrdinate2 = 0;
- ExtractLines(crop, out leftOrdinate2, leftTonghou[0] - 5, leftTonghou[0] + 5, leftOrdinate1, -1);
- double rightOrdinate1 = 0;
- ExtractLines(crop, out rightOrdinate1, rightTonghou[0] - 5, rightTonghou[0] + 5, 1);
- double rightOrdinate2 = 0;
- ExtractLines(crop, out rightOrdinate2, rightTonghou[0] - 5, rightTonghou[0] + 5, rightOrdinate1, -1);
- leftTonghou[1] = (int)leftOrdinate1 + y[0];
- leftTonghou[2] = (int)leftOrdinate2 + y[0];
- rightTonghou[1] = (int)rightOrdinate1 + y[0];
- rightTonghou[2] = (int)rightOrdinate2 + y[0];
- }
- /// <summary>
- /// 计算高度差全的防焊厚度
- /// </summary>
- /// <param name="imageGreen">绿色通道图</param>
- /// <param name="leftFanghanhou">左侧防焊厚度,0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="rightFanghanhou">右侧防焊厚度,0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="dataArea">提取区域</param>
- /// <param name="tonghou">其中一个铜厚坐标</param>
- public void GaoduchaFanhanhou(Mat imageGreen, out int[] leftFanghanhou, out int[] rightFanghanhou, int[] dataArea, int[] tonghou)
- {
- leftFanghanhou = new int[3];
- rightFanghanhou = new int[3];
- int range = 30;
- int range2 = 80;
- //将目标区域选出来
- PointEnhancement(imageGreen, out imageGreen);
- Cv2.GaussianBlur(imageGreen, imageGreen, new Size(5, 5), 3, 3);
- Mat crop = imageGreen[tonghou[1] - range, tonghou[2] + range, dataArea[1] + range2, dataArea[2] - range2].Clone();
- Mat contour = new Mat();
- double t = Cv2.Threshold(crop, contour, 0, 1, ThresholdTypes.Otsu);
- contour = 255 - crop.Threshold(t - 5, 255, ThresholdTypes.Binary);
- Mat edge = new Mat();
- Sobel(crop, out edge);
- Mat thresh = edge.Threshold(0, 255, ThresholdTypes.Otsu);
- //新增
- Mat maxArea1 = new Mat();
- GetMaxArea(contour, out maxArea1);
- maxArea1 = maxArea1 * 255;
- Mat and = new Mat();
- Cv2.BitwiseAnd(thresh, maxArea1, and);
- //ImageShow(contour, thresh,and);
- //Mat delete = new Mat();
- //GetArea(thresh, out delete, 10,true);
- //delete = delete * 255;
- //
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 2));
- Mat close = new Mat();
- Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
- Mat fill = new Mat();
- Fill(close, out fill, 255);
- //ImageShow(thresh, close, fill);
- Mat maxArea = new Mat();
- GetMaxArea(fill, out maxArea);
- Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 3));
- Cv2.MorphologyEx(maxArea, close, MorphTypes.Close, seClose2);
- Fill(close, out fill, 255);
- Mat result = fill.Clone() / 255;
- //得到提取点
- Scalar sum = new Scalar(0);
- for (int j = 0; j < result.Cols; j++)
- {
- sum = result[0, result.Rows, j, j + 40].Sum();
- if ((int)sum > 2800)
- {
- leftFanghanhou[0] = j + 100;
- break;
- }
- }
- for (int j = result.Cols - 1; j > 0; j--)
- {
- sum = result[0, result.Rows, j - 40, j].Sum();
- if ((int)sum > 2800)
- {
- rightFanghanhou[0] = j - 100;
- break;
- }
- }
- //计算高度
- double leftOrdinate1 = 0;
- ExtractLines(and, out leftOrdinate1, leftFanghanhou[0] - 5, leftFanghanhou[0] + 5, 1);
- double leftOrdinate2 = 0;
- and = and / 255;
- for (int i = and.Rows - 1; i > and.Rows / 2; i--)
- {
- sum = and[i - 1, i, 0, leftFanghanhou[0]].Sum();
- if ((int)sum > 5)
- {
- leftOrdinate2 = i;
- break;
- }
- }
- //ExtractLines(result, out leftOrdinate2, leftFanghanhou[0] - 5, leftFanghanhou[0] + 5,leftOrdinate1, -1);
- //ExtractLines2(and, out leftOrdinate2, 5, 15, 1);
- double rightOrdinate1 = 0;
- ExtractLines(and, out rightOrdinate1, rightFanghanhou[0] - 5, rightFanghanhou[0] + 5, 1);
- double rightOrdinate2 = 0;
- for (int i = and.Rows - 1; i > and.Rows / 2; i--)
- {
- sum = and[i - 1, i, rightFanghanhou[0], and.Cols].Sum();
- if ((int)sum > 5)
- {
- rightOrdinate2 = i;
- break;
- }
- }
- //ExtractLines(result, out rightOrdinate2, rightFanghanhou[0] - 5, rightFanghanhou[0] + 5, rightOrdinate1, -1);
- //ExtractLines2(and, out rightOrdinate2, and.Cols-15, and.Cols- 5, 1);
- leftFanghanhou[0] += dataArea[1] + range2;
- leftFanghanhou[1] = (int)leftOrdinate1 + tonghou[1] - range;
- leftFanghanhou[2] = (int)leftOrdinate2 + tonghou[1] - range;
- rightFanghanhou[0] += dataArea[1] + range2;
- rightFanghanhou[1] = (int)rightOrdinate1 + tonghou[1] - range;
- rightFanghanhou[2] = (int)rightOrdinate2 + tonghou[1] - range;
- //ImageShow(close*255, fill, maxArea * 255);
- //ImageShow(result * 255);
- }
- public void GaoduchaFanhanhou2(Mat imageRed, out int[] leftFanghanhou, out int[] rightFanghanhou, int[] dataArea, int[] tonghou)
- {
- leftFanghanhou = new int[3];
- rightFanghanhou = new int[3];
- int range = 30;
- int range2 = 80;
- //将目标区域选出来
- PointEnhancement(imageRed, out imageRed);
- Cv2.GaussianBlur(imageRed, imageRed, new Size(9, 9), 3, 3);
- Mat crop = imageRed[tonghou[1] - range, tonghou[2] + range, dataArea[1] + range2, dataArea[2] - range2].Clone();
- Mat sobel = new Mat();
- Sobel(crop, out sobel);
- Mat thresh = new Mat();
- double t = Cv2.Threshold(sobel, thresh, 0, 255, ThresholdTypes.Otsu);
- thresh = sobel.Threshold(5, 255, ThresholdTypes.Binary);
- Mat tangle = new Mat();
- Cv2.Rectangle(thresh, new Rect(0, 0, 10, thresh.Rows), new Scalar(255), -1);
- Cv2.Rectangle(thresh, new Rect(thresh.Cols - 11, 0, 10, thresh.Rows), new Scalar(255), -1);
- //清楚小点
- Mat noMin = new Mat();
- GetArea(thresh, out noMin, 10, true);
- //闭运算
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- Mat close = new Mat();
- Cv2.MorphologyEx(noMin, close, MorphTypes.Close, seClose);
- Mat max = new Mat();
- GetMaxArea(close, out max);
- //再次闭运算全连接
- Mat seClsoe2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 3));
- Mat close2 = new Mat();
- Cv2.MorphologyEx(max, close2, MorphTypes.Close, seClsoe2);
- //ImageShow( thresh,noMin*255,max*255,close2*255);
- Mat result = close2.Clone();
- //得到提取点
- Scalar sum = new Scalar(0);
- for (int j = 20; j < result.Cols; j++)
- {
- sum = result[0, result.Rows, j, j + 40].Sum();
- if ((int)sum > 2800)
- {
- leftFanghanhou[0] = j + 100;
- break;
- }
- }
- for (int j = result.Cols - 21; j > 0; j--)
- {
- sum = result[0, result.Rows, j - 40, j].Sum();
- if ((int)sum > 2800)
- {
- rightFanghanhou[0] = j - 100;
- break;
- }
- }
- //计算高度
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, leftFanghanhou[0] - 5, leftFanghanhou[0] + 5].Sum();
- if ((int)sum > 5)
- {
- leftFanghanhou[1] = i;
- break;
- }
- }
- for (int i = result.Rows - 1; i > 0; i--)
- {
- sum = result[i - 1, i, leftFanghanhou[0] - 5, leftFanghanhou[0] + 5].Sum();
- if ((int)sum > 5)
- {
- leftFanghanhou[2] = i;
- break;
- }
- }
- for (int i = 0; i < result.Rows; i++)
- {
- sum = result[i, i + 1, rightFanghanhou[0] - 5, rightFanghanhou[0] + 5].Sum();
- if ((int)sum > 5)
- {
- rightFanghanhou[1] = i;
- break;
- }
- }
- for (int i = result.Rows - 1; i > 0; i--)
- {
- sum = result[i - 1, i, rightFanghanhou[0] - 5, rightFanghanhou[0] + 5].Sum();
- if ((int)sum > 5)
- {
- rightFanghanhou[2] = i;
- break;
- }
- }
- int compensate = 13;
- leftFanghanhou[0] += dataArea[1] + range2;
- leftFanghanhou[1] += tonghou[1] - range;
- leftFanghanhou[2] += tonghou[1] - range - compensate;
- rightFanghanhou[0] += dataArea[1] + range2;
- rightFanghanhou[1] += tonghou[1] - range;
- rightFanghanhou[2] += tonghou[1] - range - compensate;
- }
- public void GaoduchaFanghanhoudu3(Mat image, out int[] leftFanghanhoudu, out int[] rightFanghanhoudu, int[] dataArea, int[] tonghou)
- {
- leftFanghanhoudu = new int[3];
- rightFanghanhoudu = new int[3];
- Mat gray = new Mat();
- Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
- Cv2.GaussianBlur(gray, gray, new Size(9, 9), 3, 3);
- //Cv2.MedianBlur(gray, gray, 5);
- Mat sobel = new Mat();
- //Sobel(gray, out sobel);
- EdgeY(gray, out sobel);
- Mat sobelThresh = new Mat();
- double t = Cv2.Threshold(sobel, sobelThresh, 0, 255, ThresholdTypes.Otsu);
- sobelThresh = sobel.Threshold(10, 255, ThresholdTypes.Binary);
- //Mat seClsoe = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- //Mat close = new Mat();
- //Cv2.MorphologyEx(sobelThresh, close, MorphTypes.Close, seClsoe);
- Mat seErode = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
- Mat erode = new Mat();
- Cv2.Erode(sobelThresh, erode, seErode);
- Mat crop = erode[tonghou[1] - 60, tonghou[2] + 40, dataArea[1], dataArea[2]].Clone();
- Mat noMinArea = new Mat();
- GetArea(crop, out noMinArea, 50, true);
- Mat result = new Mat();
- RemoveCircles(noMinArea, out result);
- Scalar sum = new Scalar(0);
- int startBorder = 0;
- for (int i = 10; i < result.Rows - 2; i++)
- {
- sum = result[i, i + 2, 0, result.Cols].Sum();
- if ((int)sum > 600)
- {
- startBorder = i;
- break;
- }
- }
- Cv2.Rectangle(result, new Rect(0, 0, result.Cols, startBorder), new Scalar(0), -1);
- GetArea(result, out result, 2000, true);
- //ImageShow(result * 255);
- int middle = crop.Cols / 2;
- leftFanghanhoudu[0] = middle - 150;
- rightFanghanhoudu[0] = middle + 150;
- double left1 = 0, left2 = 0;
- double right1 = 0, right2 = 0;
- ExtractLines(result, out left1, leftFanghanhoudu[0] - 10, leftFanghanhoudu[0] + 10, 1);
- ExtractLines(result, out right1, rightFanghanhoudu[0] - 10, rightFanghanhoudu[0] + 10, 1);
- //计算防焊下坐标
- Mat edgeLower = new Mat();
- Sobel(gray, out edgeLower);
- Mat edgeLowerThresh = edgeLower.Threshold(5, 1, ThresholdTypes.Binary);
- //ImageShow(edgeLowerThresh * 255);
- Mat crop2 = edgeLowerThresh[tonghou[2] - 20, tonghou[2] + 40, dataArea[1], dataArea[2]].Clone();
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- Mat close2 = new Mat();
- Cv2.MorphologyEx(crop2, close2, MorphTypes.Close, seOpen);
- Mat open = new Mat();
- Cv2.MorphologyEx(close2, open, MorphTypes.Open, seOpen);
- Mat bigArea = new Mat();
- //GetMaxArea(open, out bigArea);
- GetArea(open, out bigArea, 500, true);
- //ImageShow(bigArea * 255);
- //Mat cannyEdge = new Mat();
- //Cv2.Canny(gray, cannyEdge, 18, 14);
- //ImageShow(cannyEdge);
- int bigAreaSum = (int)bigArea.Sum();
- if (bigAreaSum < 5000)
- bigArea = result[result.Rows - 60, result.Rows, 0, result.Cols].Clone();
- ExtractLines2(bigArea, out left2, leftFanghanhoudu[0] - 10, leftFanghanhoudu[0] + 10, 1);
- ExtractLines2(bigArea, out right2, rightFanghanhoudu[0] - 10, rightFanghanhoudu[0] + 10, 1);
- int compensate1 = 10;
- int compensate2 = 0;
- leftFanghanhoudu[0] += dataArea[1];
- leftFanghanhoudu[1] = (int)left1 + tonghou[1] - 60 + compensate1;
- leftFanghanhoudu[2] = (int)left2 + tonghou[2] - 20 - compensate2;
- rightFanghanhoudu[0] += dataArea[1];
- rightFanghanhoudu[1] = (int)right1 + tonghou[1] - 60 + compensate1;
- rightFanghanhoudu[2] = (int)right2 + tonghou[2] - 20 - compensate2;
- //ImageShow(sobelThresh, crop, result * 255);
- }
- public void GaoduchaFanghanhoudu4(Mat image_0, out int[] leftFanghanhoudu, out int[] rightFanghanhoudu, int[] dataArea, int[] tonghou)
- {
- leftFanghanhoudu = new int[3];
- rightFanghanhoudu = new int[3];
- //防焊厚上层
- Mat gray = new Mat();
- Cv2.CvtColor(image_0, gray, ColorConversionCodes.BGR2GRAY);
- //Mat filter = new Mat();
- //PointEnhancement(gray, out filter);
- Mat newGray = gray.Clone();// new Mat();//
- //Cv2.GaussianBlur(gray, newGray, new Size(11, 11), 3, 3);
- Mat blur = new Mat();
- Cv2.MedianBlur(newGray, blur, 5);
- Mat edge;
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\blur.png", blur);
- EdgeY(blur, out edge);
- //Edge(blur, out edge);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\edge.png", edge);
- int meanV = (int)(edge[tonghou[1] - 30, tonghou[2], dataArea[1], dataArea[2]].Mean().Val0);
- if (meanV > 50) meanV -= 25;
- else if (meanV > 18)
- {
- if ((int)(edge[tonghou[1], tonghou[1] / 2 - 15 + tonghou[2] / 2, dataArea[1], dataArea[2]].Mean().Val0) * 2 < meanV)
- meanV = (int)(edge[tonghou[1], tonghou[1] / 2 - 15 + tonghou[2] / 2, dataArea[1], dataArea[2]].Mean().Val0) / 2;
- else if (meanV > 20)
- meanV /= 2;
- else
- meanV -= 5;
- }
- //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);
- meanV = meanV < 13 ? meanV * 2 : meanV;
- Mat thresh = edge.Threshold(meanV/*15*/, 255, ThresholdTypes.Binary);
- Mat thresh2 = thresh.Clone();
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh2_0.png", thresh2);
- Cv2.Rectangle(thresh2, new Rect(0, 0, thresh.Cols, tonghou[1] - 30), new Scalar(0), -1);
- Cv2.Rectangle(thresh2, new Rect(0, tonghou[2] + 20, thresh.Cols, thresh.Rows - tonghou[2] - 20), new Scalar(0), -1);
- Cv2.Rectangle(thresh2, new Rect(0, tonghou[1] + 40, thresh.Cols, tonghou[2] - tonghou[1]), new Scalar(255), -1);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\thresh2.png", thresh2);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- //Mat close = new Mat();
- //Cv2.MorphologyEx(thresh2, close, MorphTypes.Close, seClose);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat open = new Mat();
- Cv2.MorphologyEx(thresh2, open, MorphTypes.Open, seOpen);
- Mat max = new Mat();
- //GetMaxArea(thresh2, out max);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\open.png", open);
- //GetMaxArea(open, out max/*thresh, out maxThresh*/);
- GetArea(open, out max, 500, true);
- //ImageShow(thresh, thresh2,open, max * 255);
- Mat result = max.Clone();
- int middle = (dataArea[1] + dataArea[2]) / 2;
- leftFanghanhoudu[0] = middle - 120;// 150;
- rightFanghanhoudu[0] = middle + 120;// 150;
- Mat seOpen11 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(70/*15*/, 1/*水平线<--3, 3*/));// 开运算
- //Mat open11 = new Mat();
- ////OpenCV提取图像中的垂直线(或者水平线) 定义结构元素,开操作
- Cv2.MorphologyEx(result, result, MorphTypes.Open, seOpen11);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 120);
- //if (result.Get<byte>(tonghou[2] - 10, leftFanghanhoudu[0]) > 0)
- //{
- // for (int i = tonghou[2] - 10; i > tonghou[1] - 30; i--)
- // {
- // if (result.Get<byte>(i, leftFanghanhoudu[0]) == 0)
- // {
- // leftFanghanhoudu[1] = i;
- // break;
- // }
- // }
- //}
- //else
- {
- for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
- {
- if (result/*result*/.Get<byte>(i, leftFanghanhoudu[0]) > 0)
- {
- leftFanghanhoudu[1] = i;
- if (max[i - 1, i, leftFanghanhoudu[0], rightFanghanhoudu[0]].Sum().Val0 >
- (rightFanghanhoudu[0] - leftFanghanhoudu[0]) / 2 + 15
- || Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
- break;//对弧形的判定!
- }
- }
- }
- int upperLine = leftFanghanhoudu[1];
- for (int i = leftFanghanhoudu[1]; i > leftFanghanhoudu[1] - 5; i--)
- {
- if (max.Get<byte>(i, leftFanghanhoudu[0]) > 0)
- upperLine = i;
- else break;
- }
- leftFanghanhoudu[1] = upperLine;
- //if (result.Get<byte>(tonghou[2] - 10, rightFanghanhoudu[0]) > 0)
- //{
- // for (int i = tonghou[2] - 10; i > tonghou[1] - 30; i--)
- // {
- // if (result.Get<byte>(i, rightFanghanhoudu[0]) == 0)
- // {
- // rightFanghanhoudu[1] = i;
- // break;
- // }
- // }
- //}
- //else
- {
- for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
- {
- if (result/*result*/.Get<byte>(i, rightFanghanhoudu[0]) > 0)
- {
- rightFanghanhoudu[1] = i;
- if (max[i - 1, i, leftFanghanhoudu[0], rightFanghanhoudu[0]].Sum().Val0 >
- (rightFanghanhoudu[0] - leftFanghanhoudu[0]) / 2 + 15
- || Cv2.FloodFill(max, new Point(rightFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
- break;//对弧形的判定!
- }
- }
- }
- upperLine = rightFanghanhoudu[1];
- for (int i = rightFanghanhoudu[1]; i > rightFanghanhoudu[1] - 5; i--)
- {
- if (max.Get<byte>(i, rightFanghanhoudu[0]) > 0)
- upperLine = i;
- else break;
- }
- rightFanghanhoudu[1] = upperLine;
- Scalar color = new Scalar(255/*2*//*127*//*255*/);
- //颜色
- Cv2.Line(gray, new Point(leftFanghanhoudu[0], leftFanghanhoudu[1]), new Point(leftFanghanhoudu[0], leftFanghanhoudu[2]), color, 2, LineTypes.Link8);
- Cv2.Line(gray, new Point(rightFanghanhoudu[0], rightFanghanhoudu[1]), new Point(rightFanghanhoudu[0], rightFanghanhoudu[2]), color, 2, LineTypes.Link8);
- Rect rectMin = new Rect(Math.Max(0, leftFanghanhoudu[0] - 130), Math.Max(0, leftFanghanhoudu[1] - 200),
- Math.Min(max.Cols-1, rightFanghanhoudu[0]+130)- Math.Max(0, leftFanghanhoudu[0] - 130),
- Math.Min(max.Rows-1, leftFanghanhoudu[1]+30)- Math.Max(0, leftFanghanhoudu[1] - 200));
- Cv2.Rectangle(gray, rectMin, color, 1);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", gray/*max * 127*//*1*//*120*/);
- int[] compensate1 = new int[] { 10, 10 };
- //去掉球 以及 微调补偿数值
- Mat roundTemp = image_0[Math.Max(0, leftFanghanhoudu[1] - 200), Math.Min(max.Rows - 1, leftFanghanhoudu[1] + 30),
- Math.Max(0, leftFanghanhoudu[0] - 130), Math.Min(max.Cols - 1, rightFanghanhoudu[0] + 130)].CvtColor(ColorConversionCodes.BGR2GRAY);
- //meanV = (int)(roundTemp.Mean().Val0);
- //Mat mat_er = roundTemp.Threshold(meanV/*15*/, 255, ThresholdTypes.Binary);
- CircleSegment[] circles = Cv2.HoughCircles(roundTemp, HoughMethods.Gradient, 1, 30, 70, 30, 10, 60/*60*/);//, dp, minDist, param1, param2, minRadius, maxRadius);
- //CircleSegment[] circleSegment;
- //Base.AutoMeasure.Ceju.RemoveCircles(image1, image2, out result, 1, 30, 70, 30, 10, 60);
- if (circles.Length > 0)
- {
- int range = 10;// 5;// 10;
- for (int ic = 0; ic < circles.Length; ic++)
- {
- Point center = (Point)circles[ic].Center;
- int radius = (int)circles[ic].Radius;
- //for(int j=1;j<radius;j++)
- //Cv2.Circle(result, center, j, new Scalar(1));
- Cv2.Rectangle(roundTemp, new Rect(center.X - radius - range, center.Y - radius - range, 2 * (radius + range), 2 * (radius + range)), new Scalar(255/*1*/), /*-*/1);
- 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);
- }
- }
- else
- {
- Mat tempEr = roundTemp.Threshold(/*15*/(int)(roundTemp.Mean().Val0) - 5, 255, ThresholdTypes.BinaryInv);
- ////GetMaxArea(tempEr, out tempEr); tempEr = tempEr * 255;
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\tempEr.png", tempEr/*max * 120*//*roundTemp*/);
- circles = Cv2.HoughCircles(tempEr, HoughMethods.Gradient, 1, 30, 70, 30, 10, 90/*60*/);//, dp, minDist, param1, param2, minRadius, maxRadius);
- //Base.AutoMeasure.Ceju.RemoveCircles(image1, image2, out result, 1, 30, 70, 30, 10, 60);
- if (circles.Length > 0)
- {
- int range = 0;// 5;// 10;
- for (int ic = 0; ic < circles.Length; ic++)
- {
- Point center = (Point)circles[ic].Center;
- int radius = (int)circles[ic].Radius;
- //for(int j=1;j<radius;j++)
- //Cv2.Circle(result, center, j, new Scalar(1));
- Cv2.Rectangle(roundTemp, new Rect(center.X - radius - range, center.Y - radius - range, 2 * (radius + range), 2 * (radius + range)), new Scalar(255/*1*/), /*-*/1);
- 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);
- }
- }
- }
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", roundTemp/*max * 120*//*roundTemp*/);
- //{
- // for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
- // {
- // if (result/*result*/.Get<byte>(i, leftFanghanhoudu[0]) > 0)
- // {
- // leftFanghanhoudu[1] = i;
- // if (max[i - 1, i, leftFanghanhoudu[0], rightFanghanhoudu[0]].Sum().Val0 >
- // (rightFanghanhoudu[0] - leftFanghanhoudu[0]) / 2 + 15
- // || Cv2.FloodFill(max, new Point(leftFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
- // break;//对弧形的判定!
- // }
- // }
- //}
- int comp = 5;
- if (rightFanghanhoudu[1] > leftFanghanhoudu[1] + 10)
- comp = 10;
- upperLine = leftFanghanhoudu[1] + comp;
- for (int i = leftFanghanhoudu[1]; i < leftFanghanhoudu[1] + comp; i++)
- {
- if (max.Get<byte>(i, leftFanghanhoudu[0]) > 0)
- {
- upperLine = i;
- break;
- }
- }
- leftFanghanhoudu[1] = upperLine;
- if (rightFanghanhoudu[1] < leftFanghanhoudu[1])
- {
- comp = 5;
- upperLine = rightFanghanhoudu[1] + 5;
- for (int i = rightFanghanhoudu[1]; i < rightFanghanhoudu[1] + 5; i++)
- {
- if (max.Get<byte>(i, rightFanghanhoudu[0]) > 0)
- {
- upperLine = i;
- break;
- }
- }
- rightFanghanhoudu[1] = upperLine;
- }
- //{
- // for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
- // {
- // if (result/*result*/.Get<byte>(i, rightFanghanhoudu[0]) > 0)
- // {
- // rightFanghanhoudu[1] = i;
- // if (max[i - 1, i, leftFanghanhoudu[0], rightFanghanhoudu[0]].Sum().Val0 >
- // (rightFanghanhoudu[0] - leftFanghanhoudu[0]) / 2 + 15
- // || Cv2.FloodFill(max, new Point(rightFanghanhoudu[0], i), new Scalar(1/*255*/)) > 3500)
- // break;//对弧形的判定!
- // }
- // }
- //}
- ////Mat roundGray = new Mat();
- ////Cv2.CvtColor(roundTemp, roundGray, ColorConversionCodes.BGR2GRAY);
- ////int mean0; Scalar mean_rgb;
- //////去掉圆并灰度化,怎么能更快?
- ////Scalar mean = roundGray.Mean(/*gray*/);
- ////mean0 = (int)mean[0];
- ////mean_rgb = roundTemp/*srcImage*/.Mean();
- /////*final = */
- ////bool foundCircle;
- ////roundTemp = FangHanTools.RemoveCircle(roundTemp, mean0, mean_rgb, out foundCircle);//.CvtColor(ColorConversionCodes.BGR2GRAY);
- ///////*Mat roundTemp = */FangHanTools.RemoveCircle(roundTemp, out mean0, out roundTemp);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\gray.png", roundTemp/*max * 127*//*1*//*120*/);
- ////通过连接区域的大小判定是否在圆球范围内
- //int fillArea1 = 0;
- //for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
- //{
- // if (result.Get<byte>(i, leftFanghanhoudu[0]) > 0)
- // {
- // fillArea1 = Cv2.FloodFill(result.Clone()/*, mask1*/, new Point(leftFanghanhoudu[0], i), new Scalar(255/*127*//*255*/));
- // leftFanghanhoudu[1] = i;
- // break;
- // }
- //}
- //Console.WriteLine("fillArea1:" + fillArea1);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\result.png", result * 120);
- ////通过连接区域的大小判定是否在圆球范围内
- //int fillArea2 = 0;
- //for (int i = tonghou[1] - 30; i < tonghou[2]; i++)
- //{
- // if (result.Get<byte>(i, rightFanghanhoudu[0]) > 0)
- // {
- // fillArea2 = Cv2.FloodFill(result.Clone()/*, mask1*/, new Point(rightFanghanhoudu[0], i), new Scalar(255/*127*//*255*/));
- // rightFanghanhoudu[1] = i;
- // break;
- // }
- //}
- //Console.WriteLine("fillArea2:" + fillArea2);
- if (Math.Abs(leftFanghanhoudu[1] - rightFanghanhoudu[1]) > 100)
- {
- if (leftFanghanhoudu[1] < rightFanghanhoudu[1])
- leftFanghanhoudu[1] = rightFanghanhoudu[1];
- else
- rightFanghanhoudu[1] = leftFanghanhoudu[1];
- }
- //if (fillArea1 != fillArea2)
- //{
- // if (leftFanghanhoudu[1] < rightFanghanhoudu[1])
- // leftFanghanhoudu[1] = rightFanghanhoudu[1];
- // else
- // rightFanghanhoudu[1] = leftFanghanhoudu[1];
- //}
- //防焊厚下层
- Mat newGray2 = new Mat();
- Cv2.GaussianBlur(gray, newGray2, new Size(15, 15), 5, 5);
- Mat filter2 = new Mat();
- Cv2.MedianBlur(newGray2, filter2, 3);
- Mat edge2 = new Mat();
- Edge(filter2, out edge2);
- Mat thresh3 = new Mat();
- thresh3 = edge2.Threshold(15, 255, ThresholdTypes.Binary);
- Mat thresh4 = thresh3.Clone();
- Cv2.Rectangle(thresh4, new Rect(0, 0, thresh.Cols, tonghou[2] - 30), new Scalar(255), -1);
- Cv2.Rectangle(thresh4, new Rect(0, tonghou[2] + 40, thresh.Cols, thresh.Rows - tonghou[2] - 40), new Scalar(0), -1);
- Mat seOpen2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Mat open2 = new Mat();
- Cv2.MorphologyEx(thresh4, open2, MorphTypes.Open, seOpen2);
- Mat crop2 = open2[tonghou[2] - 20, tonghou[2] + 20, leftFanghanhoudu[0], rightFanghanhoudu[0]] / 255;
- int openSum = (int)crop2.Sum();
- int compensate2 = 8;
- if (openSum < 3000)
- {
- compensate2 = 0;
- Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 3));
- Cv2.MorphologyEx(thresh4, open2, MorphTypes.Close, seClose2);
- }
- Mat max2 = new Mat();
- GetMaxArea(thresh2, out max2);
- GetArea(open2, out max2, 500, true);
- //ImageShow(thresh3, thresh4, max2 * 255);
- //Cv2.ImWrite(@"C:\Users\54434\Desktop\max2.png", max2 * 255);
- Mat result2 = max2.Clone();
- //防焊厚上层
- Mat gray_1 = new Mat();
- Cv2.CvtColor(image_0, gray_1, ColorConversionCodes.BGR2GRAY);
- Scalar sum2 = new Scalar(0);
- for (int i = tonghou[2] + 20; i > tonghou[2] - 20; i--)
- {
- sum2 = result2[i - 1, i, leftFanghanhoudu[0] - 50, leftFanghanhoudu[0] + 50].Sum();
- if ((int)sum2 > 70)
- //if(result2.Get<byte>(i,leftFanghanhoudu[0])>0)
- {
- leftFanghanhoudu[2] = i;
- break;
- }
- }
- if (leftFanghanhoudu[2] == 0)
- leftFanghanhoudu[2] = tonghou[2];
- for (int i = tonghou[2] + 20; i > tonghou[2] - 20; i--)
- {
- sum2 = result2[i - 1, i, rightFanghanhoudu[0] - 50, rightFanghanhoudu[0] + 50].Sum();
- if ((int)sum2 > 70)
- //if(result2.Get<byte>(i, rightFanghanhoudu[0]) > 0)
- {
- rightFanghanhoudu[2] = i;
- break;
- }
- }
- if (rightFanghanhoudu[2] == 0)
- rightFanghanhoudu[2] = tonghou[2];
- //for (int i = tonghou[2] + 20; i > tonghou[2] - 20; i--)
- //{
- // sum2 = result2[i - 1, i, rightFanghanhoudu[0] - 50, rightFanghanhoudu[0] + 50].Sum();
- // if ((int)sum2 > 60)
- // {
- // rightFanghanhoudu[2] = i;
- // break;
- // }
- //}
-
-
- //int compensate1 = 10;
- leftFanghanhoudu[1] += compensate1[0];
- rightFanghanhoudu[1] += compensate1[1];
- int fanghanX1 = rightFanghanhoudu[0] - 15;// 10;// += 140;// 145;
- int fanghanX2 = rightFanghanhoudu[0] + 15;// 10;// -= 140;// 145;
- int leftY0 = leftFanghanhoudu[1];
- int rightY0 = rightFanghanhoudu[1];
- {
- int fanghanhouduY1 = leftFanghanhoudu[1];
- int minGray__2 = 255 * 300;// minGray;
- int fanghanTop = fanghanhouduY1 - 25;// 20;// 25;// 20;// 10;
- int fanghanhouduY_0 = fanghanhouduY1 + 60/*50*/ + 20;
- Mat grayRect = gray_1[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
- int fanghanhouduY1__2_bottom;
- fanghanhouduY1 -= fanghanTop;
- int fanghanhouduY1__2 = fanghanhouduY1;
- Gaoduchahoudu_ACC(grayRect, fanghanhouduY1 - 0/*19*//*15*//*5*//*(isLeft ? 8 : 1)*/, out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2, 0);
- if (true && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 10/* <<15 16*//*11*//*<-10*//*20*//*10*/
- || (fanghanhouduY1 - fanghanhouduY1__2 > 0 && fanghanhouduY1 - fanghanhouduY1__2 < 12/* <<15.12 <<10 *//*10*//*20*/))
- //|| (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/))
- fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
- else
- fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
- Console.WriteLine("leftFanghanhoudu[1]:" + leftFanghanhoudu[1] + ";fanghanhouduY1:" + fanghanhouduY1);
- leftFanghanhoudu[1] = fanghanhouduY1;
- }
- {
- int fanghanhouduY1 = rightFanghanhoudu[1];
- int minGray__2 = 255 * 300;// minGray;
- int fanghanTop = fanghanhouduY1 - 25;// 20;// 25;// 20;// 10;
- int fanghanhouduY_0 = fanghanhouduY1 + 60/*50*/ + 20;
- Mat grayRect = gray_1[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
- int fanghanhouduY1__2_bottom;
- fanghanhouduY1 -= fanghanTop;
- int fanghanhouduY1__2 = fanghanhouduY1;
- Gaoduchahoudu_ACC(grayRect, fanghanhouduY1 - 0/*19*//*15*//*5*//*(isLeft ? 8 : 1)*/, out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2, 1);
- if (true && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 10/* <<15 16*//*11*//*<-10*//*20*//*10*/
- || (fanghanhouduY1 - fanghanhouduY1__2 > 0 && fanghanhouduY1 - fanghanhouduY1__2 < 12/* <<15.12 <<10 *//*10*//*20*/))
- //|| (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/))
- fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
- else
- fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
- Console.WriteLine("rightFanghanhoudu[1]:" + rightFanghanhoudu[1] + ";fanghanhouduY1:" + fanghanhouduY1);
- rightFanghanhoudu[1] = fanghanhouduY1;
- }
- if (Math.Abs(leftFanghanhoudu[1] - rightFanghanhoudu[1]) - Math.Abs(leftY0 - rightY0) > 2)
- {//纠错2(1).JPG
- if (leftY0 == leftFanghanhoudu[1])
- rightFanghanhoudu[1] = rightY0;
- else if (rightY0 == rightFanghanhoudu[1])
- if (Math.Abs(rightY0 - leftY0) > 10)
- {
- leftFanghanhoudu[1] = rightY0;// leftY0;
- }
- else
- leftFanghanhoudu[1] = leftY0;
- }
- //Mat grayRect = gray_1[leftFanghanhoudu[1] - 10/*fanghanTop*/, leftFanghanhoudu[1] + 50/*fanghanhouduY_0 - 50*/, leftFanghanhoudu[0] - 10, leftFanghanhoudu[0] + 10];
- //Gaoduchahoudu_ACC(grayRect/*gray*/, y, marginTop/*150*//*fanghanhouduX*/, tonghouY, out fanghanhouduY1, out minGray);
- //int fanghanhouduY1__2 = fanghanhouduY1;
- //int minGray__2 = minGray;
- //fanghanX1 += 140;// 145;
- //fanghanX2 -= 140;// 145;
- //grayRect = gray[fanghanTop, fanghanhouduY_0 - 20/*50*/, fanghanX1, fanghanX2];
- //int fanghanhouduY1__2_bottom;
- //FanghanhouduForYouKaiKou_ACC(grayRect, y, marginTop, fanghanhouduY1 - (isLeft ? 8/*5*/ : 1), out fanghanhouduY1__2, out fanghanhouduY1__2_bottom, out minGray__2);
- //if (true && (Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 16/*11*//*<-10*//*20*//*10*/
- // || (!isLeft && Math.Abs(fanghanhouduY1 - fanghanhouduY1__2) < 25/*20*/)))
- // fanghanhouduY1 = fanghanhouduY1__2/*fanghanhouduY1__2_bottom*//*fanghanhouduY1__2*/ + fanghanTop;// +5;
- //else
- // fanghanhouduY1 = fanghanhouduY1 + fanghanTop;
- leftFanghanhoudu[2] -= compensate2;
- rightFanghanhoudu[2] -= compensate2;
- }
- /// <summary>
- /// 防焊 有开口 厚度 精确计算
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- private void Gaoduchahoudu_ACC(Mat gray0, int fanghanhouduY1__0, out int fanghanhouduY1, out int fanghanhouduY1Bottom, out int minGray, int a = 0)
- {
- int bottomYDistance = 15;// 30;
- int fanghanhouduY1__noSharp = -1;// fanghanhouduY1;
- {
- minGray = 300 * 255;
- int minRowIndex = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- fanghanhouduY1Bottom = 0;
- for (int i = Math.Max(0, fanghanhouduY1__0 - 19/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + bottomYDistance/*25*/, gray0.Rows) - 5; i++)
- {
- curGray = this.FanghanhouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Add(curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- fanghanhouduY1Bottom = i;
- minGray = curGray;
- }
- }
- for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 19/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i += 2)
- {
- if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
- {
- minRowIndex += 1;
- fanghanhouduY1Bottom += 2;
- }
- else
- break;
- }
- fanghanhouduY1__noSharp = minRowIndex;// 84;// 72;// minRowIndex;
- }
- {
- minGray = 300 * 255;
- //锐化
- //Mat left_small_sharp = BinaryTools.BlurMaskFunction(left_small).CvtColor(ColorConversionCodes.BGRA2GRAY);
- Mat gray = BinaryTools.BlurMaskFunction(gray0.Clone()/*grayRect*/, 4f * 3.14f, 1, 10f).CvtColor(ColorConversionCodes.BGRA2GRAY);
- //Cv2.ImWrite(@"C:\Users\win10SSD\Desktop\BlurMask" + a + "_.jpg", gray);
- int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- fanghanhouduY1Bottom = 0;
- for (int i = Math.Max(0, fanghanhouduY1__0 - 19/*1*//*5*//*10*/); i < Math.Min(fanghanhouduY1__0 + bottomYDistance/*25*/, gray.Rows) - 5; i++)
- {
- curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Add(curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- fanghanhouduY1Bottom = i;
- minGray = curGray;
- }
- }
- for (int i = minRowIndex - Math.Max(0, fanghanhouduY1__0 - 19/*1*//*5*//*10*/) + 2; i < curGrayList.Count; i += 2)
- {
- if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
- {
- minRowIndex += 1;
- fanghanhouduY1Bottom += 2;
- }
- else
- break;
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- fanghanhouduY1 = minRowIndex;// 84;// 72;// minRowIndex;
- }
- Console.Write("noSharp:" + fanghanhouduY1__noSharp + ";fanghanhouduY1:" + fanghanhouduY1 +"...");
- if (Math.Abs(fanghanhouduY1__noSharp - fanghanhouduY1) < 7/* <<7 8*//* << 6 *//*5*/
- || (fanghanhouduY1__noSharp - fanghanhouduY1 > 0 && fanghanhouduY1__noSharp - fanghanhouduY1 < 20))
- {
- fanghanhouduY1 = fanghanhouduY1__noSharp;
- }
- else
- Console.WriteLine("fanghanhouduY1 far away from fanghanhouduY1__noSharp.");
- }
- //叠构
- /// <summary>
- /// 得到叠构有导电布时铜层的数量以及当铜层大于二时的平均厚度
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="tongshu"></param>
- /// <param name="tonghou"></param>
- public void GetTongCengShuliang(Mat gray, out int tongshu, out int tonghou, out int jianhou)
- {
- tongshu = 0;
- tonghou = 0;
- jianhou = 0;
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- //ImageShow(thresh * 255);
- Mat nomin = new Mat();//去除小点防止干扰
- GetArea(thresh, out nomin, 500, true);
- Mat result = nomin.Clone();
- int[] y = new int[8];
- int middle = thresh.Cols / 2;//根据中间选取部分范围防止倾斜的干扰
- Scalar sum = new Scalar(0);
- for (int i = 0; i < thresh.Rows; i++)
- {
- sum = result[i, i + 1, middle - 100, middle + 100].Sum();
- if ((int)sum > 0)
- {
- y[0] = i;
- break;
- }
- }
- for (int i = y[0]; i < thresh.Rows; i++)
- {
- sum = result[i, i + 1, middle - 100, middle + 100].Sum();
- if ((int)sum == 0)
- {
- y[1] = i;
- break;
- }
- }
- for (int i = y[1]; i < thresh.Rows; i++)
- {
- sum = result[i, i + 1, middle - 100, middle + 100].Sum();
- if ((int)sum > 0)
- {
- y[2] = i;
- break;
- }
- }
- if (y[2] != 0)
- {
- for (int i = y[2]; i < thresh.Rows; i++)
- {
- sum = result[i, i + 1, middle - 100, middle + 100].Sum();
- if ((int)sum == 0)
- {
- y[3] = i;
- break;
- }
- }
- for (int i = y[3]; i < thresh.Rows; i++)
- {
- sum = result[i, i + 1, middle - 100, middle + 100].Sum();
- if ((int)sum > 0)
- {
- y[4] = i;
- break;
- }
- }
- if (y[4] != 0)
- {
- for (int i = y[4]; i < thresh.Rows; i++)
- {
- sum = result[i, i + 1, middle - 100, middle + 100].Sum();
- if ((int)sum == 0)
- {
- y[5] = i;
- break;
- }
- }
- for (int i = y[5]; i < thresh.Rows; i++)
- {
- sum = result[i, i + 1, middle - 100, middle + 100].Sum();
- if ((int)sum > 0)
- {
- y[6] = i;
- break;
- }
- }
- if (y[6] != 0)
- {
- for (int i = y[6]; i < thresh.Rows; i++)
- {
- sum = result[i, i + 1, middle - 100, middle + 100].Sum();
- if ((int)sum == 0)
- {
- y[7] = i;
- break;
- }
- }
- }
- }
- }
- if (y[2] == 0)
- tongshu = 1;
- else if (y[4] == 0)
- tongshu = 2;
- else if (y[6] == 0)
- tongshu = 3;
- else
- tongshu = 4;
- if (tongshu == 1)
- {
- tonghou = y[1] - y[0];
- }
- else if (tongshu == 2)
- {
- tonghou = (y[1] - y[0] + y[3] - y[2]) / 2;
- jianhou = y[2] - y[1];
- }
- }
- /// <summary>
- /// 提取有导电布时单层铜的坐标
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void DaoidanbuDanceng(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图片处理
- //无预处理图片边缘检测+阈值分割+去小面积用来提取导电布
- //高斯滤波+边缘检测+阈值分割+铜区域闭运算-反色-掩膜-+掩膜+去小面积提取中间线条
- //无预处理
- Mat sobel1 = new Mat();
- EdgeY2(gray, out sobel1);
- Mat threshSobel1 = new Mat();
- threshSobel1 = sobel1.Threshold(100, 1, ThresholdTypes.Binary);
- Mat nomin1 = new Mat();
- GetArea(threshSobel1, out nomin1, 500, true);
- Mat result1 = nomin1.Clone();
- //ImageShow(result1 * 255);
- //有预处理高斯滤波
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fanse = 1 - thresh;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 500, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255,nomin*255);
- //提取坐标
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- i += 30;
- }
- }
- if (ordinates[1] - ordinates[0] < 50)
- ordinates.RemoveAt(1);
- for (int i = ordinates[0] + 10; i < result.Rows; i++)
- {
- sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
- if ((int)sum == 0)
- {
- int newOrdinate = i;
- ordinates.Insert(1, newOrdinate);
- break;
- }
- }
- //if (ordinates[ordinates.Count - 1] - ordinates[ordinates.Count - 2] < 40)
- if (ordinates.Count == 7 || ordinates.Count == 9)
- ordinates.RemoveAt(ordinates.Count - 1);
- for (int i = ordinates[ordinates.Count - 1] + 10; i < result.Rows; i++)
- {
- sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
- if ((int)sum == 0)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- break;
- }
- }
- }
- /// <summary>
- /// 得到叠构的总厚
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="zonghou"></param>
- public void DiegouGetZonghou(Mat gray, out int zonghou)
- {
- zonghou = 0;
- Mat sobel = new Mat();
- EdgeY2(gray, out sobel);
- Mat thresh = sobel.Threshold(100, 1, ThresholdTypes.Binary);
- Mat nomin = new Mat();
- GetArea(thresh, out nomin, 500, true);
- Scalar sum = new Scalar();
- int[] y = new int[2];
- int middle = nomin.Cols / 2;
- for (int i = 0; i < thresh.Rows - 1; i++)
- {
- sum = nomin[i, i + 1, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- y[0] = i;
- break;
- }
- }
- for (int i = thresh.Rows - 1; i > 1; i--)
- {
- sum = nomin[i - 1, i, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- y[1] = i;
- break;
- }
- }
- zonghou = y[1] - y[0];
- }
- /// <summary>
- /// 无导电布单层时测总厚
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="zonghou"></param>
- public void DiegouGetZonghou2(Mat gray, out int zonghou)
- {
- zonghou = 0;
- Mat sobel = new Mat();
- EdgeY2(gray, out sobel);
- Mat thresh = sobel.Threshold(40, 1, ThresholdTypes.Binary);
- Mat nomin = new Mat();
- GetArea(thresh, out nomin, 500, true);
- //ImageShow(nomin * 255);
- Scalar sum = new Scalar();
- int[] y = new int[2];
- int middle = nomin.Cols / 2;
- for (int i = 0; i < thresh.Rows - 1; i++)
- {
- sum = nomin[i, i + 1, 0, nomin.Cols].Sum();
- if ((int)sum > 100)
- {
- y[0] = i;
- break;
- }
- }
- for (int i = thresh.Rows - 1; i > 1; i--)
- {
- sum = nomin[i - 1, i, 0, nomin.Cols].Sum();
- if ((int)sum > 100)
- {
- y[1] = i;
- break;
- }
- }
- zonghou = y[1] - y[0];
- if (zonghou > 600)//如果大于600,说明是亮图由于阈值太低而出现干扰,划到亮图里面去
- {
- zonghou = 200;
- }
- }
- /// <summary>
- /// 提取有导电布双层铜总厚大时的坐标
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void DaodianbuShuangcengZongHou(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图片处理
- //无预处理图片边缘检测+阈值分割+去小面积用来提取导电布
- //高斯滤波+边缘检测+阈值分割+铜区域闭运算-反色-掩膜-+掩膜+去小面积提取中间线条
- //无预处理
- Mat sobel1 = new Mat();
- EdgeY2(gray, out sobel1);
- Mat threshSobel1 = new Mat();
- threshSobel1 = sobel1.Threshold(100, 1, ThresholdTypes.Binary);
- Mat nomin1 = new Mat();
- GetArea(threshSobel1, out nomin1, 500, true);
- Mat result1 = nomin1.Clone();
- //ImageShow(result1 * 255);
- //有预处理高斯滤波
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(100, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fanse = 1 - thresh;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 500, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255+gray);
- //提取坐标
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- i += 30;
- }
- }
- if (ordinates[5] - ordinates[4] < 50)
- ordinates.RemoveAt(5);
- for (int i = ordinates[4] + 10; i < result.Rows; i++)
- {
- sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
- if ((int)sum == 0)
- {
- int newOrdinate = i;
- ordinates.Insert(5, newOrdinate);
- break;
- }
- }
- if (ordinates[ordinates.Count - 1] - ordinates[ordinates.Count - 2] < 40)
- ordinates.RemoveAt(ordinates.Count - 1);
- for (int i = ordinates[ordinates.Count - 1] + 10; i < result.Rows; i++)
- {
- sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
- if ((int)sum == 0)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- break;
- }
- }
- }
- /// <summary>
- /// 提取有导电布时三层铜时的坐标
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void DaodianbuSanceng(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图片处理
- //无预处理图片边缘检测+阈值分割+去小面积用来提取导电布
- //高斯滤波+边缘检测+阈值分割+铜区域闭运算-反色-掩膜-+掩膜+去小面积提取中间线条
- //无预处理
- Mat sobel1 = new Mat();
- EdgeY2(gray, out sobel1);
- Mat threshSobel1 = new Mat();
- threshSobel1 = sobel1.Threshold(100, 1, ThresholdTypes.Binary);
- Mat nomin1 = new Mat();
- GetArea(threshSobel1, out nomin1, 500, true);
- Mat result1 = nomin1.Clone();
- //ImageShow(result1 * 255);
- //有预处理高斯滤波
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fanse = 1 - thresh;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 500, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
- //提取坐标
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- i += 30;
- }
- }
- //第一层
- if (ordinates[1] - ordinates[0] < 50)
- ordinates.RemoveAt(1);
- for (int i = ordinates[0] + 10; i < result.Rows; i++)
- {
- sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
- if ((int)sum == 0)
- {
- int newOrdinate = i;
- ordinates.Insert(1, newOrdinate);
- break;
- }
- }
- //倒数第三层和倒数第四层,求倒数第四层
- if (ordinates[ordinates.Count - 3] - ordinates[ordinates.Count - 4] < 40)
- ordinates.RemoveAt(ordinates.Count - 3);
- for (int i = ordinates[ordinates.Count - 3] + 10; i < result.Rows; i++)
- {
- sum = result1[i, i + 1, middle - 10, middle + 10].Sum();
- if ((int)sum == 0)
- {
- int newOrdinate = i;
- ordinates.Insert(ordinates.Count - 2, newOrdinate);
- break;
- }
- }
- }
- /// <summary>
- /// 提取无导电布时单层铜厚大时的坐标
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void WuDaodianbuDancengHou(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图像处理
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fanse = 1 - thresh;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 500, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
- //坐标提取
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- i += 100;
- }
- }
- }
- /// <summary>
- /// 提取无导电布时,单层铜,总厚较窄时的坐标
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void WuDaodianbuDancengZhaiZonghouZhai(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图像处理
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(60, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fanse = 1 - thresh;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- //Mat close2 = new Mat();
- //Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 500, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
- //坐标提取
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- i += 30;
- }
- }
- int compensate = 5;
- ordinates[ordinates.Count - 1] += compensate;
- }
- /// <summary>
- /// 提取无导电布时,单层铜,总厚较宽时的坐标
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void WuDaodianbuDancengZhaiZonghouKuan(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图像处理
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(20, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fill = new Mat();
- Fill(thresh, out fill, 1);
- Mat fanse = 1 - fill;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- Mat close2 = new Mat();
- Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
- Mat nomin = new Mat();
- GetArea(close2, out nomin, 2000, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
- //坐标提取
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- int count = 0;
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- i += 30;
- count++;
- }
- if (count == 3)
- break;
- }
- //最后一行
- for (int i = result.Rows - 1; i > 2; i--)
- {
- sum = result[i - 1, i, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- break;
- }
- }
- for (int i = ordinates[ordinates.Count - 1] - 30; i > 2; i--)
- {
- sum = result[i - 2, i, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Insert(ordinates.Count - 1, newOrdinate);
- break;
- }
- }
- int compensate = 5;
- ordinates[0] += compensate;
- //ordinates[ordinates.Count - 1] += compensate;
- //提取中间线
- Mat crop = gray[ordinates[0], ordinates[1], middle - 100, middle + 100].Clone();
- //Mat threshCrop = crop.Threshold(160, 1, ThresholdTypes.Binary);
- Mat cropSobel = new Mat();
- EdgeY2(crop, out cropSobel);
- Mat threshCropSobel = 1 - cropSobel.Threshold(40, 1, ThresholdTypes.Binary);
- Mat result2 = threshCropSobel.Clone();
- for (int i = 20; i < result2.Rows - 1; i++)
- {
- sum = result2[i, i + 2, 0, result2.Cols].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Insert(1, newOrdinate + ordinates[0] + 20);
- break;
- }
- }
- //ImageShow(crop, threshCropSobel * 255);
- }
- /// <summary>
- /// 提取无导电布双层铜层厚较大时
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void WuDaodianbuShuangcengCenghou(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图像处理
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fill = new Mat();
- Fill(thresh, out fill, 1);
- Mat fanse = 1 - fill;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- //Mat close2 = new Mat();
- //Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 2000, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
- //坐标提取
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate + 5);
- i += 100;
- }
- }
- }
- public void WudaodianbuShuangcengD(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图像处理
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(40, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fill = new Mat();
- Fill(thresh, out fill, 1);
- Mat fanse = 1 - fill;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- Mat close2 = new Mat();
- Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
- Mat nomin = new Mat();
- GetArea(close2, out nomin, 3000, true);
- //Mat noCircle = new Mat();
- //RemoveCircles(nomin, out noCircle);
- //ImageShow(thresh * 255, and * 255,close2*255, nomin * 255 + gray);
- //坐标提取
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate + 5);
- i += 30;
- }
- }
- //迭代
- if (ordinates.Count < 11)
- {
- for (int i = 0; i < ordinates[0]; i++)
- {
- sum = result[i, i + 2, 0, result.Cols].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Insert(0, newOrdinate + 5);
- break;
- }
- }
- }
- if (ordinates.Count == 12)
- ordinates.RemoveAt(11);
- if (ordinates.Count == 11)
- {
- ordinates[6] += 6;
- ordinates[8] += 6;
- ordinates[10] += 10;
- }
- }
- public void WudaodianbuShuangcengQita(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图像处理
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fill = new Mat();
- Fill(thresh, out fill, 1);
- Mat fanse = 1 - fill;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- //Mat close2 = new Mat();
- //Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 2000, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
- //坐标提取
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate + 5);
- i += 30;
- }
- }
- }
- /// <summary>
- /// 提取无导电布三层坐标
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void WudaodianbuSanceng(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图像处理
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fill = new Mat();
- Fill(thresh, out fill, 1);
- Mat fanse = 1 - fill;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- //Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- //Mat close2 = new Mat();
- //Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 2000, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
- //坐标提取
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate + 5);
- i += 30;
- }
- }
- if (ordinates[1] - ordinates[0] > 300)
- {
- if (ordinates[2] - ordinates[1] > 120)
- {
- for (int i = ordinates[1] + 30; i < ordinates[2]; i++)
- {
- sum = result[i, i + 2, 0, result.Cols].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Insert(2, newOrdinate);
- break;
- }
- }
- }
- }
- else
- {
- if (ordinates[3] - ordinates[2] > 120)
- {
- for (int i = ordinates[2] + 30; i < ordinates[3]; i++)
- {
- sum = result[i, i + 2, 0, result.Cols].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Insert(3, newOrdinate);
- break;
- }
- }
- }
- }
- ////添加超出范围的线条
- //Mat stright = new Mat();
- //List<int> strights = new List<int>();
- //KeepStraight(result, out stright, out strights);
- ////插入原坐标没有的点
- //for (int i = 0; i < strights.Count; i++)
- //{
- // for (int j = 0; j < ordinates.Count - 1; j++)
- // {
- // if (Math.Abs(strights[i] - ordinates[j]) < 30 || Math.Abs(strights[i] - ordinates[j + 1]) < 30)
- // break;
- // else if (strights[i] > ordinates[j] && strights[i] < ordinates[j + 1])
- // {
- // ordinates.Insert(j + 1, strights[i]);
- // break;
- // }
- // }
- //}
- ////删除过近的坐标
- //List<int> ordinatesCopy = ordinates.ToList();
- //int count = 0;
- //for (int i = 0; i < ordinates.Count - 1; i++)
- //{
- // if (ordinates[i + 1] - ordinates[i] < 10)
- // {
- // ordinatesCopy.RemoveAt(i - count);
- // count++;
- // }
- //}
- //ordinates = ordinatesCopy.ToList();
- //增加最下层坐标
- if (ordinates.Count < 10)
- {
- Mat bottom = gray.Clone();
- Cv2.Rectangle(bottom, new Rect(0, 0, bottom.Cols, ordinates[ordinates.Count - 1]), new Scalar(0), -1);
- Cv2.GaussianBlur(bottom, bottom, new Size(5, 5), 3);
- Mat edge = new Mat();
- EdgeY(bottom, out edge);
- Mat threshEdge = new Mat();
- threshEdge = edge.Threshold(30, 1, ThresholdTypes.Binary);
- Mat seClose3 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- Mat close = new Mat();
- Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose3);
- Mat nomin3 = new Mat();
- GetArea(close, out nomin3, 500, true);
- //Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
- //Mat open = new Mat();
- //Cv2.MorphologyEx(threshEdge, open, MorphTypes.Open, seOpen);
- //ImageShow(threshEdge * 255,close*255,nomin3*255);
- Mat result2 = nomin3.Clone();
- for (int i = ordinates[ordinates.Count - 1] + 30; i < result2.Rows - 1; i++)
- {
- sum = result2[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate);
- break;
- }
- }
- }
- //else
- //{
- //}
- }
- /// <summary>
- /// 提取无导电布四层铜的坐标
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void WuDaodianbuSiceng(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图像处理
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(5, 5), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(60, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fill = new Mat();
- Fill(thresh, out fill, 1);
- Mat fanse = 1 - fill;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- Mat seClose2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- Mat close2 = new Mat();
- Cv2.MorphologyEx(and, close2, MorphTypes.Close, seClose2);
- Mat nomin = new Mat();
- GetArea(close2, out nomin, 2000, true);
- //ImageShow(threshSobel * 255, thresh * 255, and * 255, nomin * 255 + gray);
- //坐标提取
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate + 5);
- i += 30;
- }
- }
- }
- /// <summary>
- /// 提取有内部线条的坐标
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="ordinates"></param>
- public void WuDaodianbuQicengtong(Mat gray, out List<int> ordinates)
- {
- ordinates = new List<int>();
- //图像处理
- Mat filter = new Mat();
- Cv2.GaussianBlur(gray, filter, new Size(9, 9), 3);
- Mat sobel = new Mat();
- EdgeY2(filter, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(80, 1, ThresholdTypes.Binary);
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 7));
- Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, seClose);
- Mat fill = new Mat();
- Fill(thresh, out fill, 1);
- Mat fanse = 1 - fill;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshSobel, and);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 2000, true);
- Mat threshSobel2 = new Mat();
- threshSobel2 = sobel.Threshold(180, 1, ThresholdTypes.Binary);
- //ImageShow(threshSobel * 255, and * 255, nomin * 255 + gray,threshSobel2*255);
- //坐标提取
- Mat result = nomin.Clone();
- int middle = result.Cols / 2;
- Scalar sum = new Scalar();
- for (int i = 0; i < result.Rows - 1; i++)
- {
- sum = result[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate + 5);
- i += 30;
- }
- }
- //对内部线进行提取
- //对内部进行阈值分割
- int start1 = ordinates[0];
- int start2 = ordinates[2];
- Mat crop1 = gray[ordinates[0], ordinates[1], middle - 100, middle + 100].Clone();
- Mat crop2 = gray[ordinates[2], ordinates[3], middle - 100, middle + 100].Clone();
- Mat threshCrop1 = 1 - crop1.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat threshCrop2 = 1 - crop2.Threshold(0, 1, ThresholdTypes.Otsu);
- //ImageShow(threshCrop1 * 255, threshCrop2 * 255);
- for (int i = 10; i < threshCrop1.Rows - 1; i++)
- {
- sum = threshCrop1[i, i + 2, 0, threshCrop1.Cols].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Insert(1, newOrdinate + ordinates[0]);
- break;
- }
- }
- for (int i = threshCrop1.Rows - 30; i > 2; i--)
- {
- sum = threshCrop1[i - 2, i, 0, threshCrop1.Cols].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Insert(2, newOrdinate + ordinates[0]);
- break;
- }
- }
- for (int i = 30; i < threshCrop2.Rows - 2; i++)
- {
- sum = threshCrop2[i, i + 2, 0, threshCrop2.Cols].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Insert(5, newOrdinate + start2);
- break;
- }
- }
- for (int i = threshCrop2.Rows - 10; i > 2; i--)
- {
- sum = threshCrop2[i - 2, i, 0, threshCrop2.Cols].Sum();
- if ((int)sum > 100)
- {
- int newOrdinate = i;
- ordinates.Insert(6, newOrdinate + start2);
- break;
- }
- }
- }
- /// <summary>
- /// 通过计算二值图最集中的区域来得到叠构的提取区域
- /// </summary>
- /// <param name="contour"></param>
- /// <param name="dataArea"></param>
- public void DiegouDataArea(Mat contour, out int dataArea)
- {
- Scalar sum = new Scalar(0);
- Scalar max = new Scalar(0);
- int maxArea = 0;
- for (int j = contour.Cols / 5; j < contour.Cols / 5 * 4 && j < contour.Cols - 40; j++)
- {
- sum = contour[0, contour.Rows, j, j + 40].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- maxArea = j;
- }
- }
- dataArea = maxArea + 20;
- }
- /// <summary>
- /// 得到去噪后的边缘线
- /// </summary>
- /// <param name="image"></param>
- /// <param name="edge"></param>
- public void GetEdge(Mat image, out Mat edge)
- {
- Mat filter = new Mat();
- Cv2.GaussianBlur(image, filter, new Size(11, 11), 5, 5);
- edge = new Mat();
- Cv2.Canny(filter, edge, 10, 10);
- //去除小连通域噪声
- Mat[] contours;
- Mat hierachy = new Mat();
- Cv2.FindContours(edge, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- int area = 2;
- Mat noise = Mat.Zeros(edge.Rows, edge.Cols, edge.Type());
- for (int i = 0; i < contours.Count(); i++)
- {
- if (Cv2.ContourArea(contours[i]) > area)
- {
- Cv2.DrawContours(noise, contours, i, new Scalar(1));
- }
- }
- //减去噪声+开运算
- Mat noNoise = edge - noise * 255;
- //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- //Mat close = new Mat();
- //Cv2.MorphologyEx(noNoise, close, MorphTypes.Close, seClose);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
- Mat open = new Mat();
- Cv2.MorphologyEx(noNoise, open, MorphTypes.Open, seOpen);
- edge = open;
- }
- /// <summary>
- /// 计算线的坐标,当10行内点的数量大于10时记录平均坐标,用的是非膨胀后开运算结果图
- /// </summary>
- /// <param name="edge"></param>
- /// <param name="dataArea"></param>
- /// <param name="ordinates"></param>
- public void GetOrdinate(Mat edge, int dataArea, out List<int> ordinates)
- {
- int upper = 0;
- int lower = 0;
- int upperBound = 0, lowerBound = 0;
- int count = 0;
- int meanOrdinate = 0;
- ordinates = new List<int>();
- Scalar sum = new Scalar(0);
- for (int i = 0; i < edge.Rows; i++)
- {
- sum = edge[i, i + 3, 0, edge.Cols].Sum();
- if ((int)sum > 50)
- {
- upper = i;
- break;
- }
- }
- for (int i = edge.Rows - 1; i > 0; i--)
- {
- sum = edge[i - 3, i, 0, edge.Cols].Sum();
- if ((int)sum > 40)
- {
- lower = i;
- break;
- }
- }
- upperBound = upper - 10;
- while (lowerBound < lower + 50)
- {
- count = 0;
- meanOrdinate = 0;
- while (count < 10 /*&& lowerBound + 20 < edge.Rows*/)
- {
- upperBound += 5;
- lowerBound = upperBound + 5;
- for (int i = upperBound; i < lowerBound; i++)
- {
- for (int j = dataArea - 25; j < dataArea + 25; j++)
- {
- if (edge.Get<byte>(i, j) > 0)
- {
- meanOrdinate += i;
- count++;
- }
- }
- }
- }
- if (count > 5)
- {
- int newOrdinate = new int();
- newOrdinate = meanOrdinate / count;
- if (ordinates.Count > 0 && newOrdinate > ordinates[ordinates.Count - 1] + 40)
- {
- ordinates.Add(newOrdinate);
- upper += 10;
- }
- else if (ordinates.Count == 0)
- {
- ordinates.Add(newOrdinate);
- upper += 10;
- }
- }
- }
- }
- public void GetOrdinate2(Mat edge, int dataArea, out List<int> ordinates)
- {
- int upper = 0;
- int lower = 0;
- int upperBound = 0, lowerBound = 0;
- int count = 0;
- int meanOrdinate = 0;
- ordinates = new List<int>();
- Scalar sum = new Scalar(0);
- for (int i = 0; i < edge.Rows; i++)
- {
- sum = edge[i, i + 3, 0, edge.Cols].Sum();
- if ((int)sum > 70)
- {
- upper = i;
- break;
- }
- }
- for (int i = edge.Rows - 1; i > 0; i--)
- {
- sum = edge[i - 3, i, 0, edge.Cols].Sum();
- if ((int)sum > 40)
- {
- lower = i;
- break;
- }
- }
- upperBound = upper - 10;
- int j = 0;
- while (upperBound + 20 < lower)
- {
- for (int i = upperBound; i < lower + 50; i += 1)
- {
- sum = edge[i, i + 5, dataArea - 50, dataArea + 50].Sum();
- if ((int)sum > 10)
- {
- meanOrdinate = i + 1;
- if (ordinates.Count == 0)
- {
- ordinates.Add(meanOrdinate);
- upperBound = i + 10;
- break;
- }
- else if (ordinates.Count > 0 && meanOrdinate > ordinates[ordinates.Count - 1] + 40)
- {
- ordinates.Add(meanOrdinate);
- upperBound = (i + 10);
- break;
- }
- }
- j = i;
- }
- if (j == lower + 49)
- break;
- }
- }
- /// <summary>
- /// 对叠构的上区域重新进行处理并判断是否有线条存在
- /// </summary>
- /// <param name="image"></param>
- /// <param name="ordinate"></param>
- /// <param name="border"></param>
- public void UpperProcess(Mat image, out int ordinate, int[] border)
- {
- ordinate = 0;
- Mat result = new Mat();
- Mat crop = image[border[0], border[1], 0, image.Cols].Clone();
- //ImageShow(crop);
- Cv2.GaussianBlur(crop, crop, new Size(11, 11), 5, 5);
- //ImageShow(crop);
- //Cv2.MedianBlur(image, image, 11);
- //ImageShow(image);
- Mat edgeSobel = new Mat();
- EdgeY(crop, out edgeSobel);
- //Sobel(image, out edgeSobel);
- Mat threshEdge = new Mat();
- double t2 = Cv2.Threshold(edgeSobel, threshEdge, 0, 255, ThresholdTypes.Otsu);
- threshEdge = edgeSobel.Threshold(15, 255, ThresholdTypes.Binary);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
- Mat close = new Mat();
- Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
- close = close / 255;
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(9, 1));
- Mat open = new Mat();
- Cv2.MorphologyEx(close, open, MorphTypes.Open, seOpen);
- GetArea(open, out close, 400, true);
- result = close.Clone();
- //ImageShow(crop, edgeSobel, threshEdge, result * 255);
- Scalar sum = new Scalar(0);
- for (int i = 0; i < crop.Rows - 5; i++)
- {
- sum = result[i, i + 2, result.Cols / 3, result.Cols / 3 * 2].Sum();
- if ((int)sum > result.Cols / 2)
- {
- ordinate = i;
- break;
- }
- }
- }
- public void DiegouImageProcess(Mat gray, out Mat result)
- {
- result = new Mat();
- //大致轮廓线
- Mat newGray = new Mat();
- Cv2.GaussianBlur(gray, newGray, new Size(15, 15), 5, 5);
- Mat filter = new Mat();
- //Cv2.MedianBlur(newGray, filter, 9);
- PointEnhancement(newGray, out filter);
- //Mat blur = new Mat();
- //Cv2.MedianBlur(filter, blur, 7);
- Mat edgeSobel = new Mat();
- Edge(filter, out edgeSobel);
- Mat threshEdge = new Mat();
- double t2 = Cv2.Threshold(edgeSobel, threshEdge, 0, 255, ThresholdTypes.Otsu);
- threshEdge = edgeSobel.Threshold(20, 255, ThresholdTypes.Binary);
- //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
- //Mat close = new Mat();
- //Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(11, 1));
- Mat open = new Mat();
- Cv2.MorphologyEx(threshEdge, open, MorphTypes.Open, seOpen);
- Mat sobel4 = new Mat();
- Sobel(open, out sobel4);
- //Mat nomin = new Mat();
- //GetArea(open, out nomin, 20, true);
- //Mat nocircle = new Mat();
- //RemoveCircles(open, out nocircle);
- //ImageShow(edgeSobel, threshEdge, open/*, nocircle*255*/);
- //铜线
- Mat thresh = new Mat();
- thresh = gray.Threshold(0, 255, ThresholdTypes.Otsu);
- Mat fill = new Mat();
- Fill(thresh, out fill, 255);
- Mat edge2 = new Mat();
- Sobel(fill, out edge2);
- //铜内部线
- Mat edge3 = new Mat();
- Sobel(filter, out edge3);
- Mat thresh3 = edge3.Threshold(10, 255, ThresholdTypes.Binary);
- Mat and = new Mat();
- Cv2.BitwiseAnd(thresh, thresh3, and);
- Mat seErode = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(15, 3));
- Mat erode = new Mat();
- Cv2.Erode(and, erode, seErode);
- Mat seOpen2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 3));
- Mat open2 = new Mat();
- Cv2.MorphologyEx(and, open2, MorphTypes.Open, seOpen2);
- Mat nomin2 = new Mat();
- GetArea(open2, out nomin2, 200, true);
- //ImageShow( and,erode,open2,nomin2*255);
- //铜内部线2
- Mat and2 = new Mat();
- Cv2.BitwiseAnd(gray, thresh, and2);
- Scalar sum = and2.Sum();
- int num = 0;
- Mat idx = new Mat();
- Cv2.FindNonZero(and2, idx);
- num = idx.Rows;
- int T = (int)sum / num;
- Mat thresh4 = and2.Threshold(T, 255, ThresholdTypes.Binary);
- Mat seClose3 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- Mat close3 = new Mat();
- Cv2.MorphologyEx(thresh4, close3, MorphTypes.Close, seClose3);
- Mat fill2 = new Mat();
- Fill(close3, out fill2, 255);
- Mat nomin3 = new Mat();
- GetArea(fill2, out nomin3, 10000, true);
- Mat fanse = 255 - nomin3 * 255;
- Mat edge4 = new Mat();
- Sobel(fanse, out edge4);
- //ImageShow(thresh4, edge4);
- //最终
- Mat fanse2 = 255 - thresh;
- Mat and3 = new Mat();
- Cv2.BitwiseAnd(fanse2, sobel4, and3);
- result = and3 + edge2 + edge4;/*nomin2 * 255;*/
- //ImageShow(result);
- }
- public void DiegouImageProcess2(Mat gray, out Mat result)
- {
- result = new Mat();
- Mat filter = new Mat();
- Mat junheng = new Mat();
- Mat blur = new Mat();
- Mat zengqiang = new Mat();
- Mat edge = new Mat();
- Mat thresh = new Mat();
- //PointEnhancement(gray, out zengqiang);
- //Cv2.EqualizeHist(zengqiang, junheng);
- //Cv2.GaussianBlur(junheng, filter, new Size(15,15), 5, 5);
- ////PointEnhancement(filter, out zengqiang);
- //Cv2.Blur(filter, blur, new Size (3,3));
- //Cv2.GaussianBlur(gray, filter, new Size(15, 15), 5, 5);
- Cv2.Blur(gray, blur, new Size(3, 3));
- PointEnhancement(blur, out zengqiang);
- //Cv2.EqualizeHist(zengqiang, junheng);
- //ImageShow(gray, zengqiang, junheng, /*filter,*/blur);
- Edge(zengqiang, out edge);
- Mat threshEdge = edge.Threshold(30, 255, ThresholdTypes.Binary);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- Mat open = new Mat();
- Cv2.MorphologyEx(threshEdge, open, MorphTypes.Open, seOpen);
- double t = Cv2.Threshold(gray, thresh, 0, 255, ThresholdTypes.Otsu);
- thresh = gray.Threshold(t - 20, 255, ThresholdTypes.Binary);
- Mat fill = new Mat();
- Fill(thresh, out fill, 255);
- Mat fanse = 255 - fill;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, open, and);
- //Mat seOpen2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 1));
- //Mat open2 = new Mat();
- //Cv2.MorphologyEx(and, open2, MorphTypes.Open, seOpen2);
- Mat nomin = new Mat();
- GetArea(and, out nomin, 500, true);
- gray = gray + nomin * 100;
- ImageShow(thresh, fill, fanse);
- ImageShow(and, nomin * 255, gray);
- //Cv2.Canny(filter, edge, 7, 0);
- //Mat nomin = new Mat();
- //GetArea(edge, out nomin, 5, true);
- //ImageShow(edge, nomin * 255);
- //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
- //CvTrackbarCallback cvTrackbarCallback2 = new CvTrackbarCallback(Text2);
- //Window window = new Window("tbar");//创建一个新窗口"tbar"
- //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 25, 500, cvTrackbarCallback);
- //CvTrackbar cvTrackbar2 = new CvTrackbar("bar2", "tbar", 35, 500, cvTrackbarCallback2);
- //Cv2.WaitKey();
- //void Text(int value)
- //{
- // minValue = value;
- // Cv2.Canny(filter, edge, minValue, maxValue);
- // new Window("tbar", edge);
- //}
- //void Text2(int value)
- //{
- // maxValue = value;
- // Cv2.Canny(filter, edge, minValue, maxValue);
- // new Window("tbar", edge);
- //}
- //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
- //CvTrackbarCallback cvTrackbarCallback2 = new CvTrackbarCallback(Text2);
- //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 25, 225, cvTrackbarCallback);
- //CvTrackbar cvTrackbar2 = new CvTrackbar("bar2", "tbar", 35, 225, cvTrackbarCallback2);
- //Cv2.WaitKey();
- //void Text(int value)
- //{
- // color = value;
- // Cv2.BilateralFilter(gray, filter, d, color, 3);
- // PointEnhancement(filter, out zengqiang);
- // Sobel(zengqiang, out edge);
- // thresh = edge.Threshold(5, 255, ThresholdTypes.Binary);
- // new Window("tbar", WindowMode.Normal, thresh);
- //}
- //void Text2(int value)
- //{
- // d = value;
- // Cv2.BilateralFilter(gray, filter, d, color, 3);
- // PointEnhancement(filter, out zengqiang);
- // Sobel(zengqiang, out edge);
- // thresh = edge.Threshold(5, 255, ThresholdTypes.Binary);
- // new Window("tbar", WindowMode.Normal, thresh);
- //}
- }
- public void DiegouImageProcess3(Mat gray, out Mat result, out Mat edgeSobel)
- {
- result = new Mat();
- //大致轮廓线
- Mat newGray = new Mat();
- Cv2.GaussianBlur(gray, newGray, new Size(11, 11), 3, 3);
- Mat blur = new Mat();
- Cv2.Blur(newGray, blur, new Size(3, 3));
- Mat filter = new Mat();
- PointEnhancement(blur, out filter);
- edgeSobel = new Mat();
- //Edge(filter, out edgeSobel);
- EdgeY2(filter, out edgeSobel);
- Mat threshEdge = new Mat();
- double t2 = Cv2.Threshold(edgeSobel, threshEdge, 0, 255, ThresholdTypes.Otsu);
- threshEdge = edgeSobel.Threshold(80, 255, ThresholdTypes.Binary);
- Mat thresh = gray.Threshold(0, 255, ThresholdTypes.Otsu);
- Mat fill = new Mat();
- Fill(thresh, out fill, 255);
- Mat sobel2 = new Mat();
- Sobel(fill, out sobel2);
- Mat fanse = 255 - fill;
- Mat and = new Mat();
- Cv2.BitwiseAnd(fanse, threshEdge, and);
- //ImageShow( threshEdge,sobel2, and,gray+and);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- Mat close = new Mat();
- Cv2.MorphologyEx(and, close, MorphTypes.Close, seClose);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(7, 1));
- Mat open = new Mat();
- Cv2.MorphologyEx(close, open, MorphTypes.Open, seOpen);
- Mat nomin4 = new Mat();
- GetArea(open, out nomin4, 2000, true);
- Mat sobel4 = new Mat();
- Sobel(nomin4, out sobel4);
- //ImageShow(open,close,nomin4*255);
- result = sobel4 * 100 + sobel2;/*nomin2 * 255;*/
- //ImageShow(open,nomin4*255+gray ,result+gray);
- }
- public void DiegouImageProcess4(Mat gray, out Mat result)
- {
- result = new Mat(gray.Size(), gray.Type());
- int middle = gray.Cols / 2;
- Scalar sum = new Scalar();
- Scalar lastSum = new Scalar(0);
- byte[] cha = new byte[gray.Rows];
- for (int i = 0; i < gray.Rows; i++)
- {
- sum = gray[i, i + 1, middle - 200, middle + 200].Sum();
- if (i == 0)
- {
- cha[i] = 0;
- }
- else
- {
- cha[i] = (byte)Math.Abs((int)sum - (int)lastSum);
- }
- lastSum = sum;
- }
- for (int i = 0; i < gray.Rows; i++)
- {
- for (int j = 0; j < gray.Cols; j++)
- {
- result.Set<byte>(i, j, cha[i]);
- }
- }
- ImageShow(result);
- }
- public void DiegouOrdinates(Mat image, Mat gray, Mat edgeSobel, bool daodianbu, out List<int> ordinates)
- {
- ordinates = new List<int>();
- Scalar sum = new Scalar(0);
- int middle = image.Cols / 2;
- image = image.Threshold(10, 1, ThresholdTypes.Binary);
- //Mat hierachy = new Mat();
- //Mat[] contoursMat;
- //Cv2.FindContours(image, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- Mat thresh = gray.Threshold(0, 255, ThresholdTypes.Otsu);
- //ImageShow(thresh);
- int[] y = new int[4];
- int b = 0;
- for (int i = 0; i < thresh.Rows; i++)
- {
- sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
- if ((int)sum > 50)
- {
- y[0] = i;
- break;
- }
- }
- for (int i = y[0] + 50; i < thresh.Rows; i++)
- {
- sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
- if ((int)sum == 0)
- {
- y[1] = i;
- break;
- }
- }
- for (int i = y[1] + 50; i < thresh.Rows; i++)
- {
- sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
- if ((int)sum > 50)
- {
- y[2] = i;
- break;
- }
- }
- for (int i = y[2]; i < thresh.Rows; i++)
- {
- sum = thresh[i, i + 1, 0, thresh.Cols].Sum();
- if ((int)sum == 0)
- {
- y[3] = i;
- break;
- }
- }
- Mat newThresh = thresh / 255;
- for (int j = 0; j < thresh.Cols; j++)
- {
- sum = newThresh[0, thresh.Rows, j, j + 1].Sum();
- if ((int)sum > 300)
- {
- b = j;
- break;
- }
- }
- int type = 0;
- int[] cha = new int[] { y[1] - y[0], y[3] - y[2], y[2] - y[1] };
- if ((y[1] - y[0]) > 180 && (y[3] - y[2]) > 180)
- {
- type = 1;//单独两个铜
- }
- 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)))
- {
- type = 2;//有内部线
- }
- if (type == 1)//当是只有两层铜的时候
- {
- for (int i = 0; i < image.Rows - 20; i++)
- {
- sum = image[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 150)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate /*+ 10*/);
- i += 150;
- }
- }
- if (ordinates.Count <= 3)
- {
- ordinates.Clear();
- for (int i = 0; i < image.Rows - 20; i++)
- {
- sum = image[i, i + 5, middle - 100, middle + 100].Sum();
- if ((int)sum > 150)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate /*+ 10*/);
- i += 150;
- }
- }
- }
- }
- else if (type == 2)
- {
- Mat threshEdge = edgeSobel.Threshold(180, 255, ThresholdTypes.Binary);
- Mat fanse = 255 - threshEdge;
- //ImageShow(threshEdge, fanse);
- for (int i = 0; i < image.Rows - 20; i++)
- {
- sum = image[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 150)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate + 10);
- i += 30;
- }
- }
- for (int i = ordinates[0]; i < ordinates[0] + 30; i++)
- {
- sum = fanse[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 50)
- {
- ordinates.Insert(1, i + 7);
- break;
- }
- }
- for (int i = ordinates[0] + 40; i < ordinates[2]; i++)
- {
- sum = threshEdge[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 150)
- {
- ordinates.Insert(2, i + 10);
- break;
- }
- }
- for (int i = ordinates[0] + 350; i < threshEdge.Rows; i++)
- {
- sum = threshEdge[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > 150)
- {
- ordinates.Insert(5, i + 12);
- break;
- }
- }
- for (int i = ordinates[6] - 15; i > 0; i--)
- {
- sum = fanse[i - 2, i, middle - 100, middle + 100].Sum();
- if ((int)sum > 50)
- {
- ordinates.Insert(6, i - 7);
- break;
- }
- }
- }
- else
- {
- int t = 2;
- for (int i = 0; i < image.Rows - 20; i++)
- {
- if (ordinates.Count == 0 && daodianbu)
- t = 180;
- else
- t = 150;
- sum = image[i, i + 2, middle - 100, middle + 100].Sum();
- if ((int)sum > t)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate + 5);
- i += 30;
- //if (ordinates.Count == 1)
- // i += 10;
- //else
- // i += 30;
- }
- }
- ordinates[0] += 5;
- //ordinates[1] -= 5;
- Mat threshEdge = edgeSobel.Threshold(40, 1, ThresholdTypes.Binary);
- //ImageShow(threshEdge * 255);
- if (daodianbu)
- {
- for (int i = ordinates[0] - 40; i < ordinates[0] - 10; i++)
- {
- sum = threshEdge[i, i + 2, middle - 50, middle + 50].Sum();
- if ((int)sum > 75)
- {
- int newOrdinate = i;
- ordinates.Insert(0, i);
- break;
- }
- }
- for (int i = ordinates[ordinates.Count - 1] + 40; i > ordinates[ordinates.Count - 1] + 10; i--)
- {
- sum = threshEdge[i - 2, i, middle - 50, middle + 50].Sum();
- if ((int)sum > 75)
- {
- int newOrdinate = i;
- ordinates.Add(i);
- break;
- }
- }
- }
- //for (int i = ordinates[ordinates.Count-1] + 40; i > ordinates[ordinates.Count-1] + 5&&i<thresh.Rows-20; i--)
- //{
- // sum = image[i-5, i , middle - 100, middle + 100].Sum();
- // if ((int)sum > 100)
- // {
- // int newOrdinate = i;
- // ordinates.Add(newOrdinate /*+ 10*/);
- // break;
- // }
- //}
- //for (int i = ordinates[0] - 40; i < ordinates[0] - 5; i++)
- //{
- // sum = image[i , i+5, middle - 100, middle + 100].Sum();
- // if ((int)sum > 100)
- // {
- // ordinates.Insert(0, i);
- // break;
- // }
- //}
- if (ordinates.Count <= 3)
- {
- ordinates.Clear();
- for (int i = 0; i < image.Rows - 20; i++)
- {
- sum = image[i, i + 5, middle - 100, middle + 100].Sum();
- if ((int)sum > 150)
- {
- int newOrdinate = i;
- ordinates.Add(newOrdinate /*+ 10*/);
- i += 150;
- }
- }
- }
- }
- }
- public void DieGouTopBottom(Mat gray, out int ordinate, int[] range, string direction)
- {
- ordinate = 0;
- Mat crop = gray[range[0], range[1], gray.Cols / 2 - 100, gray.Cols / 2 + 100].Clone();
- Cv2.GaussianBlur(crop, crop, new Size(11, 11), 5, 5);
- Mat edgeSobel = new Mat();
- EdgeY(crop, out edgeSobel);
- Mat threshEdge = new Mat();
- double t2 = Cv2.Threshold(edgeSobel, threshEdge, 0, 255, ThresholdTypes.Otsu);
- threshEdge = edgeSobel.Threshold(15, 255, ThresholdTypes.Binary);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- Mat close = new Mat();
- Cv2.MorphologyEx(threshEdge, close, MorphTypes.Close, seClose);
- close = close / 255;
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- Mat open = new Mat();
- Cv2.MorphologyEx(close, open, MorphTypes.Open, seOpen);
- GetArea(open, out close, 1000, true);
- Mat result = close.Clone();
- //ImageShow(crop, edgeSobel, threshEdge, result * 255);
- Scalar sum = new Scalar(0);
- switch (direction)
- {
- case "top":
- for (int i = 0; i < crop.Rows - 10; i++)
- {
- sum = result[i, i + 2, 0, result.Cols].Sum();
- if ((int)sum > 100)
- {
- ordinate = i;
- break;
- }
- }
- break;
- case "bottom":
- for (int i = crop.Rows - 1; i > 2; i--)
- {
- sum = result[i - 2, i, 0, result.Cols].Sum();
- if ((int)sum > 100)
- {
- ordinate = i;
- break;
- }
- }
- break;
- }
- }
- //锡膏
- //锡膏H
- public void XigaoKaikouXiangnei(Mat gray, int[] dataArea, int start, out int[] leftShangOrdinates, out int[] rightShangOrdinates, out int[] leftXiaOrdinates, out int[] rightXiaOrdinates)
- {
- leftShangOrdinates = new int[3];
- rightShangOrdinates = new int[3];
- leftXiaOrdinates = new int[3];
- rightXiaOrdinates = new int[3];
- #region//找上下边界
- int[] leftExtractionRange = new int[2];
- int[] rightExtractionRange = new int[2];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
- thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
- //ImageShow(thresh * 255);
- Scalar sum = new Scalar();
- Mat result1 = thresh.Clone();
- //for (int i = start; i < result1.Rows; i++)//左侧上界
- //{
- // sum = result1[i, i + 1, dataArea[1]-350, dataArea[1] - 100].Sum();
- // if ((int)sum > 100)
- // {
- // leftExtractionRange[0] = i;
- // break;
- // }
- //}
- for (int i = start; i < result1.Rows; i++)//左侧上界
- {
- //sum = result1[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
- if (result1.Get<byte>(i, dataArea[1] - 100) > 0)
- {
- leftExtractionRange[0] = i;
- break;
- }
- }
- for (int i = leftExtractionRange[0] + 50; i < result1.Rows; i++)//左侧下界
- {
- sum = result1[i, i + 1, dataArea[1] - 200, dataArea[1] - 100].Sum();
- if ((int)sum == 0)
- {
- leftExtractionRange[1] = i;
- break;
- }
- }
- //for (int i = start; i < result1.Rows; i++)//右侧上界
- //{
- // sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2]+350].Sum();
- // if ((int)sum > 100)
- // {
- // rightExtractionRange[0] = i;
- // break;
- // }
- //}
- for (int i = start; i < result1.Rows; i++)//右侧上界
- {
- //sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
- if (result1.Get<byte>(i, dataArea[2] + 100) > 0)
- {
- rightExtractionRange[0] = i;
- break;
- }
- }
- for (int i = rightExtractionRange[0] + 50; i < result1.Rows; i++)//右侧下界
- {
- sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 200].Sum();
- if ((int)sum == 0)
- {
- rightExtractionRange[1] = i;
- break;
- }
- }
- //LineShow(gray, dataArea[1], leftExtractionRange[0], dataArea[1], leftExtractionRange[1]);
- //LineShow(gray, dataArea[2], rightExtractionRange[0], dataArea[2], rightExtractionRange[1]);
- //ImageShow(gray);
- #endregion
- #region//找质量好的位置
- int[] maxSpacing = new int[2];
- Scalar max = new Scalar(0);
- for (int j = dataArea[1] - 300; j < dataArea[1]; j += 40)
- {
- sum = result1[leftExtractionRange[0], leftExtractionRange[1], j - 20, j + 20].Sum();
- if ((int)max < (int)sum)
- {
- max = sum;
- maxSpacing[0] = j;
- }
- }
- max = new Scalar(0);
- for (int j = dataArea[2]; j < dataArea[2] + 300; j += 40)
- {
- sum = result1[rightExtractionRange[0], rightExtractionRange[1], j - 20, j + 20].Sum();
- if ((int)max < (int)sum)
- {
- max = sum;
- maxSpacing[1] = j;
- }
- }
- //LineShow(gray, dataArea[0], start, dataArea[1], start);
- //LineShow(gray, dataArea[2], start, dataArea[3], start);
- //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
- //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
- //ImageShow(gray);
- #endregion
- Mat sobel = new Mat();
- Sobel(thresh, out sobel);
- Mat nomin = new Mat();
- GetArea(sobel, out nomin, 500, true);
- Mat result = nomin.Clone();
- #region//左上
- for (int i = leftExtractionRange[0] - 30; i < leftExtractionRange[0]; i++)
- {
- sum = result[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
- if ((int)sum > 0)
- {
- leftShangOrdinates[1] = i;
- break;
- }
- }
- //if (leftExtractionRange[0] - leftShangOrdinates[1] < 5)
- // leftShangOrdinates[1] = 0;
- if (leftShangOrdinates[1] != 0)
- {
- for (int j = dataArea[1] - 350; j < dataArea[1] - 100; j++)
- {
- if (result.Get<byte>(leftShangOrdinates[1], j) > 0)
- {
- leftShangOrdinates[0] = j;
- leftShangOrdinates[2] = leftExtractionRange[0];
- break;
- }
- }
- }
- #endregion
- #region //右上
- for (int i = rightExtractionRange[0] - 30; i < rightExtractionRange[0]; i++)
- {
- sum = result[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
- if ((int)sum > 0)
- {
- rightShangOrdinates[1] = i;
- break;
- }
- }
- //if (rightExtractionRange[0] - rightShangOrdinates[1] < 5)
- // rightShangOrdinates[1] = 0;
- if (rightShangOrdinates[1] != 0)
- {
- for (int j = dataArea[2] + 100; j < dataArea[2] + 350; j++)
- {
- if (result.Get<byte>(rightShangOrdinates[1], j) > 0)
- {
- rightShangOrdinates[0] = j;
- rightShangOrdinates[2] = rightExtractionRange[0];
- break;
- }
- }
- }
- #endregion
- Mat zengqiang = new Mat();
- PointEnhancement(gray, out zengqiang);
- Mat sobel2 = new Mat();
- //EdgeY2(gray, out sobel2);
- EdgeY2(zengqiang, out sobel2);
- Mat thresh2 = new Mat();
- thresh2 = sobel2.Threshold(200, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
- Mat nomin2 = new Mat();
- GetArea(thresh2, out nomin2, 50, true);
- Mat result2 = nomin2.Clone();
- //ImageShow(nomin2 * 255);
- #region//左下
- int compensate = 5;
- for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 20; i++)
- {
- sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[1] = i;
- break;
- }
- }
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
- {
- sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- leftXiaOrdinates[0] = maxSpacing[0];
- leftXiaOrdinates[1] += compensate;
- #endregion
- #region//右下
- for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 20; i++)
- {
- sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[1] = i;
- break;
- }
- }
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
- {
- sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- rightXiaOrdinates[0] = maxSpacing[1];
- rightXiaOrdinates[1] += compensate;
- #endregion
- #region//下层迭代
- if (leftXiaOrdinates[2] == 0 || rightXiaOrdinates[2] == 0)
- {
- int leftThresh = 200, rightThresh = 200;
- while (leftXiaOrdinates[2] == 0)
- {
- if (leftThresh == 0)
- break;
- leftThresh -= 10;
- thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- }
- while (rightXiaOrdinates[2] == 0)
- {
- if (rightThresh == 0)
- break;
- rightThresh -= 10;
- thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- }
- }
- #endregion
- if (leftShangOrdinates[2] - leftShangOrdinates[1] < 5)
- {
- leftShangOrdinates[0] = 0;
- leftShangOrdinates[1] = 0;
- leftShangOrdinates[2] = 0;
- }
- if (rightShangOrdinates[2] - rightShangOrdinates[1] < 5)
- {
- rightShangOrdinates[0] = 0;
- rightShangOrdinates[1] = 0;
- rightShangOrdinates[2] = 0;
- }
- //LineShow(gray, leftXiaOrdinates[0], leftXiaOrdinates[1], leftXiaOrdinates[0], leftXiaOrdinates[2]);
- //LineShow(gray, rightXiaOrdinates[0], rightXiaOrdinates[1], rightXiaOrdinates[0], rightXiaOrdinates[2]);
- //ImageShow(gray);
- }
- public void XigaoYouheikuai(Mat gray, int[] dataArea, int start, out int[] leftShangOrdinates, out int[] rightShangOrdinates, out int[] leftXiaOrdinates, out int[] rightXiaOrdinates)
- {
- leftShangOrdinates = new int[3];
- rightShangOrdinates = new int[3];
- leftXiaOrdinates = new int[3];
- rightXiaOrdinates = new int[3];
- #region//提取上下边界与黑框边界
- int[] leftExtractionRange = new int[2];
- int[] rightExtractionRange = new int[2];
- int[] blackBorder = new int[2];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
- thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
- Scalar sum = new Scalar();
- Mat result1 = thresh.Clone();
- for (int i = start; i < result1.Rows; i++)//左侧上界
- {
- sum = result1[i, i + 1, dataArea[0], dataArea[1] - 100].Sum();
- if ((int)sum > 100)
- {
- leftExtractionRange[0] = i;
- break;
- }
- }
- for (int i = leftExtractionRange[0]; i < result1.Rows; i++)//左侧下界
- {
- sum = result1[i, i + 1, dataArea[0], dataArea[1] - 100].Sum();
- if ((int)sum == 0)
- {
- leftExtractionRange[1] = i;
- break;
- }
- }
- for (int i = start; i < result1.Rows; i++)//右侧上界
- {
- sum = result1[i, i + 1, dataArea[2] + 100, dataArea[3]].Sum();
- if ((int)sum > 100)
- {
- rightExtractionRange[0] = i;
- break;
- }
- }
- for (int i = rightExtractionRange[0]; i < result1.Rows; i++)//右侧下界
- {
- sum = result1[i, i + 1, dataArea[2] + 100, dataArea[3]].Sum();
- if ((int)sum == 0)
- {
- rightExtractionRange[1] = i;
- break;
- }
- }
- Mat sobel = new Mat();
- Sobel(gray, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(10, 1, ThresholdTypes.Binary);
- Mat nomin4 = new Mat();
- GetArea(threshSobel, out nomin4, 500, true);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat open = new Mat();
- Cv2.MorphologyEx(threshSobel, open, MorphTypes.Open, seOpen);
- //ImageShow(threshSobel*255, nomin4 * 255);
- Mat result2 = nomin4.Clone();
- for (int j = dataArea[0]; j < dataArea[1]; j++)//提取左黑块边界
- {
- sum = result2[leftExtractionRange[0] - 100, leftExtractionRange[0] - 10, j, j + 1].Sum();
- if ((int)sum > 20)
- {
- blackBorder[0] = j;
- break;
- }
- }
- for (int j = dataArea[3]; j > dataArea[2]; j--)//提取右黑块边界
- {
- sum = result2[rightExtractionRange[0] - 100, rightExtractionRange[0] - 10, j - 1, j].Sum();
- if ((int)sum > 20)
- {
- blackBorder[1] = j;
- break;
- }
- }
- //LineShow(gray, dataArea[0], leftExtractionRange[0], dataArea[0], leftExtractionRange[1]);
- //LineShow(gray, dataArea[2], rightExtractionRange[0], dataArea[2], rightExtractionRange[1]);
- //LineShow(gray, blackBorder[0], leftExtractionRange[0] - 100, blackBorder[0], leftExtractionRange[0] - 10);
- //LineShow(gray, blackBorder[1], rightExtractionRange[0] - 100, blackBorder[1], rightExtractionRange[0] - 10);
- //ImageShow(gray, threshSobel * 255,open*255);
- #endregion
- Mat thresh2 = new Mat();
- thresh2 = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
- Mat sobel2 = new Mat();
- Sobel(thresh2, out sobel2);
- Mat nomin3 = new Mat();
- GetArea(sobel2, out nomin3, 500, true);
- Mat result3 = nomin3.Clone();
- //ImageShow(nomin3 * 255);
- #region//左上
- for (int i = leftExtractionRange[0] - 30; i < leftExtractionRange[0]; i++)
- {
- sum = result3[i, i + 1, dataArea[0], blackBorder[0]].Sum();
- if ((int)sum > 0)
- {
- leftShangOrdinates[1] = i;
- break;
- }
- }
- if (leftShangOrdinates[1] != 0)
- {
- for (int j = dataArea[0]; j < blackBorder[0]; j++)
- {
- if (result3.Get<byte>(leftShangOrdinates[1], j) > 0)
- {
- leftShangOrdinates[0] = j;
- leftShangOrdinates[2] = leftExtractionRange[0];
- break;
- }
- }
- }
- #endregion
- #region//右上
- for (int i = rightExtractionRange[0] - 30; i < rightExtractionRange[0]; i++)
- {
- sum = result3[i, i + 1, blackBorder[1], dataArea[3]].Sum();
- if ((int)sum > 0)
- {
- rightShangOrdinates[1] = i;
- break;
- }
- }
- if (rightShangOrdinates[1] != 0)
- {
- for (int j = blackBorder[1]; j < dataArea[3]; j++)
- {
- if (result3.Get<byte>(rightShangOrdinates[1], j) > 0)
- {
- rightShangOrdinates[0] = j;
- rightShangOrdinates[2] = rightExtractionRange[0];
- break;
- }
- }
- }
- #endregion
- //LineShow(gray, leftShangOrdinates[0], leftShangOrdinates[1], leftShangOrdinates[0], leftShangOrdinates[2]);
- //LineShow(gray, rightShangOrdinates[0], rightShangOrdinates[1], rightShangOrdinates[0], rightShangOrdinates[2]);
- //ImageShow(gray, sobel2 * 255);
- #region //提取最大间距的位置
- int[] maxSpacing = new int[2];
- Scalar max = new Scalar(0);
- for (int j = blackBorder[0]; j < dataArea[1]; j += 40)
- {
- sum = result1[leftExtractionRange[0], leftExtractionRange[1], j - 20, j + 20].Sum();
- if ((int)max < (int)sum)
- {
- max = sum;
- maxSpacing[0] = j;
- }
- }
- max = new Scalar(0);
- for (int j = dataArea[2]; j < blackBorder[1]; j += 40)
- {
- sum = result1[rightExtractionRange[0], rightExtractionRange[1], j - 20, j + 20].Sum();
- if ((int)max < (int)sum)
- {
- max = sum;
- maxSpacing[1] = j;
- }
- }
- //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
- //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
- //ImageShow(gray);
- #endregion
- Mat sobel3 = new Mat();
- EdgeY2(gray, out sobel3);
- Mat thresh3 = new Mat();
- thresh3 = sobel3.Threshold(200, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh3, new Rect(maxSpacing[0], 0, 1, thresh3.Rows), new Scalar(0), -1);
- Cv2.Rectangle(thresh3, new Rect(maxSpacing[1], 0, 1, thresh3.Rows), new Scalar(0), -1);
- Mat nomin2 = new Mat();
- GetArea(thresh3, out nomin2, 500, true);
- //ImageShow(gray, thresh3 * 255,nomin2*255,gray+nomin2*100);
- #region//左下
- int compensate = 5;
- Mat result4 = nomin2.Clone();
- for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 20; i++)
- {
- sum = result4[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[1] = i;
- break;
- }
- }
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
- {
- sum = result4[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- leftXiaOrdinates[0] = maxSpacing[0];
- leftXiaOrdinates[1] += compensate;
- #endregion
- #region//右下
- for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 20; i++)
- {
- sum = result4[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[1] = i;
- break;
- }
- }
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
- {
- sum = result4[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- rightXiaOrdinates[0] = maxSpacing[1];
- rightXiaOrdinates[1] += compensate;
- #endregion
- #region//下层迭代
- if (leftXiaOrdinates[2] == 0 || rightXiaOrdinates[2] == 0)
- {
- int leftThresh = 200, rightThresh = 200;
- while (leftXiaOrdinates[2] == 0)
- {
- if (leftThresh == 0)
- break;
- leftThresh -= 10;
- thresh3 = sobel3.Threshold(leftThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh3, new Rect(maxSpacing[0], 0, 1, thresh3.Rows), new Scalar(0), -1);
- GetArea(thresh3, out nomin2, 500, true);
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- }
- while (rightXiaOrdinates[2] == 0)
- {
- if (rightThresh == 0)
- break;
- rightThresh -= 10;
- thresh3 = sobel3.Threshold(rightThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh3, new Rect(maxSpacing[1], 0, 1, thresh3.Rows), new Scalar(0), -1);
- GetArea(thresh3, out nomin2, 500, true);
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- }
- }
- #endregion
- //LineShow(gray, leftXiaOrdinates[0], leftXiaOrdinates[1], leftXiaOrdinates[0], leftXiaOrdinates[2]);
- //LineShow(gray, rightXiaOrdinates[0], rightXiaOrdinates[1], rightXiaOrdinates[0], rightXiaOrdinates[2]);
- //ImageShow(gray,nomin2*255);
- }
- public void XigaoXiangShangWanqu(Mat gray, int[] dataArea, int start, out int[] leftShangOrdinates, out int[] rightShangOrdinates, out int[] leftXiaOrdinates, out int[] rightXiaOrdinates)
- {
- leftShangOrdinates = new int[3];
- rightShangOrdinates = new int[3];
- leftXiaOrdinates = new int[3];
- rightXiaOrdinates = new int[3];
- #region//上下边界
- int[] leftExtractionRange = new int[2];
- int[] rightExtractionRange = new int[2];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
- thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
- //ImageShow(thresh * 255);
- Scalar sum = new Scalar();
- Mat result1 = thresh.Clone();
- for (int i = start + 300; i < result1.Rows; i++)//左侧上界
- {
- //sum = result1[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
- if (result1.Get<byte>(i, dataArea[1] + 50) > 0)
- {
- leftExtractionRange[0] = i;
- break;
- }
- }
- for (int i = leftExtractionRange[0] + 50; i < result1.Rows; i++)//左侧下界
- {
- sum = result1[i, i + 1, dataArea[1] - 200, dataArea[1] - 100].Sum();
- if ((int)sum == 0)
- {
- leftExtractionRange[1] = i;
- break;
- }
- }
- for (int i = start + 300; i < result1.Rows; i++)//右侧上界
- {
- //sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
- if (result1.Get<byte>(i, dataArea[2] - 50) > 0)
- {
- rightExtractionRange[0] = i;
- break;
- }
- }
- for (int i = rightExtractionRange[0] + 50; i < result1.Rows; i++)//右侧下界
- {
- sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 200].Sum();
- if ((int)sum == 0)
- {
- rightExtractionRange[1] = i;
- break;
- }
- }
- //LineShow(gray, dataArea[1] + 50, leftExtractionRange[0], dataArea[1] + 50, leftExtractionRange[1]);
- //LineShow(gray, dataArea[2] - 50, rightExtractionRange[0], dataArea[2] - 50, rightExtractionRange[1]);
- //ImageShow(gray);
- #endregion
- #region//找质量好的位置
- int[] maxSpacing = new int[2];
- Scalar max = new Scalar(0);
- for (int j = dataArea[1] - 250; j < dataArea[1]; j += 40)
- {
- sum = result1[leftExtractionRange[0], leftExtractionRange[1], j - 20, j + 20].Sum();
- if ((int)max < (int)sum)
- {
- max = sum;
- maxSpacing[0] = j;
- }
- }
- max = new Scalar(0);
- for (int j = dataArea[2]; j < dataArea[2] + 250; j += 40)
- {
- sum = result1[rightExtractionRange[0], rightExtractionRange[1], j - 20, j + 20].Sum();
- if ((int)max < (int)sum)
- {
- max = sum;
- maxSpacing[1] = j;
- }
- }
- //LineShow(gray, dataArea[0], start, dataArea[1], start);
- //LineShow(gray, dataArea[2], start, dataArea[3], start);
- //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
- //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
- //ImageShow(gray);
- #endregion
- #region//左上
- for (int i = leftExtractionRange[0] - 50; i < leftExtractionRange[0]; i++)
- {
- sum = thresh[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
- if ((int)sum > 0)
- {
- leftShangOrdinates[1] = i;
- break;
- }
- }
- if (leftShangOrdinates[1] != 0)
- {
- for (int j = dataArea[1] - 350; j < dataArea[1] - 100; j++)
- {
- if (thresh.Get<byte>(leftShangOrdinates[1], j) > 0)
- {
- leftShangOrdinates[0] = j;
- leftShangOrdinates[2] = leftExtractionRange[0];
- break;
- }
- }
- }
- #endregion
- #region //右上
- for (int i = rightExtractionRange[0] - 50; i < rightExtractionRange[0]; i++)
- {
- sum = thresh[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
- if ((int)sum > 0)
- {
- rightShangOrdinates[1] = i;
- break;
- }
- }
- //if (rightExtractionRange[0] - rightShangOrdinates[1] < 5)
- // rightShangOrdinates[1] = 0;
- if (rightShangOrdinates[1] != 0)
- {
- for (int j = dataArea[2] + 100; j < dataArea[2] + 350; j++)
- {
- if (thresh.Get<byte>(rightShangOrdinates[1], j) > 0)
- {
- rightShangOrdinates[0] = j;
- rightShangOrdinates[2] = rightExtractionRange[0];
- break;
- }
- }
- }
- #endregion
- //LineShow(gray, leftShangOrdinates[0], leftShangOrdinates[1], leftShangOrdinates[0], leftShangOrdinates[2]);
- //LineShow(gray, rightShangOrdinates[0], rightShangOrdinates[1], rightShangOrdinates[0], rightShangOrdinates[2]);
- //ImageShow(gray);
- Mat zengqiang = new Mat();
- PointEnhancement(gray, out zengqiang);
- Mat sobel2 = new Mat();
- //EdgeY2(gray, out sobel2);
- EdgeY2(zengqiang, out sobel2);
- Mat thresh2 = new Mat();
- thresh2 = sobel2.Threshold(200, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
- Mat nomin2 = new Mat();
- GetArea(thresh2, out nomin2, 50, true);
- Mat result2 = nomin2.Clone();
- //ImageShow(nomin2 * 255);
- #region//左下
- int compensate = 5;
- for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 40; i++)
- {
- sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[1] = i;
- break;
- }
- }
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
- {
- sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- leftXiaOrdinates[0] = maxSpacing[0];
- leftXiaOrdinates[1] += compensate;
- #endregion
- #region//右下
- for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 40; i++)
- {
- sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[1] = i;
- break;
- }
- }
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
- {
- sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- rightXiaOrdinates[0] = maxSpacing[1];
- rightXiaOrdinates[1] += compensate;
- #endregion
- #region//下层迭代
- if (leftXiaOrdinates[1] < 10 || rightXiaOrdinates[1] < 10)
- {
- int leftThresh = 200, rightThresh = 200;
- while (leftXiaOrdinates[1] < 10)
- {
- if (leftThresh == 0)
- break;
- leftThresh -= 10;
- thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 30; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[1] = i;
- break;
- }
- }
- }
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
- {
- sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- while (rightXiaOrdinates[1] < 10)
- {
- if (rightThresh == 0)
- break;
- rightThresh -= 10;
- thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 30; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[1] = i;
- break;
- }
- }
- }
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
- {
- sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- }
- if (leftXiaOrdinates[2] == 0 || rightXiaOrdinates[2] == 0)
- {
- int leftThresh = 200, rightThresh = 200;
- while (leftXiaOrdinates[2] == 0)
- {
- if (leftThresh == 0)
- break;
- leftThresh -= 10;
- thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- }
- while (rightXiaOrdinates[2] == 0)
- {
- if (rightThresh == 0)
- break;
- rightThresh -= 10;
- thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- }
- }
- #endregion
- //LineShow(gray, leftXiaOrdinates[0], leftXiaOrdinates[1], leftXiaOrdinates[0], leftXiaOrdinates[2]);
- //LineShow(gray, rightXiaOrdinates[0], rightXiaOrdinates[1], rightXiaOrdinates[0], rightXiaOrdinates[2]);
- //ImageShow(gray);
- }
- public void XigaoXiangXiaWanqu(Mat gray, int[] dataArea, int start, out int[] leftShangOrdinates, out int[] rightShangOrdinates, out int[] leftXiaOrdinates, out int[] rightXiaOrdinates)
- {
- leftShangOrdinates = new int[3];
- rightShangOrdinates = new int[3];
- leftXiaOrdinates = new int[3];
- rightXiaOrdinates = new int[3];
- #region//上下边界
- int[] leftExtractionRange = new int[2];
- int[] rightExtractionRange = new int[2];
- Mat thresh = new Mat();
- double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
- thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
- //ImageShow(thresh * 255);
- Scalar sum = new Scalar();
- Mat result1 = thresh.Clone();
- for (int i = start + 300; i < result1.Rows; i++)//左侧上界
- {
- //sum = result1[i, i + 1, dataArea[1] - 350, dataArea[1] - 100].Sum();
- if (result1.Get<byte>(i, dataArea[1] - 50) > 0)
- {
- leftExtractionRange[0] = i;
- break;
- }
- }
- for (int i = leftExtractionRange[0] + 50; i < result1.Rows; i++)//左侧下界
- {
- sum = result1[i, i + 1, dataArea[0], dataArea[1] - 100].Sum();
- if ((int)sum < 100)
- {
- leftExtractionRange[1] = i;
- break;
- }
- }
- for (int i = start + 300; i < result1.Rows; i++)//右侧上界
- {
- //sum = result1[i, i + 1, dataArea[2] + 100, dataArea[2] + 350].Sum();
- if (result1.Get<byte>(i, dataArea[2] + 50) > 0)
- {
- rightExtractionRange[0] = i;
- break;
- }
- }
- for (int i = rightExtractionRange[0] + 50; i < result1.Rows; i++)//右侧下界
- {
- sum = result1[i, i + 1, dataArea[2] + 100, dataArea[3]].Sum();
- if ((int)sum < 100)
- {
- rightExtractionRange[1] = i;
- break;
- }
- }
- //LineShow(gray, dataArea[1] + 50, leftExtractionRange[0], dataArea[1] + 50, leftExtractionRange[1]);
- //LineShow(gray, dataArea[2] - 50, rightExtractionRange[0], dataArea[2] - 50, rightExtractionRange[1]);
- //ImageShow(gray);
- #endregion
- #region//找质量好的位置
- int[] maxSpacing = new int[2];
- Scalar max = new Scalar(0);
- for (int j = dataArea[1] - 300; j < dataArea[1]; j += 40)
- {
- sum = result1[leftExtractionRange[0], leftExtractionRange[1], j - 20, j + 20].Sum();
- if ((int)max < (int)sum)
- {
- max = sum;
- maxSpacing[0] = j;
- }
- }
- max = new Scalar(0);
- for (int j = dataArea[2]; j < dataArea[2] + 300; j += 40)
- {
- sum = result1[rightExtractionRange[0], rightExtractionRange[1], j - 20, j + 20].Sum();
- if ((int)max < (int)sum)
- {
- max = sum;
- maxSpacing[1] = j;
- }
- }
- //LineShow(gray, dataArea[0], start, dataArea[1], start);
- //LineShow(gray, dataArea[2], start, dataArea[3], start);
- //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
- //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
- //ImageShow(gray);
- #endregion
- //Mat zengqiang = new Mat();
- //PointEnhancement(gray, out zengqiang);
- Mat sobel3 = new Mat();
- EdgeY2(gray, out sobel3);
- Mat thresh3 = new Mat();
- thresh3 = sobel3.Threshold(200, 1, ThresholdTypes.Binary);
- Mat nomin3 = new Mat();
- GetArea(thresh3, out nomin3, 500, true);
- //ImageShow(thresh3 * 255,nomin3*255);
- Mat result3 = nomin3.Clone();
- #region//左上
- for (int i = leftExtractionRange[0] - 50; i < result3.Rows; i++)
- {
- if (result3.Get<byte>(i, dataArea[1] - 250) > 0)
- {
- leftShangOrdinates[1] = i;
- break;
- }
- }
- for (int i = leftShangOrdinates[1] + 50; i > leftShangOrdinates[1] + 5; i--)
- {
- if (result3.Get<byte>(i, dataArea[1] - 200) > 0)
- {
- leftShangOrdinates[2] = i;
- break;
- }
- }
- if (leftShangOrdinates[2] != 0)
- leftShangOrdinates[0] = dataArea[1] - 250;
- #endregion
- #region//右上
- for (int i = rightExtractionRange[0] - 50; i < result3.Rows; i++)
- {
- if (result3.Get<byte>(i, dataArea[2] + 250) > 0)
- {
- rightShangOrdinates[1] = i;
- break;
- }
- }
- for (int i = rightShangOrdinates[1] + 50; i > rightShangOrdinates[1] + 5; i--)
- {
- if (result3.Get<byte>(i, dataArea[2] + 200) > 0)
- {
- rightShangOrdinates[2] = i;
- break;
- }
- }
- if (rightShangOrdinates[2] != 0)
- rightShangOrdinates[0] = dataArea[2] + 250;
- #endregion
- maxSpacing[0] = dataArea[1] - 100;
- maxSpacing[1] = dataArea[2] + 100;
- //LineShow(gray, leftShangOrdinates[0], leftShangOrdinates[1], leftShangOrdinates[0], leftShangOrdinates[2]);
- //LineShow(gray, rightShangOrdinates[0], rightShangOrdinates[1], rightShangOrdinates[0], rightShangOrdinates[2]);
- //LineShow(gray, maxSpacing[0], leftExtractionRange[0], maxSpacing[0], leftExtractionRange[1]);
- //LineShow(gray, maxSpacing[1], rightExtractionRange[0], maxSpacing[1], rightExtractionRange[1]);
- //ImageShow(gray);
- Mat zengqiang = new Mat();
- PointEnhancement(gray, out zengqiang);
- Mat sobel2 = new Mat();
- EdgeY2(gray, out sobel2);
- //EdgeY2(zengqiang, out sobel2);
- //Sobel(zengqiang, out sobel2);
- Mat thresh2 = new Mat();
- thresh2 = sobel2.Threshold(200, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
- Cv2.Rectangle(thresh2, new Rect(0, 0, thresh2.Cols, leftExtractionRange[0]), new Scalar(0), -1);
- Cv2.Rectangle(thresh2, new Rect(0, leftExtractionRange[1] + 20, thresh2.Cols, thresh2.Rows - leftExtractionRange[1] - 20), new Scalar(0), -1);
- Mat nomin2 = new Mat();
- GetArea(thresh2, out nomin2, 10, true);
- Mat result2 = nomin2.Clone();
- //ImageShow(thresh2*255, nomin2 * 255,nomin2*100+gray);
- #region//左下
- int compensate = 5;
- for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 40; i++)
- {
- sum = result2[i, i + 1, maxSpacing[0] - 10, maxSpacing[0] + 10].Sum();
- if ((int)sum > 10)
- {
- leftXiaOrdinates[1] = i;
- break;
- }
- }
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 20; i++)
- {
- sum = result2[i, i + 1, maxSpacing[0] - 10, maxSpacing[0] + 10].Sum();
- if ((int)sum > 10)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- leftXiaOrdinates[0] = maxSpacing[0];
- leftXiaOrdinates[1] += compensate;
- #endregion
- #region//右下
- for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 40; i++)
- {
- sum = result2[i, i + 1, maxSpacing[1] - 10, maxSpacing[1] + 10].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[1] = i;
- break;
- }
- }
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 20; i++)
- {
- sum = result2[i, i + 1, maxSpacing[1] - 10, maxSpacing[1] + 10].Sum();
- if ((int)sum > 10)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- rightXiaOrdinates[0] = maxSpacing[1];
- rightXiaOrdinates[1] += compensate;
- #endregion
- #region//下层迭代
- if (leftXiaOrdinates[1] < 10 || rightXiaOrdinates[1] < 10)
- {
- int leftThresh = 200, rightThresh = 200;
- while (leftXiaOrdinates[1] < 10)
- {
- if (leftThresh == 0)
- break;
- leftThresh -= 10;
- thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = leftExtractionRange[0] + 20; i < leftExtractionRange[1] - 30; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[1] = i;
- break;
- }
- }
- }
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
- {
- sum = result2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- while (rightXiaOrdinates[1] < 10)
- {
- if (rightThresh == 0)
- break;
- rightThresh -= 10;
- thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = rightExtractionRange[0] + 20; i < rightExtractionRange[1] - 30; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[1] = i;
- break;
- }
- }
- }
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
- {
- sum = result2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- }
- if (leftXiaOrdinates[2] == 0 || rightXiaOrdinates[2] == 0)
- {
- int leftThresh = 200, rightThresh = 200;
- while (leftXiaOrdinates[2] == 0)
- {
- if (leftThresh == 0)
- break;
- leftThresh -= 10;
- thresh2 = sobel2.Threshold(leftThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[0], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = leftXiaOrdinates[1] + 20; i < leftExtractionRange[1] - 10; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[0] - 20, maxSpacing[0] + 20].Sum();
- if ((int)sum > 20)
- {
- leftXiaOrdinates[2] = i;
- break;
- }
- }
- }
- while (rightXiaOrdinates[2] == 0)
- {
- if (rightThresh == 0)
- break;
- rightThresh -= 10;
- thresh2 = sobel2.Threshold(rightThresh, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh2, new Rect(maxSpacing[1], 0, 1, thresh2.Rows), new Scalar(0), -1);
- GetArea(thresh2, out nomin2, 500, true);
- for (int i = rightXiaOrdinates[1] + 20; i < rightExtractionRange[1] - 10; i++)
- {
- sum = nomin2[i, i + 1, maxSpacing[1] - 20, maxSpacing[1] + 20].Sum();
- if ((int)sum > 20)
- {
- rightXiaOrdinates[2] = i;
- break;
- }
- }
- }
- }
- #endregion
- //LineShow(gray, leftShangOrdinates[0], leftShangOrdinates[1], leftShangOrdinates[0], leftShangOrdinates[2]);
- //LineShow(gray, rightShangOrdinates[0], rightShangOrdinates[1], rightShangOrdinates[0], rightShangOrdinates[2]);
- //LineShow(gray, leftXiaOrdinates[0], leftXiaOrdinates[1], leftXiaOrdinates[0], leftXiaOrdinates[2]);
- //LineShow(gray, rightXiaOrdinates[0], rightXiaOrdinates[1], rightXiaOrdinates[0], rightXiaOrdinates[2]);
- //ImageShow(gray);
- }
- public void GetXigaoArea0(Mat image, out int[] dataArea, out int start)
- {
- dataArea = new int[4];
- start = 0;
- Scalar leftMax = new Scalar(0);
- Scalar rightMax = new Scalar(0);
- int thresh1 = 120, thresh2 = 200;
- for (int j = 0; j < image.Cols; j++)
- {
- leftMax = image[0, image.Rows, j, j + 1].Sum();
- if ((int)leftMax > thresh1)
- {
- dataArea[0] = j;
- break;
- }
- }
- for (int j = dataArea[0]; j < image.Cols; j++)
- {
- leftMax = image[0, image.Rows, j, j + 1].Sum();
- if ((int)leftMax > thresh2)
- {
- dataArea[1] = j;
- break;
- }
- }
- for (int j = image.Cols - 1; j > 0; j--)
- {
- rightMax = image[0, image.Rows, j - 1, j].Sum();
- if ((int)rightMax > thresh1)
- {
- dataArea[3] = j;
- break;
- }
- }
- for (int j = dataArea[3]; j > 0; j--)
- {
- rightMax = image[0, image.Rows, j - 1, j].Sum();
- if ((int)rightMax > thresh2)
- {
- dataArea[2] = j;
- break;
- }
- }
- Scalar sum = new Scalar(0);
- for (int i = 0; i < image.Rows; i++)
- {
- sum = image[i, i + 1, dataArea[0], dataArea[3]].Sum();
- if ((int)sum > 0)
- {
- start = i;
- break;
- }
- }
- }
- public void GetXigaoArea(Mat image, out int[] dataArea, out int start)
- {
- dataArea = new int[4];
- start = 0;
- Scalar leftMax = new Scalar(0);
- Scalar rightMax = new Scalar(0);
- int thresh1 = 140, thresh2 = 300;
- for (int j = 0; j < image.Cols; j++)
- {
- leftMax = image[0, image.Rows, j, j + 1].Sum();
- if ((int)leftMax > thresh1)
- {
- dataArea[0] = j;
- break;
- }
- }
- for (int j = dataArea[0]; j < image.Cols; j++)
- {
- leftMax = image[0, image.Rows, j, j + 1].Sum();
- if ((int)leftMax > thresh2)
- {
- dataArea[1] = j;
- break;
- }
- }
- for (int j = image.Cols - 1; j > 0; j--)
- {
- rightMax = image[0, image.Rows, j - 1, j].Sum();
- if ((int)rightMax > thresh1)
- {
- dataArea[3] = j;
- break;
- }
- }
- for (int j = dataArea[3]; j > 0; j--)
- {
- rightMax = image[0, image.Rows, j - 1, j].Sum();
- if ((int)rightMax > thresh2)
- {
- dataArea[2] = j;
- break;
- }
- }
- Scalar sum = new Scalar(0);
- for (int i = 0; i < image.Rows; i++)
- {
- sum = image[i, i + 1, dataArea[0], dataArea[3]].Sum();
- if ((int)sum > 0)
- {
- start = i;
- break;
- }
- }
- }
- /// <summary>
- /// 判断正向黑块是否突出
- /// </summary>
- /// <param name="image">原图</param>
- /// <param name="contour">二值图</param>
- /// <param name="tuchu">黑块是否突出,true:突出;false:不突出;</param>
- /// <param name="dataArea">提取区域</param>
- /// <param name="start">上像素边界</param>
- /// <param name="Hang"></param>
- public void HeiKuai(Mat image, Mat contour, out bool tuchu, int[] dataArea, int start, out int[] Hang)
- {
- tuchu = new bool();
- Scalar sum = new Scalar(0);
- Hang = new int[4];
- Mat imageBlur = new Mat();
- Cv2.GaussianBlur(image, imageBlur, new Size(11, 11), 5, 5);//模糊
- int minThresh = 18;
- int maxThresh = 20;
- Mat edge = new Mat();
- Cv2.Canny(imageBlur, edge, minThresh, maxThresh);//边缘检测
- ////闭运算
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat close = new Mat();
- Cv2.MorphologyEx(edge, edge, MorphTypes.Close, se);
- //new Window("edge", WindowMode.Normal, edge * 255);
- //左侧
- for (int i = start; i < edge.Rows; i++)//求左边缘检测图行
- {
- sum = edge[i, i + 1, 500, dataArea[1] - 50].Sum();
- if ((int)sum > 255)
- {
- Hang[0] = i;
- break;
- }
- }
- for (int i = start; i < contour.Rows; i++)//求左二值图行
- {
- sum = contour[i, i + 1, 500, dataArea[1] - 50].Sum();
- if ((int)sum > 5)
- {
- Hang[1] = i;
- break;
- }
- }
- for (int i = start; i < edge.Rows; i++)//求右边缘检测图行
- {
- sum = edge[i, i + 1, dataArea[2] + 50, dataArea[3] - 200].Sum();
- if ((int)sum > 255)
- {
- Hang[2] = i;
- break;
- }
- }
- for (int i = start; i < contour.Rows; i++)//求右二值图行
- {
- sum = contour[i, i + 1, dataArea[2] + 50, dataArea[3] - 200].Sum();
- if ((int)sum > 5)
- {
- Hang[3] = i;
- break;
- }
- }
- if (Hang[1] - Hang[0] > 20 & Hang[3] - Hang[2] > 20)
- {
- tuchu = true;
- }
- else
- {
- tuchu = false;
- }
- }
- public void Heikuai2(Mat gray, int start, int[] dataArea, out bool tuchu)
- {
- tuchu = false;
- #region//測試
- //Mat thresh4 = new Mat();
- //double t3 = Cv2.Threshold(gray, thresh4, 0, 1, ThresholdTypes.Otsu);
- //Mat sobel4 = new Mat();
- //Sobel(thresh4, out sobel4);
- //Mat zengqiang2 = new Mat();
- //Cv2.EqualizeHist(gray, zengqiang2);
- ////ceju.ImageShow(sobel2 * 255);
- //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
- //Window window = new Window("tbar", WindowMode.Normal);//创建一个新窗口"tbar"
- //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 100, 255, cvTrackbarCallback);
- //Cv2.WaitKey();
- //void Text(int value)
- //{
- // //thresh4 = gray.Threshold(t3 - value, 1, ThresholdTypes.Binary);
- // //sobel4 = new Mat();
- // //ceju.Sobel(thresh4, out sobel4);
- // //new Window("tbar", WindowMode.Normal, sobel4 * 255);
- // //EdgeY(zengqiang2, out sobel4);
- // Sobel(gray, out sobel4);
- // thresh4 = sobel4.Threshold(value, 1, ThresholdTypes.Binary);
- // new Window("tbar", WindowMode.Normal, thresh4 * 255);
- //}
- #endregion
- Scalar sum = new Scalar();
- Mat thresh2 = new Mat();
- double t = Cv2.Threshold(gray, thresh2, 0, 1, ThresholdTypes.Otsu);
- thresh2 = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
- Mat sobel2 = new Mat();
- Sobel(thresh2, out sobel2);
- Mat result2 = sobel2.Clone();
- int leftStart = 0;
- for (int i = start - 300; i < result2.Rows; i++)
- {
- sum = result2[i, i + 1, dataArea[0], dataArea[1]].Sum();
- if ((int)sum > 50)
- {
- leftStart = i;
- break;
- }
- }
- Mat sobel = new Mat();
- Sobel(gray, out sobel);
- Mat thresh = new Mat();
- thresh = sobel.Threshold(10, 1, ThresholdTypes.Binary);
- Cv2.Rectangle(thresh, new Rect(dataArea[1] - 50, 0, thresh.Cols - dataArea[1] + 100, thresh.Rows), new Scalar(0), -1);
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Mat close = new Mat();
- Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
- //ImageShow(close * 255);
- Mat nomin = new Mat();
- GetArea(close, out nomin, 100, true);
- Mat result1 = nomin.Clone();
- //ImageShow(nomin * 255);
- int heixian = 0;
- for (int i = start + 300; i < leftStart; i++)
- {
- sum = result1[i, i + 1, dataArea[0], dataArea[1]].Sum();
- if ((int)sum > 30)
- {
- heixian = i;
- break;
- }
- }
- //LineShow(gray, 0, leftStart, gray.Cols, leftStart);
- //LineShow(gray, 0, heixian, gray.Cols, heixian);
- //ImageShow(gray);
- if ((leftStart - heixian) > 30 && heixian != 0)
- tuchu = true;
- }
- public void Heikuai3(Mat gray, int start, int[] dataArea, out bool tuchu)
- {
- tuchu = false;
- Mat thresh = new Mat();
- double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
- thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
- Mat result1 = thresh.Clone();
- Mat sobel = new Mat();
- Sobel(gray, out sobel);
- Mat threshSobel = new Mat();
- threshSobel = sobel.Threshold(10, 1, ThresholdTypes.Binary);
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat open = new Mat();
- Cv2.MorphologyEx(threshSobel, open, MorphTypes.Open, seOpen);
- //Mat nomin = new Mat();
- //GetArea(open, out nomin, 500, true);
- Mat result2 = open.Clone();
- //ImageShow(result2 * 255);
- int leftStart = 0;
- Scalar sum = new Scalar(0);
- for (int i = start + 300; i < result1.Rows; i++)
- {
- sum = result1[i, i + 1, dataArea[0], dataArea[1] - 100].Sum();
- if ((int)sum > 20)
- {
- leftStart = i;
- break;
- }
- }
- //LineShow(gray, dataArea[1] - 400, leftStart - 100, dataArea[1] - 100, leftStart - 10);
- //ImageShow(gray, result2 * 255,result1*255);
- int leftBlackBorder = 0;
- for (int j = dataArea[1] - 400; j < dataArea[1] - 100; j++)
- {
- sum = result2[leftStart - 100, leftStart - 10, j, j + 1].Sum();
- if ((int)sum > 20)
- {
- leftBlackBorder = j;
- break;
- }
- }
- if (leftBlackBorder != 0)
- tuchu = true;
- }
- //判断是否弯曲
- public void WanQu(Mat contour, out bool wanQu, int[] dataArea, int start, out int[] a)
- {
- a = new int[3];
- wanQu = new bool();
- //int max=new int();
- for (int i = start; i < contour.Rows; i++)
- {
- if (contour.Get<byte>(i, dataArea[1] - 150) > 0)
- {
- a[0] = i;
- break;
- }
- }
- for (int i = start; i < contour.Rows; i++)
- {
- if (contour.Get<byte>(i, dataArea[1] - 50) > 0)
- {
- a[1] = i;
- break;
- }
- }
- //int[] zongzuobiao = new int[150 - 51];
- int[] zongzuobiao = new int[dataArea[1] - dataArea[0] - 200];
- int count = 0;
- //for (int j = dataArea[1] - 149; j < dataArea[1] - 51; j++)
- for (int j = dataArea[0] + 100; j < dataArea[1] - 100; j++)
- {
- for (int i = start; i < contour.Rows; i++)
- {
- if (contour.Get<byte>(i, j) > 0)
- {
- zongzuobiao[count] = i;
- count++;
- break;
- }
- }
- }
- int max = zongzuobiao.Max();
- int min = zongzuobiao.Min();
- int idxMax = 0, idxMin = 0;
- for (int i = 0; i < zongzuobiao.Length; i++)
- {
- if (zongzuobiao[i] == max)
- idxMax = i;
- if (zongzuobiao[i] == min)
- idxMin = i;
- }
- //判断是否在中间
- if (idxMax > zongzuobiao.Length / 3 && idxMax < zongzuobiao.Length / 3 * 2)
- {
- wanQu = false;
- }
- else
- {
- wanQu = true;
- }
- }
- public void WanQu2(Mat gray, out bool wanQu, int[] dataArea, int start, out int[] a)
- {
- a = new int[3];
- wanQu = false;
- int leftStart = 0;
- Mat thresh = new Mat();
- double t = Cv2.Threshold(gray, thresh, 0, 1, ThresholdTypes.Otsu);
- thresh = gray.Threshold(t - 30, 1, ThresholdTypes.Binary);
- for (int i = start + 300; i < thresh.Rows; i++)
- {
- if (thresh.Get<byte>(i, dataArea[1] + 50) > 0)
- {
- leftStart = i;
- break;
- }
- }
- Scalar sum = new Scalar();
- for (int i = start + 300; i < leftStart; i++)
- {
- sum = thresh[i, i + 1, dataArea[1] - 200, dataArea[1] - 100].Sum();
- if ((int)sum > 0)
- {
- wanQu = true;
- }
- }
- //LineShow(gray, dataArea[1] + 50, leftStart, dataArea[1] + 50, start);
- //ImageShow(gray,thresh*255);
- }
- public void youCexiangsu(Mat contour, out bool cunzai, out int lowrow, out int[] zuidalie, int[] dataArea)
- {
- cunzai = new bool();
- zuidalie = new int[2];
- lowrow = new int();
- //左最大列
- Scalar max = new Scalar(0);
- for (int j = 0; j < contour.Cols / 2; j++)
- {
- Scalar sum = new Scalar(0);
- sum = contour[0, contour.Rows, j, j + 1].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- zuidalie[0] = j;
- }
- }
- ////右最大列
- //Scalar max1 = new Scalar(0);
- //for (int j = contour.Cols; j > contour.Cols / 2; j--)
- //{
- // Scalar sum = new Scalar(0);
- // sum = contour[0, contour.Rows, j-1, j].Sum();
- // if ((int)sum > (int)max1)
- // {
- // max1 = sum;
- // zuidalie[1] = j;
- // }
- //}
- //左底行
- for (int i = 0; i < contour.Rows; i++)
- {
- if (contour.Get<byte>(i, dataArea[1] - 150) > 0)
- {
- lowrow = i;
- break;
- }
- }
- Scalar sum1 = new Scalar(0);
- sum1 = contour[0, lowrow - 150, zuidalie[0] + 100, zuidalie[0] + 101].Sum();
- //Console.WriteLine(sum1);
- if ((int)sum1 > 0)
- {
- cunzai = true;
- }
- else
- {
- cunzai = false;
- }
- }
- /// <summary>
- /// 计算锡膏的上层坐标
- /// </summary>
- /// <param name="contour">二值图</param>
- /// <param name="filter">红色通道增强后图</param>
- /// <param name="leftShang">左边上层坐标,0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="rightShang">右边上层坐标,0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="upperBorder">上界</param>
- /// <param name="dataArea">数据提取区域</param>
- public void Shang(Mat contour, Mat filter, out int[] leftShang, out int[] rightShang, int upperBorder, int[] dataArea)
- {
- leftShang = new int[3];//0:横坐标;1:上纵坐标;2:下纵坐标
- rightShang = new int[3];
- Scalar sum = new Scalar(0);
- //左侧
- for (int i = upperBorder; i < contour.Rows; i++)//求上纵坐标
- {
- sum = contour[i, i + 1, dataArea[0], dataArea[1] - 50].Sum();
- if ((int)sum > 0)
- {
- leftShang[1] = i;
- break;
- }
- }
- for (int j = dataArea[1] - 50; j > dataArea[0]; j--)//求横坐标
- {
- if (contour.Get<byte>(leftShang[1], j) > 0)
- {
- leftShang[0] = j;
- break;
- }
- }
- Mat cropLeft = filter[leftShang[1] + 2, leftShang[1] + 20, leftShang[0] - 1, leftShang[0] + 1].Clone();
- Mat threshLeft = cropLeft.Threshold(0, 1, ThresholdTypes.Otsu);
- for (int i = 0; i < threshLeft.Rows; i++)//求下纵坐标
- {
- sum = threshLeft[i, i + 1, 0, threshLeft.Cols].Sum();
- if ((int)sum == 0)
- {
- leftShang[2] = i + leftShang[1] + 2;
- break;
- }
- }
- //右侧
- for (int i = upperBorder; i < contour.Rows; i++)//求上纵坐标
- {
- sum = contour[i, i + 1, dataArea[2] + 50, dataArea[3]].Sum();
- if ((int)sum > 0)
- {
- rightShang[1] = i;
- break;
- }
- }
- for (int j = dataArea[2] + 50; j < dataArea[3]; j++)//求横坐标
- {
- if (contour.Get<byte>(rightShang[1], j) > 0)
- {
- rightShang[0] = j;
- break;
- }
- }
- Mat cropRight = filter[rightShang[1] + 2, rightShang[1] + 20, rightShang[0] - 1, rightShang[0] + 1].Clone();
- Mat threshRight = cropRight.Threshold(0, 1, ThresholdTypes.Otsu);
- for (int i = 0; i < threshRight.Rows; i++)//求下纵坐标
- {
- sum = threshRight[i, i + 1, 0, threshRight.Cols].Sum();
- if ((int)sum == 0)
- {
- rightShang[2] = i + rightShang[1] + 2;
- break;
- }
- }
- }
- /// <summary>
- /// 计算锡膏下层坐标
- /// </summary>
- /// <param name="image">原图</param>
- /// <param name="contour">二值图</param>
- /// <param name="leftXia">左边坐标,0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="rightXia">右边坐标,0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="dataArea">提取区域</param>
- /// <param name="leftUpper">左边上层,leftShangOrdinate[1]</param>
- /// <param name="rightUpper">右边上层,rightShangOrdinate[1]</param>
- public void Xia(Mat image, Mat contour, out int[] leftXia, out int[] rightXia, int[] dataArea, int leftUpper, int rightUpper)
- {
- leftXia = new int[3];
- rightXia = new int[3];
- //leftXia[0] = dataArea[1] - 50;
- //rightXia[0] = dataArea[2] + 50;
- //找下界
- Scalar sum = new Scalar(0);
- int leftLower = 0, rightLower = 0;
- for (int i = leftUpper; i < contour.Rows; i++)
- {
- sum = contour[i, i + 1, dataArea[0], dataArea[1]].Sum();
- if ((int)sum == 0)
- {
- leftLower = i;
- break;
- }
- }
- for (int i = rightUpper; i < contour.Rows; i++)
- {
- sum = contour[i, i + 1, dataArea[2], dataArea[3]].Sum();
- if ((int)sum == 0)
- {
- rightLower = i;
- break;
- }
- }
- //找到中间画线处,局部最大值
- int rangeInside = 30;
- int rangeOutside = 100;
- Scalar max = new Scalar(0);
- for (int j = dataArea[0] + rangeOutside; j < dataArea[1] - rangeInside; j++)
- {
- sum = contour[leftUpper + 20, leftLower - 5, j, j + 50].Sum();
- if ((int)sum > (int)max)
- {
- leftXia[0] = j + 25;
- max = sum;
- }
- }
- max = 0;
- for (int j = dataArea[2] + rangeInside; j < dataArea[3] - rangeInside; j++)
- {
- sum = contour[rightUpper + 20, rightLower - 5, j, j + 50].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- rightXia[0] = j + 25;
- }
- }
- //边缘检测
- int minThresh = 30;
- int maxThresh = 55;
- Mat edge = new Mat();
- Cv2.Canny(image, edge, minThresh, maxThresh);
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Mat close = new Mat();
- Cv2.MorphologyEx(edge, close, MorphTypes.Close, se);
- Mat se2 = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- Mat erode = new Mat();
- Cv2.Erode(close, erode, se2);
- //ImageShow(edge,close,erode);
- erode = erode / 255;
- //计算高度
- for (int i = leftUpper + 20; i < leftLower - 5; i++)//左边坐标上层
- {
- sum = erode[i, i + 1, leftXia[0] - 10, leftXia[0] + 10].Sum();
- if ((int)sum > 5)
- {
- leftXia[1] = i;
- break;
- }
- }
- for (int i = leftXia[1] + 10; i < leftLower - 5; i++)//左边坐标下层
- {
- sum = erode[i, i + 1, leftXia[0] - 10, leftXia[0] + 10].Sum();
- if ((int)sum > 10)
- {
- leftXia[2] = i;
- break;
- }
- }
- for (int i = rightUpper + 20; i < rightLower - 5; i++)//右边坐标上层
- {
- sum = erode[i, i + 1, rightXia[0] - 10, rightXia[0] + 10].Sum();
- if ((int)sum > 10)
- {
- rightXia[1] = i;
- break;
- }
- }
- for (int i = rightXia[1] + 10; i < rightLower - 5; i++)//右边坐标下层
- {
- sum = erode[i, i + 1, rightXia[0] - 10, rightXia[0] + 10].Sum();
- if ((int)sum > 5)
- {
- rightXia[2] = i;
- break;
- }
- }
- //Mat cropLeft = erode[leftUpper + 20, leftLower - 5, leftXia[0] - 10, leftXia[0] + 10].Clone();
- //Mat cropRight = erode[rightUpper + 20, rightLower - 5, rightXia[0] - 10, rightXia[0] + 10].Clone();
- //ImageShow(cropLeft, cropRight);
- //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
- //CvTrackbarCallback cvTrackbarCallback2 = new CvTrackbarCallback(Text2);
- //Window window = new Window("tbar");//创建一个新窗口"tbar"
- //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 20, 100, cvTrackbarCallback);
- //CvTrackbar cvTrackbar2 = new CvTrackbar("bar2", "tbar", 50, 100, cvTrackbarCallback2);
- //Cv2.WaitKey();
- //LineShow(image, leftXia[0], leftXia[1], leftXia[0], leftXia[2]);
- //LineShow(image, rightXia[0], rightXia[1], rightXia[0], rightXia[2]);
- //ImageShow(image);
- //ImageShow(threshLeft, threshRight);
- //ImageShow(sobelLeft, sobelRight);
- //void Text(int value)
- //{
- // minThresh = value;
- // Cv2.Canny(image, edge, minThresh, maxThresh);
- // new Window("tbar", edge);
- //}
- //void Text2(int value)
- //{
- // maxThresh = value;
- // Cv2.Canny(image, edge, minThresh, maxThresh);
- // new Window("tbar", edge);
- //}
- }
- /// <summary>
- /// 锡膏z 上面测量线的 精确计算
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- public void Xigaozhoudu_ACC_upLines(Mat gray0, out int xigaohouduY1, out int xigaohouduY2, out int existOneTwoPoint, out int xigaoTopY)
- {
- xigaohouduY1 = 0;// 30;
- xigaohouduY2 = 40;
- xigaoTopY = 0;
- existOneTwoPoint = 0;
- {
- int minGray = 300 * 255;
- int minRowIndex = 0; int minRowInStart = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- xigaohouduY1 = 15;// 10;
- for (int i = Math.Min(55/*65*//*55*/, gray0.Rows) - 5; i > xigaohouduY1; i--)
- {
- curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Insert(0, curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- minGray = curGray;
- }
- else if (curGray - minGray > 100/*400*//*参数调试-阈值*/)
- {
- minRowInStart = i;
- break;
- }
- }
- if (minRowIndex < 30)//继续往下多走10个像素
- {
- minRowInStart = 0; curGrayList.Clear(); minGray = 300 * 255;
- for (int i = Math.Min(65/*55*/, gray0.Rows) - 5; i > xigaohouduY1; i--)
- {
- curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Insert(0, curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- minGray = curGray;
- }
- else if (curGray - minGray > 400/*100*//*400*//*参数调试-阈值*/)
- {
- minRowInStart = i;
- break;
- }
- }
- }
- if (minRowIndex < 30 && minRowInStart > 5)//继续往上走找上端点用于最后的优化,以便找到模糊的
- {
- List<int> upGrays = new List<int>(); int upMinG = 300 * 255;
- for (int i = minRowIndex - 5; i > 5; i--)
- {
- curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- upGrays.Insert(0, curGray);
- if (curGray < minGray)
- {
- xigaoTopY = i;
- break;
- }
- //if (curGray < minGray)
- //{
- // minRowIndex = i;
- // minGray = curGray;
- //}
- //else if (curGray - minGray > 400/*100*//*400*//*参数调试-阈值*/)
- //{
- // minRowInStart = i;
- // break;
- //}
- }
- upMinG = upGrays.Min();
- }
- List<int> upGrayList = new List<int>();
- for (int i = 0; i < minRowIndex; i ++)
- {
- curGray = this.XigaohouduForAreaMin(gray0, i);
- upGrayList.Add(curGray);
- }
- xigaohouduY2 = minRowIndex;
- if (xigaohouduY2 > 30)
- {
- existOneTwoPoint = 1;
- }
- }
- if (false)
- {
- int minGray = 300 * 255;
- int minRowIndex = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- xigaohouduY1 = 10;
- for (int i = xigaohouduY1; i < Math.Min(50, gray0.Rows) - 5; i++)
- {
- int v = gray0.Row[i].CountNonZero();
- if (v > colEnd / 2)
- {
- xigaohouduY1 = i;
- break;
- }
- }
- if (xigaohouduY1 < 36)
- {
- for (int i = xigaohouduY1 + 5/*10*//*5*/; i < Math.Min(55, gray0.Rows) - 5; i++)
- {
- curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Add(curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- minGray = curGray;
- }
- }
- for (int i = (minRowIndex - xigaohouduY1 - 5/*10*/) + 2; i < curGrayList.Count; i += 2)
- {
- if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
- {
- minRowIndex += 1;
- }
- else
- break;
- }
- }
- else
- minRowIndex = xigaohouduY1 + 1;
- xigaohouduY2 = minRowIndex;// 84;// 72;// minRowIndex;
- }
- Console.WriteLine("noSharp:" + xigaohouduY2 + ";xigaohouduY1:" + xigaohouduY1 + "...");
- }
- /// <summary>
- /// 锡膏z 上面测量线的 精确计算
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- public void Xigaozhoudu_ACC_upLines__0(Mat gray0, out int xigaohouduY1, out int xigaohouduY2)
- {
- xigaohouduY1 = 30;
- xigaohouduY2 = 40;
- {
- int minGray = 300 * 255;
- int minRowIndex = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- xigaohouduY1 = 10;
- for (int i = xigaohouduY1; i < Math.Min(50, gray0.Rows) - 5; i++)
- {
- int v = gray0.Row[i].CountNonZero();
- if (v > colEnd / 2)
- {
- xigaohouduY1 = i;
- break;
- }
- }
- if (xigaohouduY1 < 36)
- {
- for (int i = xigaohouduY1 + 5/*10*//*5*/; i < Math.Min(55, gray0.Rows) - 5; i++)
- {
- curGray = this.XigaohouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Add(curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- minGray = curGray;
- }
- }
- for (int i = (minRowIndex - xigaohouduY1 - 5/*10*/) + 2; i < curGrayList.Count; i += 2)
- {
- if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
- {
- minRowIndex += 1;
- }
- else
- break;
- }
- }
- else
- minRowIndex = xigaohouduY1 + 1;
- xigaohouduY2 = minRowIndex;// 84;// 72;// minRowIndex;
- }
- Console.WriteLine("noSharp:" + xigaohouduY2 + ";xigaohouduY1:" + xigaohouduY1 + "...");
- }
- //获取当前行附件最暗的总和
- private int XigaohouduForAreaMin(Mat gray, int rowIndex)
- {
- int areaMin = 0;
- for (int i = 0; i < gray.Cols; i++)
- {
- int colMin = 255;
- for (int j = rowIndex - 0/*1*//*Math.Max(0, rowIndex - 5)*/; j < rowIndex + 1; j++)
- if (gray.At<byte>(j, i) < colMin) colMin = gray.At<byte>(j, i);
- areaMin += colMin;
- }
- return areaMin;
- }
- /// <summary>
- /// 锡膏z 有开口 厚度 精确计算
- /// </summary>
- /// <param name="gray"></param>
- /// <param name="y"></param>
- /// <param name="b"></param>
- /// <param name="tonghouY"></param>
- /// <param name="fanghanhouduY"></param>
- /// <param name="a"></param>
- public void Xigaozhoudu_ACC(Mat gray0, int fanghanhouduY1__0, out int fanghanhouduY1, out int fanghanhouduY1Bottom, out int minGray, out bool skipUpLine, int a = 0)
- {
- int bottomYDistance = 20;// 25;// 15;// 30;
- int fanghanhouduY1__noSharp = -1;// fanghanhouduY1;
- skipUpLine = false;
- {
- minGray = 300 * 255;
- int minRowIndex = 0; int colEnd = gray0.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- fanghanhouduY1Bottom = 0;
- 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++)
- {
- curGray = this.FanghanhouduForAreaMin(gray0, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Add(curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- fanghanhouduY1Bottom = i;
- minGray = curGray;
- }
- }
- while (minRowIndex <= 6 && minRowIndex > 1)
- {
- curGray = this.FanghanhouduForAreaMin(gray0, minRowIndex - 1);
- if (curGray < minGray || Math.Abs(curGray - minGray) < 10) minRowIndex -= 1;
- else break;
- }
- for (int i = Math.Max(0, minRowIndex - Math.Max(0, fanghanhouduY1__0 - 4)) + 2; i < curGrayList.Count; i += 2)
- {
- if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
- {
- minRowIndex += 1;
- fanghanhouduY1Bottom += 2;
- }
- else
- break;
- }
- fanghanhouduY1__noSharp = minRowIndex;// 84;// 72;// minRowIndex;
- }
- {
- minGray = 300 * 255;
- //锐化
- //Mat left_small_sharp = BinaryTools.BlurMaskFunction(left_small).CvtColor(ColorConversionCodes.BGRA2GRAY);
- Mat gray = BinaryTools.BlurMaskFunction(gray0.Clone()/*grayRect*/, 4f * 3.14f, 1, 10f).CvtColor(ColorConversionCodes.BGRA2GRAY);
- //Cv2.ImWrite(@"C:\Users\win10SSD\Desktop\BlurMask" + a + "_.jpg", gray);
- int minRowIndex = 0; int colEnd = gray.Cols - 1; int curGray; List<int> curGrayList = new List<int>();
- fanghanhouduY1Bottom = 0;
- 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++)
- {
- curGray = this.FanghanhouduForAreaMin(gray, i);// (int)thresh[i - 1, i, 0, colEnd].Sum().Val0;
- curGrayList.Add(curGray);
- if (curGray < minGray)
- {
- minRowIndex = i;
- fanghanhouduY1Bottom = i;
- minGray = curGray;
- }
- }
- while (minRowIndex <= 6 && minRowIndex > 1)
- {
- curGray = this.FanghanhouduForAreaMin(gray0, minRowIndex - 1);
- if (curGray < minGray || Math.Abs(curGray - minGray) < 10) minRowIndex -= 1;
- else break;
- }
- for (int i = Math.Max(0, minRowIndex - Math.Max(0, fanghanhouduY1__0 - 4)) + 2; i < curGrayList.Count; i += 2)
- {
- if (Math.Abs(curGrayList[i] - minGray) < 10/*100*/)
- {
- minRowIndex += 1;
- fanghanhouduY1Bottom += 2;
- }
- else
- break;
- }
- //Console.WriteLine("fanghanhouduY1_1:" + minRowIndex);
- ////Cv2.ImWrite(@"C:\Users\54434\Desktop\thres_3.png", gray);
- fanghanhouduY1 = minRowIndex;// 84;// 72;// minRowIndex;
- }
- Console.Write("noSharp:" + fanghanhouduY1__noSharp + ";fanghanhouduY1:" + fanghanhouduY1 + "...");
- if (fanghanhouduY1__noSharp < 8 && fanghanhouduY1 < 8 || fanghanhouduY1__noSharp == 1 && fanghanhouduY1 > 10)
- {
- skipUpLine = true;// false;// true;
- }
- if (Math.Abs(fanghanhouduY1__noSharp - fanghanhouduY1) < 7/* <<7 8*//* << 6 *//*5*/
- || (fanghanhouduY1__noSharp - fanghanhouduY1 </*>*/ 0 && fanghanhouduY1__noSharp - fanghanhouduY1 >/*<*/ -10/*20*/))
- {
- fanghanhouduY1 = fanghanhouduY1__noSharp;
- }
- else
- Console.WriteLine("fanghanhouduY1 far away from fanghanhouduY1__noSharp.");
- }
- //锡膏Z
- /// <summary>
- /// 得到锡膏Z的数据提取区域
- /// </summary>
- /// <param name="contour"></param>
- /// <param name="imageRed"></param>
- /// <param name="dataArea"></param>
- public void GetXigaoZArea(Mat contour, Mat imageRed, out Mat resulet, out int[] dataArea, out int upperBorder, out int lowerBorder)
- {
- resulet = new Mat();
- //闭运算
- Mat close = new Mat();
- Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(27, 27));
- Cv2.MorphologyEx(contour, close, MorphTypes.Open, seClose);
- if ((int)close.Sum() < 200000)
- {
- seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
- Cv2.MorphologyEx(contour, close, MorphTypes.Open, seClose);
- }
- contour = close;
- //将中间目标区域通过掩膜弄出来
- int leftBorder = 0, rightBorder = 0;
- upperBorder = 0; lowerBorder = 0;
- Scalar sum = new Scalar(0);
- for (int i = 0; i < contour.Rows; i++)
- {
- sum = contour[i, i + 1, 0, contour.Cols].Sum();
- if ((int)sum > 200)
- {
- upperBorder = i - 20;
- break;
- }
- }
- for (int i = upperBorder + 100; i < contour.Rows; i++)
- {
- sum = contour[i, i + 1, 0, contour.Cols].Sum();
- if ((int)sum < 200)
- {
- lowerBorder = i;
- break;
- }
- }
- contour = imageRed.Threshold(0, 1, ThresholdTypes.Otsu);
- Mat delete = contour.Clone();
- Cv2.Rectangle(delete, new Rect(0, upperBorder, contour.Cols, lowerBorder - upperBorder), new Scalar(0), -1);
- contour = contour - delete;
- Mat seOpen = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- Cv2.MorphologyEx(contour, contour, MorphTypes.Open, seOpen);
- resulet = contour.Clone();
- //选出两个目标的提取区域
- dataArea = new int[4];
- int middle = contour.Cols / 2;
- sum = contour[0, contour.Rows, middle, middle + 1].Sum();
- if ((int)sum > 0)
- {
- for (int j = middle; j > 0; j--)
- {
- sum = contour[0, contour.Rows, j - 1, j].Sum();
- if ((int)sum == 0)
- {
- dataArea[1] = j;
- break;
- }
- }
- for (int j = middle; j < contour.Cols; j++)
- {
- sum = contour[0, contour.Rows, j, j + 1].Sum();
- if ((int)sum == 0)
- {
- dataArea[1] = j;
- break;
- }
- }
- for (int j = dataArea[1] + 10; j < contour.Cols; j++)
- {
- sum = contour[0, contour.Rows, j, j + 1].Sum();
- if ((int)sum > 0)
- {
- dataArea[2] = j;
- break;
- }
- }
- for (int j = dataArea[2] + 10; j < contour.Cols; j++)
- {
- sum = contour[0, contour.Rows, j, j + 1].Sum();
- if ((int)sum == 0)
- {
- dataArea[3] = j;
- break;
- }
- }
- }
- else
- {
- for (int j = middle; j < contour.Cols; j++)
- {
- sum = contour[0, contour.Rows, j, j + 1].Sum();
- if ((int)sum > 0)
- {
- dataArea[0] = j;
- break;
- }
- }
- for (int j = dataArea[0] + 10; j < contour.Cols; j++)
- {
- sum = contour[0, contour.Rows, j, j + 1].Sum();
- if ((int)sum == 0)
- {
- dataArea[1] = j;
- break;
- }
- }
- for (int j = dataArea[1] + 10; j < contour.Cols; j++)
- {
- sum = contour[0, contour.Rows, j, j + 1].Sum();
- if ((int)sum > 0)
- {
- dataArea[2] = j;
- break;
- }
- }
- for (int j = dataArea[2] + 10; j < contour.Cols; j++)
- {
- sum = contour[0, contour.Rows, j, j + 1].Sum();
- if ((int)sum == 0)
- {
- dataArea[3] = j;
- break;
- }
- }
- }
- }
- /// <summary>
- /// 得到所求的坐标
- /// </summary>
- /// <param name="image"></param>
- /// <param name="contour"></param>
- /// <param name="leftOrdinate">0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="rightOrdinate">0:横坐标;1:上纵坐标;2:下纵坐标</param>
- /// <param name="upperBorder"></param>
- /// <param name="lowerBorder"></param>
- /// <param name="dataArea"></param>
- public void GetXigaoZOrdinate(Mat image, Mat contour, out int[] leftOrdinate, out int[] rightOrdinate, int upperBorder, int lowerBorder, int[] dataArea)
- {
- //边缘检测
- //ceju.ImageShow(image);
- LineEnhancement(image, out image);
- //ceju.ImageShow(image);
- Cv2.GaussianBlur(image, image, new Size(9, 9), 3, 3);
- Mat edge = new Mat();
- Cv2.Canny(image, edge, 20, 25);
- edge = edge / 255;
- //ceju.ImageShow(edge * 255);
- Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 1));
- Cv2.MorphologyEx(edge, edge, MorphTypes.Close, se);
- //每个目标的第一条线
- int leftFirstLine = 0, rightFirstLine = 0;
- Scalar sum = new Scalar(0);
- for (int i = upperBorder; i < lowerBorder; i++)
- {
- sum = contour[i, i + 1, dataArea[0], dataArea[1]].Sum();
- if ((int)sum > 0)
- {
- leftFirstLine = i; break;
- }
- }
- for (int i = upperBorder; i < lowerBorder; i++)
- {
- sum = contour[i, i + 1, dataArea[2], dataArea[3]].Sum();
- if ((int)sum > 0)
- {
- rightFirstLine = i; break;
- }
- }
- //通过最大值找出测距点
- int leftPoint = 0, rightPoint = 0;
- Scalar max = new Scalar(0);
- int middle1 = (dataArea[0] + dataArea[1]) / 2;
- int middle2 = (dataArea[2] + dataArea[3]) / 2;
- for (int j = middle1 - 40; j < middle1 + 40; j++)
- {
- sum = contour[leftFirstLine + 20, lowerBorder, j, j + 40].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- leftPoint = j + 20;
- }
- }
- max = 0;
- for (int j = middle2 - 40; j < middle2 + 40; j++)
- {
- sum = contour[rightFirstLine + 20, lowerBorder, j, j + 40].Sum();
- if ((int)sum > (int)max)
- {
- max = sum;
- rightPoint = j + 20;
- }
- }
- //测距
- leftOrdinate = new int[3];
- rightOrdinate = new int[3];
- int range = 5;
- for (int i = leftFirstLine + 40; i < lowerBorder; i++)
- {
- sum = edge[i, i + 3, leftPoint - range - 5, leftPoint + range + 5].Sum();
- if ((int)sum > 5)
- {
- leftOrdinate[1] = i;
- break;
- }
- }
- for (int i = leftOrdinate[0] + 20; i < lowerBorder; i++)
- {
- sum = edge[i, i + 3, leftPoint - range, leftPoint + range].Sum();
- if ((int)sum > 5)
- {
- leftOrdinate[2] = i;
- break;
- }
- }
- for (int i = rightFirstLine + 40; i < lowerBorder; i++)
- {
- sum = edge[i, i + 3, rightPoint - range - 5, rightPoint + range + 5].Sum();
- if ((int)sum > 5)
- {
- rightOrdinate[1] = i;
- break;
- }
- }
- for (int i = rightOrdinate[0] + 20; i < lowerBorder; i++)
- {
- sum = edge[i, i + 3, rightPoint - range, rightPoint + range].Sum();
- if ((int)sum > 5)
- {
- rightOrdinate[2] = i;
- break;
- }
- }
- leftOrdinate[0] = leftPoint;
- rightOrdinate[0] = rightPoint;
- }
- public void GetXigaoZOrdinate2(Mat gray, out int[] leftUpprOrdinate, out int[] rightUpperOrdinate, out int[] leftLwerOrdinate, out int[] rightLowerOrdinate, int upperBorder, int lowerBorder, int[] dataArea)
- {
- if (upperBorder < 0) upperBorder = 0;
- leftUpprOrdinate = new int[3];
- rightUpperOrdinate = new int[3];
- leftLwerOrdinate = new int[3];
- rightLowerOrdinate = new int[3];
- Mat zengqiang = new Mat();
- PointEnhancement(gray, out zengqiang);
- Mat thresh = new Mat();
- double t = Cv2.Threshold(zengqiang, thresh, 0, 255, ThresholdTypes.Otsu);
- thresh = zengqiang.Threshold(t + 65, 1, ThresholdTypes.Binary);
- Scalar sum = new Scalar(0);
- int[] b = new int[2];
- //尋找質量好的位置
- for (int j = 300; j < thresh.Cols - 200; j++)
- {
- sum = thresh[upperBorder, lowerBorder, j, j + 150].Sum();
- if ((int)sum > 4000)
- {
- b[0] = j;
- break;
- }
- }
- if (b[0] == 0)
- {
- for (int j = 300; j < thresh.Cols - 200; j++)
- {
- sum = thresh[upperBorder, lowerBorder, j, j + 150].Sum();
- if ((int)sum > 3000)
- {
- b[0] = j;
- break;
- }
- }
- }
- for (int j = b[0] + 300; j < thresh.Cols - 200; j++)
- {
- sum = thresh[upperBorder, lowerBorder, j, j + 150].Sum();
- if ((int)sum > 4000)
- {
- b[1] = j;
- break;
- }
- }
- if (b[1] == 0)
- {
- for (int j = b[0] + 300; j < thresh.Cols - 200; j++)
- {
- sum = thresh[upperBorder, lowerBorder, j, j + 150].Sum();
- if ((int)sum > 3000)
- {
- b[1] = j;
- break;
- }
- }
- }
- //精確目標邊界
- int[] border = new int[4];
- for (int j = b[0]; j < thresh.Cols; j += 5)
- {
- sum = thresh[upperBorder, lowerBorder, j, j + 1].Sum();
- if ((int)sum > 0)
- {
- border[0] = j;
- break;
- }
- }
- for (int j = border[0] + 50; j < thresh.Cols; j += 5)
- {
- sum = thresh[upperBorder, lowerBorder, j, (j + 10)>= thresh.Width ? thresh.Width-1: (j + 10)].Sum();
- if ((int)sum == 0)
- {
- border[1] = j;
- break;
- }
- }
- for (int j = b[1]; j < thresh.Cols; j += 5)
- {
- sum = thresh[upperBorder, lowerBorder, j, j + 1].Sum();
- if ((int)sum > 0)
- {
- border[2] = j;
- break;
- }
- }
- for (int j = border[2] + 50; j < thresh.Cols; j += 5)
- {
- sum = thresh[upperBorder, lowerBorder, j, (j + 10) >= thresh.Width ? thresh.Width - 1 : (j + 10)].Sum();
- if ((int)sum == 0)
- {
- border[3] = j;
- break;
- }
- }
- //LineShow(gray, b[0], upperBorder, b[0], lowerBorder);
- //LineShow(gray, b[1], upperBorder, b[1], lowerBorder);
- //LineShow(gray, border[0], upperBorder, border[0], lowerBorder);
- //LineShow(gray, border[1], upperBorder, border[1], lowerBorder);
- //LineShow(gray, border[2], upperBorder, border[2], lowerBorder);
- //LineShow(gray, border[3], upperBorder, border[3], lowerBorder);
- //ImageShow(gray, thresh * 255);
- //Mat seClose = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
- //Mat close = new Mat();
- //Cv2.MorphologyEx(thresh, close, MorphTypes.Close, seClose);
- Mat sobel = new Mat();
- Sobel(thresh, out sobel);
- Mat result = sobel.Clone();
- //ImageShow(sobel * 255);
- int leftMiddle = (border[0] + border[1]) / 2;
- int rightMiddle = (border[2] + border[3]) / 2;
- int compensate = 5;
- //計算下層測量綫長度
- for (int i = upperBorder + 50; i < upperBorder + 70; i++)
- {
- sum = result[i, i + 1, leftMiddle - 40<0?0: leftMiddle - 40, leftMiddle + 40].Sum();
- if ((int)sum > 50)
- {
- leftLwerOrdinate[1] = i + compensate;
- break;
- }
- }
- if (leftLwerOrdinate[1] == 0)
- {
- for (int i = upperBorder + 50; i < upperBorder + 70; i++)
- {
- sum = result[i, i + 2, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, leftMiddle + 40].Sum();
- if ((int)sum > 20)
- {
- leftLwerOrdinate[1] = i + compensate;
- break;
- }
- }
- }
- for (int i = leftLwerOrdinate[1] + 15; i < lowerBorder + 20; i++)
- {
- sum = result[i, i + 1, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, leftMiddle + 40].Sum();
- if ((int)sum > 50)
- {
- leftLwerOrdinate[2] = i + compensate;
- break;
- }
- }
- if (leftLwerOrdinate[2] == 0)
- {
- for (int i = leftLwerOrdinate[1] + 20; i < lowerBorder + 20; i++)
- {
- sum = result[i, i + 1, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, leftMiddle + 40].Sum();
- if ((int)sum > 20)
- {
- leftLwerOrdinate[2] = i + compensate;
- break;
- }
- }
- }
- leftLwerOrdinate[0] = (border[0] + border[1]) / 2;
- for (int i = upperBorder + 50; i < upperBorder + 70; i++)
- {
- sum = result[i, i + 1, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, rightMiddle + 40].Sum();
- if ((int)sum > 50)
- {
- rightLowerOrdinate[1] = i + compensate;
- break;
- }
- }
- if (rightLowerOrdinate[1] == 0)
- {
- for (int i = upperBorder + 50; i < upperBorder + 70; i++)
- {
- sum = result[i, i + 2, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, rightMiddle + 40].Sum();
- if ((int)sum > 20)
- {
- rightLowerOrdinate[1] = i + compensate;
- break;
- }
- }
- }
- for (int i = rightLowerOrdinate[1] + 15; i < lowerBorder + 20; i++)
- {
- sum = result[i, i + 1, leftMiddle - 40 < 0 ? 0 : leftMiddle - 40, rightMiddle + 40].Sum();
- if ((int)sum > 50)
- {
- rightLowerOrdinate[2] = i + compensate;
- break;
- }
- }
- rightLowerOrdinate[0] = (border[2] + border[3]) / 2;
- //LineShow(gray, leftLwerOrdinate[0], leftLwerOrdinate[1], leftLwerOrdinate[0], leftLwerOrdinate[2]);
- //LineShow(gray, rightLowerOrdinate[0], rightLowerOrdinate[1], rightLowerOrdinate[0], rightLowerOrdinate[2]);
- //ImageShow(gray, sobel * 255);
- //計算上層測量綫距離
- Mat thresh2 = zengqiang.Threshold(t - 20, 1, ThresholdTypes.Binary);
- Mat sobel2 = new Mat();
- Sobel(thresh2, out sobel2);
- Mat thresh3 = zengqiang.Threshold(t - 10, 1, ThresholdTypes.Binary);
- Mat sobel3 = new Mat();
- Sobel(thresh3, out sobel3);
- for (int i = upperBorder; i < lowerBorder; i++)
- {
- sum = sobel2[i, i + 1, leftMiddle - 60<0?0: leftMiddle - 60, leftMiddle + 60].Sum();
- //if ((int)sum > 5)
- //{
- // leftUpprOrdinate[1] = 0;
- // break;
- //}
- //else
- if ((int)sum > 0)
- {
- leftUpprOrdinate[1] = i;
- break;
- }
- }
- for (int i = leftUpprOrdinate[1] + 30; i > leftUpprOrdinate[1] + 7; i--)
- {
- sum = sobel3[i - 1, i, leftMiddle - 60 < 0 ? 0 : leftMiddle - 60, leftMiddle + 60].Sum();
- if ((int)sum > 50)
- {
- leftUpprOrdinate[2] = i;
- break;
- }
- }
- leftUpprOrdinate[0] = (border[0] + border[1]) / 2;
- for (int i = upperBorder; i < lowerBorder; i++)
- {
- sum = sobel2[i, i + 1, rightMiddle - 60<0?0 : rightMiddle - 60, rightMiddle + 60].Sum();
- //if ((int)sum > 5)
- //{
- // rightUpperOrdinate[1] = 0;
- // break;
- //}
- //else
- if ((int)sum > 0)
- {
- rightUpperOrdinate[1] = i;
- break;
- }
- }
- for (int i = rightUpperOrdinate[1] + 30; i > rightUpperOrdinate[1] + 7; i--)
- {
- sum = sobel3[i - 1, i, rightMiddle - 60 < 0 ? 0 : rightMiddle - 60, rightMiddle + 60].Sum();
- if ((int)sum > 50)
- {
- rightUpperOrdinate[2] = i;
- break;
- }
- }
- rightUpperOrdinate[0] = (border[2] + border[3]) / 2;
- //LineShow(gray, leftUpprOrdinate[0], leftUpprOrdinate[1], leftUpprOrdinate[0], leftUpprOrdinate[2]);
- //LineShow(gray, rightUpperOrdinate[0], rightUpperOrdinate[1], rightUpperOrdinate[0], rightUpperOrdinate[2]);
- //ImageShow(gray, (sobel2+sobel3) * 100,sobel3*255);
- if (leftUpprOrdinate[1] == 0 || leftUpprOrdinate[2] == 0)
- {
- leftUpprOrdinate[0] = 0;
- leftUpprOrdinate[1] = 0;
- leftUpprOrdinate[2] = 0;
- }
- if (rightUpperOrdinate[1] == 0 || rightUpperOrdinate[2] == 0)
- {
- rightUpperOrdinate[0] = 0;
- rightUpperOrdinate[1] = 0;
- rightUpperOrdinate[2] = 0;
- }
- if (leftUpprOrdinate[1] != 0 || rightUpperOrdinate[1] != 0)
- {
- for (int i = upperBorder - 10; i < lowerBorder; i++)
- {
- sum = sobel2[i, i + 1, leftMiddle - 60, leftMiddle + 60].Sum();
- //if ((int)sum > 5)
- //{
- // leftUpprOrdinate[1] = 0;
- // break;
- //}
- //else
- if ((int)sum > 0)
- {
- leftUpprOrdinate[1] = i;
- break;
- }
- }
- for (int i = leftUpprOrdinate[1] + 30; i > leftUpprOrdinate[1] + 7; i--)
- {
- sum = sobel3[i - 1, i, leftMiddle - 60, leftMiddle + 60].Sum();
- if ((int)sum > 50)
- {
- leftUpprOrdinate[2] = i;
- break;
- }
- }
- leftUpprOrdinate[0] = (border[0] + border[1]) / 2;
- for (int i = upperBorder - 10; i < lowerBorder; i++)
- {
- sum = sobel2[i, i + 1, rightMiddle - 60, rightMiddle + 60].Sum();
- //if ((int)sum > 5)
- //{
- // rightUpperOrdinate[1] = 0;
- // break;
- //}
- //else
- if ((int)sum > 0)
- {
- rightUpperOrdinate[1] = i;
- break;
- }
- }
- for (int i = rightUpperOrdinate[1] + 30; i > rightUpperOrdinate[1] + 7; i--)
- {
- sum = sobel3[i - 1, i, rightMiddle - 60, rightMiddle + 60].Sum();
- if ((int)sum > 50)
- {
- rightUpperOrdinate[2] = i;
- break;
- }
- }
- }
- //CvTrackbarCallback cvTrackbarCallback = new CvTrackbarCallback(Text);
- //Window window = new Window("tbar");//创建一个新窗口"tbar"
- //CvTrackbar cvTrackbarV = new CvTrackbar("bar1", "tbar", 0, 255, cvTrackbarCallback);
- //Cv2.WaitKey();
- //void Text(int value)
- //{
- // thresh = zengqiang.Threshold(t - value, 255, ThresholdTypes.Binary);
- // new Window("tbar", thresh);
- //}
- }
- //公用
- /// <summary>
- /// 去除圓形
- /// </summary>
- /// <param name="image1"></param>
- /// <param name="image2"></param>
- /// <param name="result"></param>
- /// <param name="dp">累加器分辨率与图像分辨率的反比。默认=1</param>
- /// <param name="minDist">检测到的圆的中心之间的最小距离。</param>
- /// <param name="param1">第一个方法特定的参数</param>
- /// <param name="param2">第二个方法特定于参数</param>
- /// <param name="minRadius">最小半径</param>
- /// <param name="maxRadius">最大半径</param>
- public void RemoveCircles(Mat image1, Mat image2, out Mat result, double dp, double minDist, double param1, double param2, int minRadius, int maxRadius)
- {
- result = 1 - image2.Clone();
- int range = 10;
- //ImageShow(result*255);
- //dp = 1;
- //minDist = 30;
- //param1 = 70;
- //param2 = 30;
- //minRadius = 10;
- //maxRadius = 60;
- CircleSegment[] circles = Cv2.HoughCircles(image1, HoughMethods.Gradient, dp, minDist, param1, param2, minRadius, maxRadius);
- for (int i = 0; i < circles.Length; i++)
- {
- Point center = (Point)circles[i].Center;
- int radius = (int)circles[i].Radius;
- //for(int j=1;j<radius;j++)
- //Cv2.Circle(result, center, j, new Scalar(1));
- Cv2.Rectangle(result, new Rect(center.X - radius - range, center.Y - radius - range, 2 * (radius + range), 2 * (radius + range)), new Scalar(1), -1);
- }
- result = 1 - result;
- //ImageShow(result*255);
- }
- public void RemoveCircles(Mat image1, Mat image2, out Mat result)
- {
- RemoveCircles(image1, image2, out result, 1, 30, 70, 30, 10, 60);
- }
- public void RemoveCircles(Mat image, out Mat result)
- {
- result = new Mat(image.Size(), image.Type());
- Mat hierachy = new Mat();
- Mat[] contoursMat;
- Cv2.FindContours(image, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- Point[][] contours;
- HierarchyIndex[] hierarchyIndices;
- Cv2.FindContours(image, out contours, out hierarchyIndices, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- for (int i = 0; i < contours.Count(); i++)
- {
- if (contours[i].Count() < 10)
- continue;
- else
- {
- int[] x = new int[contours[i].Count()];
- int[] y = new int[contours[i].Count()];
- for (int j = 0; j < contours[i].Count(); j++)
- {
- x[j] = contours[i][j].X;
- y[j] = contours[i][j].Y;
- }
- double maxY = y.Max();
- double minY = y.Min();
- double maxX = x.Max();
- double minX = x.Min();
- double hengzongbi = (double)((maxX - minX) / (maxY - minY));
- if (hengzongbi > 1.8 || hengzongbi < 0.5)
- {
- Cv2.DrawContours(result, contoursMat, i, new Scalar(1));
- }
- }
- }
- Fill(result, out result, 1);
- }
- /// <summary>
- /// 保留近似直线
- /// </summary>
- /// <param name="image"></param>
- /// <param name="result"></param>
- public void KeepStraight(Mat image, out Mat result, out List<int> ordinates)
- {
- result = new Mat(image.Size(), image.Type());
- ordinates = new List<int>();
- Mat hierachy = new Mat();
- Mat[] contoursMat;
- Cv2.FindContours(image, out contoursMat, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- Point[][] contours;
- HierarchyIndex[] hierarchyIndices;
- Cv2.FindContours(image, out contours, out hierarchyIndices, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- for (int i = 0; i < contours.Count(); i++)
- {
- if (contours[i].Count() < 10)
- continue;
- else
- {
- int[] x = new int[contours[i].Count()];
- int[] y = new int[contours[i].Count()];
- for (int j = 0; j < contours[i].Count(); j++)
- {
- x[j] = contours[i][j].X;
- y[j] = contours[i][j].Y;
- }
- double maxY = y.Max();
- double minY = y.Min();
- double maxX = x.Max();
- double minX = x.Min();
- double hengzongbi = (double)((maxX - minX) / (maxY - minY));
- if (hengzongbi > 20 || hengzongbi < 0.1)
- {
- Cv2.DrawContours(result, contoursMat, i, new Scalar(1));
- //int newOrdinate = (int)(maxY+minY)/2;
- int newOrdinate = (int)minY + 5;
- ordinates.Add(newOrdinate);
- }
- }
- }
- //Fill(result, out result, 1);
- }
- /// <summary>
- /// 保留最大连通域为1,其余为0
- /// </summary>
- /// <param name="image"></param>
- /// <param name="result"></param>
- public void GetMaxArea(Mat image, out Mat result)
- {
- Mat[] contours;
- Mat hierachy = new Mat();
- Cv2.FindContours(image, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- double max_area = 0;
- int index = 0;
- for (int i = 0; i < contours.Count(); i++)
- {
- if (Cv2.ContourArea(contours[i]) > max_area)
- {
- max_area = Cv2.ContourArea(contours[i]);
- index = i;
- }
- }
- result = Mat.Zeros(image.Rows, image.Cols, image.Type());
- Cv2.DrawContours(result, contours, index, new Scalar(1));
- Fill(result, out result, 1);
- //ImageShow(image*255,result * 255);
- }
- /// <summary>
- /// 保留面积大于或小于某个值的连通域
- /// </summary>
- /// <param name="contour"></param>
- /// <param name="result"></param>
- /// <param name="value"></param>
- /// <param name="compare">true:大于;false:小于</param>
- public void GetArea(Mat contour, out Mat result, int value, bool compare)
- {
- result = Mat.Zeros(contour.Rows, contour.Cols, contour.Type());
- Mat[] contours;
- Mat hierachy = new Mat();
- Cv2.FindContours(contour, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- for (int i = 0; i < contours.Count(); i++)
- {
- switch (compare)
- {
- case true:
- if (Cv2.ContourArea(contours[i]) > value)
- {
- Cv2.DrawContours(result, contours, i, new Scalar(1));
- }
- break;
- case false:
- if (Cv2.ContourArea(contours[i]) < value)
- {
- Cv2.DrawContours(result, contours, i, new Scalar(1));
- }
- break;
- }
- }
- Fill(result, out result, 1);
- }
- public void GetAreaNoFill(Mat contour, out Mat result, int value, bool compare)
- {
- result = Mat.Zeros(contour.Rows, contour.Cols, contour.Type());
- Mat[] contours;
- Mat hierachy = new Mat();
- Cv2.FindContours(contour, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- for (int i = 0; i < contours.Count(); i++)
- {
- switch (compare)
- {
- case true:
- if (Cv2.ContourArea(contours[i]) > value)
- {
- Cv2.DrawContours(result, contours, i, new Scalar(1));
- }
- break;
- case false:
- if (Cv2.ContourArea(contours[i]) < value)
- {
- Cv2.DrawContours(result, contours, i, new Scalar(1));
- }
- break;
- }
- }
- //Fill(result, out result, 1);
- }
- /// <summary>
- /// 得到最大连通域的轮廓图
- /// </summary>
- /// <param name="image"></param>
- /// <param name="result"></param>
- public void GetMaxContour(Mat image, out Mat result)
- {
- Mat[] contours;
- Mat hierachy = new Mat();
- Cv2.FindContours(image, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- double max_area = 0;
- int index = 0;
- for (int i = 0; i < contours.Count(); i++)
- {
- if (Cv2.ContourArea(contours[i]) > max_area)
- {
- max_area = Cv2.ContourArea(contours[i]);
- index = i;
- }
- }
- result = Mat.Zeros(image.Rows, image.Cols, image.Type());
- Cv2.DrawContours(result, contours, index, new Scalar(1));
- }
- /// <summary>
- /// 得到图像中非零点的坐标
- /// </summary>
- /// <param name="image"></param>
- /// <param name="ordinates"></param>
- public void FindContourOrdinate(Mat image, out Point[] ordinates)
- {
- int nonZeroNumber = Cv2.CountNonZero(image);
- ordinates = new Point[nonZeroNumber];
- int count = 0;
- for (int i = 0; i < image.Rows; i++)
- {
- for (int j = 0; j < image.Cols; j++)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- ordinates[count].X = j;
- ordinates[count].Y = i;
- }
- }
- }
- }
- /// <summary>
- /// 填充图片孔洞
- /// </summary>
- /// <param name="image">输入二值图像</param>
- /// <param name="result">输出图像</param>
- /// <param name="value">填充像素值</param>
- public void Fill(Mat image, out Mat result, int value)
- {
- Size imageSize = image.Size();
- Mat kuozhan = Mat.Zeros(imageSize.Height + 2, imageSize.Width + 2, image.Type());
- image.CopyTo(kuozhan[1, imageSize.Height + 1, 1, imageSize.Width + 1]);
- Cv2.FloodFill(kuozhan, new Point(0, 0), new Scalar(value));
- Mat crop = kuozhan[1, imageSize.Height + 1, 1, imageSize.Width + 1];
- result = image + value - crop;
- }
- /// <summary>
- /// 对图片进行sobel边缘检测
- /// </summary>
- /// <param name="imageContour"></param>
- /// <param name="imageSobel"></param>
- public void Sobel(Mat imageContour, out Mat imageSobel)
- {
- //横向检测
- Mat grad_x = new Mat();
- Mat grad_x2 = new Mat();
- Cv2.Sobel(imageContour, grad_x, MatType.CV_16S, 1, 0);
- Cv2.ConvertScaleAbs(grad_x, grad_x2);
- //纵向检测
- Mat grad_y = new Mat();
- Mat grad_y2 = new Mat();
- Cv2.Sobel(imageContour, grad_y, MatType.CV_16S, 0, 1);
- Cv2.ConvertScaleAbs(grad_y, grad_y2);
- //横纵向混合平均
- imageSobel = new Mat();
- Cv2.AddWeighted(grad_x2, 0.5, grad_y2, 0.5, 0, imageSobel);
- }
- /// <summary>
- /// 比较两个数的大小
- /// </summary>
- /// <param name="a"></param>
- /// <param name="b"></param>
- /// <param name="world">输入选项,big,small</param>
- /// <param name="result">输出大值或小值</param>
- public void ChooseSize(double a, double b, string world, out double result)
- {
- result = 0;
- switch (world)
- {
- case "big":
- if (a > b)
- {
- result = a;
- }
- else
- {
- result = b;
- }
- break;
- case "small":
- if (a < b)
- {
- result = a;
- }
- else
- {
- result = b;
- }
- break;
- }
- }
- /// <summary>
- /// 判断是否有水平直线
- /// </summary>
- /// <param name="image"></param>
- /// <param name="ordinate">输出直线纵坐标</param>
- /// <param name="border">判断区域</param>
- public void DetectStraightLine(Mat image, out int ordinate, int[] border)
- {
- /*
- * 得到每个连通域的坐标集
- * 求每个连通域坐标集最大最小横纵坐标
- * 求横纵长度
- * 同时满足左右长度和上下长度的阈值条件,判定为直线
- */
- ordinate = 0;
- Mat crop = image[border[0], border[1], 0, image.Cols].Clone();
- //Mat hierachy = new Mat();
- //Mat[] contours;
- //Cv2.FindContours(crop, out contours, hierachy, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- Point[][] contours;
- HierarchyIndex[] hierarchyIndices;
- Cv2.FindContours(crop, out contours, out hierarchyIndices, RetrievalModes.External, ContourApproximationModes.ApproxNone);
- Mat idx = new Mat();
- for (int i = 0; i < contours.Count(); i++)
- {
- if (contours[i].Count() < 200 || contours[i].Count() > 4000)
- continue;
- else
- {
- int[] x = new int[contours[i].Count()];
- int[] y = new int[contours[i].Count()];
- for (int j = 0; j < contours[i].Count(); j++)
- {
- x[j] = contours[i][j].X;
- y[j] = contours[i][j].Y;
- }
- int maxY = y.Max();
- int minY = y.Min();
- int maxX = x.Max();
- int minX = x.Min();
- if ((maxY - minY) < 25 && (maxX - minX) > 250)
- {
- ordinate = (maxY + minY) / 2 + border[0];
- break;
- }
- }
- }
- }
- public void LineShow(Mat image, int x1, int y1, int x2, int y2)
- {
- Point p1 = new Point();
- Point p2 = new Point();
- p1.X = x1;
- p1.Y = y1;
- p2.X = x2;
- p2.Y = y2;
- Scalar color = new Scalar(0, 0, 255);
- //颜色
- Cv2.Line(image, p1, p2, color, 2, LineTypes.Link8);
- }
- /// <summary>
- /// 图像画线,线颜色是红色
- /// </summary>
- /// <param name="image"></param>
- /// <param name="point1"></param>
- /// <param name="point2"></param>
- public void LineShow(Mat image, Point point1, Point point2)
- {
- Scalar color = new Scalar(0, 0, 255);
- //颜色
- Cv2.Line(image, point1, point2, color, 2, LineTypes.Link8);
- }
- /// <summary>
- /// 图像画线,线条颜色可以选择红,绿,蓝
- /// </summary>
- /// <param name="image">画线图像</param>
- /// <param name="x1"></param>
- /// <param name="y1"></param>
- /// <param name="x2"></param>
- /// <param name="y2"></param>
- /// <param name="colorChoose">颜色选择,red、blue、green</param>
- public void LineShow(Mat image, int x1, int y1, int x2, int y2, string colorChoose)
- {
- Point p1 = new Point();
- Point p2 = new Point();
- p1.X = x1;
- p1.Y = y1;
- p2.X = x2;
- p2.Y = y2;
- Scalar color = new Scalar();//颜色
- switch (colorChoose)
- {
- case "blue":
- color = new Scalar(255, 0, 0);
- break;
- case "green":
- color = new Scalar(0, 255, 0);
- break;
- case "red":
- color = new Scalar(0, 0, 255);
- break;
- default:
- break;
- }
- Cv2.Line(image, p1, p2, color, 2, LineTypes.Link8);
- }
- /// <summary>
- /// 图像画线,线条颜色可以选择红,绿,蓝
- /// </summary>
- /// <param name="image"></param>
- /// <param name="point1"></param>
- /// <param name="point2"></param>
- /// <param name="colorChoose">颜色选择,red、blue、green</param>
- public void LineShow(Mat image, Point point1, Point point2, string colorChoose)
- {
- Scalar color = new Scalar();//颜色
- switch (colorChoose)
- {
- case "blue":
- color = new Scalar(255, 0, 0);
- break;
- case "green":
- color = new Scalar(0, 255, 0);
- break;
- case "red":
- color = new Scalar(0, 0, 255);
- break;
- default:
- break;
- }
- Cv2.Line(image, point1, point2, color, 2, LineTypes.Link8);
- }
- /// <summary>
- /// 图片显示数字,两位小数
- /// </summary>
- /// <param name="image"></param>
- /// <param name="data"></param>
- /// <param name="x"></param>
- /// <param name="y"></param>
- public void TextShow(Mat image, double data, int x, int y)
- {
- data = Math.Round(data, 2);
- Scalar color = new Scalar(255, 0, 0);
- Point p = new Point();
- p.X = x;
- p.Y = y;
- Cv2.PutText(image, data.ToString() + "um", p, HersheyFonts.HersheyComplex, 0.8, color, 2, LineTypes.Link8);
- }
- /// <summary>
- /// 图像线条标记(竖向)
- /// </summary>
- /// <param name="image"></param>
- /// <param name="point1"></param>
- /// <param name="point2"></param>
- /// <param name="data"></param>
- public void LabelVertical(Mat image, Point point1, Point point2, double data)
- {
- double proportion = 0.2689;
- data = data * proportion;
- double middle = (point1.Y + point2.Y) / 2;
- LineShow(image, point1, point2, "blue");
- LineShow(image, point1.X - 5, point1.Y, point1.X + 5, point1.Y, "blue");
- LineShow(image, point2.X - 5, point2.Y, point2.X + 5, point2.Y, "blue");
- TextShow(image, data, point1.X, (int)middle);
- }
- /// <summary>
- /// 图像线条标记(橫向)
- /// </summary>
- /// <param name="image"></param>
- /// <param name="point1"></param>
- /// <param name="point2"></param>
- /// <param name="data"></param>
- public void LabelHorizontal(Mat image, Point point1, Point point2, double data)
- {
- double proportion = 0.2689;
- data = data * proportion;
- double middle = (point1.X + point2.X) / 2;
- LineShow(image, point1, point2, "blue");
- LineShow(image, point1.X, point1.Y - 5, point1.X, point1.Y + 5, "blue");
- LineShow(image, point2.X, point2.Y - 5, point2.X, point2.Y + 5, "blue");
- TextShow(image, data, (int)middle, point1.Y);
- }
- public void ImageShow(Mat image1)
- {
- new Window("image1", WindowMode.Normal, image1);
- Cv2.WaitKey();
- }
- public void ImageShow(Mat image1, Mat image2)
- {
- new Window("image1", WindowMode.Normal, image1);
- new Window("image2", WindowMode.Normal, image2);
- Cv2.WaitKey();
- }
- public void ImageShow(Mat image1, Mat image2, Mat image3)
- {
- new Window("image1", WindowMode.Normal, image1);
- new Window("image2", WindowMode.Normal, image2);
- new Window("image3", WindowMode.Normal, image3);
- Cv2.WaitKey();
- }
- public void ImageShow(Mat image1, Mat image2, Mat image3, Mat image4)
- {
- new Window("image1", WindowMode.Normal, image1);
- new Window("image2", WindowMode.Normal, image2);
- new Window("image3", WindowMode.Normal, image3);
- new Window("image4", WindowMode.Normal, image4);
- Cv2.WaitKey();
- }
- public void ImageShow(Mat image1, Mat image2, Mat image3, Mat image4, Mat image5)
- {
- new Window("image1", WindowMode.Normal, image1);
- new Window("image2", WindowMode.Normal, image2);
- new Window("image3", WindowMode.Normal, image3);
- new Window("image4", WindowMode.Normal, image4);
- new Window("image5", WindowMode.Normal, image5);
- Cv2.WaitKey();
- }
- /// <summary>
- /// 点增强
- /// </summary>
- /// <param name="image"></param>
- /// <param name="result"></param>
- public void PointEnhancement(Mat image, out Mat result)
- {
- result = new Mat(image.Size(), image.Type());
- InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });
- Cv2.Filter2D(image, result, -1, kernel);
- Cv2.ConvertScaleAbs(result, result);
- }
- public void LineEnhancement(Mat image, out Mat result)
- {
- result = new Mat(image.Size(), image.Type());
- InputArray kernel = InputArray.Create<int>(new int[3, 3] { { -1, -1, -1 }, { 1, 5, 1 }, { -1, -1, -1 } });
- Cv2.Filter2D(image, result, -1, kernel);
- //result += 100;
- Cv2.ConvertScaleAbs(result, result);
- }
- /// <summary>
- /// 纵向边缘检测
- /// </summary>
- /// <param name="image"></param>
- /// <param name="result"></param>
- public void EdgeY(Mat image, out Mat result)
- {
- InputArray kernel1 = InputArray.Create<int>(new int[3, 3] { { -5, -10, -5 }, { 0, 0, 0 }, { 5, 10, 5 } });
- InputArray kernel2 = InputArray.Create<int>(new int[3, 3] { { 5, 10, 5 }, { 0, 0, 0 }, { -5, -10, -5 } });
- Mat result1 = new Mat();
- Mat result2 = new Mat();
- Cv2.Filter2D(image, result1, MatType.CV_16SC1, kernel1);
- Cv2.Filter2D(image, result2, MatType.CV_16SC1, kernel2);
- Cv2.ConvertScaleAbs(result1, result1);
- Cv2.ConvertScaleAbs(result2, result2);
- result = new Mat();
- Cv2.AddWeighted(result1, 0.5, result2, 0.5, 0, result);
- }
- public void EdgeY2(Mat image, out Mat result)
- {
- 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 } });
- 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 } });
- Mat result1 = new Mat();
- Mat result2 = new Mat();
- Cv2.Filter2D(image, result1, MatType.CV_16SC1, kernel1);
- Cv2.Filter2D(image, result2, MatType.CV_16SC1, kernel2);
- Cv2.ConvertScaleAbs(result1, result1);
- Cv2.ConvertScaleAbs(result2, result2);
- result = new Mat();
- Cv2.AddWeighted(result1, 0.5, result2, 0.5, 0, result);
- }
- /// <summary>
- /// 横向边缘检测
- /// </summary>
- /// <param name="image"></param>
- /// <param name="result"></param>
- public void EdgeX(Mat image, out Mat result)
- {
- InputArray kernel1 = InputArray.Create<int>(new int[3, 3] { { -5, 0, 5 }, { -10, 0, 10 }, { -5, 0, 5 } });
- InputArray kernel2 = InputArray.Create<int>(new int[3, 3] { { 5, 0, -5 }, { 10, 0, -10 }, { 5, 0, -5 } });
- Mat result1 = new Mat();
- Mat result2 = new Mat();
- Cv2.Filter2D(image, result1, MatType.CV_16SC1, kernel1);
- Cv2.Filter2D(image, result2, MatType.CV_16SC1, kernel2);
- Cv2.ConvertScaleAbs(result1, result1);
- Cv2.ConvertScaleAbs(result2, result2);
- result = new Mat();
- Cv2.AddWeighted(result1, 0.5, result2, 0.5, 0, result);
- }
- /// <summary>
- /// 边缘检测,横向边缘检测与纵向边缘检测的均值
- /// </summary>
- /// <param name="image"></param>
- /// <param name="result"></param>
- public void Edge(Mat image, out Mat result)
- {
- result = new Mat();
- Mat result1 = new Mat();
- Mat result2 = new Mat();
- EdgeX(image, out result1);
- EdgeY(image, out result2);
- Cv2.AddWeighted(result1, 0.5, result2, 0.5, 0, result);
- }
- /// <summary>
- /// 求一组坐标点的平均坐标值,去掉最大横纵坐标和最小横纵坐标
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="averOrdinate"></param>
- public void AverOrdinate(int[] x, int[] y, out double[] averOrdinate)
- {
- averOrdinate = new double[2];
- int length = x.Length;
- int maxX = 0, minX = 0, maxY = 0, minY = 0;
- maxX = x.Max();
- minX = x.Min();
- maxY = y.Max();
- minY = y.Min();
- int[] newX = new int[length - 2];
- int[] newY = new int[length - 2];
- bool maxTag = false;
- bool minTag = false;
- int count = 0;
- int zeroX = 0;
- int zeroY = 0;
- for (int i = 0; i < length; i++)
- {
- if (x[i] == maxX && maxTag == false)
- {
- maxTag = true;
- continue;
- }
- else if (x[i] == minX && minTag == false)
- {
- minTag = true;
- continue;
- }
- else if (x[i] != 0)
- {
- newX[count] = x[i];
- count++;
- }
- else if (x[i] == 0)
- zeroX++;
- }
- maxTag = false;
- minTag = false;
- count = 0;
- for (int i = 0; i < length; i++)
- {
- if (y[i] == maxY && maxTag == false)
- {
- maxTag = true;
- continue;
- }
- else if (y[i] == minY && minTag == false)
- {
- minTag = true;
- continue;
- }
- else if (y[i] != 0)
- {
- newY[count] = y[i];
- count++;
- }
- else if (y[i] == 0)
- zeroY++;
- }
- double averX = ((newX.Length - zeroX) == 0) ? 0 : newX.Sum() / (newX.Length - zeroX);
- double averY = ((newY.Length - zeroY) == 0) ? 0 : newY.Sum() / (newY.Length - zeroY);
- averOrdinate[0] = averX;
- averOrdinate[1] = averY;
- }
- /// <summary>
- /// 得到图中非零点的坐标
- /// </summary>
- /// <param name="image"></param>
- /// <param name="idx"></param>
- public void FindNonZeros(Mat image, out Point[] idx)
- {
- Mat thresh = image.Threshold(0, 1, ThresholdTypes.Binary);
- int num = (int)thresh.Sum();//不为0点的数量
- idx = new Point[num];
- int count = 0;
- for (int i = 0; i < image.Rows; i++)
- {
- if ((int)image[i, i + 1, 0, image.Cols].Sum() != 0)
- {
- for (int j = 0; j < image.Cols; j++)
- {
- if (image.Get<byte>(i, j) > 0)
- {
- idx[count].X = j;
- idx[count].Y = i;
- count++;
- }
- }
- }
- }
- }
- /// <summary>
- /// 得到图中非零点最左上角的坐标
- /// </summary>
- /// <param name="image"></param>
- /// <param name="leftTop"></param>
- public void FindLeftTop(Mat image, out Point leftTop)
- {
- leftTop = new Point();
- Point[] idx;
- FindNonZeros(image, out idx);
- int min = 1000000;
- for (int i = 0; i < idx.Length; i++)
- {
- if (min > (idx[i].X + idx[i].Y))
- {
- min = idx[i].X + idx[i].Y;
- leftTop.X = idx[i].X;
- leftTop.Y = idx[i].Y;
- }
- }
- }
- }
- }
|