1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278 |
- #pragma once
- #include "stdafx.h"
-
- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/opencv.hpp>
- #include "OTSImageProcess.h"
- #include "OTSImageProcessParam.h"
- #include <OTSFieldData.h>
- #include "../OTSLog/COTSUtilityDllFunExport.h"
- #include "FieldMgr.h"
- using namespace cv;
- using namespace std;
- namespace OTSIMGPROC
- {
- namespace
- {
-
- /***** 求两点间距离*****/
- float getDistance(Point pointO, Point pointA)
- {
- float distance;
- distance = powf((pointO.x - pointA.x), 2) + powf((pointO.y - pointA.y), 2);
- distance = sqrtf(distance);
- return distance;
- }
- /***** 点到直线的距离:P到AB的距离*****/
- //P为线外一点,AB为线段两个端点
- float getDist_P2L(Point pointP, Point pointA, Point pointB)
- {
- //求直线方程
- int A = 0, B = 0, C = 0;
- A = pointA.y - pointB.y;
- B = pointB.x - pointA.x;
- C = pointA.x*pointB.y - pointA.y*pointB.x;
- //代入点到直线距离公式
- float distance = 0;
- distance = ((float)abs(A*pointP.x + B * pointP.y + C)) / ((float)sqrtf(A*A + B * B));
- return distance;
- }
- int Side(Point P1, Point P2, Point point)
- {
- /*Point P1 = line.P1;
- Point P2 = line.P2;*/
- return ((P2.y - P1.y) * point.x + (P1.x - P2.x) * point.y + (P2.x*P1.y - P1.x*P2.y));
- }
- void FindInnerCircleInContour(vector<Point> contour, Point ¢er, int &radius)
- {
- Rect r = boundingRect(contour);
- int nL = r.x, nR = r.br().x; //轮廓左右边界
- int nT = r.y, nB = r.br().y; //轮廓上下边界
- double dist = 0;
- double maxdist = 0;
- for (int i = nL; i < nR; i++) //列
- {
- for (int j = nT; j < nB; j++) //行
- {
- //计算轮廓内部各点到最近轮廓点的距离
- dist = pointPolygonTest(contour, Point(i, j), true);
- if (dist > maxdist)
- {
- //求最大距离,只有轮廓最中心的点才距离最大
- maxdist = dist;
- center = Point(i, j);
- }
- }
- }
- radius = maxdist; //圆半径
- }
- BOOL GetParticleAverageChord(std::vector<Point> listEdge, double a_PixelSize, double &dPartFTD)
- {
- // safety check
- double nx = 0, ny = 0;
- Moments mu;
- mu = moments(listEdge, false);
- nx = mu.m10 / mu.m00;
- ny = mu.m01 / mu.m00;
- //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
- Point ptCenter = Point((int)nx, (int)ny);
- // coordinate transformation
- Point ptPosition;
- int radiusNum = 0;
- // get ferret diameter
- double sumFltDiameter = 0;
- int interval;
- int edgePointNum = listEdge.size();
- if (edgePointNum > 100)
- {
- interval = edgePointNum / 100;//get one line per 10 degree aproxemately
- }
- else
- {
- interval = 1;
- }
- for (int i = 0; i < edgePointNum; i++)
- {
- Point pt = listEdge[i];
- ptPosition.x = abs(pt.x - ptCenter.x);
- ptPosition.y = abs(pt.y - ptCenter.y);
- if (i % interval == 0)//calculate one line per 10 point ,so to speed up.don't calculate all the diameter.
- {
- double r1 = sqrt(pow(ptPosition.x, 2) + pow(ptPosition.y, 2));
- sumFltDiameter += r1;
- radiusNum += 1;
- //line(cvImageData, ptCenter, pt, Scalar(nBlackColor), nThickness, nLineType);
- }
- }
- if (radiusNum == 0)
- {
- dPartFTD = 0;
- }
- else
- {
- dPartFTD = a_PixelSize * sumFltDiameter / radiusNum * 2;
- }
- //imshow("feret center", cvImageData);
- return TRUE;
- }
- void linearSmooth5(WORD wordIn[], WORD wordOut[], int N = 255)//smooth algorithm
- {
- double in[256];
- double out[256];
- double smoothCurveData[256];
- for (int i = 0; i < 256; i++)
- {
- in[i] = (double)wordIn[i];
- }
- int i;
- if (N < 5)
- {
- for (i = 0; i <= N - 1; i++)
- {
- out[i] = in[i];
- }
- }
- else
- {
- out[0] = (3.0 * in[0] + 2.0 * in[1] + in[2] - in[4]) / 5.0;
- out[1] = (4.0 * in[0] + 3.0 * in[1] + 2 * in[2] + in[3]) / 10.0;
- for (i = 2; i <= N - 3; i++)
- {
- out[i] = (in[i - 2] + in[i - 1] + in[i] + in[i + 1] + in[i + 2]) / 5.0;
- }
- out[N - 2] = (4.0 * in[N - 1] + 3.0 * in[N - 2] + 2 * in[N - 3] + in[N - 4]) / 10.0;
- out[N - 1] = (3.0 * in[N - 1] + 2.0 * in[N - 2] + in[N - 3] - in[N - 5]) / 5.0;
- }
- for (int i = 0; i < N; i++)
- {
- wordOut[i] = (WORD)out[i];
- }
- }
- }
- COTSImageProcess::COTSImageProcess()
- {
- }
- COTSImageProcess::~COTSImageProcess()
- {
- }
- // use verticl line of 3 pixel to erode a image
- void COTSImageProcess::BErodeVertical3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
- {
- WORD x, y, wcounts;
- if (rows <= 2 || columns <= 2)return;
- // top line
- for (x = 0; x < columns; x++)
- {
- *(target + x) = 0;
- }
- // bottom line
- for (x = 0; x < columns; x++)
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0;
- }
- for (y = 1; y<rows - 1; y++)
- {
- for (x = 0; x<columns; x++)
- {
- if (*(source + (DWORD)y*columns + x) == 0)
- {
- *(target + (DWORD)y*columns + x) = 0;
- continue;
- }
- wcounts = 0;
- if (*(source + (DWORD)(y - 1)*columns + x) == 255)
- {
- wcounts++;
- }
- if (*(source + (DWORD)(y + 1)*columns + x) == 255)
- {
- wcounts++;
- }
- if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
- else *(target + (DWORD)y*columns + x) = 0;
- }
- }
- }
- // use left 45 degree line of 3 pixel to erode a image
- void COTSImageProcess::BErodeLeft45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
- {
- WORD x, y, wcounts;
- if (rows <= 2 || columns <= 2)return;
- // top line
- for (x = 0; x < columns; x++)
- {
- *(target + x) = 0;
- }
- // bottom line
- for (x = 0; x < columns; x++)
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0;
- }
- // left line
- for (y = 0; y<rows; y++)
- {
- *(target + (DWORD)y*columns) = 0;
- }
- // right line
- for (y = 0; y<rows; y++)
- {
- *(target + (DWORD)y*columns + columns - 1) = 0;
- }
- for (y = 1; y < rows - 1; y++)
- {
- for (x = 1; x < columns - 1; x++)
- {
- if (*(source + (DWORD)y*columns + x) == 0)
- {
- *(target + (DWORD)y*columns + x) = 0;
- continue;
- }
- wcounts = 0;
- if (*(source + (DWORD)(y - 1)*columns + x - 1) == 255)
- {
- wcounts++;
- }
- if (*(source + (DWORD)(y + 1)*columns + x + 1) == 255)
- {
- wcounts++;
- }
- if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
- else *(target + (DWORD)y*columns + x) = 0;
- }
- }
-
- }
- // use horizoontal line of 3 pixel to erode a image
- void COTSImageProcess::BErodeHorizontal3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
- {
- WORD x, y, wcounts;
- if (rows <= 2 || columns <= 2)return;
- // left line
- for (y = 0; y<rows; y++)
- {
- *(target + (DWORD)y*columns) = 0;
- }
- // right line
- for (y = 0; y<rows; y++)
- {
- *(target + (DWORD)y*columns + columns - 1) = 0;
- }
- for (y = 0; y<rows; y++)
- {
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)y*columns + x) == 0)
- {
- *(target + (DWORD)y*columns + x) = 0;
- continue;
- }
- wcounts = 0;
- if (*(source + (DWORD)y*columns + x - 1) == 255)
- {
- wcounts++;
- }
- if (*(source + (DWORD)y*columns + x + 1) == 255)
- {
- wcounts++;
- }
- if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
- else *(target + (DWORD)y*columns + x) = 0;
- }
- }
- }
- // use right 45 degree line of 3 pixel to erode a image
- void COTSImageProcess::BErodeRight45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
- {
- WORD x, y, wcounts;
- if (rows <= 2 || columns <= 2)return;
- // top line
- for (x = 0; x < columns; x++)
- {
- *(target + x) = 0;
- }
- // bottom line
- for (x = 0; x < columns; x++)
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0;
- }
- // left line
- for (y = 0; y<rows; y++)
- {
- *(target + (DWORD)y*columns) = 0;
- }
- // right line
- for (y = 0; y<rows; y++)
- {
- *(target + (DWORD)y*columns + columns - 1) = 0;
- }
- for (y = 1; y<rows - 1; y++)
- {
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)y*columns + x) == 0)
- {
- *(target + (DWORD)y*columns + x) = 0;
- continue;
- }
- wcounts = 0;
- if (*(source + (DWORD)(y - 1)*columns + x + 1) == 255)
- {
- wcounts++;
- }
- if (*(source + (DWORD)(y + 1)*columns + x - 1) == 255)
- {
- wcounts++;
- }
- if (wcounts == 2) *(target + (DWORD)y*columns + x) = 255;
- else *(target + (DWORD)y*columns + x) = 0;
- }
- }
-
- }
- void COTSImageProcess::BDilateVertical3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
- {
- WORD x, y;
- if (rows <= 2 || columns <= 2)return;
- // top line
- for (x = 0; x<columns; x++)
- {
- if (*(source + x) != 0)
- {
- *(target + x) = 0xff;
- }
- }
- // bottom line
- for (x = 0; x<columns; x++)
- {
- if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
- }
- }
- for (y = 1; y<rows - 1; y++)
- {
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)y*columns + x) != 0)
- {
- *(target + (DWORD)y*columns + x) = 0xff;
- *(target + (DWORD)(y - 1)*columns + x) = 255;
- *(target + (DWORD)(y + 1)*columns + x) = 255;
- }
- else *(target + (DWORD)y*columns + x) = 0;
- }
- }
- }
- void COTSImageProcess::BDilateLeft45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
- {
- WORD x, y;
- if (rows <= 2 || columns <= 2)return;
- // top line
- for (x = 0; x<columns; x++)
- {
- if (*(source + x) != 0)
- {
- *(target + x) = 0xff;
- }
- }
- // bottom line
- for (x = 0; x<columns; x++)
- {
- if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
- }
- }
- // left line
- for (y = 0; y<rows; y++)
- {
- if (*(source + (DWORD)y*columns) != 0)
- {
- *(target + (DWORD)y*columns) = 0xff;
- }
- }
- // right line
- for (y = 0; y<rows; y++)
- {
- if (*(source + (DWORD)y*columns + columns - 1) != 0)
- {
- *(target + (DWORD)y*columns + columns - 1) = 0xff;
- }
- }
- for (y = 1; y<rows - 1; y++)
- {
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)y*columns + x) != 0)
- {
- *(target + (DWORD)y*columns + x) = 0xff;
- *(target + (DWORD)(y - 1)*columns + x - 1) = 255;
- *(target + (DWORD)(y + 1)*columns + x + 1) = 255;
- }
- else *(target + (DWORD)y*columns + x) = 0;
- }
- }
- }
- void COTSImageProcess::BDilateHorizontal3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
- {
- WORD x, y;
- if (rows <= 2 || columns <= 2)return;
- // left line
- for (y = 0; y<rows; y++)
- {
- if (*(source + (DWORD)y*columns) != 0)
- {
- *(target + (DWORD)y*columns) = 0xff;
- }
- }
- // right line
- for (y = 0; y<rows; y++)
- {
- if (*(source + (DWORD)y*columns + columns - 1) != 0)
- {
- *(target + (DWORD)y*columns + columns - 1) = 0xff;
- }
- }
- for (y = 1; y<rows - 1; y++)
- {
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)y*columns + x) != 0)
- {
- *(target + (DWORD)y*columns + x) = 0xff;
- *(target + (DWORD)y*columns + x - 1) = 255;
- *(target + (DWORD)y*columns + x + 1) = 255;
- }
- else *(target + (DWORD)y*columns + x) = 0;
- }
- }
- }
- void COTSImageProcess::BDilateRight45Degree3(LPBYTE source, LPBYTE target, WORD rows, WORD columns)
- {
- WORD x, y;
- if (rows <= 2 || columns <= 2)return;
- // top line
- for (x = 0; x<columns; x++)
- {
- if (*(source + x) != 0)
- {
- *(target + x) = 0xff;
- }
- }
- // bottom line
- for (x = 0; x<columns; x++)
- {
- if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
- }
- }
- // left line
- for (y = 0; y<rows; y++)
- {
- if (*(source + (DWORD)y*columns) != 0)
- {
- *(target + (DWORD)y*columns) = 0xff;
- }
- }
- // right line
- for (y = 0; y<rows; y++)
- {
- if (*(source + (DWORD)y*columns + columns - 1) != 0)
- {
- *(target + (DWORD)y*columns + columns - 1) = 0xff;
- }
- }
- for (y = 1; y<rows - 1; y++)
- {
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)y*columns + x) != 0)
- {
- *(target + (DWORD)y*columns + x) = 0xff;
- *(target + (DWORD)(y - 1)*columns + x + 1) = 255;
- *(target + (DWORD)(y + 1)*columns + x - 1) = 255;
- }
- else *(target + (DWORD)y*columns + x) = 0;
- }
- }
- }
- void COTSImageProcess::BErode3(LPBYTE source, LPBYTE target, WORD wDegree, WORD rows, WORD columns)
- {
- WORD x, y, i, j, wcounts;
- if (rows == 1 || columns == 1)return;
- for (y = 1; y<rows - 1; y++)
- {
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)y*columns + x) == 0)
- {
- *(target + (DWORD)y*columns + x) = 0;
- continue;
- }
- wcounts = 0;
- for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
- {
- for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
- {
- if (*(source + (DWORD)i*columns + j) == 0)
- {
- wcounts++;
- }
- }
- }
- if (wcounts >= wDegree) *(target + (DWORD)y*columns + x) = 0;
- else *(target + (DWORD)y*columns + x) = 0xff;
- }
- }
- // top line
- for (x = 1; x<columns - (WORD)1; x++)
- {
- if (*(source + x) == 0)
- {
- *(target + x) = 0;
- continue;
- }
- wcounts = 0;
- for (i = 0; i <= 1; i++)
- {
- for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
- {
- if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
- }
- }
- if (wcounts >= wDegree * 5 / 8) *(target + x) = 0;
- else *(target + x) = 0xff;
- }
- // bottom line
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)(rows - 1)*columns + x) == 0)
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0;
- continue;
- }
- wcounts = 0;
- for (i = (WORD)(rows - 2); i <= (WORD)(rows - 1); i++)
- {
- for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
- {
- if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
- }
- }
- if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)(rows - 1)*columns + x) = 0;
- else *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
- }
- // left line
- for (y = 1; y<rows - 1; y++)
- {
- if (*(source + (DWORD)y*columns) == 0)
- {
- *(target + (DWORD)y*columns) = 0;
- continue;
- }
- wcounts = 0;
- for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
- {
- for (j = 0; j <= 1; j++)
- {
- if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
- }
- }
- if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)y*columns) = 0;
- else *(target + (DWORD)y*columns) = 0xff;
- }
- // right line
- for (y = 1; y<rows - 1; y++)
- {
- if (*(source + (DWORD)y*columns + columns - 1) == 0)
- {
- *(target + (DWORD)y*columns + columns - 1) = 0;
- continue;
- }
- wcounts = 0;
- for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
- {
- for (j = (WORD)(columns - 2); j <= (WORD)(columns - 1); j++)
- {
- if (*(source + (DWORD)i*columns + j) == 0) wcounts++;
- }
- }
- if (wcounts >= wDegree * 5 / 8) *(target + (DWORD)y*columns + columns - 1) = 0;
- else *(target + (DWORD)y*columns + columns - 1) = 0xff;
- }
- return;
- }
- void COTSImageProcess::BDilate3(LPBYTE source, LPBYTE target, WORD wDegree, WORD rows, WORD columns)
- {
- WORD x, y, i, j, wcounts;
- for (y = 1; y<rows - 1; y++)
- {
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)y*columns + x) != 0)
- {
- *(target + (DWORD)y*columns + x) = 0xff;
- continue;
- }
- wcounts = 0;
- for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
- {
- for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
- {
- if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
- }
- }
- if (wcounts >= wDegree) *(target + (DWORD)y*columns + x) = 0xff;
- else *(target + (DWORD)y*columns + x) = 0;
- }
- }
- // top line
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + x) != 0)
- {
- *(target + x) = 0xff;
- continue;
- }
- wcounts = 0;
- for (i = 0; i <= 1; i++)
- {
- for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
- {
- if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
- }
- }
- if (wcounts >= wDegree * 5 / 8) // but does not mater, as we have border of 2 now
- {
- *(target + x) = 0xff;
- }
- else { *(target + x) = 0; }
- }
- // bottom line
- for (x = 1; x<columns - 1; x++)
- {
- if (*(source + (DWORD)(rows - 1)*columns + x) != 0)
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
- continue;
- }
- wcounts = 0;
- for (i = (WORD)(rows - 2); i <= (WORD)(rows - 1); i++)
- {
- for (j = (WORD)(x - 1); j <= (WORD)(x + 1); j++)
- {
- if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
- }
- }
- if (wcounts > wDegree * 5 / 8)
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0xff;
- }
- else
- {
- *(target + (DWORD)(rows - 1)*columns + x) = 0;
- }
- }
- // left line
- for (y = 1; y<rows - 1; y++)
- {
- if (*(source + (DWORD)y*columns) != 0)
- {
- *(target + (DWORD)y*columns) = 0xff;
- continue;
- }
- wcounts = 0;
- for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
- {
- for (j = 0; j <= (WORD)1; j++)
- {
- if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
- }
- }
- if (wcounts >= wDegree * 5 / 8)
- {
- *(target + (DWORD)y*columns) = 0xff;
- }
- else
- {
- *(target + (DWORD)y*columns) = 0;
- }
- }
- // right line
- for (y = 1; y<rows - 1; y++)
- {
- if (*(source + (DWORD)y*columns + columns - 1) != 0)
- {
- *(target + (DWORD)y*columns + columns - 1) = 0xff;
- continue;
- }
- wcounts = 0;
- for (i = (WORD)(y - 1); i <= (WORD)(y + 1); i++)
- {
- for (j = (WORD)(columns - 2); j <= (WORD)(columns - 1); j++)
- {
- if (*(source + (DWORD)i*columns + j) != 0) wcounts++;
- }
- }
- if (wcounts >= wDegree * 5 / 8)
- {
- *(target + (DWORD)y*columns + columns - 1) = 0xff;
- }
- else
- {
- *(target + (DWORD)y*columns + columns - 1) = 0;
- }
- }
- // four cornor points treated separately here
- // top-left
- if (*(source) != 0)
- {
- *target = 0xff;
- }
- else
- {
- wcounts = 0;
- if (*(source + 1) != 0) wcounts++;
- if (*(source + columns) != 0) wcounts++;
- if (*(source + columns + 1) != 0) wcounts++;
- // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
- if (wcounts * 8 >= wDegree * 3)
- {
- *target = 0xff;
- }
- else
- {
- *target = 0;
- }
- }
- //top-right
- if (*(source + columns - 1) != 0)
- {
- *(target + columns - 1) = 0xff;
- }
- else
- {
- wcounts = 0;
- if (*(source + columns - 2) != 0) wcounts++;
- if (*(source + columns * 2 - 1) != 0) wcounts++;
- if (*(source + columns * 2 - 2) != 0) wcounts++;
- // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
- if (wcounts * 8 >= wDegree * 3)
- {
- *(target + columns - 1) = 0xff;
- }
- else
- {
- *(target + columns - 1) = 0;
- }
- }
- //bottom-left
- if (*(source + (DWORD)columns * (rows - 1)) != 0)
- {
- *(target + (DWORD)columns * (rows - 1)) = 0xff;
- }
- else
- {
- wcounts = 0;
- if (*(source + (DWORD)columns * (rows - 1) + 1) != 0) wcounts++;
- if (*(source + (DWORD)columns * (rows - 2)) != 0) wcounts++;
- if (*(source + (DWORD)columns * (rows - 2) + 1) != 0) wcounts++;
- // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
- if (wcounts * 8 >= wDegree * 3)
- {
- *(target + (DWORD)columns * (rows - 1)) = 0xff;
- }
- else
- {
- *(target + (DWORD)columns * (rows - 1)) = 0;
- }
- }
- //bottom-right
- if (*(source + (DWORD)columns * rows - 1) != 0)
- {
- *(target + (DWORD)columns * rows - 1) = 0xff;
- }
- else
- {
- wcounts = 0;
- if (*(source + (DWORD)columns * rows - 2) != 0) wcounts++;
- if (*(source + (DWORD)columns * (rows - 1) - 2) != 0) wcounts++;
- if (*(source + (DWORD)columns * (rows - 1) - 1) != 0) wcounts++;
- // if (wcounts >= wDegree*3/8) // this is a bug here - interger division
- if (wcounts * 8 >= wDegree * 3)
- {
- *(target + (DWORD)columns * rows - 1) = 0xff;
- }
- else
- {
- *(target + (DWORD)columns * rows - 1) = 0;
- }
- }
- return;
- }
- // ReZoom the picture with re-magnification
- BOOL COTSImageProcess::ReZoom(CString InPutPath, CString OutPutPath)
- {
- Mat cvSrcImg;
- string strInputPath;
- strInputPath = CStringA(InPutPath);
- // Pictures loop in folder
- std::vector<cv::String> ImageFolder;
- cv::glob(strInputPath, ImageFolder);
- if (ImageFolder.size() == 0)
- {
- return FALSE;
- }
- for (unsigned int nImgNum = 0; nImgNum < ImageFolder.size(); ++nImgNum) {
- cvSrcImg = cv::imread(ImageFolder[nImgNum], CV_LOAD_IMAGE_GRAYSCALE);
- // Image convolution operation
- //// convolution kernel
- float kernel[] = { -1, -1 , -1, -1 , 0, -1, -1 , -1 , -1 };
- cv::Mat ker = cv::Mat(nImage_Size, nImage_Size, CV_32F, &kernel);
- cv::Mat cvDstImg = cv::Mat(cvSrcImg.size(), cvSrcImg.type());
- // anchor of the kernel
- cv::Point anchor(-1, -1);
- cv::filter2D(cvSrcImg, cvDstImg, CV_32F, ker, anchor, delta, cv::THRESH_TRUNC);
- // Maximum Pixel Value
- cvDstImg = abs(cvDstImg);
- double minVal, maxVal;
- minMaxLoc(cvDstImg, &minVal, &maxVal);
- // Grayscale image
- int nReduce;
- Mat onesImg = Mat::ones(cvDstImg.rows, cvDstImg.cols, CV_32F) * (int)minVal;
- absdiff(cvDstImg, onesImg, cvDstImg);
- nReduce = (int)maxVal - minVal;
- cvDstImg = cvDstImg * nBlackColor / nReduce;
- // Output image convert data to int
- cvDstImg.convertTo(cvDstImg, CV_8U);
- // Process the picture to 128 pixels
- resize(cvDstImg, cvDstImg, Size(nPictureSize, nPictureSize));
- threshold(cvDstImg, cvDstImg, nProcessParam, nBlackColor, CV_THRESH_BINARY);
- string strOutPutPath;
- strOutPutPath = CStringA(OutPutPath);
- imwrite(strOutPutPath , cvDstImg);
- }
- return TRUE;
- }
- BOOL COTSImageProcess::RemoveBSEImageBG(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImgProcessParam,COTSFieldDataPtr m_pFieldData)
- {
- ASSERT(m_pFieldData);
- if (!m_pFieldData)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: there is no field data"));
- return FALSE;
- }
- ASSERT(m_pBSEImg);
- if (!m_pBSEImg)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: there is no image data"));
- return FALSE;
- }
- ASSERT(a_pImgProcessParam);
- if (!a_pImgProcessParam)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: there is no image process data"));
- return FALSE;
- }
- int nWidthImg = m_pBSEImg->GetWidth();
- int nHeightImg = m_pBSEImg->GetHeight();
- m_pFieldData->Width = nWidthImg;
- m_pFieldData->Height = nHeightImg;
- long nImgSize = nWidthImg * nHeightImg;
-
-
- BYTE* pSrcImg = m_pBSEImg->GetImageDataPointer();
- BYTE* pTempImg = new BYTE[nImgSize];
-
-
-
- CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg());
- long nNumParticle = 0;
- RemoveBackGround(m_pBSEImg, a_pImgProcessParam, imgNoBGBinary,nNumParticle);
-
-
- BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
-
-
-
- long nPtStart = a_pImgProcessParam->GetParticleGray().GetStart();
- long nPtEnd = a_pImgProcessParam->GetParticleGray().GetEnd();
- if (nNumParticle == 0)
- {
- COTSParticleList listParticleEmpty;
- listParticleEmpty.clear();
- m_pFieldData->SetParticleList(listParticleEmpty);
-
- //LogInfoTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: no particle is found."));
- }
- else
- {
-
- // get the area image
- COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
- COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
- COTSParticleList listParticleOut;
- if (!GetParticles(0,0,nWidthImg, nHeightImg, pPixel, listParticleOut))
- {
- COTSParticleList listParticleEmpty;
- listParticleEmpty.clear();
- m_pFieldData->SetParticleList(listParticleEmpty);
-
- }
-
- // form a image only have particles on
- COTSSegmentsList listImage;
- for (auto pParticle : listParticleOut)
- {
- COTSFeaturePtr pFeature = pParticle->GetFeature();
- COTSSegmentsList listSegment = pFeature->GetSegmentsList();
- long nPixelNum = 0;
- long nPixelAll = 0;
- int nStartS = 0;
- int nHeightS = 0;
- int nLengthS = 0;
- for (auto pSegment : listSegment)
- {
- // update image list
- COTSSegmentPtr pSegNew = COTSSegmentPtr(new COTSSegment(*pSegment.get()));
- listImage.push_back(pSegNew);
- // get particle average gray
- nStartS = pSegment->GetStart();
- nHeightS = pSegment->GetHeight();
- nLengthS = pSegment->GetLength();
- nPixelNum += (long)nLengthS;
- if (nHeightS > nHeightImg)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
- return FALSE;
- }
- if ((nStartS + nLengthS - 1) > nWidthImg)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("seg starst and length is wrong."));
- return FALSE;
- }
- for (unsigned int i = 0; i < nLengthS; i++)
- {
- if ((nStartS + i) > nWidthImg)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("seg start is wrong."));
- return FALSE;
- }
- else if (nHeightS > nHeightImg)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
- return FALSE;
- }
- long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
- nPixelAll += nValueTemp;
- }
- }
- BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
- pParticle->SetAveGray(nAveGray);
- pParticle->SetArea(nPixelNum);
- }
- m_pFieldData->SetParticleList(listParticleOut);
- }
-
- delete[]pTempImg;
- return TRUE;
-
-
- }
- BOOL COTSImageProcess::RemoveBGByFindContour(CBSEImgPtr m_pBSEImg, COTSImageProcessParamPtr a_pImageProcessParam, COTSFieldDataPtr m_pFieldData)
- {
- ASSERT(m_pFieldData);
- ASSERT(m_pBSEImg);
- ASSERT(a_pImageProcessParam);
-
- int nWidthImg = m_pBSEImg->GetWidth();
- int nHeightImg = m_pBSEImg->GetHeight();
- m_pFieldData->Width = nWidthImg;
- m_pFieldData->Height = nHeightImg;
- long nImgSize = nWidthImg * nHeightImg;
- BYTE* pSrcImg = m_pBSEImg->GetImageDataPointer();
- BYTE* pTempImg = new BYTE[nImgSize];
- CBSEImgPtr imgNoBGBinary = CBSEImgPtr(new CBSEImg());
- long nNumParticle = 0;
- RemoveBackGround(m_pBSEImg, a_pImageProcessParam, imgNoBGBinary, nNumParticle);
- BYTE* pPixel = imgNoBGBinary->GetImageDataPointer();
- long nPtStart = a_pImageProcessParam->GetParticleGray().GetStart();
- long nPtEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
- if (nNumParticle == 0)
- {
- COTSParticleList listParticleEmpty;
- listParticleEmpty.clear();
- m_pFieldData->SetParticleList(listParticleEmpty);
-
- //LogInfoTrace(__FILE__, __LINE__, _T("RemoveBSEImageBG: no particle is found."));
- }
- else
- {
- // get the area image
-
- Mat cvcopyImg = Mat(nHeightImg, nWidthImg, CV_8UC1, pPixel);
- Mat blurImg;
- medianBlur(cvcopyImg, blurImg, 5);//smooth the edge
- vector<vector<Point>>contours;
-
- findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
- COTSParticleList listParticleOut;
- for (size_t i = 0; i < contours.size(); i++)
- {
-
- Rect rectMax = boundingRect(contours[i]);
- Mat rectROI = cvcopyImg(rectMax).clone();
- //exclude the point which intersect into this bounding box but is not in this contour.
- for (int nX = 0; nX < rectROI.rows; nX++)
- {
- for (int nY = 0; nY < rectROI.cols; nY++)
- {
- double localPos = pointPolygonTest(contours[i], Point2f(nX + rectMax.x, nY + rectMax.y), false);
- if (localPos == -1)
- {
- rectROI.data[nX, nY] = 0;//set the value to 0,so we won't consider it when we find segment and feature in this ROI.
- }
- }
- }
- GetParticles(rectMax.x, rectMax.y, rectMax.width, rectMax.height, rectROI.data, listParticleOut);
-
- }
- // form a image only have particles on
- COTSSegmentsList listImage;
- for (auto pParticle : listParticleOut)
- {
- COTSFeaturePtr pFeature = pParticle->GetFeature();
- COTSSegmentsList listSegment = pFeature->GetSegmentsList();
- long nPixelNum = 0;
- long nPixelAll = 0;
- int nStartS = 0;
- int nHeightS = 0;
- int nLengthS = 0;
- for (auto pSegment : listSegment)
- {
- // update image list
- COTSSegmentPtr pSegNew = COTSSegmentPtr(new COTSSegment(*pSegment.get()));
- listImage.push_back(pSegNew);
- // get particle average gray
- nStartS = pSegment->GetStart();
- nHeightS = pSegment->GetHeight();
- nLengthS = pSegment->GetLength();
- nPixelNum += (long)nLengthS;
- if (nHeightS > nHeightImg)
- {
- //LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
- return FALSE;
- }
- if ((nStartS + nLengthS - 1) > nWidthImg)
- {
- //LogErrorTrace(__FILE__, __LINE__, _T("seg starst and length is wrong."));
- return FALSE;
- }
- for (unsigned int i = 0; i < nLengthS; i++)
- {
- if ((nStartS + i) > nWidthImg)
- {
- //LogErrorTrace(__FILE__, __LINE__, _T("seg start is wrong."));
- return FALSE;
- }
- else if (nHeightS > nHeightImg)
- {
- //LogErrorTrace(__FILE__, __LINE__, _T("seg height is wrong."));
- return FALSE;
- }
- long nValueTemp = (long)*(pSrcImg + nHeightS * nWidthImg + nStartS + i);
- nPixelAll += nValueTemp;
- }
- }
- BYTE nAveGray = (BYTE)(nPixelAll / nPixelNum);
- pParticle->SetAveGray(nAveGray);
- pParticle->SetArea(nPixelNum);
- }
- m_pFieldData->SetParticleList(listParticleOut);
- }
- delete[]pTempImg;
- return TRUE;
- }
- CIntRangePtr COTSImageProcess::CalBackground(CBSEImgPtr m_pBSEImg)
- {
- CIntRangePtr pBackground = CIntRangePtr(new CIntRange());
- WORD nBSEChart[MAXBYTE];
- //1. get chart data
- m_pBSEImg->SetChartData();
- linearSmooth5(m_pBSEImg->GetBSEChart(), nBSEChart,MAXBYTE);
- //2. get down edge
- int nLengthEdge = MAXBYTE + 2;
- WORD n_aBSEChart[MAXBYTE + 2];
- memset(n_aBSEChart, 0, sizeof(WORD) * nLengthEdge);
- std::map<long, std::vector <int>> upEdgeSeries;
- std::map<long, std::vector<int>> downEdgeSeries;
- std::vector<int> currentUpSeries;
- std::vector<int> currentDownSeries;
- // make sure the wave begin with up edge and end with down edge
- n_aBSEChart[0] = 0;
- n_aBSEChart[nLengthEdge - 1] = 0;
- memcpy(&n_aBSEChart[1], &nBSEChart, sizeof(WORD) * MAXBYTE);
- int nLengthCom = MAXBYTE + 1;
- // up edge
- for (int i = 0; i < nLengthCom; i++)
- {
- if (n_aBSEChart[i] <= n_aBSEChart[i + 1])
- {
- if (currentDownSeries.size() > 0)
- {
- int seriesSize = currentDownSeries.size();
- long area = 0;
- for (int i = 0; i < seriesSize; i++)
- {
- area = area + n_aBSEChart[currentDownSeries[i]];
- }
- downEdgeSeries[area] = currentDownSeries;
- currentDownSeries.clear();
- }
- currentUpSeries.push_back(i);
- }
- else
- {
- if (currentUpSeries.size() > 0)
- {
- int seriesSize = currentUpSeries.size();
- long area = 0;
- for (int i = 0; i < seriesSize; i++)
- {
- area = area + n_aBSEChart[currentUpSeries[i]];
- }
- upEdgeSeries[area] = currentUpSeries;
- currentUpSeries.clear();
- }
- currentDownSeries.push_back(i);
- }
- }
- CIntRangePtr pRangeFirst = CIntRangePtr(new CIntRange());
- if (upEdgeSeries.size() > 0)
- {
- pRangeFirst->SetStart((upEdgeSeries.rbegin())->second[0]);
- }
- if (downEdgeSeries.size() > 0)
- {
- auto s = downEdgeSeries.rbegin()->second;
- pRangeFirst->SetEnd((long)(s[s.size() - 1]));
- }
-
- return pRangeFirst;
- }
- void COTSImageProcess::RemoveBackGround(CBSEImgPtr a_pImgIn, COTSImageProcessParamPtr a_pImageProcessParam, CBSEImgPtr a_pBinImgOut,long& foundedPixelNum)
- {
- // the background pixel will be 0,and the other part will be 255.
- ASSERT(a_pImgIn);
-
- ASSERT(a_pImageProcessParam);
- int nWidthImg = a_pImgIn->GetWidth();
- int nHeightImg = a_pImgIn->GetHeight();
- long nImgSize = nWidthImg * nHeightImg;
- BYTE* pTempImg = new BYTE[nImgSize];
- BYTE* pSrcImg = a_pImgIn->GetImageDataPointer();
-
- BYTE* pPixel = new byte[nImgSize];
-
-
- long nBGStart;
- long nBGEnd;
- long nPartStart;
- long nPartEnd;
- long nNumParticle = 0;
- if (a_pImageProcessParam->GetBGRemoveType() == OTS_BGREMOVE_TYPE::MANUAL)
- {
- nBGStart = a_pImageProcessParam->GetBGGray().GetStart();
- nBGEnd = a_pImageProcessParam->GetBGGray().GetEnd();
- nPartStart = a_pImageProcessParam->GetParticleGray().GetStart();
- nPartEnd = a_pImageProcessParam->GetParticleGray().GetEnd();
- // delete background
- for (unsigned int i = 0; i < nImgSize; i++)
- {
- if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
- {
- pPixel[i] = 0;
- }
- else
- {
- pPixel[i] = 255;
- nNumParticle++;
- }
- if (pSrcImg[i]<nPartStart || pSrcImg[i]>nPartEnd)
- {
- pPixel[i] = 0;
- }
- }
- COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
- COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
- }
- else
- {
-
- auto range = CalBackground(a_pImgIn);
- nBGStart = range->GetStart();
- nBGEnd = range->GetEnd();
- switch (a_pImageProcessParam->GetAutoBGRemoveType())
- {
- case OTS_AUTOBGREMOVE_TYPE::DOWNWARD:
- for (unsigned int i = 0; i < nImgSize; i++)
- {
- if (pSrcImg[i] <= nBGEnd)
- {
- pPixel[i] = 0;
- }
- else
- {
- pPixel[i] = 255;
- nNumParticle++;
- }
- }
- break;
- case OTS_AUTOBGREMOVE_TYPE::UPWARD:
- for (unsigned int i = 0; i < nImgSize; i++)
- {
- if (pSrcImg[i] >= nBGStart)
- {
- pPixel[i] = 0;
- }
- else
- {
- pPixel[i] = 255;
- nNumParticle++;
- }
- }
- break;
- case OTS_AUTOBGREMOVE_TYPE::MIDDLE:
- for (unsigned int i = 0; i < nImgSize; i++)
- {
- if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
- {
- pPixel[i] = 0;
- }
- else
- {
- pPixel[i] = 255;
- nNumParticle++;
- }
- }
- break;
- default:
- break;
- }
- COTSImageProcess::BErode3(pPixel, pTempImg, 5, nHeightImg, nWidthImg);
- COTSImageProcess::BDilate3(pTempImg, pPixel, 5, nHeightImg, nWidthImg);
- }
- a_pBinImgOut->SetImageData(pPixel,nWidthImg,nHeightImg);
-
- foundedPixelNum = nNumParticle;
- delete[] pTempImg;
- //delete[] pTempImg2;
- return ;
- }
- BOOL COTSImageProcess::GetParticles(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSParticleList& a_listParticles)
- {
- ASSERT(a_pPixel);
- if (!a_pPixel)
- {
- return FALSE;
- }
- //a_listParticles.clear();
- COTSParticleList findedParts;
- COTSSegmentsList listSegment;
- listSegment.clear();
- //1. get segment line by line
- if (!GetSegmentList(left, top, a_nWidth, a_nHeight, a_pPixel, listSegment))
- {
- return FALSE;
- }
- if ((int)listSegment.size() == 0)
- {
- return FALSE;
- }
- //2. save the temp feature
- COTSFeatureList listFeature;
- listFeature.clear();
- if (!GetFeatureList(listSegment, listFeature))//get every feature for all the particle,the complete feature.
- {
- return FALSE;
- }
- if ((int)listFeature.size() == 0)
- {
- return FALSE;
- }
- COTSParticleList listParticles;
- listParticles.clear();
- if (!ChangeFeaturelist(listFeature, findedParts))
- {
- return FALSE;
- }
- for (auto f : findedParts)
- {
- a_listParticles.push_back(f);
- }
- return TRUE;
- }
- BOOL COTSImageProcess::GetSegmentList(long left, long top, long a_nWidth, long a_nHeight, const BYTE* a_pPixel, COTSSegmentsList& a_listSegments)
- {
- ASSERT(a_pPixel);
- if (!a_pPixel)
- {
- //LogErrorTrace(__FILE__, __LINE__, _T("GetSegments: there is no image data"));
- return FALSE;
- }
- long nImgSize = a_nWidth * a_nHeight;
- a_listSegments.clear();
- //1. get segment line by line
- long nLine, nm, nn;
- long nStart = 0, nLength = 0;
- for (nLine = 0; nLine < a_nHeight; nLine++)
- {
- for (nm = 0; nm < a_nWidth; nm += (nLength + 1))
- {
- nLength = 0;
- // get start
- if (*(a_pPixel + nLine * a_nWidth + nm) != 0)
- {
- nStart = nm;
- nLength++;
- //get length
- for (nn = nm + 1; nn < a_nWidth; nn++)
- {
- // check if segment is over, break
- if (nLength != 0)
- {
- if (*(a_pPixel + nLine * a_nWidth + nn) == 0)
- break;
- }
- if (*(a_pPixel + nLine * a_nWidth + nn) != 0)
- {
- nLength++;
- }
- }
- // generate segment
- COTSSegmentPtr pSegment = COTSSegmentPtr(new COTSSegment(nLine + top, nStart + left, nLength));
- a_listSegments.push_back(pSegment);
- }
- else
- {
- continue;
- }
- }
- }
- if ((int)a_listSegments.size() == 0)
- {
- //LogErrorTrace(__FILE__, __LINE__, _T("no particle is found."));
- return FALSE;
- }
- return TRUE;
- }
- BOOL COTSImageProcess::GetFeatureList(COTSSegmentsList& a_listSegments, COTSFeatureList& a_listFeatures)
- {
- COTSSegmentsList listSegmentNew;
- std::map<long, COTSSegmentsList > mapOneLineSegments;
- for each (auto s in a_listSegments)
- {
- mapOneLineSegments[s->GetHeight()].push_back(s);//sorting all the segments base on the line number.
- }
- std::map<long, COTSSegmentsList >::iterator lineItr = mapOneLineSegments.begin();//find the highest line
- while (lineItr != mapOneLineSegments.end())
- {
- for (auto s = lineItr->second.begin(); s < lineItr->second.end(); )//find one segment of this line.
- {
- COTSSegmentPtr bottomSeg = *s;
- listSegmentNew.clear();
- listSegmentNew.push_back(*s);
- s = lineItr->second.erase(s);
- std::map<long, COTSSegmentsList >::iterator tempItr = lineItr;
- tempItr++;
- for (; tempItr != mapOneLineSegments.end(); tempItr++)//find all other lines of segments
- {
- if (tempItr->first - bottomSeg->GetHeight() > 1)
- {
- break;
- }
- for (auto nextLineSegment = tempItr->second.begin(); nextLineSegment < tempItr->second.end();)//find next line's all segments
- {
- if (((*nextLineSegment)->GetStart() - (bottomSeg->GetStart() + bottomSeg->GetLength())) > 1)
- {
- break;
- }
- if (bottomSeg->UpDownConection(**nextLineSegment))
- {
- listSegmentNew.push_back(*nextLineSegment);
- bottomSeg = *nextLineSegment;
- nextLineSegment = tempItr->second.erase(nextLineSegment);
- break;
- }
- if (tempItr->second.size() > 0)
- {
- nextLineSegment++;
- }
- else
- {
- break;
- }
- }
- }
- COTSFeaturePtr pFeature = COTSFeaturePtr(new COTSFeature());
- pFeature->SetSegmentsList(listSegmentNew);
- //check if this new feature is connected with other found feature.
- COTSSegmentPtr topSeg = listSegmentNew[0];//find the toppest segment of this new feature.
- COTSSegmentPtr bottomSegment = listSegmentNew[listSegmentNew.size() - 1];//find the lowest segment of this new feature.
- bool haveMerged = false;
- for each (auto f in a_listFeatures)
- {
- for (auto seg : f->GetSegmentsList())
- {
- if (bottomSegment->UpDownConection(*seg) || topSeg->UpDownConection(*seg))
- {
- COTSSegmentsList segs = f->GetSegmentsList();
- for (auto s : listSegmentNew)
- {
- segs.push_back(s);
- }
- f->SetSegmentsList(segs);
- haveMerged = true;
- break;
- }
- }
- if (haveMerged)
- {
- break;
- }
- }
- if (!haveMerged)
- {
- a_listFeatures.push_back(pFeature);
- }
- if (lineItr->second.size() == 0)
- {
- break;
- }
- }
- lineItr++;
- }
- return true;
- }
- BOOL COTSImageProcess::ChangeFeaturelist(COTSFeatureList& a_listFeatures, COTSParticleList& a_listParticle)
- {
- if (a_listFeatures.size() == 0)
- {
- return FALSE;
- }
- // compute Rect
- for (auto pFeature : a_listFeatures)
- {
- COTSParticlePtr pParticle = COTSParticlePtr(new COTSParticle());
- pParticle->SetFeature(pFeature);
- if (!pParticle->CalCoverRect())
- {
- return FALSE;
- }
- a_listParticle.push_back(pParticle);
- }
- if ((int)a_listParticle.size() == 0)
- {
- return FALSE;
- }
- return TRUE;
- }
- BOOL COTSImageProcess::CalcuParticleImagePropertes(COTSParticlePtr a_pOTSPart, double a_PixelSize)
- {
- //--------- convert this particle data to image data,construct an image only with this particle.------
- const int nExpand_Size = 3;
- const int nWhiteColor = 0;
- const int nThickness = 1;
- // lineType Type of the line
- const int nLineType = 8;
- // get rectangle of the particle
- CRect rect = a_pOTSPart->GetParticleRect();
- if (a_pOTSPart->GetArea() < 30 * a_PixelSize)// the particle is too small that openCV can't calculate a width value of it. Then we take the upright rect of the particle as it's minArea rect.
- {
- double w = 0, h = 0;
- w = (double)rect.Width()*a_PixelSize;
- h = (double)rect.Height()*a_PixelSize;
- a_pOTSPart->SetDMax(MAX(w, h));
- a_pOTSPart->SetDMin(MIN(w, h));
- a_pOTSPart->SetDMean((w + h) / 2);
- a_pOTSPart->SetFeretDiameter((w + h) / 2);
- a_pOTSPart->SetDElong(MAX(w, h));
- a_pOTSPart->SetPerimeter((w+h)*2);
- a_pOTSPart->SetDPerp(MIN(w, h));
- a_pOTSPart->SetDInscr(MIN(w, h));
- return true;
- }
- // calculate the particle image data size, expand 3 pixel at the edge
- Mat particleImage = Mat::zeros(rect.Height() + nExpand_Size , rect.Width() + nExpand_Size , CV_8U);
- // get the segment list
- COTSSegmentsList listSegment = a_pOTSPart->GetFeature()->GetSegmentsList();
- for (auto pSegment : listSegment)
- {
- int nStart = pSegment->GetStart() - rect.left + nExpand_Size;
- int nEnd = pSegment->GetStart() + pSegment->GetLength() - rect.left - 1 + nExpand_Size;
- int nHeight = pSegment->GetHeight() - rect.top + nExpand_Size;
- line(particleImage, Point(nStart, nHeight), Point(nEnd, nHeight), Scalar(nBlackColor), nThickness, nLineType);
- }
- //--------abstract the contour of the particle.
- Mat cvcopyImg;
- medianBlur(particleImage, cvcopyImg, 5);//smooth the edge
-
- vector<vector<Point>>contours;
-
- findContours(cvcopyImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
- if (contours.size()==0)// the particle is too odd that openCV can't find a contour of it. Then we take the upright rect of the particle as it's minArea rect.
- {
- double w = 0, h = 0;
- w = (double)rect.Width()*a_PixelSize;
- h = (double)rect.Height()*a_PixelSize;
- a_pOTSPart->SetDMax(MAX(w, h));
- a_pOTSPart->SetDMin(MIN(w, h));
- a_pOTSPart->SetDMean((w + h) / 2);
- a_pOTSPart->SetFeretDiameter((w + h) / 2);
- a_pOTSPart->SetDElong(MAX(w, h));
- a_pOTSPart->SetPerimeter((w + h) * 2);
- a_pOTSPart->SetDPerp(MIN(w, h));
- a_pOTSPart->SetDInscr(MIN(w, h));
- return true;
- }
- int imaxcontour = 0, imax = 0;
- for (unsigned int i = 0; i < contours.size(); i++) {
- int itmp = contourArea(contours[i]);
- if (imaxcontour < itmp) {
- imax = i;
- imaxcontour = itmp;
- }
- }
- vector<Point > listEdge = contours[imax];
-
- vector<vector<Point>>Outcontours;
- Outcontours.push_back(listEdge);
-
- //---------calculate the minimium rectangle
- auto rRect = cv::minAreaRect(listEdge);
- Point2f p[4];
- rRect.points(p);
- int D_MIN =getDistance(p[0], p[1]);
- int D_MinRecLen = 0;//minareaRect's length(the lenger side).
- for (int j = 0; j <= 2; j++)
- {
- //line(cvContourImg, p[j], p[(j + 1) % 4], Scalar(100, 100, 0), 2);
- int d = getDistance(p[j], p[j + 1]);
- if (d < D_MIN)
- {
- D_MIN = d;
- }
- if (d > D_MinRecLen)
- {
- D_MinRecLen = d;
- }
- }
- a_pOTSPart->SetDMin(D_MIN*a_PixelSize);
- a_pOTSPart->SetOrientation(rRect.angle);
- //----------calculate the perimeter
- double d = arcLength(listEdge, true);
- a_pOTSPart->SetPerimeter(d*a_PixelSize);
- //-----------calculate the Max Diameter. Find the min enclosing circle first ,then find the two farthest circle connected point.
- Point2f center; float radius;
- minEnclosingCircle(listEdge, center, radius);
- //circle(cvContourImg, center, radius, Scalar(100), 2);
- std::vector <Point> outContour = listEdge;
-
- std::vector <Point> rst;
- for (unsigned int k = 0; k < outContour.size(); k++)
- {
- Point p = outContour[k];
- double d = sqrt(pow((p.x - center.x), 2) + pow((p.y - center.y), 2));
- if (fabs(d - radius) < 0.01)
- {
- rst.push_back(p);
- }
- }
- double D_MAX = 0;
- Point lineDmax[2];
- for (unsigned int m = 0; m < rst.size(); m++)
- {
- Point p = rst[m];
- for (unsigned int n = m + 1; n < rst.size(); n++)
- {
- Point p1 = rst[n];
- double d = sqrt(powf((p.x - p1.x), 2) + powf((p.y - p1.y), 2));
- if (d > D_MAX)
- {
- D_MAX = d;
- lineDmax[0] = p;
- lineDmax[1] = p1;
- }
- }
- }
- a_pOTSPart->SetDMax(D_MAX*a_PixelSize);
-
- //--------calculate the D_PERP property using the D_MAX's two endpoints.
- std::vector<Point> curve1;
- std::vector<Point> curve2;
- for (unsigned int i = 0; i < outContour.size(); i++)
- {
- Point pt = outContour[i];
- bool start = false;
- int clockwise = Side(lineDmax[0], lineDmax[1], pt);// devide these points into two group ,separate into the two sides.
- if (clockwise > 0)
- {
- curve1.push_back(pt);
- }
- else
- {
- curve2.push_back(pt);
- }
- }
- double d_perp1 = 0, d_perp2 = 0;
- for (unsigned int i = 0; i < curve1.size(); i++)
- {
- double d = getDist_P2L(curve1[i], lineDmax[0], lineDmax[1]);
- if (d > d_perp1)
- {
- d_perp1 = d;
- }
- }
- for (unsigned int i = 0; i < curve2.size(); i++)
- {
- double d = getDist_P2L(curve2[i], lineDmax[0], lineDmax[1]);
- if (d > d_perp2)
- {
- d_perp2 = d;
- }
- }
- a_pOTSPart->SetDPerp((d_perp1 + d_perp2)*a_PixelSize);
- //----------find the diameter of max inscribed circle
- int r;
- Point inscribeCirclecenter;
- FindInnerCircleInContour(outContour, inscribeCirclecenter, r);
- //circle(cvContourImg, inscribeCirclecenter, r, Scalar(200));
- a_pOTSPart->SetDInscr(r * 2 * a_PixelSize);
- //---------------calculate the image other caracater: length/width realArea/minRectangeArea etc. we can use these propertes to do forward process.
- double minRectArea = D_MIN * D_MinRecLen*a_PixelSize*a_PixelSize;//最小外接矩形面积
- double fillRatio = a_pOTSPart->GetArea() / minRectArea;//实际面积与最小外接矩形面积比,that's the fill rate.
- double lengthWidthRatio;
- lengthWidthRatio = (double)D_MinRecLen / D_MIN;//长宽比
-
- //decide if this shape is a strip shape :if the lenthWidthRatio>2 then it is. if the lengthWidthRatio<2 and the areaRatio<0.5 then it is.
- bool isStripShape = false;
- double curveLength = 0;
- double D_MEAN=0;
- Moments mu;
- mu = moments(listEdge, false);
- int nx = mu.m10 / mu.m00;
- int ny = mu.m01 / mu.m00;
- //circle(cvcopyImg, Point(nx, ny), 1, (255), 1);
- Point ptCenter = Point((int)nx, (int)ny);
- if (pointPolygonTest(outContour, ptCenter, false) != 1)// the center point doesn't contain in the contour, we think it as curve shape.
- {
- isStripShape = true;
- }
- /*if (lengthWidthRatio >= 2 )// in PartA software this is true,but IncA because of the GB definition the everage feret diameter is always the mean value of all the chord.
- {
-
- isStripShape = true;
- }*/
-
- if (fillRatio <= 0.4)// only when the fill rate is very low,we think it as a curve shape,then we choose the mean width as the feret diameter.
- {
- isStripShape = true;
- }
-
- if (isStripShape)
- {
- curveLength = a_pOTSPart->GetPerimeter()/2 - a_pOTSPart->GetDInscr()/2;// thinking this particle as a strip rectangle.the width is the max inscribe circle diameter/2.
- if (curveLength < D_MAX)
- {
- curveLength = D_MAX;
- }
- if (curveLength < MIN_DOUBLE_VALUE || a_pOTSPart->GetArea()<MIN_DOUBLE_VALUE)
- {
- D_MEAN = 0;
- }
- else
- {
- D_MEAN = a_pOTSPart->GetArea() / curveLength;
- }
-
- a_pOTSPart->SetDMean(D_MEAN*a_PixelSize);
- a_pOTSPart->SetFeretDiameter(D_MEAN*a_PixelSize);
- a_pOTSPart->SetDElong (curveLength*a_PixelSize);
- }
- else//it's a ball shape particle
- {
- curveLength = D_MAX;
- double ftd = 0, maxD = 0, minD = 0, dratio = 0;
- GetParticleAverageChord(outContour, a_PixelSize, ftd);
-
- a_pOTSPart->SetDMean(ftd);
- a_pOTSPart->SetFeretDiameter(ftd);
- a_pOTSPart->SetDElong(curveLength*a_PixelSize);
- }
- return true;
-
- }
- BOOL COTSImageProcess::MergeBigBoundaryParticles(COTSFieldDataList allFields,double pixelSize,int scanFieldSize, CSize ResolutionSize, COTSParticleList& mergedParts)
- {
- COTSSegmentsList boarderSegs;
- auto FldMgr = new CFieldMgr(scanFieldSize, ResolutionSize);
-
- std::map<COTSParticle*, COTSParticleList> mapMergeParticles;//hold up all the boundary connected particles. the pair's first is also the member of these particles.
- std::map<COTSParticle*, COTSSegmentsList> mapMergedSegments;//hold up all the segment's corresponding clone in the connected particles.
- for (auto centerfld : allFields)
- {
- // find neighbor field on the left.
- auto leftFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::LEFT);
- if (leftFld != nullptr)
- {
- auto leftParts = centerfld->GetLeftBorderedBigParticles();
- auto rightParts = leftFld->GetRightBorderedBigParticles();
- for (auto leftp : leftParts)
- {
- for (auto rightp : rightParts)
- {
- if (leftp->IsConnected(rightp.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::LEFT))
- {
- if (leftp->headerParticle != NULL)
- {
- if (rightp->headerParticle == NULL)
- {
- rightp->headerParticle = leftp->headerParticle;
- mapMergeParticles[leftp->headerParticle].push_back(rightp);
- }
- }
- else
- {
- if (rightp->headerParticle != NULL)
- {
- leftp->headerParticle = rightp.get();
- mapMergeParticles[rightp.get()].push_back(leftp);
- }
- else
- {
- leftp->headerParticle = leftp.get();
- rightp->headerParticle = leftp.get();
- mapMergeParticles[leftp.get()].push_back(rightp);
- }
- }
- }
- }
- }
- }
- //find neighbor field on the upward
- auto upFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::UP);
- if (upFld != nullptr)
- {
- auto upParts = centerfld->GetTopBorderedBigParticles();
- auto downParts = upFld->GetBottomBorderedBigParticles();
- for (auto upprt : upParts)
- {
- for (auto downprt : downParts)
- {
- if (upprt->IsConnected(downprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::UP))
- {
- if (upprt->headerParticle != NULL)
- {
- if (downprt->headerParticle == NULL)
- {
- downprt->headerParticle = upprt->headerParticle;
- mapMergeParticles[upprt->headerParticle].push_back(downprt);
- }
- }
- else
- {
- if (downprt->headerParticle != NULL)
- {
- upprt->headerParticle = downprt->headerParticle;
- mapMergeParticles[downprt.get()].push_back(upprt);
- }
- else
- {
- upprt->headerParticle = upprt.get();
- downprt->headerParticle = upprt.get();
- mapMergeParticles[upprt.get()].push_back(downprt);
- }
- }
- }
- }
- }
- }
- //find neighbor field on the downward.
- auto downFld = FldMgr->FindNeighborField(allFields, centerfld,SORTING_DIRECTION::DOWN);
- if (downFld != nullptr)
- {
- auto downParts = centerfld->GetBottomBorderedBigParticles();
- auto upParts = downFld->GetTopBorderedBigParticles();
- for (auto downprt : downParts)
- {
- for (auto upprt : upParts)
- {
- if (downprt->IsConnected(upprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::DOWN))
- {
- if (downprt->headerParticle != NULL)
- {
- if (upprt->headerParticle == NULL)
- {
- upprt->headerParticle = downprt->headerParticle;
- mapMergeParticles[downprt->headerParticle].push_back(upprt);
- }
- }
- else
- {
- if (upprt->headerParticle != NULL)
- {
- downprt->headerParticle = upprt->headerParticle;
- mapMergeParticles[upprt->headerParticle].push_back(downprt);
- }
- else
- {
- downprt->headerParticle = downprt.get();
- upprt->headerParticle = downprt.get();
- mapMergeParticles[downprt.get()].push_back(upprt);
- }
- }
- }
- }
- }
- }
- //find neighbor field on the right.
- auto rightFld = FldMgr->FindNeighborField(allFields, centerfld, SORTING_DIRECTION::RIGHT);
- if (rightFld != nullptr)
- {
- auto rightParts = centerfld->GetRightBorderedBigParticles();
- auto leftParts = rightFld->GetLeftBorderedBigParticles();
- for (auto rightprt : rightParts)
- {
- for (auto leftprt : leftParts)
- {
- if (rightprt->IsConnected(leftprt.get(), centerfld->Width, centerfld->Height, (int)SORTING_DIRECTION::RIGHT))
- {
- if (rightprt->headerParticle != NULL)
- {
- if (leftprt->headerParticle == NULL)
- {
- leftprt->headerParticle = rightprt->headerParticle;
- mapMergeParticles[rightprt->headerParticle].push_back(leftprt);
- }
- }
- else
- {
- if (leftprt->headerParticle != NULL)
- {
- rightprt->headerParticle = leftprt->headerParticle;
- mapMergeParticles[leftprt->headerParticle].push_back(rightprt);
- }
- else
- {
- rightprt->headerParticle = rightprt.get();
- leftprt->headerParticle = rightprt.get();
- mapMergeParticles[rightprt.get()].push_back(leftprt);
- }
- }
- }
- }
- }
- }
- }
- for (auto pair : mapMergeParticles)
- {
- struct EleAreaPercentage
- {
- EleAreaPercentage(double p, CElementChemistryPtr e)
- {
- areaPercentage = p;
- eleData = e;
- }
- double areaPercentage;
- CElementChemistryPtr eleData;
- };
- auto newPart = COTSParticlePtr(new COTSParticle());
- COTSSegmentsList newSegs;
- auto p = pair.first;
- newPart->SetAbsolutePos(p->GetAbsolutPos());
-
- //firstly,we sum up all the merged particles's area and get the represent string.
- std::string partsStr = std::to_string(p->GetFieldId()) + ":" + std::to_string(p->GetAnalysisId());
- double allPartArea = p->GetArea();//Get the first particle's area.
- for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
- {
- partsStr += "," + std::to_string(other->GetFieldId()) + ":" + std::to_string(other->GetAnalysisId());//Get the subparticles string such as "1:1,2:1" etc.
- allPartArea += other->GetArea();//Get other particle's area
- }
- // calculate all the new segment's position.
- std::vector <COTSParticle*> allSubParts;
- allSubParts.push_back(p);
- for (auto other : pair.second)// Get the total area of all these particles for the use of ele calcu.
- {
- allSubParts.push_back(other.get());
- }
- for (auto subp : allSubParts)
- {
- int fid = subp->GetFieldId();
- CPoint myFldPos;
- for (auto f : allFields)//find this particle's filed.
- {
- if (f->GetId() == fid)
- {
- myFldPos = f->GetPosition();
- }
- }
- int fldWidth = allFields[0]->Width;
- int fldHeight = allFields[0]->Height;
- CPoint fldLeftUpPos = CPoint(myFldPos.x + fldWidth / 2 , myFldPos.y + fldHeight / 2 );
- for (auto s : subp->GetFeature()->GetSegmentsList())
- {
- COTSSegmentPtr newseg = COTSSegmentPtr(new COTSSegment());
- newseg->SetStart(s->GetStart() + fldLeftUpPos.x);
- newseg->SetHeight((0 - s->GetHeight()) + fldLeftUpPos.y);//the coordinate system of segment in a field is different with the OTS coordinate system.OTS system's y axis is upward positive ,yet the field is downward positive.
- newseg->SetLength(s->GetLength());
- newSegs.push_back(newseg);
- }
- }
- COTSFeaturePtr newFeature = COTSFeaturePtr(new COTSFeature());
- newFeature->SetSegmentsList(newSegs);
- newPart->SetFeature(newFeature);
- newPart->CalCoverRect();
- //second, we get all the element data and their area percentage .
- std::map<std::string, std::vector<EleAreaPercentage>> mapEleData;
- CPosXrayPtr pXray1 = p->GetXrayInfo();
- for (auto ele : pXray1->GetElementQuantifyData())
- {
- mapEleData[ele->GetName().GetBuffer()].push_back(EleAreaPercentage(p->GetArea() / allPartArea, ele));
- }
- for (auto other : pair.second)
- {
- auto otherXray = other->GetXrayInfo();
- for (auto eledata : otherXray->GetElementQuantifyData())
- {
- mapEleData[eledata->GetName().GetBuffer()].push_back(EleAreaPercentage(other->GetArea() / allPartArea, eledata));
- }
- }
- // third,we calculate all the element's new percentage data and get a new element chemistry list.
- CElementChemistriesList newCheList;
- for (auto eledata : mapEleData)
- {
- CElementChemistryPtr newEleche = CElementChemistryPtr(new CElementChemistry());
- newEleche->SetName(CString(eledata.first.c_str()));
- double newPercentage = 0;
- for (auto d : eledata.second)
- {
- newPercentage += d.areaPercentage * d.eleData->GetPercentage();
- }
- newEleche->SetPercentage(newPercentage);
- newCheList.push_back(newEleche);
- }
- CPosXrayPtr xray(new CPosXray());
- xray->SetElementQuantifyData(newCheList);
- newPart->SetXrayInfo(xray);
- newPart->SetSubParticles(partsStr);
- newPart->SetArea(allPartArea);
- newPart->SetTagId(mergedParts.size());
- newPart->SetAnalysisId(mergedParts.size());
- std::string name = p->TypeName();
- newPart->TypeName(name);
- newPart->TypeColor(p->TypeColor());
- mergedParts.push_back(newPart);
- }
- return true;
- }
-
-
-
- }
|