int outfeapot::Draw(IplImage *pImage,int nMode) { int i,j; if(nMode==0) { size.width=width; size.height=height; unifiedimage=cvCreateImage(size,depth,channels); memcpy(unifiedimage->imageData,pImage->imageData,pImage->widthStep*height); vecPixelsXY.push_back(pImage->width/2); vecPixelsXY.push_back(pImage->height/2); } else { //估算拼接图大小 int nLeftOffset,nRightOffset,nTopOffset,nBottomOffset; int nLeft,nRight,nTop,nBottom; int nWidth=pImage->width; int nHeight=pImage->height; int points[4][2]; points[0][0]=0*m_tm[0]+0*m_tm[1]+m_tm[2]+offsetx; points[0][1]=0*m_tm[3]+0*m_tm[4]+m_tm[5]+offsety; points[1][0]=nWidth*m_tm[0]+0*m_tm[1]+m_tm[2]+offsetx; points[1][1]=nWidth*m_tm[3]+0*m_tm[4]+m_tm[5]+offsety; points[2][0]=0*m_tm[0]+nHeight*m_tm[1]+m_tm[2]+offsetx; points[2][1]=0*m_tm[3]+nHeight*m_tm[4]+m_tm[5]+offsety; points[3][0]=nWidth*m_tm[0]+nHeight*m_tm[1]+m_tm[2]+offsetx; points[3][1]=nWidth*m_tm[3]+nHeight*m_tm[4]+m_tm[5]+offsety; nLeft=nRight=points[0][0]; nTop=nBottom=points[0][1]; for(i=0;i<4;i++) { if(points[i][0]<nLeft) nLeft=points[i][0]; if(points[i][0]>nRight) nRight=points[i][0]; if(points[i][1]<nTop) nTop=points[i][1]; if(points[i][1]>nBottom) nBottom=points[i][1]; } if(nLeft<0) { nLeftOffset=abs(nLeft); } else { nLeftOffset=0; } if(nRight>=unifiedimage->width) { nRightOffset=nRight-unifiedimage->width+1; } else { nRightOffset=0; } if(nTop<0) { nTopOffset=abs(nTop); } else { nTopOffset=0; } if(nBottom>=unifiedimage->height) { nBottomOffset=nBottom-unifiedimage->height+1; } else { nBottomOffset=0; } //重新设置偏移量(因为坐标系的改变,不能叠加) curOffsetx=nLeftOffset; curOffsety=nTopOffset; offsetx+=nLeftOffset; offsety+=nTopOffset; CvSize newSize; int newWidth,newHeight; int standcx,standcy,pos0,pos1; //估算拼接图大小 newWidth=unifiedimage->width+nLeftOffset+nRightOffset; newHeight=unifiedimage->height+nTopOffset+nBottomOffset; newSize.width=newWidth; newSize.height=newHeight; pUnifiedNewimage=cvCreateImage(newSize,pImage->depth,pImage->nChannels); //绘制原图到拼接图 cvSetImageROI(pUnifiedNewimage,cvRect(curOffsetx,curOffsety,unifiedimage->width,unifiedimage->height)); cvCopyImage(unifiedimage,pUnifiedNewimage); cvResetImageROI(pUnifiedNewimage); int nCenterX = pImage->width/2; int nCenterY = pImage->height/2; int nX = nCenterX*m_tm[0]+nCenterY*m_tm[1]+m_tm[2]+(offsetx-curOffsetx); int nY = nCenterX*m_tm[3]+nCenterY*m_tm[4]+m_tm[5]+(offsety-curOffsety); for(std::vector<int>::size_type ix=0;ix<vecPixelsXY.size();ix+=2) { vecPixelsXY[ix] += curOffsetx; vecPixelsXY[ix+1] += curOffsety; } vecPixelsXY.push_back(nX); vecPixelsXY.push_back(nY); CalAntiTransMatirx(); //绘制新图到拼接图 采用插值的方法(有可能放大,需要插值) int ii,jj; double u,v; //多出的浮点 uchar r,g,b; //插值三个通道的值 uchar f1,f2,f3,f4; for(ii=(nLeft+curOffsetx);ii<(nRight+curOffsetx);ii++) { for(jj=(nTop+curOffsety);jj<(nBottom+curOffsety);jj++) //前面(上面)不用去掉黑边 { //插值 int nI=ii-offsetx; int nJ=jj-offsety; double dX=nI*m_tm1[0]+nJ*m_tm1[1]+m_tm1[2]/(nI*m_tm1[6]+nJ*m_tm1[7]+m_tm1[8]); double dY=nI*m_tm1[3]+nJ*m_tm1[4]+m_tm1[5]/(nI*m_tm1[6]+nJ*m_tm1[7]+m_tm1[8]); b=g=r=0; i=(int)dX; //列 x坐标 j=(int)dY; //行 y坐标 u=dX-i; v=dY-j; if((i>=0)&&(i<pImage->width-1)&&(j>=0)&&(j<pImage->height-1)) { f1=((uchar*)(pImage->imageData+j*pImage->widthStep))[i*pImage->nChannels+0];//B f2=((uchar*)(pImage->imageData+j*pImage->widthStep))[(i+1)*pImage->nChannels+0]; f3=((uchar*)(pImage->imageData+(j+1)*pImage->widthStep))[i*pImage->nChannels+0]; f4=((uchar*)(pImage->imageData+(j+1)*pImage->widthStep))[(i+1)*pImage->nChannels+0]; b=(1-u)*(1-v)*f1+(1-u)*v*f2+u*(1-v)*f3+u*v*f4; f1=((uchar*)(pImage->imageData+j*pImage->widthStep))[i*pImage->nChannels+1];//G f2=((uchar*)(pImage->imageData+j*pImage->widthStep))[(i+1)*pImage->nChannels+1]; f3=((uchar*)(pImage->imageData+(j+1)*pImage->widthStep))[i*pImage->nChannels+1]; f4=((uchar*)(pImage->imageData+(j+1)*pImage->widthStep))[(i+1)*pImage->nChannels+1]; g=(1-u)*(1-v)*f1+(1-u)*v*f2+u*(1-v)*f3+u*v*f4; f1=((uchar*)(pImage->imageData+j*pImage->widthStep))[i*pImage->nChannels+2];//R f2=((uchar*)(pImage->imageData+j*pImage->widthStep))[(i+1)*pImage->nChannels+2]; f3=((uchar*)(pImage->imageData+(j+1)*pImage->widthStep))[i*pImage->nChannels+2]; f4=((uchar*)(pImage->imageData+(j+1)*pImage->widthStep))[(i+1)*pImage->nChannels+2]; r=(1-u)*(1-v)*f1+(1-u)*v*f2+u*(1-v)*f3+u*v*f4; ((uchar*)(pUnifiedNewimage->imageData+jj*pUnifiedNewimage->widthStep))[ii*pUnifiedNewimage->nChannels+0]=b; ((uchar*)(pUnifiedNewimage->imageData+jj*pUnifiedNewimage->widthStep))[ii*pUnifiedNewimage->nChannels+1]=g; ((uchar*)(pUnifiedNewimage->imageData+jj*pUnifiedNewimage->widthStep))[ii*pUnifiedNewimage->nChannels+2]=r; } //if else { continue; //b=0; //g=0; //r=0; } //下面把值赋给frame0 // ((uchar*)(pUnifiedNewimage->imageData+jj*pUnifiedNewimage->widthStep))[ii*pUnifiedNewimage->nChannels+0]=b; // ((uchar*)(pUnifiedNewimage->imageData+jj*pUnifiedNewimage->widthStep))[ii*pUnifiedNewimage->nChannels+1]=g; // ((uchar*)(pUnifiedNewimage->imageData+jj*pUnifiedNewimage->widthStep))[ii*pUnifiedNewimage->nChannels+2]=r; } } //保存新的拼接图到unifiedimage cvReleaseImage(&unifiedimage); unifiedimage=NULL; //unifiedimage=cvCreateImage(newSize,pImage->depth,pImage->nChannels); unifiedimage=cvCloneImage(pUnifiedNewimage); //memcpy(unifiedimage->imageData,pUnifiedNewimage->imageData,(pUnifiedNewimage->widthStep)*(pUnifiedNewimage->height)); cvReleaseImage(&pUnifiedNewimage); pUnifiedNewimage=NULL; }//else //if(unifiedimage->width>MAXWIDTH || unifiedimage->height>MAXHEIGHT) if(unifiedimage->width*unifiedimage->height>MAXSIZE) { return 2; } // else // { // char sTempPath[1024]; // strcpy(sTempPath,sSavePath); // strcat(sTempPath,"result.jpg"); // // cvSaveImage(sTempPath,unifiedimage); // return 1; // } return 1; }