3

I got the following code to find the template in the source image:

private void button1_Click(object sender, EventArgs e)
    {
        toggleStatus();
        using (Mat template = CvInvoke.Imread("C:\\Users\\Hendr\\Desktop\\temp.png", Emgu.CV.CvEnum.ImreadModes.Grayscale))
        using (Mat source = CvInvoke.Imread("C:\\Users\\Hendr\\Desktop\\yVLsd.png", Emgu.CV.CvEnum.ImreadModes.Grayscale))
        {
            log("Image loaded into memory...");
         //   pictureBox1.Image = template.Bitmap;
            pictureBox1.Image = source.Bitmap;

            var width = source.Width - template.Width + 1;
            var height = source.Height - template.Height + 1;
            // Mat result = new Mat((new System.Drawing.Size(width,height), Emgu.CV.CvEnum.IplDepth.IplDepth32F, 1);
            Mat result = new Mat(width, height, DepthType.Cv8U, 1);

            CvInvoke.MatchTemplate(source, template, result ,Emgu.CV.CvEnum.TemplateMatchingType.SqdiffNormed);
            var THRESHOLD = 0.0;

            double minVal = 0, maxVal = 0;
            System.Drawing.Point minLoc = new Point(), maxLoc = new Point();
            CvInvoke.MinMaxLoc(result, ref minVal, ref maxVal, ref minLoc, ref maxLoc);
            Rectangle rect = new Rectangle(minLoc.X,minLoc.Y, width,height);
            var outlineColor = (minVal > THRESHOLD) ?Color.Green : Color.Red;
            CvInvoke.Rectangle(result, rect, new MCvScalar(0,0,0));

            log(rect.Location.ToString());
            pictureBox1.Image = result.Bitmap;
        }
        foreach (object itemChecked in checkedListBox1.CheckedItems)
        {
            // Use the IndexOf method to get the index of an item.
            MessageBox.Show("Item with title: \"" + itemChecked.ToString() +
                            "\", is checked. Checked state is: " +
                            checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
        }
    }

The source image looks like: enter image description here

and the respective template: enter image description here

However, my result is all blacked out:

enter image description here

I verified, that the template and the source Mat get properly loaded into the memory. See pictureBox1.Image = source.Bitmap; ....

In the end, I can't find any information from the doc, why my result Mat(pictureBox1.Image = result.Bitmap;) is black. Anyone else has/had this issue too and could give me some reference link or a quickfix?

0x45
  • 779
  • 3
  • 7
  • 26
  • What if you multiply the match image by 255 ? The result of `matchTemplate` is a floating-point single channel image with values between 0 and 1. Not the usual [0, 255] range for 8-bit images. – Sunreef Jul 05 '18 at 13:15
  • @Sunreef "multiply the match image" - what exactly do you mean? – 0x45 Jul 05 '18 at 13:18
  • Multiply the values stored in the result `Mat` by 255. Though I've never used EmguCV, so I don't know if that'll help. – Sunreef Jul 05 '18 at 13:19
  • Check the result matrix if that is black then there is some issue in implementing the matchTemplate function. – Nikita Chopra Jul 05 '18 at 18:12
  • I couldn't fix it, however I'm doing it with cpp now... – 0x45 Jul 07 '18 at 00:01

2 Answers2

0

I had the same issue when running the same example for Java. I solved it by using the data type CV_32FC1. For your C# code I think it should be:

Mat result = new Mat(width, height, DepthType.Cv32F, 1);

For full explanation of the same template matching example in c++, java, and python please check:

https://docs.opencv.org/3.4/de/da9/tutorial_template_matching.html

Sameh Yassin
  • 424
  • 3
  • 9
0

Make sure result is within range [0,255] by normalization like this:

normalize( result, result, 0, 255, NORM_MINMAX, CV_8UC1); 

how-to-use-cv2-imshow-correctly-for-the-float

corticalhazard
  • 189
  • 2
  • 6