Of Course ,you can!! Now in this episode
I will play with Mr.Smile in OpenCV
I will play with Mr.Smile in OpenCV
picture 0_Add_images_difference_size
How to make it:
- 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
- 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
- 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);
- Finish!! Let's show the result
picture 7_Result
#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; }
3 comments:
hello, I tried your source code . But I got error in here:
cvCvtColor(thr,roi2,CV_BGR2GRAY);
"thr" and "roi2" have different size.
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
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