我正在尝试根据直方图的相似性进行某种图像排名。我拍摄了一张图像,需要将其直方图与图像数据库进行比较,根据与源图像的相似程度对它们进行排序。这应该像过滤器一样工作,获取具有最相似图像的子组,然后将它们与其他更准确且计算成本更高的方法(模式匹配、SURF 等)进行比较。
这背后的想法是,例如,一些图像有很多蓝色,而在库中有 6 个图像有很多蓝色,所以它会将这些图像排名更高。其他图像有很多黄色(蓝色和绿色)......
此时我的代码是这样的:
Image<Bgr, byte> colorCard = frame.Copy();
DenseHistogram histBlue = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
DenseHistogram histRed = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
DenseHistogram histGreen = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
Image<Gray, byte> imgBlue = colorCard[0];
Image<Gray, byte> imgRed = colorCard[1];
Image<Gray, byte> imgGreen = colorCard[2];
imgBlue._EqualizeHist();
imgRed._EqualizeHist();
imgGreen._EqualizeHist();
//Also tried whithout equalizing histograms
histBlue.Calculate(new Image<Gray, byte>[] { imgBlue }, true, null);
histRed.Calculate(new Image<Gray, byte>[] { imgRed }, true, null);
histGreen.Calculate(new Image<Gray, byte>[] { imgGreen }, true, null);
List<Match> matchList = new List<Match>();
foreach (String filename in image_paths)
{
Image<Bgr, byte> imgToCompare = new Image<Bgr, byte>(filename);
imgToCompare = imgToCompare.PyrDown().PyrUp().PyrDown().PyrUp();
DenseHistogram histBlueToCompare = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
DenseHistogram histRedToCompare = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
DenseHistogram histGreenToCompare = new DenseHistogram(256, new RangeF(0.0f, 255.0f));
Image<Gray, byte> imgBlueToCompare = colorCard[0];
Image<Gray, byte> imgRedToCompare = colorCard[1];
Image<Gray, byte> imgGreenToCompare = colorCard[2];
imgBlueToCompare._EqualizeHist();
imgRedToCompare._EqualizeHist();
imgGreenToCompare._EqualizeHist();
histBlueToCompare.Calculate(new Image<Gray, byte>[] { imgBlueToCompare }, true, null);
histRedToCompare.Calculate(new Image<Gray, byte>[] { imgRedToCompare }, true, null);
histGreenToCompare.Calculate(new Image<Gray, byte>[] { imgGreenToCompare }, true, null);
double cBlue = CvInvoke.cvCompareHist(histBlue, histBlueToCompare, Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD.CV_COMP_CORREL);
double cRed = CvInvoke.cvCompareHist(histRed, histRedToCompare, Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD.CV_COMP_CORREL);
double cGreen = CvInvoke.cvCompareHist(histGreen, histGreenToCompare, Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD.CV_COMP_CORREL);
double matchValue = (cBlue + cGreen + cRed) / 3.0;
matchList.Add(new Match(matchValue, Path.GetFileNameWithoutExtension(filename)));
}
matchList = matchList.OrderBy(X => X.MatchValue).ToList<Match>();
foreach (Match m in matchList)
{
Logger.Log(m.Card + ": " + m.MatchValue);
}
我可以比较每个颜色直方图,但不知道如何合并这些比较以获得单个值。女巫(cBlue + cGreen + cRed) / 3.0
我没有得到好的结果。
我读了一种方法来做到这一点是地球移动距离(EMD)。EmguCV 有一个名为 的函数cvCalcEMD2
,但我不知道如何使用它(参数是什么意思),也找不到它的用法示例。