本文整理汇总了C#中Image.MatchTemplate方法的典型用法代码示例。如果您正苦于以下问题:C# Image.MatchTemplate方法的具体用法?C# Image.MatchTemplate怎么用?C# Image.MatchTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Image
的用法示例。
在下文中一共展示了Image.MatchTemplate方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Detect_objects
private bool Detect_objects(Image<Gray, Byte> Input_Image, Image<Gray, Byte> object_Image)
{
Point dftSize = new Point(Input_Image.Width + (object_Image.Width * 2), Input_Image.Height + (object_Image.Height * 2));
bool Success = false;
using (Image<Gray, Byte> pad_array = new Image<Gray, Byte>(dftSize.X, dftSize.Y))
{
//copy centre
pad_array.ROI = new Rectangle(object_Image.Width, object_Image.Height, Input_Image.Width, Input_Image.Height);
CvInvoke.cvCopy(Input_Image.Convert<Gray, Byte>(), pad_array, IntPtr.Zero);
// CvInvoke.cvMatchTemplate
//CvInvoke.cvShowImage("pad_array", pad_array);
pad_array.ROI = (new Rectangle(0, 0, dftSize.X, dftSize.Y));
using (Image<Gray, float> result_Matrix = pad_array.MatchTemplate(object_Image, TM_TYPE.CV_TM_CCOEFF_NORMED))
{
result_Matrix.ROI = new Rectangle(object_Image.Width, object_Image.Height, Input_Image.Width, Input_Image.Height);
Point[] MAX_Loc, Min_Loc;
double[] min, max;
result_Matrix.MinMax(out min, out max, out Min_Loc, out MAX_Loc);
using (Image<Gray, double> RG_Image = result_Matrix.Convert<Gray, double>().Copy())
{
//#TAG WILL NEED TO INCREASE SO THRESHOLD AT LEAST 0.8...used to have 0.7
if (max[0] > 0.85)
{
Object_Location = MAX_Loc[0];
Success = true;
}
}
}
}
return Success;
}
示例2: AreImagesSame
public static bool AreImagesSame(Image<Gray, Byte> inputImage, Image<Gray, Byte> templateImage, double comparisonFactor)
{
bool Success = false;
try
{
//LogHelper.logger.Info("AreImagesSame called...");
//Point Object_Location = new Point();
Point dftSize = new Point(inputImage.Width + (templateImage.Width * 2), inputImage.Height + (templateImage.Height * 2));
using (Image<Gray, Byte> pad_array = new Image<Gray, Byte>(dftSize.X, dftSize.Y))
{
//copy centre
pad_array.ROI = new Rectangle(templateImage.Width, templateImage.Height, inputImage.Width, inputImage.Height);
CvInvoke.cvCopy(inputImage.Convert<Gray, Byte>(), pad_array, IntPtr.Zero);
//CvInvoke.cvShowImage("pad_array", pad_array);
pad_array.ROI = (new Rectangle(0, 0, dftSize.X, dftSize.Y));
using (Image<Gray, float> result_Matrix = pad_array.MatchTemplate(templateImage, TM_TYPE.CV_TM_CCOEFF_NORMED))
{
result_Matrix.ROI = new Rectangle(templateImage.Width, templateImage.Height, inputImage.Width, inputImage.Height);
Point[] MAX_Loc, Min_Loc;
double[] min, max;
result_Matrix.MinMax(out min, out max, out Min_Loc, out MAX_Loc);
using (Image<Gray, double> RG_Image = result_Matrix.Convert<Gray, double>().Copy())
{
//#TAG WILL NEED TO INCREASE SO THRESHOLD AT LEAST 0.8
if (max[0] > comparisonFactor)
//if (max[0] > 0.75)
{
//Object_Location = MAX_Loc[0];
Success = true;
}
}
}
}
}
catch (Exception exception)
{
LogHelper.logger.Error("GetNextBestMove: " + exception.Message);
LogHelper.logger.Error("GetNextBestMove: " + exception.StackTrace);
MessageBox.Show("An error occurred. Please restart bot", "Chessbot", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//LogHelper.logger.Info("AreImagesSame finished...");
return Success;
}
示例3: TestMatchTemplate
public void TestMatchTemplate()
{
#region prepare synthetic image for testing
int templWidth = 50;
int templHeight = 50;
Point templCenter = new Point(120, 100);
//Create a random object
Image<Bgr, Byte> randomObj = new Image<Bgr, byte>(templWidth, templHeight);
randomObj.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
//Draw the object in image1 center at templCenter;
Image<Bgr, Byte> img = new Image<Bgr, byte>(300, 200);
Rectangle objectLocation = new Rectangle(templCenter.X - (templWidth >> 1), templCenter.Y - (templHeight >> 1), templWidth, templHeight);
img.ROI = objectLocation;
randomObj.Copy(img, null);
img.ROI = Rectangle.Empty;
#endregion
Image<Gray, Single> match = img.MatchTemplate(randomObj, Emgu.CV.CvEnum.TM_TYPE.CV_TM_SQDIFF);
double[] minVal, maxVal;
Point[] minLoc, maxLoc;
match.MinMax(out minVal, out maxVal, out minLoc, out maxLoc);
Assert.AreEqual(minLoc[0].X, templCenter.X - templWidth / 2);
Assert.AreEqual(minLoc[0].Y, templCenter.Y - templHeight / 2);
}
示例4: findAndAnalyzeMissionSummaryPage
public MissionInfo findAndAnalyzeMissionSummaryPage(string pathToBmp, bool includeImages = false)
{
Image<Gray, byte> source = null;
int tries = 5;
while (tries > 0)
{
try
{
source = new Image<Gray, byte>(pathToBmp);
tries -= 1;
Thread.Sleep(300);
break;
}
catch (Exception ex){}
}
double widthFactor = ((double)source.Width / (double)baseSize.Width);
double heightFactor = ((double)source.Height / (double)baseSize.Height);
Image<Gray, byte> yearTemplate = new Image<Gray, byte>("Assets\\3302_" + language.Code + ".bmp"); // Image A
yearTemplate = yearTemplate.Resize(widthFactor, Emgu.CV.CvEnum.Inter.Cubic);
Image<Gray, byte> rewardTemplate = new Image<Gray, byte>("Assets\\reward_" + language.Code + ".bmp"); // Image A
rewardTemplate = rewardTemplate.Resize(widthFactor, Emgu.CV.CvEnum.Inter.Cubic);
MissionInfo missionInfo = new MissionInfo();
try {
using (Image<Gray, float> result = source.MatchTemplate(yearTemplate, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
{
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
if (maxValues[0] > 0.7)
{
//mission name
var match = new Rectangle(new Point(maxLocations[0].X + yearTemplate.Width + (int)(105 * widthFactor), maxLocations[0].Y - (int)(25 * heightFactor)),
new Size((int)(650 * widthFactor), (int)(40 * heightFactor)));
if (match.X + match.Width > source.Width || match.Y + match.Height > source.Height)
{
return null;
}
var missionRegion = source.GetSubRect(match);
try {
//match = new Rectangle(new Point(maxLocations[0].X, maxLocations[0].Y), new Size(yearTemplate.Width, yearTemplate.Height));
//imageToShow.Draw(match, new Gray(255), 3);
//imageToShow.ToBitmap().Save("E:\\test.bmp");
if (missionRegion.Width < 850)
missionRegion = missionRegion.Resize((double)850 / missionRegion.Width, Emgu.CV.CvEnum.Inter.Cubic);
ocr.Recognize(missionRegion.Convert<Gray, byte>());
var words = ocr.GetText();
var missionName = words.Replace("\r\n", " ").Trim();
Trace.TraceInformation("Mission name: " + missionName);
missionInfo.MissionName = missionName;
if (includeImages)
{
missionInfo.Images.Add(missionRegion.ToBitmap());
}
}
finally
{
missionRegion.Dispose();
}
}
}
source = source.GetSubRect(new Rectangle((int)(450*widthFactor), 0, source.Width - (int)(450*widthFactor) - 10, source.Height));
using (Image<Gray, float> result = source.MatchTemplate(rewardTemplate, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
{
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
if (maxValues[0] > 0.65)
{
int reward = 0;
// reward
var match = new Rectangle(new Point(maxLocations[0].X + rewardTemplate.Width,
maxLocations[0].Y),
new Size((int)(210 * widthFactor), rewardTemplate.Height));
if (match.X + match.Width > source.Width || match.Y + match.Height > source.Height)
{
return missionInfo;
}
var rewardRegion = source.GetSubRect(match);
try
{
if (rewardRegion.Height < 20)
rewardRegion = rewardRegion.Resize((double)20 / rewardRegion.Height, Emgu.CV.CvEnum.Inter.Cubic);
ocr.Recognize(rewardRegion.Convert<Gray, byte>());
var words = ocr.GetText();
int.TryParse(Regex.Replace(words, @"[^0-9$]", "").Trim(), out reward);
Trace.TraceInformation("Reward: " + reward);
missionInfo.Reward = reward;
}
finally
{
rewardRegion.Dispose();
}
}
}
source = source.GetSubRect(new Rectangle(0, 0, source.Width, source.Height - (int)(600*heightFactor)));
// find mission type
var missionTypePatterns = Directory.GetFiles("Assets\\mTypes", "*.bmp");
//.........这里部分代码省略.........
示例5: ImageMatch
/// <summary>
/// Check the model image and observed image, the matched features.
/// </summary>
/// <param name="modelImageFileName">The model image</param>
/// <param name="observedBitmap">The observed image</param>
/// <param name="rate">similarity</param>
/// <returns>The center point object of matched image</returns>
private System.Drawing.Point ImageMatch(String modelImageFileName, Bitmap observedBitmap, double rate = 0.90)
{
double[] min_val;
double[] max_val;
System.Drawing.Point[] min_loc;
System.Drawing.Point[] max_loc;
try
{
Bitmap modelImageBitmap = new Bitmap(modelImageFileName);
Image<Gray, Byte> modelImage = new Image<Gray, byte>(modelImageFileName);
Image<Gray, Byte> observedImage = new Image<Gray, byte>(observedBitmap);
Image<Gray, float> result = observedImage.MatchTemplate(modelImage, TM_TYPE.CV_TM_CCORR_NORMED);
Image<Gray, float> resultPow = result.Pow(2);
resultPow.MinMax(out min_val, out max_val, out min_loc, out max_loc);
Console.WriteLine("max_val[0] rate : {0} : {1}", max_val[0], rate);
Console.WriteLine("max_loc[0] locatipon : {0} {1}", max_loc[0].X, max_loc[0].Y);
System.Drawing.Point targetCenter = new System.Drawing.Point(max_loc[0].X + modelImageBitmap.Width / 2, max_loc[0].Y + modelImageBitmap.Height / 2);
if (max_val[0] > rate)
return targetCenter;
else
return new System.Drawing.Point(-1, -1);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return new System.Drawing.Point(-1, -1);
}
}
示例6: getMissionType
private Tuple<string, double> getMissionType(Image<Gray, byte> sourceImage, string patternFilePath, double widthFactor, double heightFactor)
{
Image<Gray, byte> templateImage = new Image<Gray, byte>(patternFilePath); // Image A
templateImage = templateImage.Resize(widthFactor, Emgu.CV.CvEnum.Inter.Cubic);
string missionType = Path.GetFileName(patternFilePath).Replace(".bmp", "");
using (Image<Gray, float> result = sourceImage.MatchTemplate(templateImage, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
{
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
return new Tuple<string, double>(missionType, maxValues[0]);
}
}
示例7: DetectArm
public Image<Bgr, byte> DetectArm(Image<Bgr, byte> img)
{
var image = new Image<Bgr, byte>("C:/Users/Manuel/SoftwareEntwicklung/C#/HumanRemote/HumanRemote/Images/armtemplate.png");
return img.MatchTemplate(image, TM_TYPE.CV_TM_CCORR_NORMED).Convert<Bgr, byte>();
}
示例8: IsTemplateInWindow
public ScreenSearchResult IsTemplateInWindow(
Image<Bgra, byte> windowContents,
Image<Bgra, byte> template,
float tolerance = 0.95f)
{
var match = windowContents.MatchTemplate(template, Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF_NORMED);
float[, ,] matches = match.Data;
for (int y = 0; y < match.Height; y++)
{
for (int x = 0; x < match.Width; x++)
{
double matchScore = matches[y, x, 0];
if (matchScore > tolerance)
{
return new ScreenSearchResult { Success = true, Point = new Point(x, y) };
}
}
}
return new ScreenSearchResult { Success = false };
}
示例9: GetLetterForGear
private char GetLetterForGear(Image<Bgra, byte> gearImage)
{
double max = 0;
char bestCharacter = '?';
const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
foreach (var character in _gearFont.Characters.Where(x => alphabet.Contains(x.Character)))
{
var match = gearImage.MatchTemplate(character.MatchImage, Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF_NORMED);
double[] currentMins, currentMaxes;
Point[] minPoints, maxPoints;
match.MinMax(out currentMins, out currentMaxes, out minPoints, out maxPoints);
if (currentMaxes[0] > max)
{
max = currentMaxes[0];
bestCharacter = character.Character;
}
}
return bestCharacter;
}
示例10: Match
public static Point? Match(Image<Gray, byte> image, Image<Gray, byte> template)
{
Image<Gray, float> result = image.MatchTemplate(template, TemplateMatchingType.CcoeffNormed);
double[] min, max;
Point[] point1, point2;
result.MinMax(out min, out max, out point1, out point2);
return point2.Length != 0 && max[0] > 0.75 ? point2[0] : Point.Empty;
}
示例11: TestMatchTemplate
public void TestMatchTemplate()
{
if (!CudaInvoke.HasCuda)
return;
#region prepare synthetic image for testing
int templWidth = 50;
int templHeight = 50;
Point templCenter = new Point(120, 100);
//Create a random object
Image<Bgr, Byte> randomObj = new Image<Bgr, byte>(templWidth, templHeight);
randomObj.SetRandUniform(new MCvScalar(), new MCvScalar(255, 255, 255));
//Draw the object in image1 center at templCenter;
Image<Bgr, Byte> img = new Image<Bgr, byte>(300, 200);
Rectangle objectLocation = new Rectangle(templCenter.X - (templWidth >> 1), templCenter.Y - (templHeight >> 1), templWidth, templHeight);
img.ROI = objectLocation;
randomObj.Copy(img, null);
img.ROI = Rectangle.Empty;
#endregion
Image<Gray, Single> match = img.MatchTemplate(randomObj, Emgu.CV.CvEnum.TemplateMatchingType.Sqdiff);
double[] minVal, maxVal;
Point[] minLoc, maxLoc;
match.MinMax(out minVal, out maxVal, out minLoc, out maxLoc);
double gpuMinVal = 0, gpuMaxVal = 0;
Point gpuMinLoc = Point.Empty, gpuMaxLoc = Point.Empty;
GpuMat cudaImage = new GpuMat(img);
GpuMat gpuRandomObj = new GpuMat(randomObj);
GpuMat gpuMatch = new GpuMat();
using (CudaTemplateMatching buffer = new CudaTemplateMatching(DepthType.Cv8U, 3, CvEnum.TemplateMatchingType.Sqdiff))
using (Stream stream = new Stream())
{
buffer.Match(cudaImage, gpuRandomObj, gpuMatch, stream);
//GpuInvoke.MatchTemplate(CudaImage, gpuRandomObj, gpuMatch, CvEnum.TM_TYPE.CV_TM_SQDIFF, buffer, stream);
stream.WaitForCompletion();
CudaInvoke.MinMaxLoc(gpuMatch, ref gpuMinVal, ref gpuMaxVal, ref gpuMinLoc, ref gpuMaxLoc, null);
}
EmguAssert.AreEqual(minLoc[0].X, templCenter.X - templWidth / 2);
EmguAssert.AreEqual(minLoc[0].Y, templCenter.Y - templHeight / 2);
EmguAssert.IsTrue(minLoc[0].Equals(gpuMinLoc));
EmguAssert.IsTrue(maxLoc[0].Equals(gpuMaxLoc));
}