Ask Your Question

Opencv sum()[]

asked 2013-06-04 22:14:02 -0500

xiaojidan gravatar image

I am new to opencv , I do not understand the function "sum".

Mat img=(Mat_<double>(3,3)<<0,-1,0,-1,5,-1,2,-1,0); double rr=sum(img)[0]; cout<<"rr"<<rr&lt;<endl;< p="">

return 0;

What is the meaning of this function "sum", sum(img)[0] is standing for?? sum(img)[1] is standing for ? sum(img)[2] is stranding for??

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-06-05 02:41:19 -0500

berak gravatar image

sum() returns a Scalar with the sum of all pixels per channel

so, for a 3channel img (think a color image),

Scalar s = sum(img);
// s[0] is the sum of the 1st channel (B probably)
// s[1] is the sum of the 2nd channel (G probably)
// s[2] ...

in your case, the Mat only has one channel, so double s = sum(mat)[0] is the way to access it.

edit flag offensive delete link more


Thank you

xiaojidan gravatar imagexiaojidan ( 2013-06-05 06:29:20 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-06-04 22:14:02 -0500

Seen: 2,857 times

Last updated: Jun 05 '13