How to define arrays in C++/ Open CV as I do in matlab ?
for example:
x=a:b:c;
or
y=linspace(a,b,n);
You can use MATLAB algorithms in your C and C++ applications. The MATLAB Engine API for C and C++ enables your applications to use and modify variables in the MATLAB workspace, call MATLAB functions, and evaluate MATLAB commands.
There are 17 fundamental classes in MATLAB. Each of these classes is in the form of a matrix or array. With the exception of function handles, this matrix or array is a minimum of 0-by-0 in size and can grow to an n-dimensional array of any size.
Accepted Answer Many MATLAB functions use an empty numeric array (i.e. []) to indicate that an input argument is undefined, which allows further input arguments to be specified.
Refer the previous answers for general answers to your question.
Specifically, to address the two examples that you mention, here is some equivalent c++ code using vectors for dynamically generating the arrays you mentioned (haven't tested):
#include <vector>
using std::vector;
vector<double> generateRange(double a, double b, double c) {
vector<double> array;
while(a <= c) {
array.push_back(a);
a += b; // could recode to better handle rounding errors
}
return array;
}
vector<double> linspace(double a, double b, int n) {
vector<double> array;
double step = (b-a) / (n-1);
while(a <= b) {
array.push_back(a);
a += step; // could recode to better handle rounding errors
}
return array;
}
OpenCV offers some functions that are similar to Matlab, but their number is very limited.
You can
cv::Mat a = cv::Mat::eye(5);
cv::Mat b = cv::Mat::zeros(5);
cv::Mat img = cv::imread("myGorgeousPic.jpg");
cv::imwrite(img, "aCopyOfMyGorgeousPic.jpg");
It also supports diag()
But for most of that tricky Matlab functionality like linspace
or magic
or whatever, there is no correspondent in OpenCV, mostly because OpenCV is not a mathematics package, but a computer vision one. If you need some specific function, you can clone it in your project (aka write it by yourself)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With