Ask Your Question

Sine or Cosine of every element in Mat (c++)

asked 2015-02-18 09:53:49 -0500

TrustMeImAnEngineer gravatar image

updated 2015-11-04 15:17:33 -0500

I was wondering if there is any way to take the sine or cosine of every element in a matrix?


EDIT: For future reference, I did the Taylor expansion of Sine. For pretty good accuracy, I only had to calculate up to the fourth term. However, keep note that if you are trying to estimate sin(pi) or sin(-pi) you have to calculate additional terms (probably 8 total terms).

This link will be useful for those who want to do this:

edit retag flag offensive close merge delete


Actually, you only need to go from -pi/4 to pi/4. For angles between pi/4 and pi/2, you can use the series for cos, substituting (pi/2-x) for your angle. Angles outside this range can be translated into it.

Guyygarty gravatar imageGuyygarty ( 2015-02-20 05:52:23 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2015-02-19 08:11:15 -0500

Guyygarty gravatar image

updated 2015-02-19 08:15:41 -0500

How precise do you need it?

You can get within a few percent with a third order polynomial. You need to:

1) transfer all matrix values to the range -pi/2 to pi/2 by adding whole multiples of pi. (I assume matrix values are in radians, if they are in degrees multiply by pi/180).

2) calculate Sin(x)=x-(x^3)/6

3) for Cosine calculate Sin(pi/2-x)

if you want better precision you can use higher order polynomials: sin(x) is exactly the infinite polynomial x-(x^3/3!)+(x^5/5!)-(x^7/7!) +.... (look up "Taylor series" for more info) but the accuracy increases pretty fast as you add terms.

I have not tried it, but I guarantee that this would be faster than an element by element sin function (which is probably implemented by a polynomial approximation anyway).


edit flag offensive delete link more



That's exactly what I ended up doing! Thanks (: +1

TrustMeImAnEngineer gravatar imageTrustMeImAnEngineer ( 2015-02-19 09:58:50 -0500 )edit

Could you simply except the question instead of closing it down? We do not close quality solved questions :)

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-20 02:08:34 -0500 )edit

^^Sorry, I didn't know -.- I will make sure to keep it open!

TrustMeImAnEngineer gravatar imageTrustMeImAnEngineer ( 2015-02-20 11:35:31 -0500 )edit

Excuse me . Can I know how to do the first step without any for loop (element by element)?

houmengmeng gravatar imagehoumengmeng ( 2018-06-24 22:01:13 -0500 )edit

answered 2015-02-19 07:59:16 -0500

This should be the fastest way of retrieving Mat elements

Size contSize = mat.size();
if (mat.isContinous())
    contSize.width *= contSize.height;
    contSize.height = 1;
for (int i = 0; i < contSize.height; ++i)
    T* ptr = mat.ptr<T>(y);
    for (int j = 0; j < contSize.width; ++j)
        ptr[j] = ...;

according to one of the core developers of OpenCV posted in this topic. Then inside the loop, do apply a sine or cosine on the value based on standard C++ functionality.

edit flag offensive delete link more

answered 2015-02-19 03:00:38 -0500

Guanta gravatar image

I haven't found such a function in the API, so I guess you need to loop over the Matrix yourself.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-02-18 09:53:49 -0500

Seen: 4,419 times

Last updated: Feb 19 '15