Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

More efficient way to sum several images with Mat

Hello the community,

Actually, I need to sum five images, and keep the average value. My code is :

//image 1
cv::Mat INT_IMAGE1 = imread("Original_Image/4wCcW.png", 0);
cv::Mat OUT_IMAGE1 ;
cv::medianBlur (INT_IMAGE1, OUT_IMAGE1, Window ) ;

//image 2
cv::Mat INT_IMAGE2 = imread("Original_Image/4wCcW.png", 0);
cv::Mat OUT_IMAGE2 ;
cv::medianBlur (INT_IMAGE2, OUT_IMAGE2, Window ) ;

//image 3
cv::Mat INT_IMAGE3 = imread("Original_Image/4wCcW.png", 0);
cv::Mat OUT_IMAGE3 ;
cv::medianBlur (INT_IMAGE3, OUT_IMAGE3, Window ) ;

//image 4
cv::Mat INT_IMAGE4 = imread("Original_Image/4wCcW.png", 0);
cv::Mat OUT_IMAGE4 ;
cv::medianBlur (INT_IMAGE4, OUT_IMAGE4, Window ) ;

//image 5
cv::Mat INT_IMAGE5 = imread("Original_Image/4wCcW.png", 0);
cv::Mat OUT_IMAGE5 ;
cv::medianBlur (INT_IMAGE5, OUT_IMAGE5, Window ) ;


cv::Mat FINALE_IMAGE= Mat::zeros(height,width, CV_8U);
cv::Mat BLA= Mat::zeros(height,width, CV_8U);

for(int i=0 ; i<height ; i++){
    for(int j=0 ; j<width ; j++){
        FINALE_IMAGE.at<uchar>(i,j)=(OUT_IMAGE1.at<uchar>(i,j)+OUT_IMAGE2.at<uchar>(i,j)+OUT_IMAGE3.at<uchar>(i,j)+OUT_IMAGE4.at<uchar>(i,j)+OUT_IMAGE5.at<uchar>(i,j))/5;
    }
}

I know that is not an efficient method, but despite all my research, I didn´t find a function which paralellize the task. Furthermore, I can´t use :

FINALE_IMAGE=OUT_IMAGE1+OUT_IMAGE2+OUT_IMAGE3+OUT_IMAGE4+OUT_IMAGE5 ; FINALE_IMAGE=FINALE_IMAGE/5 ;

With this method, I lost information when the sum of pixels is greater than 255. I can´t use to this method too, because of the lost conversion :

FINALE_IMAGE=OUT_IMAGE1/5 +OUT_IMAGE2/5 +OUT_IMAGE3/5 +OUT_IMAGE4/5 +OUT_IMAGE5/5 ;

Some of you have an idea ? thank you very much !

Sincerely