Ask Your Question
0

how to add each pixel of mat in android

asked 2014-10-28 07:36:22 -0600

Deepak Kumar gravatar image

updated 2014-10-28 15:01:00 -0600

Moster gravatar image

hi, how can i add each pixel of a matrix in a loop in android

below is my c++ code :

for(int x=0;x<=4;x++)
{
    for(int y=0;y<=4;y++)
    {
    sum1=sum1+ block.at<uchar>(x, y);  // Addition of all elements
    sum2=sum2+block.at<uchar>(x,y)*block.at<uchar>(x,y); //Addition of all element square                   
    }
}

where block is a 5x5 mat

edit retag flag offensive close merge delete

Comments

1

Have you seen cv::mean or cv::sum and cv::meanstddev?

FooBar gravatar imageFooBar ( 2014-10-28 15:36:46 -0600 )edit

i want to add each element of mat variable without having any loop. how is it possible.

Deepak Kumar gravatar imageDeepak Kumar ( 2014-10-29 00:30:43 -0600 )edit

http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#sum This of course only works if you want the sum. Since you also calculated the square in your example, you need to have a loop. The java version seems to be this: http://docs.opencv.org/java/2.4.2/org/opencv/core/Core.html#sumElems%28org.opencv.core.Mat%29

Moster gravatar imageMoster ( 2014-10-29 00:34:41 -0600 )edit

sum and sumElems returns scalar value. i want integer value to be returned. i am not able to get integer value.

Deepak Kumar gravatar imageDeepak Kumar ( 2014-10-29 01:12:50 -0600 )edit

sum1 = (int) Core.sumElems(block).val[0]; sum2 = (int) Core.sumElems(block.mul(block)).val[0];

Deepak Kumar gravatar imageDeepak Kumar ( 2014-10-29 12:27:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-10-29 11:14:41 -0600

You could write it as

sum1 = cv::sum(block).val[0]
sum2 = cv::sum(block.mul(block)).val[0]
edit flag offensive delete link more

Comments

how can i do the same in java. because there is no funuction named sum()

Deepak Kumar gravatar imageDeepak Kumar ( 2014-10-29 12:21:44 -0600 )edit
FooBar gravatar imageFooBar ( 2014-10-29 17:50:23 -0600 )edit

Question Tools

Stats

Asked: 2014-10-28 07:36:22 -0600

Seen: 1,649 times

Last updated: Oct 29 '14