-2

I write a function using Mat_ type as an input, the syntax of this function is like this:

#include<iostream>
#include"opencv2\opencv.hpp"

using namespace std;
using namespace cv;

Mat_<double> histcost(Mat_<double> BH1, Mat_<double> BH2) {
    Mat_<double> output;
    ...
    return output;
}

But when I tried to write header file of this function:

#pragma once
extern Mat_<double> histcost(Mat_<double> BH1, Mat_<double> BH2);

It gives me the error: Mat_ is not a template.

I am not very familiar with function management and I'd really appreciate it if anyone can help!

  • You need to include the "opencv2\opencv.hpp" header file in your source code. That header file contains the declaration of the function and all types/classes/templates needed. – Cris Luengo Aug 24 '18 at 14:38
  • Thanks. But I did include "opencv2\opencv.hpp" header file in my function source code, which is obvious in my code above. It does not work. Maybe I misunderstood your point, and I also tried to include it in header file, does not work either. Or if I did not follow your comment, can you please give me more details? – Chris Ding Aug 24 '18 at 15:45
  • Add the `#include` statement before you declare `histcost` in your header file. Your header file will be included in other code that doesn't know about OpenCV, but it will need to know about OpenCV to be able to use your function. So, include the OpenCV header file in your header file, so that when your header file is included, OpenCV header files are included automatically as well, and the compiler will understand those classes and templates. In your CPP file, include your own header file. And in your header file, don't use `extern`, it's not used since C++11. – Cris Luengo Aug 24 '18 at 15:52
  • I really appreciate your help! I tried to add `#include"opencv2/opencv.hpp"` in my header file. But it still gives me the error. But you've helped a lot, thanks. It would be nice if you know more about this problem. – Chris Ding Aug 24 '18 at 16:54
  • @Cris Luengo, yes you are right! All I need to do is `#include`. I forgot `using namespace cv;` So that's why it still doesn't work. This is such a stupid mistake. And thank you so much for helping! – Chris Ding Aug 24 '18 at 19:41

1 Answers1

0

Don't use Mat_< > just use regular cv::Mat() The cv::Mat can contain data of any type, you don't need to tell it the type.

The Mat_<double> is a special short cut trick to make a cv:Mat directly from fixed data, you shouldn't use it anywhere unless you are really performance tuning and know what you are doing,

Example of Mat_ < > syntax (just for search)

Mat X = (Mat_<double>(4, 4) <<0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14,15);

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • Thank you for your answer. I'd never go to `Mat_` if I can get things done with `cv::Mat`. My code involves with a lot of matrix arithmetic like multiplication, row operation ... So, I have to. But by any chance do you have a clue about the solution to my problem? – Chris Ding Aug 24 '18 at 16:53
  • @ChrisDing, just use cv::Mat in the function call. Your problem is that you are using a macro in the wrong way - see https://stackoverflow.com/questions/12139402/initialize-an-opencv-mat-with-an-2d-array#12139573 – Martin Beckett Aug 24 '18 at 17:42
  • Isn't `Mat_` more typesafe? Eg. `cv::Mat m1(2, 5, CV_32FC1); m1.at(1, 2) = 42; std::cout << m1.at(1, 2) << std::endl;` will silently give a wrong result, because I accidentally used `at()` instead of `at()`. With `cv::Mat_ m2(2, 5, CV_32FC1); m2.at(1, 2) = 42; std::cout << m2(1, 2) << std::endl;` this doesn't happen. So as a novice to OpenCV I would say that `Mat_` is preferable over `Mat`, no? – oliver Sep 08 '22 at 08:21