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:

No comments:

Post a Comment