Ask Your Question
0

Operator and foreach

asked 2020-02-24 02:52:20 -0600

NL37 gravatar image

updated 2020-02-24 06:08:55 -0600

LBerger gravatar image

Hello the community,

Actually, I have to manipulate an image of 5120 x 3840. I need to divide by 5 the image, save it and then write 0 for each pixel.

I just use :

Image = Image * 0.2;
 ...
Image = Image * 0 ;

I don´t how it codes. Is it more efficient to use foreach ?

Thank you,

edit retag flag offensive close merge delete

Comments

just curious, why do you need to multiply with 0 ?

what is the context of your code ? what is it for ?

berak gravatar imageberak ( 2020-02-24 03:59:48 -0600 )edit
2

I get 5 images from a Gige camera (as fast as possible). Then, I sum those images (8 bit in grayscale) in a 32 bites image grayscale, I apply a median filter, and then I divide by 5. At the end of the process, I need to multiply the 32 bit image by 0, and then sum 5 new images. Am I clear ?

NL37 gravatar imageNL37 ( 2020-02-24 04:53:18 -0600 )edit

may be you can use a IIr filter

LBerger gravatar imageLBerger ( 2020-02-24 06:30:36 -0600 )edit

2 answers

Sort by » oldest newest most voted
1

answered 2020-02-24 05:47:34 -0600

mvuori gravatar image

It should be assumed that an operator is always faster than a loop, as OpenCV can use its own, optimized code. Similarly, an assignment is always faster than multiplication and anything multiplied by 0 is 0, so one should use simple zeroing

edit flag offensive delete link more
0

answered 2020-02-24 06:28:54 -0600

berak gravatar image

you could use accumulate(), and only multiply once:

Mat dst(image.size(), CV_32S, Scalar(0));
accumulate(image, dst);
accumulate(image, dst);
accumulate(image, dst);
accumulate(image, dst);
accumulate(image, dst);
dst *= 0.2;

// later:
dst.setTo(0);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-24 02:52:20 -0600

Seen: 305 times

Last updated: Feb 24 '20