-
c to c++ convert
I want to convert the c code to c++
my code in c is
// Calculate the moments to estimate the position of the ball
CvMoments *moments = (CvMoments*)mallocsizeof(CvMoments));
cvMoments(imgYellowThresh, moments, 1);
// The actual moment values
double moment10 = cvGetSpatialMoment(moments, 1, 0);
double moment01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
in c++
cv::Moments ourMoment;//moments variable
ourMoment=moments(imgYellowThresh); //calculat all the moment of image
double moment10=moments.m10; //extract spatial moment 10
double moment01=moments.m01; //extract spatial moment 01
double area=moments.m00; //extract central moment 00
But I am getting error at ourMoment=moments(imgYellowThresh); as identifier moments is undefined.
Please help me in this
-
This error should not surprise you. It is the same error as you get in C. "moments" in your C++ code is undefined.
-
But in c I am not facing any error
-
What do you intend this to do?
ourMoment=moments(imgYellowThresh); //calculat all the moment of image
-
to get the actual position of the imgYellowThresh
-
Which is equal to what in C?
This?
cvMoments(imgYellowThresh, moments, 1);
-
-
That's a big difference.
ourMoment=moments(imgYellowThresh); //calculat all the moment of image
cvMoments(imgYellowThresh, moments, 1);
This isn't about C and C++ differences; it's more about what you have written is nonsensical and makes no sense (in C++).
-
I don't know much abt c++ code but I found it some where in net.
-
Then I suggest you learn C++ before you try to use it. There are tutorials on the site.
-
-
For the most part, C code tends to also compile using a C++ compiler.
e.g. you can do this in C if you have a struct variable called 'moments'. Same goes for C++...
Code:
double moment10=moments.m10; //extract spatial moment 10
double moment01=moments.m01; //extract spatial moment 01
double area=moments.m00; //extract central moment 00
Really all I see that you're trying to do here is to convert code to a more object-oriented approach.