int main(int argc, char *argv[]) { if (argc != 4) { cout << "Usage: " << endl; cout << "ACCV2009.exe Folder imfn1 imfn2" << endl; return -1; } string folder = argv[1]; string imfn1 = argv[2]; string imfn2 = argv[3]; string outfolder = "../output"; _mkdir(outfolder.c_str()); Mat im1 = imread(folder + imfn1 + ".png", 1); Mat im2 = imread(folder + imfn2 + ".png", 1); int width = im1.cols; int height = im1.rows; //颜色空间转换 Mat im1_H, im1_S, im1_I; Mat im2_H, im2_S, im2_I; //CvtColorBGR2HSI(im1, im1_H, im1_S, im1_I); //CvtColorBGR2HSI(im2, im2_H, im2_S, im2_I); cv_CvtColorBGR2HSI(im1, im1_H, im1_S, im1_I); cv_CvtColorBGR2HSI(im2, im2_H, im2_S, im2_I); int sigmaS = 5; float sigmaR = 10; int minR = 800; vector<int> labels1(0), labels2(0); //#define RQ_DEBUG #ifdef RQ_DEBUG int regionum1, regionum2; DoMeanShift(im1, sigmaS, sigmaR, minR, labels1, regionum1); DoMeanShift(im2, sigmaS, sigmaR, minR, labels2, regionum2); cout << endl; saveLabels(labels1, width, height, outfolder+"/labels_" + imfn1 + ".txt"); saveLabels(labels2, width, height, outfolder+"/labels_" + imfn2 + ".txt"); #else int regionum1, regionum2; readLabels(outfolder+"/labels_" + imfn1 + ".txt", width, height, labels1, regionum1); readLabels(outfolder+"/labels_" + imfn2 + ".txt", width, height, labels2, regionum2); #endif string siftmatchfn = folder + "matches_sift.txt"; vector<Point2f> features1(0), features2(0); ReadSiftMatches(siftmatchfn, features1, features2); vector<vector<Point2f>> matches1(0), matches2(0); vector<vector<Point2f>> pixelTable2(0); ComputeRegionMatches(labels2, regionum2, width, features1, features2, matches1, matches2); cout << "all regions have matches." << endl << endl; //显示每个区域的matches : 显示一次就好 vector<float> DeltaH(0), DeltaS(0), DeltaI(0); RegionDeltaColor(im1_H, im2_H, matches1, matches2, DeltaH); RegionDeltaColor(im1_S, im2_S, matches1, matches2, DeltaS); RegionDeltaColor(im1_I, im2_I, matches1, matches2, DeltaI); Mat new_im2_H, new_im2_S, new_im2_I; CorrectColor(im2_H, labels2, DeltaH, new_im2_H); CorrectColor(im2_S, labels2, DeltaS, new_im2_S); CorrectColor(im2_I, labels2, DeltaI, new_im2_I); Mat new_im2; //CvtColorHSI2BGR(new_im2_H, new_im2_S, new_im2_I, new_im2); cv_CVtColorHSI2BGR(new_im2_H, new_im2_S, new_im2_I, new_im2); cout << "done." << endl; imshow("new im2", new_im2); waitKey(0); destroyAllWindows(); string savefn = outfolder + "/accv2009_" + imfn2 + ".png"; cout << "save " << savefn << endl; imwrite(savefn, new_im2); }
void RGBtoHSBService (double dRed, double dGreen, double dBlue, double &rdHue, double &rdSat, double &rdBright) { // 1. Helligkeit berechnen double dYR = HelligkeitRot(dRed); double dYG = HelligkeitGruen(dGreen); double dYB = HelligkeitBlau(dBlue); double dY = dYR + dYG + dYB; // Gesamtluminanz rdBright = Wahrnehmung(dY); // 2. Sättigung berechnen double dYRGrau = BEZUGSWERT_ROT*dY; // anteilige Helligkeiten des zugehörigen Grauwertes double dYGGrau = BEZUGSWERT_GRUEN*dY; double dYBGrau = BEZUGSWERT_BLAU*dY; HIGHLOW rgStatus = HIGHLOW_UNKNOWN; TARGETCOLOR rgTarget = TARGETCOLOR_UNKNOWN; // 2.1. testen, ob eine der Farbanteile bereits gesättigt ist double dYRSatt = dYR; double dYGSatt = dYG; double dYBSatt = dYB; int fSaturatedRot = IsSaturated (dYR, BEZUGSWERT_ROT); int fSaturatedGruen = IsSaturated (dYG, BEZUGSWERT_GRUEN); int fSaturatedBlau = IsSaturated (dYB, BEZUGSWERT_BLAU); int fSaturatedSum = fSaturatedRot & fSaturatedGruen; fSaturatedSum &= fSaturatedBlau; if (fSaturatedSum) { // weiß oder schwarz rdSat = 0.0; // Farbe ist vollständig entsättigt rdHue = 0.0; // per Definition return; } if (fSaturatedRot) { rgStatus = (0.0 == dYR) ? HIGHLOW_LOW : HIGHLOW_HIGH; rgTarget = TARGETCOLOR_RED; rdSat = 1.0; // volle Sättigung } else if (fSaturatedGruen) { rgStatus = (0.0 == dYG) ? HIGHLOW_LOW : HIGHLOW_HIGH; rgTarget = TARGETCOLOR_GREEN; rdSat = 1.0; // volle Sättigung } else if (fSaturatedBlau) { rgStatus = (0.0 == dYB) ? HIGHLOW_LOW : HIGHLOW_HIGH; rgTarget = TARGETCOLOR_BLUE; rdSat = 1.0; // volle Sättigung } else { // gesättigte Farbe bestimmen HIGHLOW rgRStatus = HIGHLOW_UNKNOWN; HIGHLOW rgGStatus = HIGHLOW_UNKNOWN; HIGHLOW rgBStatus = HIGHLOW_UNKNOWN; double dKR = EvalSatKoeff (dYRGrau, dYR, BEZUGSWERT_ROT, rgRStatus); double dKG = EvalSatKoeff (dYGGrau, dYG, BEZUGSWERT_GRUEN, rgGStatus); double dKB = EvalSatKoeff (dYBGrau, dYB, BEZUGSWERT_BLAU, rgBStatus); // der Farbanteil mit dem maximalen (inversen) Koeffizienten // ist die entscheidende double dKMax = __max(fabs(dKR), __max(fabs(dKG), fabs(dKB))); if (0.0 == dKMax) { rdSat = 0.0; // Farbe ist vollständig entsättigt rdHue = 0.0; // per Definition return; } else { if (dKMax == fabs(dKR)) { rgStatus = rgRStatus; rgTarget = TARGETCOLOR_RED; } else if (dKMax == fabs(dKG)) { rgStatus = rgGStatus; rgTarget = TARGETCOLOR_GREEN; } else if (dKMax == fabs(dKB)) { rgStatus = rgBStatus; rgTarget = TARGETCOLOR_BLUE; } // gesättigte Farbanteile berechnen dYRSatt = CorrectColor (dYRGrau, dYR, dKMax); dYGSatt = CorrectColor (dYGGrau, dYG, dKMax); dYBSatt = CorrectColor (dYBGrau, dYB, dKMax); if (dYRSatt != dYRGrau) rdSat = (dYR - dYRGrau)/(dYRSatt - dYRGrau); else if (dYGSatt != dYGGrau) rdSat = (dYG - dYGGrau)/(dYGSatt - dYGGrau); else if (dYBSatt != dYBGrau) rdSat = (dYB - dYBGrau)/(dYBSatt - dYBGrau); else ASSERT(false); } } ASSERT(rgTarget != TARGETCOLOR_UNKNOWN); ASSERT(rgStatus != HIGHLOW_UNKNOWN); // 3. Farbwert bestimmen double dRSatt = Farbanteil (dYRSatt, BEZUGSWERT_ROT); double dGSatt = Farbanteil (dYGSatt, BEZUGSWERT_GRUEN); double dBSatt = Farbanteil (dYBSatt, BEZUGSWERT_BLAU); if (HIGHLOW_LOW == rgStatus) { switch (rgTarget) { case TARGETCOLOR_RED: rdHue = GetHue (dGSatt, dBSatt) + 3.0; break; case TARGETCOLOR_GREEN: rdHue = GetHue (dBSatt, dRSatt) + 5.0; break; case TARGETCOLOR_BLUE: rdHue = GetHue (dRSatt, dGSatt) + 1.0; break; } } else { switch (rgTarget) { case TARGETCOLOR_RED: rdHue = GetHue (1.0-dGSatt, 1.0-dBSatt); if (rdHue < 0.0) rdHue += 6.0; break; case TARGETCOLOR_GREEN: rdHue = GetHue (1.0-dBSatt, 1.0-dRSatt) + 2.0; break; case TARGETCOLOR_BLUE: rdHue = GetHue (1.0-dRSatt, 1.0-dGSatt) + 4.0; break; } } rdHue *= 60.0; while (rdHue >= 360.0) rdHue -= 360.0; }