float NNClassifier::classifyPatch(NormalizedPatch * patch) { if(truePositives->empty()) { return 0; } if(falsePositives->empty()) { return 1; } float ccorr_max_p = 0; //Compare patch to positive patches for(size_t i = 0; i < truePositives->size(); i++) { float ccorr = ncc(truePositives->at(i).values, patch->values); if(ccorr > ccorr_max_p) { ccorr_max_p = ccorr; } } float ccorr_max_n = 0; //Compare patch to positive patches for(size_t i = 0; i < falsePositives->size(); i++) { float ccorr = ncc(falsePositives->at(i).values, patch->values); if(ccorr > ccorr_max_n) { ccorr_max_n = ccorr; } } float dN = 1-ccorr_max_n; float dP = 1-ccorr_max_p; float distance = dN/(dN+dP); return distance; }
float NearestNeighbourClassifier::classify(Mat& patch) { if(positiveExamples.empty()) { return 0; } if(negativeExamples.empty()) { return 1; } float ccorr_max_p = 0; //Compare patch to positive patches for(size_t i = 0; i < positiveExamples.size(); i++) { float ccorr = ncc(positiveExamples.at(i).data, patch.data); if(ccorr > ccorr_max_p) { ccorr_max_p = ccorr; } } float ccorr_max_n = 0; //Compare patch to negative patches for(size_t i = 0; i < negativeExamples.size(); i++) { float ccorr = ncc(negativeExamples.at(i).data, patch.data); if(ccorr > ccorr_max_n) { ccorr_max_n = ccorr; } } float dN = 1 - ccorr_max_n; float dP = 1 - ccorr_max_p; float distance = dN / (dN + dP); return distance; }
void Foam::primitiveMesh::calcCellCells() const { // Loop through faceCells and mark up neighbours if (debug) { Pout<< "primitiveMesh::calcCellCells() : calculating cellCells" << endl; if (debug == -1) { // For checking calls:abort so we can quickly hunt down // origin of call FatalErrorIn("primitiveMesh::calcCellCells()") << abort(FatalError); } } // It is an error to attempt to recalculate cellCells // if the pointer is already set if (ccPtr_) { FatalErrorIn("primitiveMesh::calcCellCells() const") << "cellCells already calculated" << abort(FatalError); } else { // 1. Count number of internal faces per cell labelList ncc(nCells(), 0); const labelList& own = faceOwner(); const labelList& nei = faceNeighbour(); forAll (nei, faceI) { ncc[own[faceI]]++; ncc[nei[faceI]]++; } // Create the storage ccPtr_ = new labelListList(ncc.size()); labelListList& cellCellAddr = *ccPtr_; // 2. Size and fill cellFaceAddr forAll (cellCellAddr, cellI) { cellCellAddr[cellI].setSize(ncc[cellI]); }
void Ferns::NNConf(cv::Mat const& example, std::vector<int>& isin, float& rsconf, float& csconf){ isin = std::vector<int>(3, -1); if (pEx.empty()) { rsconf = 0; csconf = 0; return; } if (nEx.empty()) { rsconf = 1; csconf = 1; return; } cv::Mat ncc(1, 1, CV_32F); float nccP, csmaxP, maxP = 0; bool anyP = false; int maxPidx, validatedPart = std::ceil(pEx.size()*valid); float nccN, maxN = 0; bool anyN = false; for (int i=0; i<pEx.size(); i++){ cv::matchTemplate(pEx[i],example,ncc,CV_TM_CCORR_NORMED); nccP = (reinterpret_cast<float*>(ncc.data)[0]+1)*0.5; if (nccP > ncc_thesame) anyP = true; if(nccP > maxP){ maxP = nccP; maxPidx = i; if(i < validatedPart) csmaxP = maxP; } } for (int i=0;i<nEx.size();i++){ cv::matchTemplate(nEx[i],example,ncc,CV_TM_CCORR_NORMED); nccN = (reinterpret_cast<float*>(ncc.data)[0]+1)*0.5; if (nccN > ncc_thesame) anyN = true; if(nccN > maxN) maxN = nccN; } if (anyP) isin[0] = 1; isin[1] = maxPidx; if (anyN) isin[2] = 1; //Measure Relative Similarity float dN = 1-maxN; float dP = 1-maxP; rsconf = static_cast<float>(dN/(dN+dP)); //Measure Conservative Similarity dP = 1-csmaxP; csconf = static_cast<float>(dN/(dN+dP)); }
static double imgerr(char *m, float *x, float *y, int n) { double r; if (false); else if (0 == strcmp(m, "MSE")) r = mean_square_error(x, y, n); else if (0 == strcmp(m, "RMSE")) r = root_mean_square_error(x, y, n); else if (0 == strcmp(m, "MAE")) r = mean_absolute_error(x, y, n); else if (0 == strcmp(m, "UIQI")) r = uiqi(x, y, n); else if (0 == strcmp(m, "SSIM")) r = ssim(x, y, n); else if (0 == strcmp(m, "PSNR")) r = psnr(x, y, n); else if (0 == strcmp(m, "NCC")) r = ncc(x, y, n); else if (string_is_lp(m, &r)) r = ell_pee_distance(x, y, n, r); else fail("unrecognized metric \"%s\"", m); return r; }
/** * Berechnet die Ähnlichkeit (similarity) des Patches: * s_max_pos_50, die Ähnlichkeiten des Patches zu den ersten 50% der positiven Beispiele, * s_relative = s_pos / (s_pos + s_neg), die relative Ähnlichkeit, und * s_conservative = s_pos_50 / (s_pos_50 + s_neg), die konservative Ähnlichkeit berechnet * * patch: der normalisierte 15 x 15 Patch * s_conservative: der berechnete konservative Wert * s_relative: der berechnete relative Wert * isPositive: true, falls patch signifikant positiv * isNegative: true, falls patch signifikant negativ * */ void NearestNeighbourClassifier::calculateSimilarity(Mat& patch, float& s_conservative, float& s_relative, bool& isPositive, bool& isNegative) { // Falls keine positiven Beispiele existieren, dann ist alles NEGATIV => s_c = 0 if(positiveExamples.empty()) { //cout << "kein positives Beispiel, also ist alles negativ" << endl; s_conservative = 0; s_relative = 0; isNegative = true; isPositive = false; return; } // Falls keine negativen Beispiele existieren, dann ist alles POSITIV = s_c = 1 if(negativeExamples.empty()) { //cout << "kein negatives Beispiel, also ist alles positiv" << endl; s_conservative = 1; s_relative = 1; isNegative = false; isPositive = true; return; } // Ähnlichkeit ist definiert als s = .5(ncc(p,p') + 1), mit ncc = normalized correlation coefficient // die Funktion templateMatching() berechnet genau das, je nachdem, welche Funktion gewollte ist. // Hier wird immer gleich die konservative Berechnung durchgeführt. float sim; float sim_max_pos = 0; float sim_max_pos_50 = 0; float sim_max_neg = 0; size_t i = 0; int half = positiveExamples.size() / 2; //Mat result = Mat(1,1,patch.type()); //cout << "NNC: PosCount:" << positiveExamples.size() << " NegCount:" << negativeExamples.size() << endl; //int matchMethod = CV_TM_CCORR_NORMED; ///0: SQDIFF 1: SQDIFF NORMED 2: TM CCORR 3: TM CCORR NORMED 4: TM COEFF 5: TM COEFF NORMED //Iteriere über alle positiven Beispiele for(i = 0; i < positiveExamples.size(); i++) { //matchTemplate(positiveExamples[i],patch,result,matchMethod); // Das ist die Formel s = .5(ncc(p,p') + 1) //sim = .5 * (((float*)result.data)[0] + 1); /// TEST sim = .5 * (ncc(positiveExamples[i].data,patch.data ) + 1); //cout << " SIM_2:" << sim << endl; // wenn eine neue maximale Änlichkeit gefunden wurde, dann merke sie dir if (sim > sim_max_pos) { sim_max_pos = sim; if ((int) i <= half) sim_max_pos_50 = sim; } // Falls sim größer als similarity, dann ist es ein signifikant positiver Patch! if (sim > similarity) { isPositive = true; // EGAL } //imshow("Box",positiveExamples[i]); } // Nun mit negativen Beispielen vergleichen! for(i = 0; i < negativeExamples.size(); i++) { //matchTemplate(negativeExamples[i],patch,result,matchMethod); // Das ist die Formel s = .5(ncc(p,p') + 1) //sim = .5 * (((float*)result.data)[0] + 1); sim = .5 * (ncc(negativeExamples[i].data,patch.data ) + 1); // wenn eine neue maximale Änlichkeit gefunden wurde, dann merke sie dir if (sim > sim_max_neg) { sim_max_neg = sim; } // Falls sim größer als similarity, dann ist es ein Signifikant negativer Patch! if (sim > similarity) { isNegative = true; // EGAL } } //Nun werden die Distanzen berechnet float distancePos = 1 - sim_max_pos; float distanceNeg = 1 - sim_max_neg; //cout << "dist+:" << distancePos << " dist-:" << distanceNeg << endl; s_relative =(float)distanceNeg / (distanceNeg + distancePos); distancePos = 1 - sim_max_pos_50; s_conservative = (float) distanceNeg / (distanceNeg + distancePos); }