RSS
 

Archive for the ‘C++’ Category

Color interpolation in OpenCV

23 Dec

I could not find in Internet a function which can return a RGB color value between two colors So I had to do it myself. I hope it will be useful for someone.

CvScalar interpolate(CvScalar c1, CvScalar c2, double value){
if( value <= 0.0 ){ return c1; }
if( value >= 1.0 ){ return c2; }
int red = cvRound((1.0 - value) * c1.val[2] + value * c2.val[2]);
int green = cvRound((1.0 - value) * c1.val[1] + value * c2.val[1]);
int blue = cvRound((1.0 - value) * c1.val[0] + value * c2.val[0]);
return CV_RGB(red, green, blue);
}

 
No Comments

Posted in C++, OpenCV

 

Remove items from deque, list or vector in a loop

28 Nov

Recently I needed to delete items from a sequence like a deque , list or vector from a for loop, I figured out that I have to do something like this:

for(iter = list.begin(); iter != list.end(); ++iter)
{
// your code
iter = list.erase(iter);
--iter;
// your code
}

It looks basic but it took me a while to figure it out so I wanted to share in case somebody need it.

 
No Comments

Posted in C++

 

C++: Convert int to string

24 Jul

This is an elegant function to convert int to string to use anywhere:

std::string intToString(int i)
{
    std::stringstream ss;
    std::string s;
    ss < < i;
    s = ss.str();

    return s;
}
 
No Comments

Posted in C++

 

C++: Delete or remove a file

15 Jun

To delete or remove a file in C++ you can use the remove function from stdio.h like follows:


#include 

int main(){ if(remove("myfile.txt") == -1)

    fprintf(stderr,"Remove failed");

// your code .......

}
 
No Comments

Posted in C++

 

OpenCV: Implementation of Eigenface

10 Jun

I found a good tutorial which explain how the face recognition method called eigenface works: Cognotics
This tutorial uses the deprecated function cvCalcEigenObjects().
The advised function is cvCalcPCA().


 
No Comments

Posted in C++, OpenCV