Я хотел бы получить ваш совет относительно того, почему код может перестать отвечать на запросы и как его исправить.Accord.NET Сравнение двух изображений для определения сходства
Я использую Accord.NET для сравнения изображений. Первый этап моего проекта - сравнить два изображения, наблюдаемое изображение и образ модели и определить, насколько они похожи; во-вторых, сравнить наблюдаемое изображение с моей всей базой данных, чтобы определить, какое из наблюдаемых изображений, скорее всего, основано на том, как модели были классифицированы. Сейчас я сосредоточен на первом. Сначала я попытался использовать ExhaustiveTemplateMatching.ProcessImage(), но это не соответствовало моей потребности. Теперь я использую SURF. Вот мой код, как это:
public class ProcessImage
{
public static void Similarity(System.IO.Stream model, System.IO.Stream observed,
out float similPercent)
{
Bitmap bitModel = new Bitmap(model);
Bitmap bitObserved = new Bitmap(observed);
// For method Difference, see http://www.aforgenet.com/framework/docs/html/673023f7-799a-2ef6-7933-31ef09974dde.htm
// Inspiration for this process: https://www.youtube.com/watch?v=YHT46f2244E
// Greyscale class http://www.aforgenet.com/framework/docs/html/d7196dc6-8176-4344-a505-e7ade35c1741.htm
// Convert model and observed to greyscale
Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
// apply the filter to the model
Bitmap greyModel = filter.Apply(bitModel);
// Apply the filter to the observed image
Bitmap greyObserved = filter.Apply(bitObserved);
int modelPoints = 0, matchingPoints = 0;
/*
* This doesn't work. Images can have different sizes
// For an example, https://thecsharper.com/?p=94
// Match
var tm = new ExhaustiveTemplateMatching(similarityThreshold);
// Process the images
var results = tm.ProcessImage(greyModel, greyObserved);
*/
using (SpeededUpRobustFeaturesDetector detector = new SpeededUpRobustFeaturesDetector())
{
List<SpeededUpRobustFeaturePoint> surfModel = detector.ProcessImage(greyModel);
modelPoints = surfModel.Count();
List<SpeededUpRobustFeaturePoint> surfObserved = detector.ProcessImage(greyObserved);
KNearestNeighborMatching matcher = new KNearestNeighborMatching(5);
var results = matcher.Match(surfModel, surfObserved);
matchingPoints = results.Length;
}
// Determine if they represent the same points
// Obtain the pairs of associated points, we determine the homography matching all these pairs
// Compare the results, 0 indicates no match so return false
if (matchingPoints <= 0)
{
similPercent = 0.0f;
}
similPercent = (matchingPoints * 100)/modelPoints;
}
}
До сих пор я получаю, чтобы получить список точек, а затем при совпадении кода, кажется, перестает отвечать на запросы.
Я вызываю вышеуказанный код с веб-страницы ASP.NET после того, как пользователь отправляет растровое изображение. Вот код, если это может помочь:
public ActionResult Compare(int id)
{
ViewData["SampleID"] = id;
return View();
}
[HttpPost]
public ActionResult Compare(int id, HttpPostedFileBase uploadFile)
{
Sample model = _db.Sample_Read(id);
System.IO.Stream modelStream = null;
float result = 0;
_db.Sample_Stream(model.FileId, out modelStream);
ImgProc.ProcessImage.Similarity(modelStream, uploadFile.InputStream,
out result);
ViewData["SampleID"] = id;
ViewData["match"] = result;
return View();
}
Сама страница довольно проста, скрытое поле, входной тип файла и нажмите кнопку.
Вы должны запустить этот процесс в другой поток, чтобы не блокировать поток приложений с огромной обработкой. –