stitch 2 images

asked 2012-12-17 02:47:45 -0500

mrgloom gravatar image

updated 2012-12-24 02:11:25 -0500

I want to place one image on another(and optionally then blend them). I have just shift, for example I have shift point of 2nd image relative to upper left corner of 1st image (coordinates of point can be negative). Is there any prebuild finction to do this? something like align(Mat im1,Mat im2,Point pt,Mat out);

here is the code:

Mat align(Mat src, Mat tmp,int cx,int cy)
{
    int w_s= src.cols;
int h_s= src.rows;
int w_t= tmp.cols;
int h_t= tmp.rows;
Mat out;
if(cx>=0&&cy>=0)
{
    out.create(max(cy+h_t,h_s),max(cx+w_t,w_s),src.type());
    src.copyTo(out(Rect(0,0,src.cols,src.rows)));
    tmp.copyTo(out(Rect(cx,cy,tmp.cols,tmp.rows)));
}
else if(cx>=0&&cy<0)
{
    out.create(max(h_t-abs(cy),abs(cy)+h_s),max(cx+w_t,w_s),src.type());
    src.copyTo(out(Rect(0,abs(cy),src.cols,src.rows)));
    tmp.copyTo(out(Rect(cx,0,tmp.cols,tmp.rows)));
}
else if(cx<0&&cy>=0)
{
    out.create(max(cy+h_t,h_s),max(w_t-abs(cx),abs(cx)+w_s),src.type());
    src.copyTo(out(Rect(abs(cx),0,src.cols,src.rows)));
    tmp.copyTo(out(Rect(0,cy,tmp.cols,tmp.rows)));
}
else if(cx<0&&cy<0)
{
    out.create(max(h_t-abs(cy),abs(cy)+h_s),max(w_t-abs(cx),abs(cx)+w_s),src.type());
    src.copyTo(out(Rect(abs(cx),abs(cy),src.cols,src.rows)));
    tmp.copyTo(out(Rect(0,0,tmp.cols,tmp.rows)));
}
    return out;
}
edit retag flag offensive close merge delete