Friday 6 May 2016

Opencv C++ code of Operation on Arrays:add


Here is the OpenCv C++ Example on Operation on Arrays by using add() function.
It calculates the per-element sum of two arrays or an array and a scalar.

Syntax:
C++: void add(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1)

Parameters:
src1 – first input array or a scalar.
src2 – second input array or a scalar.
src – single input array.
value – scalar value.
dst – output array that has the same size and number of channels as the input array(s); the depth is defined by dtype or src1/src2.
mask – optional operation mask - 8-bit single channel array, that specifies elements of the output array to be changed.
dtype – optional depth of the output array (see the discussion below).

We can also use
dst=image1+image2;

Here is the Opencv Code for calculating the per-element sum of two arrays or an array and a scalar.
//Opencv C++ Example of Operation on Arrays:add 
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
 
using namespace cv;
using namespace std;
 
int main( )
{
 
       Mat image1,image2,dst;
       image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg",CV_LOAD_IMAGE_COLOR);
    if( !image1.data ) { printf("Error loading image1 \n"); return -1;}
    image2 = imread("C:\\Users\\arjun\\Desktop\\opencv-test.png",CV_LOAD_IMAGE_COLOR);
    if( !image2.data ) { printf("Error loading image2 \n"); return -1;}
           
    add( image1,image2, dst);
    //dst=image1+image2;

       namedWindow( "Display window", CV_WINDOW_AUTOSIZE );  
       imshow( "Display window", image2 );                 

    namedWindow( "Display windo", CV_WINDOW_AUTOSIZE );  
       imshow( "Display windo", image1 );         

       namedWindow( "Result window", CV_WINDOW_AUTOSIZE );   
       imshow( "Result window", dst );
    
    //imwrite("C:\\Users\\arjun\\Desktop\\opencv-dst.jpg",dst);
       waitKey(0);                                         
       return 0;
}

Input Image1:

Input Image2:

Output Image:

No comments:

Post a Comment