I am trying to use pointer with cv::Mat, but I don't quite understand it.
When I try this:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat src = imread("image.png");
Mat img;
Mat temp;
img = Mat(src.rows, src.cols, CV_8UC1, cv::Scalar(0));
temp = Mat(src.rows, src.cols, CV_8UC1, cv::Scalar(0));
temp = img(Range(10, 20), Range(40, 60));
temp.setTo(255);
imshow("img", img);
waitKey();
return 0;
}
It works and there is no problem. However, when I change it to:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat src = imread("image.png");
Mat* img;
Mat* temp;
*img = Mat(src.rows, src.cols, CV_8UC1, cv::Scalar(0));
*temp = Mat(src.rows, src.cols, CV_8UC1, cv::Scalar(0));
temp = img(Range(10, 20), Range(40, 60));
temp.setTo(255);
imshow("img", *img);
waitKey();
return 0;
}
I get this error:
expression preceding parentheses of apparent call must have (pointer-to-) function type
at
temp = img(Range(10, 20), Range(40, 60));
and the error:
expression must have class type
at
temp.setTo(255);
What is the general rule in dealing with Mats as pointers to speed up the code?
I know for example, in function arguments we use &
for input Mats and *
for output Mats. But is there a general rule how to define and use Mats inside the functions?
Please tell me if there are other things wrong with this code, since I am a beginner. Thank you!