Today I'd like to introduce another interesting image processing function to handle a subimage of a picture to realize the image part replacement. This has been done by lots of image processing softwares, such as photoshop. Of cource they are much more professional. Here the example is just to demonstrate how to choose a region of a picture and process it as will. In my example I use the michael Jordan's head picture to take place of mine. Although the visual effect is not perfect, we could lernen the function of this ROI(region of interest) application. The code is below:
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
int main(int argc, char** argv)
{
IplImage *src1, *src2;
if( argc == 9 && ((src1=cvLoadImage("lzsNew.jpg",1)) != 0
)&&((src2=cvLoadImage("jordan.jpg",1)) != 0 ))//load the images
{
int x = 340;//set the beginning of region
int y = 215;
int width = 105;//set the size of regino
int height = 105;
double alpha = 0.25;//set how strong will original image influence
//the result
double beta = 0.75;//set how strong will added image influence
//the result
cvSetImageROI(src1, cvRect(x,y,width,height));
cvSetImageROI(src2, cvRect(20,5,width,height));
cvAddWeighted(src1, alpha, src2, beta,0.0,src1);
cvResetImageROI(src1);
cvNamedWindow( "Alpha_blend", 1 );
cvShowImage( "Alpha_blend", src1 );
cvSaveImage("lzsReplace.jpg",src1);
cvWaitKey();
}
else
printf("Couldn't load one or both of %s, %s\n",argv[1],argv[2]);
return 0;
}
As we can see above, the main job to implement it is as follows:
first, loading the two images by cvLoadImage(); and then set the range of the interesting region, including the beginning and the size as welll as the weighting parameters of each image. After that with the help of cvSetImageROI() we are able to active the region of the image which we just want to process, with this function during the matrix adding or other operations, just the actived region will take place, the others keep the same state as before. At last after the weighted added function cvAddWeighted(), we achieve the final result and save it as an image file. The final visual result is below. And the original code is here:
ontheweg



0 comments:
Post a Comment