Skip to content

OpenCV: adding two images

This is a very simple example of how to open two images and display them added.

I got two pictures at project Commons from Wikimedia that were highlighted on Featured Pictures. I did a crop on both to have the same size, as I’m trying to make this example as simple as possible.

The first one is a photo of our Milky Way, taken at Paranal Observatory by Stéphane Guisard.


The second one is a California surfer inside wave, taken by Mila Zinkova.


In this simple OpenCV code below, we open the images, create a new one to display the result and use cvAdd to add them. We do not save the result or handle more than the ordinary case of two images with the same size.


int main( int argc, char **argv ){
    IplImage *surfer, *milkyway, *result;
    int key = 0;
    CvSize size;

    /* load images, check, get size (both should have the same) */
    surfer = cvLoadImage("surfer.jpg", CV_LOAD_IMAGE_COLOR);
    milkyway = cvLoadImage("milkyway.jpg", CV_LOAD_IMAGE_COLOR);
        printf("Could not open one or more images.");
        exit -1;
    size = cvGetSize(surfer);

    /* create a empty image, same size, depth and channels of others */
    result = cvCreateImage(size, surfer->depth, surfer->nChannels);

    /* result = surfer + milkyway (NULL mask)*/
    cvAdd(surfer, milkyway, result, NULL);
    /* create a window, display the result, wait for a key */
    cvNamedWindow("example", CV_WINDOW_AUTOSIZE);
    cvShowImage("example", result);

    /* free memory and get out */
    return 0;

/* gcc add.c -o add `pkg-config opencv --libs --cflags` */

Compile it (on a well configured OpenCV development environment) and run it:

gcc add.c -o add `pkg-config opencv –libs –cflags`

The result got pretty cool, a milky way surfer.

surfer in the milk way

Published inenglish


  1. marcos marcos

    ola num consigo usar de maneira correta o cvAdd
    ele compila mas na hora de executar ele trava o executavel…
    eu uso o visual 2010
    por favor me ajude

  2. mashhur mashhur

    what about adding two different sized images?
    let’s assume I have 2000×1000 sized a gray scaled image and wanna add 100×200 new image. Could you please solve it?

  3. Lewis Christie Lewis Christie


    what about adding two different sized images?
    let’s assume I have 2000×1000 sized a gray scaled image and wanna add 100×200 new image. Could you please solve it?

    Hi, the way to achieve this is to set the Region of Interest (ROI) of the larger image to a square the size of the smaller image. cvSetImageROI takes two arguments, the first is the image you are chainging the ROI on, the second is a cvRect. When creating a cvRect, the first two arguments are the location of the rectangle and the second two are the dimensions.

    So for your situation you would create a cvRect with the dimensions 100×200 and the location you want to add to.

    Now that the ROI has been changed, you can add the smaller image to the larger. After adding, just call cvResetImageROI on your large image and you can work with it as normal.

  4. bastidererste bastidererste

    works fine on static images… but if i use it on a camera image my memory gets filled very fast. any ideas?

  5. Ben Ben


    This post was very useful. I just wanted to know if you have two (or more) images of the moon and you wanted to find the best added average image of it. How would you go about this?


  6. Roe Roe

    Hello ^^
    Do you think it would work for this?

    IplImage** images;

    cvAdd(result, images[i], result, NULL);

  7. sakda sakda

    Hi Silveira,
    My name is Sakda Piangkoa, will study in Survey Engineering at soon.(Ph.d, Chulalongkon University)
    My professor adsistance has give an assignment to me, about programming in Image Processing fields. And now, I’m programming its with OpenCV. Your code in this page, acculate to my assignment: adding two images become one. your CV is very nice.

Leave a Reply

Your email address will not be published. Required fields are marked *