Question

I have a class called "myClass", which returns "cv::Scalar" type, and I want to do this:

cv::Scalar myValue; 
for ( myValue > myClass (i,j) )
.... 

But the comparison part in the "for" line gives out error, saying "no operator > matches these operands". Could someone help me? Thank you.

Was it helpful?

Solution

If there's no operator to compare two cv::Scalar elements you can define it:

#include "OpenCVStuff.h"

// Custom operator to compare cv::Scalar class...
bool operator >(const cv::Scalar &a, const cv::Scalar &b)
{
    bool Result = false;
    // Do whatever you think a Scalar comparison must be.
    return Result;
}

int main(int argc, char **argv)
{
    cv::Scalar myValue; 

    // Assuming myClass (i,j) returns a cv::Scalar
    for ( myValue > myClass (i,j) )
    {
        // Do something...
    }

    return 0;
}

Doing this you can define the way two cv::Scalar are compared without bothering the cv::Scalar class itself.

I've put it into the main.cpp just as an example, but you can define the operator wherever you want as long it is visible where the comparison is performed.

OTHER TIPS

In order to complete Zhi Lu's answer:

If you want to compare an element of Scalar, you should do next:

cv::Scalar scalar(myValue);   //here you assign a value to the element (0,0)
for (scalar.val[0,0] > myClass (i,j)) // access the elment of Scalar
{
}

Anyway there is no point on using Scalar if you just want a single value. And also note that you need a proper for loop expression like

for(i = 0; i < 0; i++){}

Scalar is a four-double-type-element array. You can either store any number of elements (double type) in such structure. So, you cannot compare one instance of Scalar with another directly like that of int type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top