private HashSet<float[]> symbolRecognition(out Image<Gray, Byte> gElement, string path)
{
#region
Stopwatch watch = Stopwatch.StartNew();
ArrayList allMatches = new ArrayList();
Tuple<Image<Bgr, byte>, float[]> drawResult;
float[] recStat;
// Handling file names.
string topic = "";
//TextWriter log = File.AppendText(path + topic + "/log.txt");
//path = "C:/Emgu/emgucv-windows-universal-cuda 2.9.0.1922/Emgu.CV.Example/Strabo_Map_Processing/Hollywood";
// Read images.
Image<Bgr, Byte> element = new Image<Bgr, Byte>(string.Format("{0}{1}/element.png", _rasterInfo.rasterIn, topic));
Image<Bgr, Byte> test = new Image<Bgr, Byte>(string.Format("{0}{1}", _rasterInfo.ratserImgPath, topic));
Bitmap window = test.ToBitmap();
// Convert to gray-level images and save.
gElement = element.Convert<Gray, Byte>();
Image<Gray, Byte> gTest = test.Convert<Gray, Byte>();
gElement.Save(string.Format("{0}{1}/in/g-element.png", path, topic));
gTest.Save(string.Format("{0}{1}/in/g-test.png", path, topic));
// Get image dimensions.
int wfactor = 2;
// The size of the element image.
int ex = element.Width;
int ey = element.Height;
// The size of the test image.
int tx = test.Width;
int ty = test.Height;
// The distance that the sliding window shifts.
int xshift = tx / ex / wfactor * 2 - 1;
int yshift = ty / ey / wfactor * 2 - 1;
Image<Bgr, Byte> pTest;
//log.WriteLine(string.Format("Element Image: ({0}*{1})\nTest Image:({2}*{3})\n", ex, ey, tx, ty));
try
{
for (int j = 0; j < yshift; j++)
{
for (int i = 0; i < xshift; i++)
{
int xstart = i * ex * wfactor / 2;
int ystart = j * ey * wfactor / 2;
int counter = i + j * xshift;
Rectangle r = new Rectangle(xstart, ystart, ex * wfactor, ey * wfactor);
pTest = new Image<Bgr, Byte>(window.Clone(r, window.PixelFormat));
pTest.Save(string.Format("{0}{1}/in/part.jpg", path, topic));
try
{
drawResult = Strabo.Core.SymbolRecognition.DrawMatches.Draw(gElement, pTest.Convert<Gray, Byte>(), test, xstart, ystart, path, topic, counter, null);
}
catch (Exception)
{
throw;
}
#region Sima test
//long matchTime;
//Image<Bgr, byte> resultmatches = SURFFeatureExample.DrawMatches.Draw(gElement, pTest.Convert<Gray, Byte>(), out matchTime);
//resultmatches.Save(@"C:\result\"+i + ".png");
// ImageViewer.Show(resultmatches, String.Format("Matched using {0} in {1} milliseconds", GpuInvoke.HasCuda ? "GPU" : "CPU", matchTime));
#endregion
// log.WriteLine(string.Format("\n\nSub-image #{0}:\n\tLoop #({1}, {2})\n\tSW1 location: ({3}, {4})", counter, i, j, xstart, ystart));
test = drawResult.Item1;
recStat = drawResult.Item2;
if (recStat[2] > 0)
{
allMatches.Add(recStat);
//log.WriteLine(string.Format("\n\tSW2 location: ({0}, {1})\n\tHistogram score: {2}]", recStat[0], recStat[1], recStat[2]));
}
}
}
}
catch (Exception)
{
throw;
}
// log.WriteLine("The count before consolidation: " + allMatches.Count);
HashSet<float[]> hash0 = consolidate(allMatches, gElement.Width - 1, gElement.Height - 1, null);
ArrayList al = new ArrayList();
foreach (float[] i in hash0)
{
al.Add(i);
}
HashSet<float[]> hash = consolidate(al, gElement.Width - 1, gElement.Height - 1, null);
// log.WriteLine("The count after consolidation: " + hash.Count);
//Blue
TextWriter coordinatesOnMapBlue = File.AppendText(path + topic + "/coordinatesOnMapBlue.txt");
int k = 0;
foreach (float[] i in hash)
{
test.Draw(new Rectangle(new System.Drawing.Point((int)i[0], (int)i[1]), gElement.Size), new Bgr(Color.Red), 5);
coordinatesOnMapBlue.WriteLine("x[" + k + "]= " + (int)i[0] + ", y[" + k + "]= " + (int)i[1] + "");
coordinatesOnMapBlue.WriteLine("x[" + k + "]= " + (int)i[0] + gElement.Width + ", y[" + k + "]= " + (int)i[1] + "");
coordinatesOnMapBlue.WriteLine("x[" + k + "]= " + (int)i[0] + ", y[" + k + "]= " + (int)i[1] + gElement.Height + "");
coordinatesOnMapBlue.WriteLine("x[" + k + "]= " + (int)i[0] + gElement.Width + ", y[" + k + "]= " + (int)i[1] + gElement.Height + "");
k++;
}
coordinatesOnMapBlue.Close();
test.Save(string.Format("{0}{1}/out.jpg", path, topic));
watch.Stop();
//log.WriteLine(watch.Elapsed);
//log.Close();
return hash;
#endregion
}