123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823 |
- //
- // Created by lloyd on 2020/9/28.
- //
- #include <iostream>
- #include <vector>
- #include "opencv2/opencv.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "gtrunconf.h"
- #include "gtimgcreator.h"
- #include "gtcommonkits.h"
- #include "gtcvcore.h"
- #include "DataMapping/dgns_gtstandard.h"
- #include "DataMapping/dgns_gtbaseparam.h"
- /*
- ***执行预测
- @STR 功图标准化数据
- @FuncType 归一法1图像输入1 ,归一法2图像输入2
- @load 1 指定必须重新加载 其它默认
- @CacheName GUID生成的一个PNGname
- */
- std::string
- gtcvcore::HOGSVMPredict(std::vector<std::vector<int>> >1, std::vector<std::vector<int>> >2, int FuncType, int load,
- const char *CacheName = NULL) {
- //std::cout<<"个数:" << gtArray.size() << std::endl;
- int ImgHeight = gtrunconf_rows;
- int ImgWidht = gtrunconf_cols;
- cv::Mat mat1(ImgHeight, ImgWidht, CV_8UC3);
- cv::Mat mat2(ImgHeight, ImgWidht, CV_8UC3);
- //mat生成器
- gtimgcreator cvmc;
- cvmc.creatGTMat(mat1, mat2, ImgHeight, ImgWidht, gt1, gt2, gt1.size(), 1);
- //std::string PNGName = MyToolsKit::convertTCHAR_string(CacheName);
- //std::cout << "文件名:" << PNGName << std::endl;
- //cvmc.saveGTPNG(mat1, path1);
- //cvmc.saveGTPNG(mat2, path2);
- //mat1 = cv::imread(path1);
- //mat2 = cv::imread(path2);
- cv::imshow("标准化功图", mat1);
- //cv::imshow("输入的法2功图", mat2);
- //cv::waitKey();
- cv::waitKey(gtrunconf_interval_delaytime);
- //cv::resize(src, trainImg, cv::Size(ImgWidht, ImgHeight), 0, 0, cv::INTER_CUBIC);
- cv::HOGDescriptor hog(cv::Size(ImgWidht, ImgHeight), cv::Size(16, 16), cv::Size(8, 8), cv::Size(8, 8),
- 9); //具体意思见参考文章1,2
- std::vector<float> descriptors;//结果数组
- cv::Mat saveMat;
- //hog.compute(trainImg, descriptors, cv::Size(8, 8), cv::Size(0, 0)); //调用计算函数开始计算
- if (FuncType == 1) {
- hog.compute(mat1, descriptors, cv::Size(8, 8)); //调用计算函数开始计算
- saveMat = mat1;
- } else {
- hog.compute(mat2, descriptors, cv::Size(8, 8)); //调用计算函数开始计算
- saveMat = mat2;
- }
- //creat SVM classfier
- //cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
- //load train file
- initSVM(load);
- std::string PredictResult = "Error";
- try {
- if (FuncType == 1) {
- int r = gtcvcore::MySvm1->predict(descriptors); //进行预测
- std::vector<std::string> gtTypes = gtcommonkits::str_Split(gtcvcore::GTYypes1, ",");
- PredictResult = gtTypes[r - 1];
- }
- }
- catch (std::runtime_error &ex) {
- printf("预测时发生错误:%s\n", ex.what());
- PredictResult = "Error";
- }
- if (PredictResult == "Error") {
- //std::cout << std::endl << "预测结果:" << PredictResult << std::endl;
- } else {
- //std::cout << std::endl << "预测结果:" << PredictResult << std::endl;
- /*path3 += PredictResult + ".png";
- cvmc.saveGTPNG(saveMat, path3);*/
- }
- return PredictResult;
- //char* predictresult_tchar = MyToolsKit::convertstring_char(PredictResult);
- //std::string retresult = MyToolsKit::convertTCHAR_string(predictresult_tchar);
- //std::cout << std::endl << "返回的预测结果:" << retresult << std::endl;
- //return predictresult_tchar;
- //cv::waitKey(0);
- }
- /*
- ***svm 加载
- @load 1 必须重新加载 其它默认
- */
- void gtcvcore::initSVM(int load) {
- if (load == 1) {
- try {
- std::string svmstr = gtrunconf_xml_basepath;
- std::string txtstr = gtrunconf_txt_basepath;
- // MemoryShare::readShareMemory(L"ShareMemory0", svmstr);
- //MyToolsKit::saveTxt("D:\\t.xml", svmstr);
- //std::cout << svmstr << std::endl;
- const cv::String &objname = "xml";
- const cv::String &strModel = svmstr;
- // MemoryShare::string_replace(svmstr, "MemoryURL:", "");
- // std::cout << svmstr << std::endl;
- //CVHOGSVMCategorize::MySvm1 = cv::ml::SVM::loadFromString<cv::ml::SVM>(strModel);
- if (gtcvcore::MySvm1.empty() == false)
- gtcvcore::MySvm1->clear();
- gtcvcore::MySvm1 = cv::ml::SVM::load(svmstr);
- std::string resultTypes = gtcommonkits::getTextFromTxt(txtstr);
- // std::cout << resultTypes << std::endl;
- gtcvcore::GTYypes1 = resultTypes;
- }
- catch (std::runtime_error &ex) {
- printf("读取svm1发生错误:%s\n", ex.what());
- }
- } else {
- if (gtcvcore::GTYypes1.size() == 0) {
- try {
- std::string svmstr = gtrunconf_xml_basepath;
- std::string txtstr = gtrunconf_txt_basepath;
- //std::cout << svmstr << std::endl;
- const cv::String &objname = "xml";
- const cv::String &strModel = svmstr;
- gtcvcore::MySvm1 = cv::ml::SVM::load(svmstr);
- std::string resultTypes = gtcommonkits::getTextFromTxt(txtstr);
- // std::cout << resultTypes << std::endl;
- gtcvcore::GTYypes1 = resultTypes;
- }
- catch (std::runtime_error &ex) {
- printf("读取svm1发生错误:%s\n", ex.what());
- }
- }
- }
- }
- cv::Ptr<cv::ml::SVM> gtcvcore::MySvm1 = cv::ml::SVM::create();
- std::string gtcvcore::GTYypes1 = "";
- /***Not ideal***/
- double gtcvcore::C_GTSimillar_PSNR(std::vector<std::vector<int>> >1_1, std::vector<std::vector<int>> >1_2,
- std::vector<std::vector<int>> >2_1, std::vector<std::vector<int>> >2_2) {
- int ImgHeight = gtrunconf_rows;
- int ImgWidht = gtrunconf_cols;
- cv::Mat I1_1(ImgHeight, ImgWidht, CV_8UC3);
- cv::Mat I1_2(ImgHeight, ImgWidht, CV_8UC3);
- gtimgcreator cvmc;
- cvmc.creatGTMat(I1_1, I1_2, ImgHeight, ImgWidht, gt1_1, gt1_2, gt1_1.size(), 1);
- cv::Mat I2_1(ImgHeight, ImgWidht, CV_8UC3);
- cv::Mat I2_2(ImgHeight, ImgWidht, CV_8UC3);
- cvmc.creatGTMat(I2_1, I2_2, ImgHeight, ImgWidht, gt2_1, gt2_2, gt2_1.size(), 1);
- cv::Mat s1;
- absdiff(I1_1, I2_1, s1); // |I1 - I2|
- s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits
- s1 = s1.mul(s1); // |I1 - I2|^2
- cv::Scalar s = sum(s1); // sum elements per channel
- double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels
- if (sse <= 1e-10) // for small values return zero
- return 0;
- else {
- double mse = sse / (double) (I1_1.channels() * I1_1.total());
- double psnr = 10.0 * log10((255 * 255) / mse);
- return psnr;
- }
- }
- /***Not ideal***/
- double gtcvcore::C_GTSimillar_MSSIM(std::vector<std::vector<int>> >1_1, std::vector<std::vector<int>> >1_2,
- std::vector<std::vector<int>> >2_1, std::vector<std::vector<int>> >2_2) {
- const double C1 = 6.5025, C2 = 58.5225;
- int ImgHeight = gtrunconf_rows;
- int ImgWidht = gtrunconf_cols;
- cv::Mat Im1_1(ImgHeight, ImgWidht, CV_8UC3);
- cv::Mat Im1_2(ImgHeight, ImgWidht, CV_8UC3);
- gtimgcreator cvmc;
- cvmc.creatGTMat(Im1_1, Im1_2, ImgHeight, ImgWidht, gt1_1, gt1_2, gt1_1.size(), 1);
- cv::Mat Im2_1(ImgHeight, ImgWidht, CV_8UC3);
- cv::Mat Im2_2(ImgHeight, ImgWidht, CV_8UC3);
- cvmc.creatGTMat(Im2_1, Im2_2, ImgHeight, ImgWidht, gt2_1, gt2_2, gt2_1.size(), 1);
- //cv::imshow("1", Im1_1);
- cv::imshow("标准工况功图", Im2_1);
- /***************************** INITS **********************************/
- int d = CV_32F;
- cv::Mat I1, I2;
- Im1_1.convertTo(I1, d); // cannot calculate on one byte large values
- Im2_1.convertTo(I2, d);
- cv::Mat I2_2 = I2.mul(I2); // I2^2
- cv::Mat I1_2 = I1.mul(I1); // I1^2
- cv::Mat I1_I2 = I1.mul(I2); // I1 * I2
- /*************************** END INITS **********************************/
- int blocksize = 15;//%2==1
- if (blocksize % 2 == 0) blocksize += 1;
- double _sigmaX = 1.2;//表示高斯核函数在X方向的的标准偏差。
- cv::Mat mu1, mu2; // PRELIMINARY COMPUTING
- GaussianBlur(I1, mu1, cv::Size(blocksize, blocksize), _sigmaX);//高斯滤波
- GaussianBlur(I2, mu2, cv::Size(blocksize, blocksize), _sigmaX);
- cv::Mat mu1_2 = mu1.mul(mu1);
- cv::Mat mu2_2 = mu2.mul(mu2);
- cv::Mat mu1_mu2 = mu1.mul(mu2);
- cv::Mat sigma1_2, sigma2_2, sigma12;
- GaussianBlur(I1_2, sigma1_2, cv::Size(blocksize, blocksize), _sigmaX);
- sigma1_2 -= mu1_2;
- GaussianBlur(I2_2, sigma2_2, cv::Size(blocksize, blocksize), _sigmaX);
- sigma2_2 -= mu2_2;
- GaussianBlur(I1_I2, sigma12, cv::Size(blocksize, blocksize), _sigmaX);
- sigma12 -= mu1_mu2;
- //FORMULA
- cv::Mat t1, t2, t3;
- t1 = 2 * mu1_mu2 + C1;
- t2 = 2 * sigma12 + C2;
- t3 = t1.mul(t2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
- t1 = mu1_2 + mu2_2 + C1;
- t2 = sigma1_2 + sigma2_2 + C2;
- t1 = t1.mul(t2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
- cv::Mat ssim_map;
- divide(t3, t1, ssim_map); // ssim_map = t3./t1;
- cv::Scalar mssim = mean(ssim_map); // mssim = average of ssim map
- return (mssim.val[0] + mssim.val[1] + mssim.val[2]) / 3.0;
- //return mssim;
- }
- /***Not ideal***/
- double gtcvcore::C_GTSimillar_HOG(std::vector<std::vector<int>> >1_1, std::vector<std::vector<int>> >1_2,
- std::vector<std::vector<int>> >2_1, std::vector<std::vector<int>> >2_2) {
- int ImgHeight = gtrunconf_rows;
- int ImgWidht = gtrunconf_cols;
- cv::Mat Im1_1(ImgHeight, ImgWidht, CV_8UC3);
- cv::Mat Im1_2(ImgHeight, ImgWidht, CV_8UC3);
- gtimgcreator cvmc;
- cvmc.creatGTMat(Im1_1, Im1_2, ImgHeight, ImgWidht, gt1_1, gt1_2, gt1_1.size(), 1);
- cv::Mat Im2_1(ImgHeight, ImgWidht, CV_8UC3);
- cv::Mat Im2_2(ImgHeight, ImgWidht, CV_8UC3);
- cvmc.creatGTMat(Im2_1, Im2_2, ImgHeight, ImgWidht, gt2_1, gt2_2, gt2_1.size(), 1);
- cv::imshow("1", Im1_1);
- cv::imshow("2", Im2_1);
- /**********************************************start*****************************************************************************/
- cv::HOGDescriptor hog(cv::Size(ImgWidht, ImgHeight), cv::Size(16, 16),
- cv::Size(8, 8), cv::Size(8, 8), 9);
- std::vector<float> descriptors1, descriptors2;
- hog.compute(Im1_1, descriptors1, cv::Size(8, 8));
- hog.compute(Im2_1, descriptors2, cv::Size(8, 8));
- double s = cv::compareHist(descriptors1, descriptors2, 0);
- std::cout << "The 1 Fun compare simillar:" << s << std::endl;
- s = cv::compareHist(descriptors1, descriptors2, 1);
- std::cout << "The 2 Fun compare simillar:" << s << std::endl;
- s = cv::compareHist(descriptors1, descriptors2, 2);
- std::cout << "The 3 Fun compare simillar:" << s << std::endl;
- s = cv::compareHist(descriptors1, descriptors2, 3);
- std::cout << "The 4 Fun compare simillar:" << s << std::endl;
- //return s;
- //计算相似度
- if (Im1_1.channels() == 1) {//单通道时,
- int histSize = 256;
- float range[] = {0, 256};
- const float *histRange = {range};
- bool uniform = true;
- bool accumulate = false;
- cv::Mat hist1, hist2;
- cv::calcHist(&Im1_1, 1, 0, cv::Mat(), hist1, 1, &histSize, &histRange, uniform, accumulate);
- cv::normalize(hist1, hist1, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
- cv::calcHist(&Im2_1, 1, 0, cv::Mat(), hist2, 1, &histSize, &histRange, uniform, accumulate);
- cv::normalize(hist2, hist2, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
- s = cv::compareHist(hist1, hist2,
- cv::HISTCMP_CORREL);//,CV_COMP_CHISQR,CV_COMP_INTERSECT,CV_COMP_BHATTACHARYYA CV_COMP_CORREL
- std::cout << "similarity = " << s << std::endl;
- } else {//三通道时
- //图像转换到HSV空间进行比较
- cv::cvtColor(Im1_1, Im1_1, cv::COLOR_BGR2HSV);
- cv::cvtColor(Im2_1, Im2_1, cv::COLOR_BGR2HSV);
- //设定直方图需要相关参数
- int h_bins = 50, s_bins = 60;
- int histSize[] = {h_bins, s_bins};
- float h_ranges[] = {0,
- 180};//色调H用角度度量,取值范围为0°~360°,从红色开始按逆时针方向计算,红色为0°,绿色为120°,蓝色为240°。它们的补色是:黄色为60°,青色为180°,品红为300°;
- float s_ranges[] = {0, 256};
- const float *ranges[] = {h_ranges, s_ranges};
- int channels[] = {0, 1};//比较H和S通道直方图,由于函数只能最多比较2维直方图,所以需要进行选择。
- //int channels[] = { 1, 2};//比较S和V通道直方图
- //int channels[] = {0};//只比较第一个H通道的直方图选择
- cv::MatND hist1, hist2;
- //进行直方图提取以及归一化计算
- cv::calcHist(&Im1_1, 1, channels, cv::Mat(), hist1, 2, histSize, ranges, true, false);
- cv::normalize(hist1, hist1, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
- cv::calcHist(&Im2_1, 1, channels, cv::Mat(), hist2, 2, histSize, ranges, true, false);
- cv::normalize(hist2, hist2, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
- s = cv::compareHist(hist1, hist2,
- cv::HISTCMP_CORREL); //,CV_COMP_CHISQR,CV_COMP_INTERSECT,CV_COMP_BHATTACHARYYA CV_COMP_CORREL
- std::cout << "similarity = " << s << std::endl;
- return s;
- }
- }
- gtcvcore::calgray
- gtcvcore::calgray::checkCardForCalGray(std::vector<std::vector<double>> gt, int gt_pointNum, double cycle) {
- gtcvcore::calgray Card_Array;
- if (!gt.empty() && gt.size() > 0 && cycle > 0 && gt_pointNum > gtPointsNumConditionlimit) {
- Card_Array.MaxLoad = -1000;
- Card_Array.MinLoad = 1000;
- Card_Array.Maxshift = -1000;
- Card_Array.Minshift = 1000;
- Card_Array.DenseMaxLoad = -1000;
- Card_Array.DenseMinLoad = 1000;
- Card_Array.DenseMaxshift = -1000;
- Card_Array.DenseMinshift = 1000;
- Card_Array.Power = 0;
- Card_Array.PointNum = gt_pointNum;
- //Card_Array.CardStr = SGT.ToString();//无用数据
- Card_Array.Cycle = cycle;
- Card_Array.Card = new double *[2];
- Card_Array.Card[0] = new double[Card_Array.PointNum + 1];//末点后再次加入首点,插值时将不用考虑闭环问题,直接单链插值
- Card_Array.Card[1] = new double[Card_Array.PointNum + 1];//末点后再次加入首点,插值时将不用考虑闭环问题,直接单链插值
- int i = 0;
- for (i = 0; i <= Card_Array.PointNum - 1; i++) {
- Card_Array.Card[0][i] = gt[i][0];//位移
- Card_Array.Card[1][i] = gt[i][1];//载荷
- if (Card_Array.MaxLoad < Card_Array.Card[1][i]) {
- Card_Array.MaxLoad = Card_Array.Card[1][i];
- Card_Array.MaxLoadPoint = i;
- }
- if (Card_Array.MinLoad > Card_Array.Card[1][i]) {
- Card_Array.MinLoad = Card_Array.Card[1][i];
- Card_Array.MinLoadPoint = i;
- }
- if (Card_Array.Maxshift < Card_Array.Card[0][i]) {
- Card_Array.Maxshift = Card_Array.Card[0][i];
- Card_Array.MaxshiftPoint = i;
- Card_Array.MaxShiftLoad = Card_Array.Card[1][i];//最大位移处的载荷
- }
- if (Card_Array.Minshift > Card_Array.Card[0][i]) {
- Card_Array.Minshift = Card_Array.Card[0][i];
- Card_Array.MinshiftPoint = i;
- Card_Array.MinShiftLoad = Card_Array.Card[1][i];//最小位移处的载荷
- }
- if (i != 0) {
- if (i != (Card_Array.PointNum - 1)) {
- Card_Array.Power = Card_Array.Power +
- (Card_Array.Card[1][i] + Card_Array.Card[1][i - 1]) *
- (Card_Array.Card[0][i] - Card_Array.Card[0][i - 1]) *
- cycle / 120;
- } else {
- Card_Array.Power = Card_Array.Power +
- (Card_Array.Card[1][i] + Card_Array.Card[1][i - 1]) *
- (Card_Array.Card[0][i] - Card_Array.Card[0][i - 1]) *
- cycle / 120;
- Card_Array.Power = Card_Array.Power +
- (Card_Array.Card[1][0] + Card_Array.Card[1][i]) *
- (Card_Array.Card[0][0] - Card_Array.Card[0][i]) *
- cycle / 120;
- //if (Card_Array.Cardate < DateTime.Now) break;//enabled timer set
- }
- }
- }
- //末点后再次加入首点,插值时将不用考虑闭环问题,直接单链插值
- Card_Array.Card[0][Card_Array.PointNum] = gt[0][0];
- Card_Array.Card[1][Card_Array.PointNum] = gt[0][1];
- Card_Array.Stroke = Card_Array.Maxshift - Card_Array.Minshift;//冲程
- //计算功图速度平衡度
- if ((Card_Array.PointNum - Card_Array.MaxshiftPoint - 1) > 0) {
- Card_Array.SdBalanceDegree = (Card_Array.PointNum - Card_Array.MaxshiftPoint - 1) /
- (Card_Array.MaxshiftPoint + 1);
- }
- if (Card_Array.SdBalanceDegree > 1.2) {
- Card_Array.SdBalanceResult = "不平衡,上行过快";
- }
- if (Card_Array.SdBalanceDegree < 1.2 && Card_Array.SdBalanceDegree >= 1) {
- Card_Array.SdBalanceResult = "平衡,上快下慢";
- }
- if (Card_Array.SdBalanceDegree < 1 && Card_Array.SdBalanceDegree > 0.8) {
- Card_Array.SdBalanceResult = "平衡,上慢下快";
- }
- if (Card_Array.SdBalanceDegree <= 0.8) {
- Card_Array.SdBalanceResult = "不平衡,下行过快";
- } else
- Card_Array.DiagnosisResult = "功图正常";
- } else {
- if (cycle <= 0)
- Card_Array.DiagnosisResult = "功图错误或冲次错误";
- if (gt_pointNum < gtPointsNumConditionlimit)
- Card_Array.DiagnosisResult = "功图错误,采集点数不足50";
- if (gt.empty())
- Card_Array.DiagnosisResult = "无功图";
- }
- return Card_Array;
- }
- bool gtcvcore::calgray::calGrayCharacters(dgns_gtstandard &cha_obj, dgns_gtbaseparam _baseparam) {
- double *cha = cal_cardgray_cha(_baseparam.sgt, _baseparam.sgtpointnum, _baseparam.s, _baseparam.n);
- if (cha == NULL) return false;
- cha_obj.sgtprodate = _baseparam.recivetime;
- cha_obj.sgtcharacters = new double[cardgray_characters_num];
- for (int i = 0; i < cardgray_characters_num; ++i) {
- cha_obj.sgtcharacters[i] = cha[i];
- }
- return true;
- }
- double *
- gtcvcore::calgray::cal_cardgray_cha(std::vector<std::vector<double>> P_Card, int gt_PointNum, double S, double N) {
- gtcvcore::calgray Card_Array;
- double Gray_Typemax, Gray_Typemin;
- int Den_MaxDistance = 1000, Hui_bottom = 1000, Hui_upper = 1000, Hui_left = 1000, Hui_right = 1000;
- int Insert_Num, i = 1, j, k, L;
- if (P_Card.empty() || P_Card.size() == 0) return NULL;
- Card_Array = checkCardForCalGray(P_Card, gt_PointNum, N);
- Card_Array.CardType = new double[cardgray_characters_num]{0};
- //printf("maxload : %2.2f ,minload : %2.2f \n", Card_Array.MaxLoad, Card_Array.MinLoad);
- if (strcmp(Card_Array.DiagnosisResult, "无功图") != 0 && Card_Array.PointNum > 0) {
- /***********linear **********/
- Insert_Num = densecard_point_num_max / Card_Array.PointNum;
- Card_Array.DensePointNum = Insert_Num * Card_Array.PointNum;
- Card_Array.DenseCard = new double *[2];
- //DenseCard[0]、Card[0] 是位移 DenseCard[1]、Card[0]是载荷
- Card_Array.DenseCard[0] = new double[Card_Array.PointNum * Insert_Num];
- Card_Array.DenseCard[1] = new double[Card_Array.PointNum * Insert_Num];
- for (j = 0; j < Card_Array.PointNum; j++) {
- //not need to consider the ring problem,by insert the first point to end ,change it to opened cycle
- for (i = 0; i < Insert_Num; ++i) {
- int DenseCardIndex = j * (Insert_Num) + i;
- //TODO : alter linear interpolation to bezier 修改使用bezier方法实现插值
- Card_Array.DenseCard[0][DenseCardIndex] =
- Card_Array.Card[0][j] + (Card_Array.Card[0][j + 1] - Card_Array.Card[0][j]) / (Insert_Num) * i;
- //std::cout << "xx::" << Card_Array.DenseCard[0][DenseCardIndex] << std::endl;
- Card_Array.DenseCard[1][DenseCardIndex] =
- Card_Array.Card[1][j] + (Card_Array.Card[1][j + 1] - Card_Array.Card[1][j]) / (Insert_Num) * i;
- //std::cout << "yy::" << Card_Array.DenseCard[1][DenseCardIndex] << std::endl;
- if (Card_Array.DenseCard[0][DenseCardIndex] >= Card_Array.DenseMaxshift) {
- Card_Array.DenseMaxshift = Card_Array.DenseCard[0][DenseCardIndex];
- Card_Array.DenseMaxshiftPoint = DenseCardIndex;
- }
- if (Card_Array.DenseCard[0][DenseCardIndex] < Card_Array.DenseMinshift) {
- Card_Array.DenseMinshift = Card_Array.DenseCard[0][DenseCardIndex];
- Card_Array.DenseMinshiftPoint = DenseCardIndex;
- }
- if (Card_Array.DenseCard[1][DenseCardIndex] > Card_Array.DenseMaxLoad) {
- Card_Array.DenseMaxLoad = Card_Array.DenseCard[1][DenseCardIndex];
- Card_Array.DenseMaxLoadPoint = DenseCardIndex;
- }
- if (Card_Array.DenseCard[1][DenseCardIndex] < Card_Array.DenseMinLoad) {
- Card_Array.DenseMinLoad = Card_Array.DenseCard[1][DenseCardIndex];
- Card_Array.DenseMinLoadPoint = DenseCardIndex;
- }
- }
- }
- /***********Gray Cal IMG************/
- Card_Array.CardGray = NULL;
- Card_Array.CardGray = new int **[cardgray_nlgt_func_count];
- for (int l = 0; l < cardgray_nlgt_func_count; ++l) {
- Card_Array.CardGray[l] = new int *[cardgray_width];
- for (i = 0; i < cardgray_width; i++) {
- Card_Array.CardGray[l][i] = new int[cardgray_height];
- for (j = 0; j < cardgray_height; j++) {
- Card_Array.CardGray[l][i][j] = cardgray_value_default;
- }
- }
- }
- int cardgray_width_value = cardgray_width - 1;
- int cardgray_height_value = cardgray_height - 1;
- double d_densecardshift = Card_Array.DenseMaxshift - Card_Array.DenseMinshift;
- if (d_densecardshift <= 0) d_densecardshift = 0.000001;
- double d_densecardload = Card_Array.DenseMaxLoad - Card_Array.DenseMinLoad;
- if (d_densecardload <= 0) d_densecardload = 0.000001;
- if (Card_Array.DenseMaxLoad <= 0) Card_Array.DenseMaxLoad = 0.000001;
- /*std::cout << "DenseMaxshift:" << Card_Array.DenseMaxshift << "DenseMinshift:" << Card_Array.DenseMinshift
- << std::endl;
- std::cout << "DenseMinLoad:" << Card_Array.DenseMinLoad << "DenseMaxLoad:" << Card_Array.DenseMaxLoad
- << std::endl;*/
- for (i = 0; i < Card_Array.DensePointNum; i++) {
- int Gray_x = (int) ((Card_Array.DenseCard[0][i] - Card_Array.DenseMinshift) / d_densecardshift *
- cardgray_width_value);
- if (Gray_x > cardgray_width_value) Gray_x = cardgray_width_value;
- int Gray_y1 = (int) ((Card_Array.DenseCard[1][i] - Card_Array.DenseMinLoad) / d_densecardload *
- cardgray_height_value);
- if (Gray_y1 > cardgray_height_value) Gray_y1 = cardgray_height_value;
- int Gray_y2 = (int) (Card_Array.DenseCard[1][i] / Card_Array.DenseMaxLoad * cardgray_height_value);
- if (Gray_y2 > cardgray_height_value) Gray_y2 = cardgray_height_value;
- Card_Array.CardGray[0][Gray_x][Gray_y1] = cardgray_value_usage;
- Card_Array.CardGray[1][Gray_x][Gray_y2] = cardgray_value_usage;
- }
- int block_width = cardgray_width / cardgray_col_block_num;
- int block_height = cardgray_height / cardgray_row_block_num;
- int block_area = block_width * block_height;
- for (int nlgtindex = 0; nlgtindex < cardgray_nlgt_func_count; ++nlgtindex) {
- /*std::cout << "像素两值化图像 gt:::" << std::endl;
- for (i = cardgray_height - 1; i >= 0; i--) {
- for (j = 0; j < cardgray_width; j++) {
- if (Card_Array.CardGray[nlgtindex][j][i] == 1)
- std::cout << std::setw(2) << "11";//<<Card_Array.CardGray[0][j][i];
- else std::cout << std::setw(2) << " ";
- }
- std::cout << std::endl;
- }*/
- for (i = 0; i < cardgray_height; i++) {
- for (j = 0; j < cardgray_width; j++) {
- Den_MaxDistance = 1000;
- Hui_bottom = 1000;
- Hui_upper = 1000;
- Hui_left = 1000;
- Hui_right = 1000;
- if (Card_Array.CardGray[nlgtindex][j][i] == cardgray_value_default) {
- for (k = j; k < cardgray_width; k++) {
- if (Card_Array.CardGray[nlgtindex][k][i] == cardgray_value_usage) {
- Hui_right = std::abs(k - j);
- break;
- }
- }
- for (k = i; k < cardgray_height; k++) {
- if (Card_Array.CardGray[nlgtindex][j][k] == cardgray_value_usage) {
- Hui_upper = std::abs(k - i);
- break;
- }
- }
- for (k = 0; k <= i; k++) {
- if (Card_Array.CardGray[nlgtindex][j][i - k] == cardgray_value_usage) {
- Hui_bottom = std::abs(k);
- break;
- }
- }
- for (k = 0; k <= j; k++) {
- if (Card_Array.CardGray[nlgtindex][j - k][i] != cardgray_value_default) {
- if (Hui_bottom == 1000 || Hui_upper == 1000 || Hui_right == 1000) {
- Hui_left = std::abs(Card_Array.CardGray[nlgtindex][j - k][i] - k - 1);
- } else {
- Hui_left = std::abs(Card_Array.CardGray[nlgtindex][j - k][i] + k - 1);
- }
- break;
- }
- }
- if (Hui_bottom <= Den_MaxDistance) Den_MaxDistance = Hui_bottom;
- if (Hui_upper <= Den_MaxDistance) Den_MaxDistance = Hui_upper;
- if (Hui_left <= Den_MaxDistance) Den_MaxDistance = Hui_left;
- if (Hui_right <= Den_MaxDistance) Den_MaxDistance = Hui_right;
- if (Hui_bottom == 1000 || Hui_upper == 1000 || Hui_left == 1000 || Hui_right == 1000) {
- Card_Array.CardGray[nlgtindex][j][i] = cardgray_value_usage - Den_MaxDistance;
- } else {
- Card_Array.CardGray[nlgtindex][j][i] = cardgray_value_usage + Den_MaxDistance;
- }
- }
- }
- }
- /*std::cout << "灰度加权图像:::f" << nlgtindex << "::::" << std::endl;
- for (i = cardgray_height - 1; i >= 0; i--) {
- for (j = 0; j < cardgray_width; j++) {
- std::cout << std::setw(3) << Card_Array.CardGray[nlgtindex][j][i];
- }
- std::cout << std::endl;
- }*/
- Gray_Typemax = -10000000;
- Gray_Typemin = 10000000;
- for (int rowbolockindex = 0; rowbolockindex < cardgray_col_block_num; ++rowbolockindex) {
- for (int colbolockindex = 0; colbolockindex < cardgray_row_block_num; ++colbolockindex) {
- int cardgray_block_x_start = block_width * rowbolockindex;
- int cardgray_block_x_end = block_width * (rowbolockindex + 1);
- int cardgray_block_y_start = block_height * colbolockindex;
- int cardgray_block_y_end = block_height * (colbolockindex + 1);
- std::map<int, int> grayvalue_counter;
- for (int x = cardgray_block_x_start; x < cardgray_block_x_end; ++x) {
- for (int y = cardgray_block_y_start; y < cardgray_block_y_end; ++y) {
- int temp = Card_Array.CardGray[nlgtindex][x][y];
- if (grayvalue_counter.find(temp) != grayvalue_counter.end())
- grayvalue_counter[temp] += 1;
- else
- grayvalue_counter.insert(std::pair<int, int>(temp, 1));
- }
- }
- std::map<int, double> grayvalue_probability;
- std::vector<double> cha_block;
- double cha0 = 0;
- for (std::map<int, int>::iterator grayvalue_counter_item = grayvalue_counter.begin();
- grayvalue_counter_item != grayvalue_counter.end(); grayvalue_counter_item++) {
- double temp = grayvalue_counter_item->second * 1.0 / block_area;
- grayvalue_probability.insert(std::pair<int, double>(grayvalue_counter_item->first, temp));
- cha0 += grayvalue_counter_item->first * temp;
- }
- cha_block.push_back(cha0);
- double cha1 = 0, cha2 = 0, cha3 = 0, cha4 = 0, cha5 = 0;
- for (std::map<int, double>::iterator grayvalue_probability_item = grayvalue_probability.begin();
- grayvalue_probability_item != grayvalue_probability.end(); grayvalue_probability_item++) {
- cha1 += std::pow(grayvalue_probability_item->first - cha0, 2) *
- grayvalue_probability_item->second;
- cha2 += std::pow(grayvalue_probability_item->first - cha0, 3) *
- grayvalue_probability_item->second;
- cha3 += std::pow(grayvalue_probability_item->first - cha0, 4) *
- grayvalue_probability_item->second;
- cha4 += std::pow(grayvalue_probability_item->second, 2);
- cha5 += (1 - grayvalue_probability_item->second) *
- std::log10(1 - grayvalue_probability_item->second);
- }
- cha2 = cha2 / std::pow(cha1, 3 / 2.0);
- cha3 = cha3 / std::pow(cha1, 2);
- cha_block.push_back(cha1);
- cha_block.push_back(cha2);
- cha_block.push_back(cha3);
- cha_block.push_back(cha4);
- cha_block.push_back(cha5);
- int cha_start_point_index = nlgtindex * cardgray_col_block_num * cardgray_row_block_num;
- cha_start_point_index += rowbolockindex * cardgray_col_block_num + colbolockindex;
- cha_start_point_index *= cardgray_block_cha_num;
- for (int cha_l = 0; cha_l < cha_block.size(); ++cha_l) {
- if (cha_block[cha_l] > Gray_Typemax) Gray_Typemax = cha_block[cha_l];
- if (cha_block[cha_l] < Gray_Typemin) Gray_Typemin = cha_block[cha_l];
- Card_Array.CardType[cha_start_point_index + cha_l] = cha_block[cha_l];
- }
- }
- }
- /* std::cout << "cha&&" << nlgtindex << std::endl;
- std::cout << "max cha:" << Gray_Typemax << " min cha:" << Gray_Typemin << std::endl;
- std::stringstream ss1;
- std::stringstream ss2;*/
- for (int chacc_l = nlgtindex * cardgray_row_block_num * cardgray_col_block_num * cardgray_block_cha_num;
- chacc_l < (nlgtindex + 1) * cardgray_row_block_num * cardgray_col_block_num *
- cardgray_block_cha_num; ++chacc_l) {
- Card_Array.CardType[chacc_l] =
- (Card_Array.CardType[chacc_l] - Gray_Typemin) / (Gray_Typemax - Gray_Typemin);
- /* ss2 << std::setw(10) << chacc_l << ";";
- ss1 << std::setw(10) << Card_Array.CardType[chacc_l] << ";";*/
- }
- /* std::cout << ss2.str() << std::endl;
- std::cout << ss1.str() << std::endl;
- std::cout << std::endl << "cha eeend" << nlgtindex << std::endl;*/
- }
- //printf("Here you are outgoing!\n");
- //return Card_Array.CardType;
- }
- return Card_Array.CardType;
- }
- double gtcvcore::calgray::compareCharacters(double *s_cha, double *k_cha, int method) {
- // method = 23;
- if (s_cha == NULL || k_cha == NULL) return 0;
- int i = 0;
- double l_res1 = 0.0;
- //double L_EPS1=0;// unused variable
- //double L_EPS2=0;// unused variable
- double Com_max = -200.00;
- double Com_min = 200.01;
- double Mid_Value1 = 0;
- double Mid_Value2 = 0;
- double L_DLT1 = 0;
- double L_DLT2 = 0;
- double l_res2 = 0;
- double d_cha[cardgray_characters_num] = {0};
- switch (method) {
- case 23: {
- Com_max = -200.00;
- Com_min = 200.01;
- //L_EPS1=0;
- //L_EPS2=0;
- Mid_Value1 = 0;
- Mid_Value2 = 0;
- /*std::string c1="cha1:";
- std::string c2="cha2:";*/
- for (i = 0; i <= method; i++) {
- /* c1+=std::to_string(s_cha[i])+",";
- c2+=std::to_string(k_cha[i])+",";*/
- d_cha[i] = std::abs(s_cha[i] - k_cha[i]);
- if (d_cha[i] >= Com_max) Com_max = d_cha[i];
- if (d_cha[i] <= Com_min) Com_min = d_cha[i];
- }
- /*std::cout<<c1<<std::endl;
- std::cout<<c2<<std::endl;*/
- if (Com_max == Com_min) l_res1 = 1;
- else {
- for (i = 0; i <= method; i++) {
- Mid_Value1 = Mid_Value1 + 1 / (d_cha[i] + Com_max / 2);
- Mid_Value2 = Mid_Value2 + d_cha[i];
- }
- //L_EPS1=(Com_min+Com_max/2)/(method+1)*Mid_Value1;
- L_DLT1 = 1.0 / (1 + Mid_Value2 / (method + 1));
- l_res1 = L_DLT1;
- }
- //L_EPS2=0;
- L_DLT2 = 0;
- l_res2 = 0;
- Mid_Value1 = 0;
- Mid_Value2 = 0;
- Com_max = -200.00;
- Com_min = 200.01;
- for (i = method + 1; i <= 47; i++) {
- d_cha[i] = std::abs(s_cha[i] - k_cha[i]);
- if (d_cha[i] >= Com_max) Com_max = d_cha[i];
- if (d_cha[i] <= Com_min) Com_min = d_cha[i];
- }
- if (Com_max == Com_min) l_res2 = 1;
- else {
- for (i = method + 1; i <= 47; i++) {
- Mid_Value1 = Mid_Value1 + 1 / (d_cha[i] + Com_max / 2);
- Mid_Value2 = Mid_Value2 + d_cha[i];
- }
- //L_EPS2=(Com_min+Com_max/2)/(47-method)*Mid_Value1;
- L_DLT2 = 1 / (1 + Mid_Value2 / (47 - method));
- l_res2 = L_DLT2;
- }
- l_res1 = l_res1 * 0.8 + l_res2 * 0.2;
- return l_res1;
- }
- }
- return l_res1;
- }
- double
- gtcvcore::calgray::C_GTSimillar_Gray(std::vector<std::vector<double>> gt1, int pointnum1, double S1, double N1,
- std::vector<std::vector<double>> gt2, int pointnum2, double S2, double N2) {
- double *cha1 = cal_cardgray_cha(gt1, pointnum1, S1, N1);
- double *cha2 = cal_cardgray_cha(gt2, pointnum2, S2, N2);
- return compareCharacters(cha1, cha2);
- }
|