gtcvcore.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. //
  2. // Created by lloyd on 2020/9/28.
  3. //
  4. #include <iostream>
  5. #include <vector>
  6. #include "opencv2/opencv.hpp"
  7. #include "opencv2/highgui/highgui.hpp"
  8. #include "opencv2/imgproc/imgproc.hpp"
  9. #include "gtrunconf.h"
  10. #include "gtimgcreator.h"
  11. #include "gtcommonkits.h"
  12. #include "gtcvcore.h"
  13. #include "DataMapping/dgns_gtstandard.h"
  14. #include "DataMapping/dgns_gtbaseparam.h"
  15. /*
  16. ***执行预测
  17. @STR 功图标准化数据
  18. @FuncType 归一法1图像输入1 ,归一法2图像输入2
  19. @load 1 指定必须重新加载 其它默认
  20. @CacheName GUID生成的一个PNGname
  21. */
  22. std::string
  23. gtcvcore::HOGSVMPredict(std::vector<std::vector<int>> &gt1, std::vector<std::vector<int>> &gt2, int FuncType, int load,
  24. const char *CacheName = NULL) {
  25. //std::cout<<"个数:" << gtArray.size() << std::endl;
  26. int ImgHeight = gtrunconf_rows;
  27. int ImgWidht = gtrunconf_cols;
  28. cv::Mat mat1(ImgHeight, ImgWidht, CV_8UC3);
  29. cv::Mat mat2(ImgHeight, ImgWidht, CV_8UC3);
  30. //mat生成器
  31. gtimgcreator cvmc;
  32. cvmc.creatGTMat(mat1, mat2, ImgHeight, ImgWidht, gt1, gt2, gt1.size(), 1);
  33. //std::string PNGName = MyToolsKit::convertTCHAR_string(CacheName);
  34. //std::cout << "文件名:" << PNGName << std::endl;
  35. //cvmc.saveGTPNG(mat1, path1);
  36. //cvmc.saveGTPNG(mat2, path2);
  37. //mat1 = cv::imread(path1);
  38. //mat2 = cv::imread(path2);
  39. cv::imshow("标准化功图", mat1);
  40. //cv::imshow("输入的法2功图", mat2);
  41. //cv::waitKey();
  42. cv::waitKey(gtrunconf_interval_delaytime);
  43. //cv::resize(src, trainImg, cv::Size(ImgWidht, ImgHeight), 0, 0, cv::INTER_CUBIC);
  44. cv::HOGDescriptor hog(cv::Size(ImgWidht, ImgHeight), cv::Size(16, 16), cv::Size(8, 8), cv::Size(8, 8),
  45. 9); //具体意思见参考文章1,2
  46. std::vector<float> descriptors;//结果数组
  47. cv::Mat saveMat;
  48. //hog.compute(trainImg, descriptors, cv::Size(8, 8), cv::Size(0, 0)); //调用计算函数开始计算
  49. if (FuncType == 1) {
  50. hog.compute(mat1, descriptors, cv::Size(8, 8)); //调用计算函数开始计算
  51. saveMat = mat1;
  52. } else {
  53. hog.compute(mat2, descriptors, cv::Size(8, 8)); //调用计算函数开始计算
  54. saveMat = mat2;
  55. }
  56. //creat SVM classfier
  57. //cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
  58. //load train file
  59. initSVM(load);
  60. std::string PredictResult = "Error";
  61. try {
  62. if (FuncType == 1) {
  63. int r = gtcvcore::MySvm1->predict(descriptors); //进行预测
  64. std::vector<std::string> gtTypes = gtcommonkits::str_Split(gtcvcore::GTYypes1, ",");
  65. PredictResult = gtTypes[r - 1];
  66. }
  67. }
  68. catch (std::runtime_error &ex) {
  69. printf("预测时发生错误:%s\n", ex.what());
  70. PredictResult = "Error";
  71. }
  72. if (PredictResult == "Error") {
  73. //std::cout << std::endl << "预测结果:" << PredictResult << std::endl;
  74. } else {
  75. //std::cout << std::endl << "预测结果:" << PredictResult << std::endl;
  76. /*path3 += PredictResult + ".png";
  77. cvmc.saveGTPNG(saveMat, path3);*/
  78. }
  79. return PredictResult;
  80. //char* predictresult_tchar = MyToolsKit::convertstring_char(PredictResult);
  81. //std::string retresult = MyToolsKit::convertTCHAR_string(predictresult_tchar);
  82. //std::cout << std::endl << "返回的预测结果:" << retresult << std::endl;
  83. //return predictresult_tchar;
  84. //cv::waitKey(0);
  85. }
  86. /*
  87. ***svm 加载
  88. @load 1 必须重新加载 其它默认
  89. */
  90. void gtcvcore::initSVM(int load) {
  91. if (load == 1) {
  92. try {
  93. std::string svmstr = gtrunconf_xml_basepath;
  94. std::string txtstr = gtrunconf_txt_basepath;
  95. // MemoryShare::readShareMemory(L"ShareMemory0", svmstr);
  96. //MyToolsKit::saveTxt("D:\\t.xml", svmstr);
  97. //std::cout << svmstr << std::endl;
  98. const cv::String &objname = "xml";
  99. const cv::String &strModel = svmstr;
  100. // MemoryShare::string_replace(svmstr, "MemoryURL:", "");
  101. // std::cout << svmstr << std::endl;
  102. //CVHOGSVMCategorize::MySvm1 = cv::ml::SVM::loadFromString<cv::ml::SVM>(strModel);
  103. if (gtcvcore::MySvm1.empty() == false)
  104. gtcvcore::MySvm1->clear();
  105. gtcvcore::MySvm1 = cv::ml::SVM::load(svmstr);
  106. std::string resultTypes = gtcommonkits::getTextFromTxt(txtstr);
  107. // std::cout << resultTypes << std::endl;
  108. gtcvcore::GTYypes1 = resultTypes;
  109. }
  110. catch (std::runtime_error &ex) {
  111. printf("读取svm1发生错误:%s\n", ex.what());
  112. }
  113. } else {
  114. if (gtcvcore::GTYypes1.size() == 0) {
  115. try {
  116. std::string svmstr = gtrunconf_xml_basepath;
  117. std::string txtstr = gtrunconf_txt_basepath;
  118. //std::cout << svmstr << std::endl;
  119. const cv::String &objname = "xml";
  120. const cv::String &strModel = svmstr;
  121. gtcvcore::MySvm1 = cv::ml::SVM::load(svmstr);
  122. std::string resultTypes = gtcommonkits::getTextFromTxt(txtstr);
  123. // std::cout << resultTypes << std::endl;
  124. gtcvcore::GTYypes1 = resultTypes;
  125. }
  126. catch (std::runtime_error &ex) {
  127. printf("读取svm1发生错误:%s\n", ex.what());
  128. }
  129. }
  130. }
  131. }
  132. cv::Ptr<cv::ml::SVM> gtcvcore::MySvm1 = cv::ml::SVM::create();
  133. std::string gtcvcore::GTYypes1 = "";
  134. /***Not ideal***/
  135. double gtcvcore::C_GTSimillar_PSNR(std::vector<std::vector<int>> &gt1_1, std::vector<std::vector<int>> &gt1_2,
  136. std::vector<std::vector<int>> &gt2_1, std::vector<std::vector<int>> &gt2_2) {
  137. int ImgHeight = gtrunconf_rows;
  138. int ImgWidht = gtrunconf_cols;
  139. cv::Mat I1_1(ImgHeight, ImgWidht, CV_8UC3);
  140. cv::Mat I1_2(ImgHeight, ImgWidht, CV_8UC3);
  141. gtimgcreator cvmc;
  142. cvmc.creatGTMat(I1_1, I1_2, ImgHeight, ImgWidht, gt1_1, gt1_2, gt1_1.size(), 1);
  143. cv::Mat I2_1(ImgHeight, ImgWidht, CV_8UC3);
  144. cv::Mat I2_2(ImgHeight, ImgWidht, CV_8UC3);
  145. cvmc.creatGTMat(I2_1, I2_2, ImgHeight, ImgWidht, gt2_1, gt2_2, gt2_1.size(), 1);
  146. cv::Mat s1;
  147. absdiff(I1_1, I2_1, s1); // |I1 - I2|
  148. s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits
  149. s1 = s1.mul(s1); // |I1 - I2|^2
  150. cv::Scalar s = sum(s1); // sum elements per channel
  151. double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels
  152. if (sse <= 1e-10) // for small values return zero
  153. return 0;
  154. else {
  155. double mse = sse / (double) (I1_1.channels() * I1_1.total());
  156. double psnr = 10.0 * log10((255 * 255) / mse);
  157. return psnr;
  158. }
  159. }
  160. /***Not ideal***/
  161. double gtcvcore::C_GTSimillar_MSSIM(std::vector<std::vector<int>> &gt1_1, std::vector<std::vector<int>> &gt1_2,
  162. std::vector<std::vector<int>> &gt2_1, std::vector<std::vector<int>> &gt2_2) {
  163. const double C1 = 6.5025, C2 = 58.5225;
  164. int ImgHeight = gtrunconf_rows;
  165. int ImgWidht = gtrunconf_cols;
  166. cv::Mat Im1_1(ImgHeight, ImgWidht, CV_8UC3);
  167. cv::Mat Im1_2(ImgHeight, ImgWidht, CV_8UC3);
  168. gtimgcreator cvmc;
  169. cvmc.creatGTMat(Im1_1, Im1_2, ImgHeight, ImgWidht, gt1_1, gt1_2, gt1_1.size(), 1);
  170. cv::Mat Im2_1(ImgHeight, ImgWidht, CV_8UC3);
  171. cv::Mat Im2_2(ImgHeight, ImgWidht, CV_8UC3);
  172. cvmc.creatGTMat(Im2_1, Im2_2, ImgHeight, ImgWidht, gt2_1, gt2_2, gt2_1.size(), 1);
  173. //cv::imshow("1", Im1_1);
  174. cv::imshow("标准工况功图", Im2_1);
  175. /***************************** INITS **********************************/
  176. int d = CV_32F;
  177. cv::Mat I1, I2;
  178. Im1_1.convertTo(I1, d); // cannot calculate on one byte large values
  179. Im2_1.convertTo(I2, d);
  180. cv::Mat I2_2 = I2.mul(I2); // I2^2
  181. cv::Mat I1_2 = I1.mul(I1); // I1^2
  182. cv::Mat I1_I2 = I1.mul(I2); // I1 * I2
  183. /*************************** END INITS **********************************/
  184. int blocksize = 15;//%2==1
  185. if (blocksize % 2 == 0) blocksize += 1;
  186. double _sigmaX = 1.2;//表示高斯核函数在X方向的的标准偏差。
  187. cv::Mat mu1, mu2; // PRELIMINARY COMPUTING
  188. GaussianBlur(I1, mu1, cv::Size(blocksize, blocksize), _sigmaX);//高斯滤波
  189. GaussianBlur(I2, mu2, cv::Size(blocksize, blocksize), _sigmaX);
  190. cv::Mat mu1_2 = mu1.mul(mu1);
  191. cv::Mat mu2_2 = mu2.mul(mu2);
  192. cv::Mat mu1_mu2 = mu1.mul(mu2);
  193. cv::Mat sigma1_2, sigma2_2, sigma12;
  194. GaussianBlur(I1_2, sigma1_2, cv::Size(blocksize, blocksize), _sigmaX);
  195. sigma1_2 -= mu1_2;
  196. GaussianBlur(I2_2, sigma2_2, cv::Size(blocksize, blocksize), _sigmaX);
  197. sigma2_2 -= mu2_2;
  198. GaussianBlur(I1_I2, sigma12, cv::Size(blocksize, blocksize), _sigmaX);
  199. sigma12 -= mu1_mu2;
  200. //FORMULA
  201. cv::Mat t1, t2, t3;
  202. t1 = 2 * mu1_mu2 + C1;
  203. t2 = 2 * sigma12 + C2;
  204. t3 = t1.mul(t2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
  205. t1 = mu1_2 + mu2_2 + C1;
  206. t2 = sigma1_2 + sigma2_2 + C2;
  207. t1 = t1.mul(t2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
  208. cv::Mat ssim_map;
  209. divide(t3, t1, ssim_map); // ssim_map = t3./t1;
  210. cv::Scalar mssim = mean(ssim_map); // mssim = average of ssim map
  211. return (mssim.val[0] + mssim.val[1] + mssim.val[2]) / 3.0;
  212. //return mssim;
  213. }
  214. /***Not ideal***/
  215. double gtcvcore::C_GTSimillar_HOG(std::vector<std::vector<int>> &gt1_1, std::vector<std::vector<int>> &gt1_2,
  216. std::vector<std::vector<int>> &gt2_1, std::vector<std::vector<int>> &gt2_2) {
  217. int ImgHeight = gtrunconf_rows;
  218. int ImgWidht = gtrunconf_cols;
  219. cv::Mat Im1_1(ImgHeight, ImgWidht, CV_8UC3);
  220. cv::Mat Im1_2(ImgHeight, ImgWidht, CV_8UC3);
  221. gtimgcreator cvmc;
  222. cvmc.creatGTMat(Im1_1, Im1_2, ImgHeight, ImgWidht, gt1_1, gt1_2, gt1_1.size(), 1);
  223. cv::Mat Im2_1(ImgHeight, ImgWidht, CV_8UC3);
  224. cv::Mat Im2_2(ImgHeight, ImgWidht, CV_8UC3);
  225. cvmc.creatGTMat(Im2_1, Im2_2, ImgHeight, ImgWidht, gt2_1, gt2_2, gt2_1.size(), 1);
  226. cv::imshow("1", Im1_1);
  227. cv::imshow("2", Im2_1);
  228. /**********************************************start*****************************************************************************/
  229. cv::HOGDescriptor hog(cv::Size(ImgWidht, ImgHeight), cv::Size(16, 16),
  230. cv::Size(8, 8), cv::Size(8, 8), 9);
  231. std::vector<float> descriptors1, descriptors2;
  232. hog.compute(Im1_1, descriptors1, cv::Size(8, 8));
  233. hog.compute(Im2_1, descriptors2, cv::Size(8, 8));
  234. double s = cv::compareHist(descriptors1, descriptors2, 0);
  235. std::cout << "The 1 Fun compare simillar:" << s << std::endl;
  236. s = cv::compareHist(descriptors1, descriptors2, 1);
  237. std::cout << "The 2 Fun compare simillar:" << s << std::endl;
  238. s = cv::compareHist(descriptors1, descriptors2, 2);
  239. std::cout << "The 3 Fun compare simillar:" << s << std::endl;
  240. s = cv::compareHist(descriptors1, descriptors2, 3);
  241. std::cout << "The 4 Fun compare simillar:" << s << std::endl;
  242. //return s;
  243. //计算相似度
  244. if (Im1_1.channels() == 1) {//单通道时,
  245. int histSize = 256;
  246. float range[] = {0, 256};
  247. const float *histRange = {range};
  248. bool uniform = true;
  249. bool accumulate = false;
  250. cv::Mat hist1, hist2;
  251. cv::calcHist(&Im1_1, 1, 0, cv::Mat(), hist1, 1, &histSize, &histRange, uniform, accumulate);
  252. cv::normalize(hist1, hist1, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
  253. cv::calcHist(&Im2_1, 1, 0, cv::Mat(), hist2, 1, &histSize, &histRange, uniform, accumulate);
  254. cv::normalize(hist2, hist2, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
  255. s = cv::compareHist(hist1, hist2,
  256. cv::HISTCMP_CORREL);//,CV_COMP_CHISQR,CV_COMP_INTERSECT,CV_COMP_BHATTACHARYYA CV_COMP_CORREL
  257. std::cout << "similarity = " << s << std::endl;
  258. } else {//三通道时
  259. //图像转换到HSV空间进行比较
  260. cv::cvtColor(Im1_1, Im1_1, cv::COLOR_BGR2HSV);
  261. cv::cvtColor(Im2_1, Im2_1, cv::COLOR_BGR2HSV);
  262. //设定直方图需要相关参数
  263. int h_bins = 50, s_bins = 60;
  264. int histSize[] = {h_bins, s_bins};
  265. float h_ranges[] = {0,
  266. 180};//色调H用角度度量,取值范围为0°~360°,从红色开始按逆时针方向计算,红色为0°,绿色为120°,蓝色为240°。它们的补色是:黄色为60°,青色为180°,品红为300°;
  267. float s_ranges[] = {0, 256};
  268. const float *ranges[] = {h_ranges, s_ranges};
  269. int channels[] = {0, 1};//比较H和S通道直方图,由于函数只能最多比较2维直方图,所以需要进行选择。
  270. //int channels[] = { 1, 2};//比较S和V通道直方图
  271. //int channels[] = {0};//只比较第一个H通道的直方图选择
  272. cv::MatND hist1, hist2;
  273. //进行直方图提取以及归一化计算
  274. cv::calcHist(&Im1_1, 1, channels, cv::Mat(), hist1, 2, histSize, ranges, true, false);
  275. cv::normalize(hist1, hist1, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
  276. cv::calcHist(&Im2_1, 1, channels, cv::Mat(), hist2, 2, histSize, ranges, true, false);
  277. cv::normalize(hist2, hist2, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
  278. s = cv::compareHist(hist1, hist2,
  279. cv::HISTCMP_CORREL); //,CV_COMP_CHISQR,CV_COMP_INTERSECT,CV_COMP_BHATTACHARYYA CV_COMP_CORREL
  280. std::cout << "similarity = " << s << std::endl;
  281. return s;
  282. }
  283. }
  284. gtcvcore::calgray
  285. gtcvcore::calgray::checkCardForCalGray(std::vector<std::vector<double>> gt, int gt_pointNum, double cycle) {
  286. gtcvcore::calgray Card_Array;
  287. if (!gt.empty() && gt.size() > 0 && cycle > 0 && gt_pointNum > gtPointsNumConditionlimit) {
  288. Card_Array.MaxLoad = -1000;
  289. Card_Array.MinLoad = 1000;
  290. Card_Array.Maxshift = -1000;
  291. Card_Array.Minshift = 1000;
  292. Card_Array.DenseMaxLoad = -1000;
  293. Card_Array.DenseMinLoad = 1000;
  294. Card_Array.DenseMaxshift = -1000;
  295. Card_Array.DenseMinshift = 1000;
  296. Card_Array.Power = 0;
  297. Card_Array.PointNum = gt_pointNum;
  298. //Card_Array.CardStr = SGT.ToString();//无用数据
  299. Card_Array.Cycle = cycle;
  300. Card_Array.Card = new double *[2];
  301. Card_Array.Card[0] = new double[Card_Array.PointNum + 1];//末点后再次加入首点,插值时将不用考虑闭环问题,直接单链插值
  302. Card_Array.Card[1] = new double[Card_Array.PointNum + 1];//末点后再次加入首点,插值时将不用考虑闭环问题,直接单链插值
  303. int i = 0;
  304. for (i = 0; i <= Card_Array.PointNum - 1; i++) {
  305. Card_Array.Card[0][i] = gt[i][0];//位移
  306. Card_Array.Card[1][i] = gt[i][1];//载荷
  307. if (Card_Array.MaxLoad < Card_Array.Card[1][i]) {
  308. Card_Array.MaxLoad = Card_Array.Card[1][i];
  309. Card_Array.MaxLoadPoint = i;
  310. }
  311. if (Card_Array.MinLoad > Card_Array.Card[1][i]) {
  312. Card_Array.MinLoad = Card_Array.Card[1][i];
  313. Card_Array.MinLoadPoint = i;
  314. }
  315. if (Card_Array.Maxshift < Card_Array.Card[0][i]) {
  316. Card_Array.Maxshift = Card_Array.Card[0][i];
  317. Card_Array.MaxshiftPoint = i;
  318. Card_Array.MaxShiftLoad = Card_Array.Card[1][i];//最大位移处的载荷
  319. }
  320. if (Card_Array.Minshift > Card_Array.Card[0][i]) {
  321. Card_Array.Minshift = Card_Array.Card[0][i];
  322. Card_Array.MinshiftPoint = i;
  323. Card_Array.MinShiftLoad = Card_Array.Card[1][i];//最小位移处的载荷
  324. }
  325. if (i != 0) {
  326. if (i != (Card_Array.PointNum - 1)) {
  327. Card_Array.Power = Card_Array.Power +
  328. (Card_Array.Card[1][i] + Card_Array.Card[1][i - 1]) *
  329. (Card_Array.Card[0][i] - Card_Array.Card[0][i - 1]) *
  330. cycle / 120;
  331. } else {
  332. Card_Array.Power = Card_Array.Power +
  333. (Card_Array.Card[1][i] + Card_Array.Card[1][i - 1]) *
  334. (Card_Array.Card[0][i] - Card_Array.Card[0][i - 1]) *
  335. cycle / 120;
  336. Card_Array.Power = Card_Array.Power +
  337. (Card_Array.Card[1][0] + Card_Array.Card[1][i]) *
  338. (Card_Array.Card[0][0] - Card_Array.Card[0][i]) *
  339. cycle / 120;
  340. //if (Card_Array.Cardate < DateTime.Now) break;//enabled timer set
  341. }
  342. }
  343. }
  344. //末点后再次加入首点,插值时将不用考虑闭环问题,直接单链插值
  345. Card_Array.Card[0][Card_Array.PointNum] = gt[0][0];
  346. Card_Array.Card[1][Card_Array.PointNum] = gt[0][1];
  347. Card_Array.Stroke = Card_Array.Maxshift - Card_Array.Minshift;//冲程
  348. //计算功图速度平衡度
  349. if ((Card_Array.PointNum - Card_Array.MaxshiftPoint - 1) > 0) {
  350. Card_Array.SdBalanceDegree = (Card_Array.PointNum - Card_Array.MaxshiftPoint - 1) /
  351. (Card_Array.MaxshiftPoint + 1);
  352. }
  353. if (Card_Array.SdBalanceDegree > 1.2) {
  354. Card_Array.SdBalanceResult = "不平衡,上行过快";
  355. }
  356. if (Card_Array.SdBalanceDegree < 1.2 && Card_Array.SdBalanceDegree >= 1) {
  357. Card_Array.SdBalanceResult = "平衡,上快下慢";
  358. }
  359. if (Card_Array.SdBalanceDegree < 1 && Card_Array.SdBalanceDegree > 0.8) {
  360. Card_Array.SdBalanceResult = "平衡,上慢下快";
  361. }
  362. if (Card_Array.SdBalanceDegree <= 0.8) {
  363. Card_Array.SdBalanceResult = "不平衡,下行过快";
  364. } else
  365. Card_Array.DiagnosisResult = "功图正常";
  366. } else {
  367. if (cycle <= 0)
  368. Card_Array.DiagnosisResult = "功图错误或冲次错误";
  369. if (gt_pointNum < gtPointsNumConditionlimit)
  370. Card_Array.DiagnosisResult = "功图错误,采集点数不足50";
  371. if (gt.empty())
  372. Card_Array.DiagnosisResult = "无功图";
  373. }
  374. return Card_Array;
  375. }
  376. bool gtcvcore::calgray::calGrayCharacters(dgns_gtstandard &cha_obj, dgns_gtbaseparam _baseparam) {
  377. double *cha = cal_cardgray_cha(_baseparam.sgt, _baseparam.sgtpointnum, _baseparam.s, _baseparam.n);
  378. if (cha == NULL) return false;
  379. cha_obj.sgtprodate = _baseparam.recivetime;
  380. cha_obj.sgtcharacters = new double[cardgray_characters_num];
  381. for (int i = 0; i < cardgray_characters_num; ++i) {
  382. cha_obj.sgtcharacters[i] = cha[i];
  383. }
  384. return true;
  385. }
  386. double *
  387. gtcvcore::calgray::cal_cardgray_cha(std::vector<std::vector<double>> P_Card, int gt_PointNum, double S, double N) {
  388. gtcvcore::calgray Card_Array;
  389. double Gray_Typemax, Gray_Typemin;
  390. int Den_MaxDistance = 1000, Hui_bottom = 1000, Hui_upper = 1000, Hui_left = 1000, Hui_right = 1000;
  391. int Insert_Num, i = 1, j, k, L;
  392. if (P_Card.empty() || P_Card.size() == 0) return NULL;
  393. Card_Array = checkCardForCalGray(P_Card, gt_PointNum, N);
  394. Card_Array.CardType = new double[cardgray_characters_num]{0};
  395. //printf("maxload : %2.2f ,minload : %2.2f \n", Card_Array.MaxLoad, Card_Array.MinLoad);
  396. if (strcmp(Card_Array.DiagnosisResult, "无功图") != 0 && Card_Array.PointNum > 0) {
  397. /***********linear **********/
  398. Insert_Num = densecard_point_num_max / Card_Array.PointNum;
  399. Card_Array.DensePointNum = Insert_Num * Card_Array.PointNum;
  400. Card_Array.DenseCard = new double *[2];
  401. //DenseCard[0]、Card[0] 是位移 DenseCard[1]、Card[0]是载荷
  402. Card_Array.DenseCard[0] = new double[Card_Array.PointNum * Insert_Num];
  403. Card_Array.DenseCard[1] = new double[Card_Array.PointNum * Insert_Num];
  404. for (j = 0; j < Card_Array.PointNum; j++) {
  405. //not need to consider the ring problem,by insert the first point to end ,change it to opened cycle
  406. for (i = 0; i < Insert_Num; ++i) {
  407. int DenseCardIndex = j * (Insert_Num) + i;
  408. //TODO : alter linear interpolation to bezier 修改使用bezier方法实现插值
  409. Card_Array.DenseCard[0][DenseCardIndex] =
  410. Card_Array.Card[0][j] + (Card_Array.Card[0][j + 1] - Card_Array.Card[0][j]) / (Insert_Num) * i;
  411. //std::cout << "xx::" << Card_Array.DenseCard[0][DenseCardIndex] << std::endl;
  412. Card_Array.DenseCard[1][DenseCardIndex] =
  413. Card_Array.Card[1][j] + (Card_Array.Card[1][j + 1] - Card_Array.Card[1][j]) / (Insert_Num) * i;
  414. //std::cout << "yy::" << Card_Array.DenseCard[1][DenseCardIndex] << std::endl;
  415. if (Card_Array.DenseCard[0][DenseCardIndex] >= Card_Array.DenseMaxshift) {
  416. Card_Array.DenseMaxshift = Card_Array.DenseCard[0][DenseCardIndex];
  417. Card_Array.DenseMaxshiftPoint = DenseCardIndex;
  418. }
  419. if (Card_Array.DenseCard[0][DenseCardIndex] < Card_Array.DenseMinshift) {
  420. Card_Array.DenseMinshift = Card_Array.DenseCard[0][DenseCardIndex];
  421. Card_Array.DenseMinshiftPoint = DenseCardIndex;
  422. }
  423. if (Card_Array.DenseCard[1][DenseCardIndex] > Card_Array.DenseMaxLoad) {
  424. Card_Array.DenseMaxLoad = Card_Array.DenseCard[1][DenseCardIndex];
  425. Card_Array.DenseMaxLoadPoint = DenseCardIndex;
  426. }
  427. if (Card_Array.DenseCard[1][DenseCardIndex] < Card_Array.DenseMinLoad) {
  428. Card_Array.DenseMinLoad = Card_Array.DenseCard[1][DenseCardIndex];
  429. Card_Array.DenseMinLoadPoint = DenseCardIndex;
  430. }
  431. }
  432. }
  433. /***********Gray Cal IMG************/
  434. Card_Array.CardGray = NULL;
  435. Card_Array.CardGray = new int **[cardgray_nlgt_func_count];
  436. for (int l = 0; l < cardgray_nlgt_func_count; ++l) {
  437. Card_Array.CardGray[l] = new int *[cardgray_width];
  438. for (i = 0; i < cardgray_width; i++) {
  439. Card_Array.CardGray[l][i] = new int[cardgray_height];
  440. for (j = 0; j < cardgray_height; j++) {
  441. Card_Array.CardGray[l][i][j] = cardgray_value_default;
  442. }
  443. }
  444. }
  445. int cardgray_width_value = cardgray_width - 1;
  446. int cardgray_height_value = cardgray_height - 1;
  447. double d_densecardshift = Card_Array.DenseMaxshift - Card_Array.DenseMinshift;
  448. if (d_densecardshift <= 0) d_densecardshift = 0.000001;
  449. double d_densecardload = Card_Array.DenseMaxLoad - Card_Array.DenseMinLoad;
  450. if (d_densecardload <= 0) d_densecardload = 0.000001;
  451. if (Card_Array.DenseMaxLoad <= 0) Card_Array.DenseMaxLoad = 0.000001;
  452. /*std::cout << "DenseMaxshift:" << Card_Array.DenseMaxshift << "DenseMinshift:" << Card_Array.DenseMinshift
  453. << std::endl;
  454. std::cout << "DenseMinLoad:" << Card_Array.DenseMinLoad << "DenseMaxLoad:" << Card_Array.DenseMaxLoad
  455. << std::endl;*/
  456. for (i = 0; i < Card_Array.DensePointNum; i++) {
  457. int Gray_x = (int) ((Card_Array.DenseCard[0][i] - Card_Array.DenseMinshift) / d_densecardshift *
  458. cardgray_width_value);
  459. if (Gray_x > cardgray_width_value) Gray_x = cardgray_width_value;
  460. int Gray_y1 = (int) ((Card_Array.DenseCard[1][i] - Card_Array.DenseMinLoad) / d_densecardload *
  461. cardgray_height_value);
  462. if (Gray_y1 > cardgray_height_value) Gray_y1 = cardgray_height_value;
  463. int Gray_y2 = (int) (Card_Array.DenseCard[1][i] / Card_Array.DenseMaxLoad * cardgray_height_value);
  464. if (Gray_y2 > cardgray_height_value) Gray_y2 = cardgray_height_value;
  465. Card_Array.CardGray[0][Gray_x][Gray_y1] = cardgray_value_usage;
  466. Card_Array.CardGray[1][Gray_x][Gray_y2] = cardgray_value_usage;
  467. }
  468. int block_width = cardgray_width / cardgray_col_block_num;
  469. int block_height = cardgray_height / cardgray_row_block_num;
  470. int block_area = block_width * block_height;
  471. for (int nlgtindex = 0; nlgtindex < cardgray_nlgt_func_count; ++nlgtindex) {
  472. /*std::cout << "像素两值化图像 gt:::" << std::endl;
  473. for (i = cardgray_height - 1; i >= 0; i--) {
  474. for (j = 0; j < cardgray_width; j++) {
  475. if (Card_Array.CardGray[nlgtindex][j][i] == 1)
  476. std::cout << std::setw(2) << "11";//<<Card_Array.CardGray[0][j][i];
  477. else std::cout << std::setw(2) << " ";
  478. }
  479. std::cout << std::endl;
  480. }*/
  481. for (i = 0; i < cardgray_height; i++) {
  482. for (j = 0; j < cardgray_width; j++) {
  483. Den_MaxDistance = 1000;
  484. Hui_bottom = 1000;
  485. Hui_upper = 1000;
  486. Hui_left = 1000;
  487. Hui_right = 1000;
  488. if (Card_Array.CardGray[nlgtindex][j][i] == cardgray_value_default) {
  489. for (k = j; k < cardgray_width; k++) {
  490. if (Card_Array.CardGray[nlgtindex][k][i] == cardgray_value_usage) {
  491. Hui_right = std::abs(k - j);
  492. break;
  493. }
  494. }
  495. for (k = i; k < cardgray_height; k++) {
  496. if (Card_Array.CardGray[nlgtindex][j][k] == cardgray_value_usage) {
  497. Hui_upper = std::abs(k - i);
  498. break;
  499. }
  500. }
  501. for (k = 0; k <= i; k++) {
  502. if (Card_Array.CardGray[nlgtindex][j][i - k] == cardgray_value_usage) {
  503. Hui_bottom = std::abs(k);
  504. break;
  505. }
  506. }
  507. for (k = 0; k <= j; k++) {
  508. if (Card_Array.CardGray[nlgtindex][j - k][i] != cardgray_value_default) {
  509. if (Hui_bottom == 1000 || Hui_upper == 1000 || Hui_right == 1000) {
  510. Hui_left = std::abs(Card_Array.CardGray[nlgtindex][j - k][i] - k - 1);
  511. } else {
  512. Hui_left = std::abs(Card_Array.CardGray[nlgtindex][j - k][i] + k - 1);
  513. }
  514. break;
  515. }
  516. }
  517. if (Hui_bottom <= Den_MaxDistance) Den_MaxDistance = Hui_bottom;
  518. if (Hui_upper <= Den_MaxDistance) Den_MaxDistance = Hui_upper;
  519. if (Hui_left <= Den_MaxDistance) Den_MaxDistance = Hui_left;
  520. if (Hui_right <= Den_MaxDistance) Den_MaxDistance = Hui_right;
  521. if (Hui_bottom == 1000 || Hui_upper == 1000 || Hui_left == 1000 || Hui_right == 1000) {
  522. Card_Array.CardGray[nlgtindex][j][i] = cardgray_value_usage - Den_MaxDistance;
  523. } else {
  524. Card_Array.CardGray[nlgtindex][j][i] = cardgray_value_usage + Den_MaxDistance;
  525. }
  526. }
  527. }
  528. }
  529. /*std::cout << "灰度加权图像:::f" << nlgtindex << "::::" << std::endl;
  530. for (i = cardgray_height - 1; i >= 0; i--) {
  531. for (j = 0; j < cardgray_width; j++) {
  532. std::cout << std::setw(3) << Card_Array.CardGray[nlgtindex][j][i];
  533. }
  534. std::cout << std::endl;
  535. }*/
  536. Gray_Typemax = -10000000;
  537. Gray_Typemin = 10000000;
  538. for (int rowbolockindex = 0; rowbolockindex < cardgray_col_block_num; ++rowbolockindex) {
  539. for (int colbolockindex = 0; colbolockindex < cardgray_row_block_num; ++colbolockindex) {
  540. int cardgray_block_x_start = block_width * rowbolockindex;
  541. int cardgray_block_x_end = block_width * (rowbolockindex + 1);
  542. int cardgray_block_y_start = block_height * colbolockindex;
  543. int cardgray_block_y_end = block_height * (colbolockindex + 1);
  544. std::map<int, int> grayvalue_counter;
  545. for (int x = cardgray_block_x_start; x < cardgray_block_x_end; ++x) {
  546. for (int y = cardgray_block_y_start; y < cardgray_block_y_end; ++y) {
  547. int temp = Card_Array.CardGray[nlgtindex][x][y];
  548. if (grayvalue_counter.find(temp) != grayvalue_counter.end())
  549. grayvalue_counter[temp] += 1;
  550. else
  551. grayvalue_counter.insert(std::pair<int, int>(temp, 1));
  552. }
  553. }
  554. std::map<int, double> grayvalue_probability;
  555. std::vector<double> cha_block;
  556. double cha0 = 0;
  557. for (std::map<int, int>::iterator grayvalue_counter_item = grayvalue_counter.begin();
  558. grayvalue_counter_item != grayvalue_counter.end(); grayvalue_counter_item++) {
  559. double temp = grayvalue_counter_item->second * 1.0 / block_area;
  560. grayvalue_probability.insert(std::pair<int, double>(grayvalue_counter_item->first, temp));
  561. cha0 += grayvalue_counter_item->first * temp;
  562. }
  563. cha_block.push_back(cha0);
  564. double cha1 = 0, cha2 = 0, cha3 = 0, cha4 = 0, cha5 = 0;
  565. for (std::map<int, double>::iterator grayvalue_probability_item = grayvalue_probability.begin();
  566. grayvalue_probability_item != grayvalue_probability.end(); grayvalue_probability_item++) {
  567. cha1 += std::pow(grayvalue_probability_item->first - cha0, 2) *
  568. grayvalue_probability_item->second;
  569. cha2 += std::pow(grayvalue_probability_item->first - cha0, 3) *
  570. grayvalue_probability_item->second;
  571. cha3 += std::pow(grayvalue_probability_item->first - cha0, 4) *
  572. grayvalue_probability_item->second;
  573. cha4 += std::pow(grayvalue_probability_item->second, 2);
  574. cha5 += (1 - grayvalue_probability_item->second) *
  575. std::log10(1 - grayvalue_probability_item->second);
  576. }
  577. cha2 = cha2 / std::pow(cha1, 3 / 2.0);
  578. cha3 = cha3 / std::pow(cha1, 2);
  579. cha_block.push_back(cha1);
  580. cha_block.push_back(cha2);
  581. cha_block.push_back(cha3);
  582. cha_block.push_back(cha4);
  583. cha_block.push_back(cha5);
  584. int cha_start_point_index = nlgtindex * cardgray_col_block_num * cardgray_row_block_num;
  585. cha_start_point_index += rowbolockindex * cardgray_col_block_num + colbolockindex;
  586. cha_start_point_index *= cardgray_block_cha_num;
  587. for (int cha_l = 0; cha_l < cha_block.size(); ++cha_l) {
  588. if (cha_block[cha_l] > Gray_Typemax) Gray_Typemax = cha_block[cha_l];
  589. if (cha_block[cha_l] < Gray_Typemin) Gray_Typemin = cha_block[cha_l];
  590. Card_Array.CardType[cha_start_point_index + cha_l] = cha_block[cha_l];
  591. }
  592. }
  593. }
  594. /* std::cout << "cha&&" << nlgtindex << std::endl;
  595. std::cout << "max cha:" << Gray_Typemax << " min cha:" << Gray_Typemin << std::endl;
  596. std::stringstream ss1;
  597. std::stringstream ss2;*/
  598. for (int chacc_l = nlgtindex * cardgray_row_block_num * cardgray_col_block_num * cardgray_block_cha_num;
  599. chacc_l < (nlgtindex + 1) * cardgray_row_block_num * cardgray_col_block_num *
  600. cardgray_block_cha_num; ++chacc_l) {
  601. Card_Array.CardType[chacc_l] =
  602. (Card_Array.CardType[chacc_l] - Gray_Typemin) / (Gray_Typemax - Gray_Typemin);
  603. /* ss2 << std::setw(10) << chacc_l << ";";
  604. ss1 << std::setw(10) << Card_Array.CardType[chacc_l] << ";";*/
  605. }
  606. /* std::cout << ss2.str() << std::endl;
  607. std::cout << ss1.str() << std::endl;
  608. std::cout << std::endl << "cha eeend" << nlgtindex << std::endl;*/
  609. }
  610. //printf("Here you are outgoing!\n");
  611. //return Card_Array.CardType;
  612. }
  613. return Card_Array.CardType;
  614. }
  615. double gtcvcore::calgray::compareCharacters(double *s_cha, double *k_cha, int method) {
  616. // method = 23;
  617. if (s_cha == NULL || k_cha == NULL) return 0;
  618. int i = 0;
  619. double l_res1 = 0.0;
  620. //double L_EPS1=0;// unused variable
  621. //double L_EPS2=0;// unused variable
  622. double Com_max = -200.00;
  623. double Com_min = 200.01;
  624. double Mid_Value1 = 0;
  625. double Mid_Value2 = 0;
  626. double L_DLT1 = 0;
  627. double L_DLT2 = 0;
  628. double l_res2 = 0;
  629. double d_cha[cardgray_characters_num] = {0};
  630. switch (method) {
  631. case 23: {
  632. Com_max = -200.00;
  633. Com_min = 200.01;
  634. //L_EPS1=0;
  635. //L_EPS2=0;
  636. Mid_Value1 = 0;
  637. Mid_Value2 = 0;
  638. /*std::string c1="cha1:";
  639. std::string c2="cha2:";*/
  640. for (i = 0; i <= method; i++) {
  641. /* c1+=std::to_string(s_cha[i])+",";
  642. c2+=std::to_string(k_cha[i])+",";*/
  643. d_cha[i] = std::abs(s_cha[i] - k_cha[i]);
  644. if (d_cha[i] >= Com_max) Com_max = d_cha[i];
  645. if (d_cha[i] <= Com_min) Com_min = d_cha[i];
  646. }
  647. /*std::cout<<c1<<std::endl;
  648. std::cout<<c2<<std::endl;*/
  649. if (Com_max == Com_min) l_res1 = 1;
  650. else {
  651. for (i = 0; i <= method; i++) {
  652. Mid_Value1 = Mid_Value1 + 1 / (d_cha[i] + Com_max / 2);
  653. Mid_Value2 = Mid_Value2 + d_cha[i];
  654. }
  655. //L_EPS1=(Com_min+Com_max/2)/(method+1)*Mid_Value1;
  656. L_DLT1 = 1.0 / (1 + Mid_Value2 / (method + 1));
  657. l_res1 = L_DLT1;
  658. }
  659. //L_EPS2=0;
  660. L_DLT2 = 0;
  661. l_res2 = 0;
  662. Mid_Value1 = 0;
  663. Mid_Value2 = 0;
  664. Com_max = -200.00;
  665. Com_min = 200.01;
  666. for (i = method + 1; i <= 47; i++) {
  667. d_cha[i] = std::abs(s_cha[i] - k_cha[i]);
  668. if (d_cha[i] >= Com_max) Com_max = d_cha[i];
  669. if (d_cha[i] <= Com_min) Com_min = d_cha[i];
  670. }
  671. if (Com_max == Com_min) l_res2 = 1;
  672. else {
  673. for (i = method + 1; i <= 47; i++) {
  674. Mid_Value1 = Mid_Value1 + 1 / (d_cha[i] + Com_max / 2);
  675. Mid_Value2 = Mid_Value2 + d_cha[i];
  676. }
  677. //L_EPS2=(Com_min+Com_max/2)/(47-method)*Mid_Value1;
  678. L_DLT2 = 1 / (1 + Mid_Value2 / (47 - method));
  679. l_res2 = L_DLT2;
  680. }
  681. l_res1 = l_res1 * 0.8 + l_res2 * 0.2;
  682. return l_res1;
  683. }
  684. }
  685. return l_res1;
  686. }
  687. double
  688. gtcvcore::calgray::C_GTSimillar_Gray(std::vector<std::vector<double>> gt1, int pointnum1, double S1, double N1,
  689. std::vector<std::vector<double>> gt2, int pointnum2, double S2, double N2) {
  690. double *cha1 = cal_cardgray_cha(gt1, pointnum1, S1, N1);
  691. double *cha2 = cal_cardgray_cha(gt2, pointnum2, S2, N2);
  692. return compareCharacters(cha1, cha2);
  693. }