Tuesday, August 9, 2011

Episode_11_Add_images_with_difference_size

"Can I play with Mr.Smile ?"
Of Course ,you can!! Now in this episode
I will play with Mr.Smile in OpenCV

Maybe you are familiar adding image from image with the same sizes. How about with difference size? Have you never tried before?? So this is the right time for you to learn more about image processing, it's look like a Gif format isn't it?
picture 0_Add_images_difference_size

How to make it:
  1. First we need to prepare two different size images. if you need a transparent background, you should make a black background like as My Mr.Smile icon.

    picture 1_Example_of_images_with_difference_size

  2. Now prepare second layers with black background and different sizes. First layer, I use 1024x768 pixels and the second layer 640x480 pixels. Why?? When I put the image exceeds 640x480 size with Mr.Smile icon, the program will crash.


    picture 2_The_image_exceeds_640x480_size

    IplImage* src, * res, * roi, *cmb, *roi2, *thr;
    src = cvLoadImage("Smile.jpg",1);
    cmb = cvLoadImage("Scientist.jpg",1);
    res = cvCreateImage(cvSize(640,480), 8, 3); //640x480 for Scientist.jpg
    thr = cvCreateImage(cvSize(640,480), 8, 3); //640x480 for Scientist.jpg
    roi = cvCreateImage(cvSize(1024,768), 8, 3); //1024x768
    roi2 = cvCreateImage(cvGetSize(cmb), 8, 1); //for mask

    picture 3_first_layer_&_second_layer

  3. Now we use ImageROI to make it two layers with same size.

    /* First ROI 1024x768 */
    CvRect rect = cvRect(150, 400, src->width, src->height); // change coordinates
    cvSetImageROI(roi, rect);
    cvAdd(src, roi, roi, NULL);
    cvResetImageROI(roi);

    picture 4_Mr.Smile_icon_add_to_first_layer
    /* Second ROI 640x480 */
    CvRect rect2 = cvRect(250, 250, res->width, res->height);
    cvSetImageROI(roi, rect2);
    cvAdd(res, roi, res, NULL);
    cvResetImageROI(roi);

    picture 5_First_layer_add_to_second_layer

  4. After that we use image processing to make a mask layer.
    /* Filtering image to make a mask */
    cvThreshold(res,thr,50,255,1);
    cvCvtColor(thr,roi2,CV_BGR2GRAY);
    cvThreshold(roi2,roi2,250,255,1);
    cvNot(roi2,roi2);
    cvAdd(cmb,res,res,roi2);
    picture 6_Mask_layer
  5. Finish!! Let's show the result

    picture 7_Result
Full Source Code :

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int main(int argc, char** argv)
{
IplImage* src, * res, * roi, *cmb, *roi2, *thr;
src = cvLoadImage("Smile.jpg",1);
cmb = cvLoadImage("Scientist.jpg",1);
res = cvCreateImage(cvSize(640,480), 8, 3); //640x480 for camera
thr = cvCreateImage(cvSize(640,480), 8, 3); //640x480 for camera
roi = cvCreateImage(cvSize(1024,768), 8, 3); //1024x768
roi2 = cvCreateImage(cvGetSize(cmb), 8, 1); //for mask

/* prepare the 'ROI' image */
cvZero(res);
cvZero(roi);
cvZero(roi2);

/* First ROI 1024x768 */
CvRect rect = cvRect(350, 350, src->width, src->height); // change coordinates
cvSetImageROI(roi, rect);
cvAdd(src, roi, roi, NULL);
cvResetImageROI(roi);
/* Second ROI 640x480 */
CvRect rect2 = cvRect(250, 250, res->width, res->height);
cvSetImageROI(roi, rect2);
cvAdd(res, roi, res, NULL);
cvResetImageROI(roi);

/* Filtering image to make a mask */
cvThreshold(res,thr,50,255,1);
cvCvtColor(thr,roi2,CV_BGR2GRAY);
cvThreshold(roi2,roi2,250,255,1);
cvNot(roi2,roi2);
cvAdd(cmb,res,res,roi2);

cvShowImage("src", res);
cvShowImage("res", src);
cvWaitKey(0);
/* be tidy */
cvDestroyAllWindows();
cvReleaseImage(&src);
cvReleaseImage(&res);
cvReleaseImage(&roi);
return 0;

}
More of my experiments using cvadd

3 comments:

Unknown said...

hello, I tried your source code . But I got error in here:
cvCvtColor(thr,roi2,CV_BGR2GRAY);
"thr" and "roi2" have different size.

Unknown said...

1. Check/Make sure your image size of scientist.jpg 640x480
2. Have you put your image on your project folder ?

thr and roi2 have different size.. it's couse your roi2(scientist.jpg) that isn't same size with thr(threshold image)

thr = cvCreateImage(cvSize(640,480), 8, 3); //640x480 for camera
roi = cvCreateImage(cvSize(1024,768), 8, 3); //1024x768
roi2 = cvCreateImage(cvGetSize(cmb), 8, 1); //for mask

Anonymous said...

Hello, thanks for sharing all this great stuff!!

I'm starting with opencv and I wonder if you could send me the code of the program showed in the video where you cover your face with the smile face.

My email is the_acid_king@hotmail.com

Post a Comment