Finding sum using mask in opencv [closed]

asked 2017-06-21 04:55:38 -0600

Nani gravatar image

I want to sum only non zero pixel value, so is there any function in opencv to get that like the function mean(src, mask) as I used sumElmes() but it gives the sum of all values. Any suggestions or any way to solve that eve without builtin functions.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-18 04:38:24.057511

Comments

1

I want to sum only non zero pixel value

why so ? zero's never change the sum.

berak gravatar imageberak ( 2017-06-21 05:10:44 -0600 )edit

Yes, I new that but what if i want to log the pixel value. for example I have this equation:

                            Mat a_logA = new Mat();
            Core.log(a_channel, a_logA);
            double a_entropyA = Core.sumElems(a_channel.mul(a_logA)).val[0];

            Mat b_logB = new Mat();
            Core.log(b_channel, b_logB);
            double b_entropyB = Core.sumElems(b_channel.mul(b_logB)).val[0];

It give me error for black pixels.

Nani gravatar imageNani ( 2017-06-21 05:28:43 -0600 )edit

Do you try countNonZero() ???

hoang anh tuan gravatar imagehoang anh tuan ( 2017-06-21 05:30:23 -0600 )edit

and the error is ?

berak gravatar imageberak ( 2017-06-21 06:11:06 -0600 )edit

Yes I did Mat location = new Mat(); Core.findnonezero(src, location) but I confused with the location mat, Can I apply sumEmes() directly on it or it just a location and I need to match those locations with the src image?

Please help me.

Nani gravatar imageNani ( 2017-06-21 06:11:08 -0600 )edit

you need to get the pixel value from the image, at that location (x,y).

but again, where's the error with the original approach ? (most probably you do not need any workarounds)

berak gravatar imageberak ( 2017-06-21 06:16:57 -0600 )edit

There is black pixels in the first approach which not valid using Core.log

Nani gravatar imageNani ( 2017-06-21 06:27:05 -0600 )edit

hmm, i very much doubt so. 0 values are valid for log(). proof ? exact error ?

berak gravatar imageberak ( 2017-06-21 06:38:34 -0600 )edit

"I want to sum only non zero pixel value" <- if you sum also pixel with 0 value, the total would be exactly the same with or without them because they are zeros...

swiss_knight gravatar imageswiss_knight ( 2017-06-21 06:39:18 -0600 )edit

@berak sorry it was another mistake which makes my app stop. I get the entropy value but it seems a big number which mean it counts even zero values and I only want to count the entropy of none zero values.

Nani gravatar imageNani ( 2017-06-21 06:54:30 -0600 )edit