Friday 20 May 2016

Opencv C++ code of Operation on Arrays:absdiff


Here is the OpenCv C++ Example on Operation on Arrays by using absdiff()
It calculates the per-element absolute difference between two arrays or between an array and a scalar.


Syntax:
C++: void absdiff(InputArray src1, InputArray src2, OutputArray dst)



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 type as input arrays.



Here is the Opencv Code for calculating absolute difference of each element of matrix.
//Opencv C++ Example of Operation on Arrays:absdiff  
#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;}
           
    absdiff( image1,  image2,  dst);
  
    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:

Friday 6 May 2016

Opencv C++ code of Operation on Arrays:subtract


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


Syntax:
C++: void subtract(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.
dst – output array of the same size and the same number of channels as the input array.
mask – optional operation mask; this is an 8-bit single channel array that specifies elements of the output array to be changed. dtype – optional depth of the output array (see the details below).



We can also use:
dst=image1-image2;


//How to subtract two images in OpenCV
#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;}
            
    subtract( 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;
}

OpenCV-Input1:
OpenCV-Input2:
OpenCV-Output:

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: