Loading image from file as given "type" into an OpenCV Mat?
<p>I recently started working with the Java bindings for OpenCV to make a quick and dirty project to do template matching. Basically I am trying to read a set of jpg images (saved in MS Paint) into <code>Mat</code>s and then use template matching to find their locations from a screen shot taken with <code>Java.Robot</code>.</p>

<p>When it comes time to do the template matching this error is thrown</p>

<pre><code>OpenCV Error: Assertion failed ((depth == CV_8U || depth == CV_32F) 
&amp;&amp; type == _templ.type() &amp;&amp; _img.dims() &lt;= 2) in cv::matchTemplate
</code></pre>

<p><a href="https://stackoverflow.com/a/31642693/2985796">After searching</a> it looks like the issue is that the two <code>Mat</code>s I am trying to use do not have the same "type". What I am not sure of is what this refers to. I assume it is the <code>Mat</code>s <code>CvType</code>, if I print out the <code>CvType</code> of the image and template I get a <code>type()</code> of <a href="http://ninghang.blogspot.com/2012/11/list-of-mat-type-in-opencv.html" rel="nofollow noreferrer"><code>4 == CvType.CV_32SC1</code></a> for my template I get a <code>type()</code> of <code>20 == CvType.CV_32SC3</code>.</p>

<p>But I feel like this is not the correct <code>type()</code> I am trying to compare, I have feeling it refers to the data type of how the data is stored in the <code>Mat</code>? But I have no good links to back this up just remembrances from many SO searches. </p>

<p>Here is my code for loading in my jpg images into a <code>Mat</code></p>

<pre><code>Mat pic_ = Imgcodecs.imread("MyPath\\image.jpg");
pic_.convertTo(pic_, CvType.CV_32SC1); 
</code></pre>

<p>Here the second line turns my <code>type()</code> from 20 to 16, though as per my last comment I don't think this is the proper way to alter the <code>Mat</code> to match the image?... Because <code>convertTo</code>'ing this <code>Mat</code> to match the type of the screen shot `(below) does not fix the error?</p>

<p>Here is how I am creating the image <code>Mat</code></p>

<pre><code>Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenShot = rob.createScreenCapture(screenRect);
Mat screenImage = bufferedImageToMat(screenShot);
</code></pre>

<p>So I first take a screenshot with <code>Java.Robot.createScreenCapture</code> I then convert it to a <code>Mat</code> with</p>

<pre><code>private Mat bufferedImageToMat(BufferedImage inBuffImg) 
{
    BufferedImage image = new BufferedImage(inBuffImg.getWidth(), inBuffImg.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d= image.createGraphics();
    g2d.drawImage(inBuffImg, 0, 0, null);
    g2d.dispose();

    Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_32SC1);
    int[] data = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    mat.put(0, 0, data);

    return mat;
}
</code></pre>

<p><a href="https://stackoverflow.com/a/18667034/2985796">From what I could tell</a> the <code>BufferedImage</code> created by <code>Robot</code> is of type <code>BufferedImage.TYPE_3BYTE_BGR</code> which gives me an error "DataBufferInt cannot be cast to DataBufferByte" when trying to get the pixel data. So per the linked question I redraw the <code>BufferedImage</code> as type <code>BufferedImage.TYPE_INT_RGB</code> and pull out the data as a <code>DataBufferInt</code>. </p>

<p><strong>So in all,</strong> should I be trying to match the <code>Mat.type()</code> or does my problem lie elsewhere? If not elsewhere how can I alter either of the <code>Mat</code>s so that they can be used with <code>Imgproc.matchTemplate</code> properly?</p>

<p>I feel like the easiest solution would be to convert the image loaded from file to match the screenshot <code>Mat</code>? </p>

<p><strong>EDIT:</strong> The exact section of code that gives the error is below</p>

<pre><code>// Mat imageTemplate is a function argument; the loaded jpg image
// Take a picture of the screen
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenShot = rob.createScreenCapture(screenRect);
Mat screenImage = bufferedImageToMat(screenShot);

// Create the result matrix
int result_cols = screenImage.cols() - imageTemplate.cols() + 1;
int result_rows = screenImage.rows() - imageTemplate.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32SC1);

newStatus("ScreenType: " + screenImage.type());
newStatus("TemplaType: " + imageTemplate.type());

// Choose a matching method
int matchMethod = Imgproc.TM_SQDIFF_NORMED;

// Do the Matching and Normalize
Imgproc.matchTemplate(screenImage, imageTemplate, result, matchMethod);
// Error occurs on previous line
</code></pre>