//旋转图像并掩码去除背景色
//x,y是图像的中心
IplImage* CLowCarbonDoc::RotateImg(IplImage* srcimg,float angle)
{
float m[6];
CvMat rotM=cvMat(2,3,CV_32F,m);
int srcwidth=srcimg->width;
int srcheight=srcimg->height;
int rotR=ceil(2.0*sqrtf(pow(srcwidth/2,2)+pow(srcheight/2,2)));
IplImage* frontimg=cvCreateImage(cvSize(rotR,rotR),srcimg->depth,srcimg->nChannels);
m[0]=cosf(-angle*2.0f*CV_PI/360.0f);
m[1]=-sinf(-angle*2.0f*CV_PI/360.0f);
m[3]=-m[1];
m[4]=m[0];
//将旋转中心移至图像中心
m[2]=srcwidth*0.5f;
m[5]=srcheight*0.5f;
cvZero(frontimg);
cvGetQuadrangleSubPix(srcimg,frontimg,&rotM);
return frontimg;
}
IplImage* CLowCarbonDoc::MaskImg(IplImage* frontimg)
{
IplImage* maskimg=cvCreateImage(cvGetSize(frontimg),IPL_DEPTH_8U,1);
cvCvtColor(frontimg,maskimg,CV_RGB2GRAY);
cvThreshold(maskimg,maskimg,200,255,CV_THRESH_BINARY);
cvNot(maskimg,maskimg);
return maskimg;
}
void CLowCarbonDoc::cvCopySubImg(IplImage* srcimg,CvRect rect,IplImage*subimg,IplImage* maskimg)
{
cvSetImageROI(srcimg,rect);
cvCopy(subimg,srcimg,maskimg);
cvResetImageROI(srcimg);
}
void CLowCarbonView::DrawBallon(IplImage* smallimg,CDC *pDC, int x, int y)
{
CLowCarbonDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
plImage* frontimg=pDoc->RotateImg(smallimg,90);
IplImage* maskimg=pDoc->MaskImg(frontimg);
int srcwidth=smallimg->width;
int srcheight=smallimg->height;
int rotR=ceil(2.0*sqrtf(pow(srcwidth/2,2)+pow(srcheight/2,2)));
pDoc->cvCopySubImg(bkimg,cvRect(x,y,rotR,rotR),frontimg,maskimg);
CRect rect;
GetClientRect(&rect);
img.CopyOf(bkimg);
img.DrawToHDC(pDC->GetSafeHdc(),&rect);
ReleaseDC(pDC);
}