// Imagejoint.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "Imagejoint.h" #include "math.h" #include <afxwin.h> #ifdef _DEBUG #define new DEBUG_NEW #endif #include <atlimage.h>//CImage类 #include <locale.h> // The one and only application object CWinApp theApp; using namespace std; //双三次插值系数 double fs(double w) { double a=-0.5; double fs; if (abs(w)<=1) fs=(a+2)*pow(abs(w),3)-(a+3)*pow(abs(w),2)+1; else if (abs(w)>1&&abs(w)<=2) fs=a*pow(abs(w),3)-5*a*pow(abs(w),2)+8*a*abs(w)-4*a; else fs=0; return fs; } HRESULT Imagejoint(PBYTE pbSrc,int iWidth,int iHeight,double dbZoom,PBYTE pbTag,PBYTE pbFinal); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0;//表示整数类型的函数返回码。n表示整数类型,Ret是Return的缩写,表示返回值,Code表示代码。 setlocale(LC_ALL,"chs"); HMODULE hModule = ::GetModuleHandle(NULL); if (hModule != NULL) { // initialize MFC and print and error on failure CImage cImage; HRESULT hResult; //初始化一些变量 int iWidth,iHeight,iBytePerPixel,iPitch; int x,y; PBYTE pbSrc=NULL,pbTag=NULL,pbFinal=NULL;//源图、目标图 PBYTE pbImage=NULL;//load图像后存在这 PDWORD pdwImage=NULL;//用于保存图像 double dbZoom=4.5; CString str = "frame1.bmp"; LPCTSTR filename = (LPCTSTR)str; do{ if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed ")); nRetCode = 1; break; } //Load 图像到cImage对象中 hResult=cImage.Load(filename); if(hResult!=ERROR_SUCCESS) { _tprintf(_T("源图像文件名错误! ")); nRetCode= -3; break; } iWidth=cImage.GetWidth(); iHeight=cImage.GetHeight(); //分配源图内存 pbSrc = (PBYTE)malloc(iWidth*iHeight); pbFinal = (PBYTE)malloc(1280*1024); //while (dbZoom<3); //{ //分配目标图内存 if(dbZoom>1) pbTag = (PBYTE)malloc(ceil(iWidth*dbZoom)*ceil(iHeight*dbZoom)); else pbTag = (PBYTE)malloc(iWidth*iHeight); if(pbSrc==NULL || pbTag==NULL ) { _tprintf(_T("内存申请错误! ")); nRetCode= -4; break; } //cImage数据存到pbImage,后再转换为源灰度图pbSrc iPitch=cImage.GetPitch(); iBytePerPixel=(cImage.GetBPP()+7)/8; if(iBytePerPixel==3) { for(y=0;y<iHeight;y++) { //load的图像数据放到pbImage pbImage=(PBYTE)(PBYTE(cImage.GetBits())+iPitch*y);//得到的是图像初始像素地址 for(x=0;x<iWidth;x++) { //pbImage转换为灰度图pbSrc pbSrc[y*iWidth+x]=(pbImage[3*x]*0.15+pbImage[3*x+1]*0.55+pbImage[3*x+2]*0.3); } } } cImage.Destroy(); //TO DO:执行操作 hResult=Imagejoint(pbSrc,iWidth,iHeight,dbZoom,pbTag,pbFinal); if(hResult!=ERROR_SUCCESS) { _tprintf(_T("图像处理错误! ")); nRetCode= -5; break; } //处理后保存图像 iWidth=1280; iHeight=1024; cImage.Create(iWidth,-iHeight,32); iPitch=cImage.GetPitch(); for(y=0;y<iHeight;y++) { pdwImage=(PDWORD)(PBYTE(cImage.GetBits())+iPitch*y); for(x=0;x<iWidth;x++) { pdwImage[x]=pbFinal[y*iWidth+x]*0x10101; } } //待保存图像文件名 CString name1="target"; CString name2; name2.Format(_T("%.1lf"),dbZoom); CString csTagName; csTagName=name1+name2; //CString csTagName="target"; csTagName.Trim(); csTagName.MakeUpper(); if(csTagName.Right(4)!=_T(".BMP") ) csTagName.Append(_T(".BMP")); hResult=cImage.Save(csTagName); if(hResult!=ERROR_SUCCESS) { _tprintf(_T("图像结果保存错误! ")); nRetCode= -5; break; } _tprintf(_T("图像处理成功! ")); nRetCode= ERROR_SUCCESS; break; //dbZoom=dbZoom+0.5; //} }while(0); if(pbSrc) free(pbSrc); if(pbTag) free(pbTag); } else { _tprintf(_T("Fatal Error: GetModuleHandle failed ")); nRetCode = 1; } getchar(); return nRetCode; } HRESULT Imagejoint(PBYTE pbSrc,int iWidth,int iHeight,double dbZoom,PBYTE pbTag,PBYTE pbFinal) { //旋转中心为图像中心 double rx0=iWidth; double ry0=iHeight; double srcx,srcy,u,v; int xOr,yOr; int newWidth=ceil(dbZoom*iWidth); int newHeight=ceil(dbZoom*iHeight); for (int y=0;y<ceil(dbZoom*iHeight);y++) { for (int x=0;x<ceil(dbZoom*iWidth);x++) { srcx=(double)x; srcy=(double)y ; srcx=srcx*1/dbZoom; srcy=srcy*1/dbZoom; xOr = floor(srcx); yOr = floor(srcy); u=srcx-xOr; v=srcy-yOr; if( !(srcx>=0 && srcx<=iWidth && srcy>=0 && srcy<=iHeight)) { pbTag[y*2*iWidth+x]=0;//255 } else { double middle= pbSrc[(yOr-1)*iWidth+(xOr-1)]*fs(1+u)*fs(1+v)+ pbSrc[(yOr)*iWidth+(xOr-1)]*fs(1+u)*fs(v)+ pbSrc[(yOr+1)*iWidth+(xOr-1)]*fs(1+u)*fs(1-v)+ pbSrc[(yOr+2)*iWidth+(xOr-1)]*fs(1+u)*fs(2-v)+ pbSrc[(yOr-1)*iWidth+(xOr)]*fs(u)*fs(1+v)+ pbSrc[(yOr)*iWidth+(xOr)]*fs(u)*fs(v)+ pbSrc[(yOr+1)*iWidth+(xOr)]*fs(u)*fs(1-v)+ pbSrc[(yOr+2)*iWidth+(xOr)]*fs(u)*fs(2-v)+ pbSrc[(yOr-1)*iWidth+(xOr+1)]*fs(1-u)*fs(1+v)+ pbSrc[(yOr)*iWidth+(xOr+1)]*fs(1-u)*fs(v)+ pbSrc[(yOr+1)*iWidth+(xOr+1)]*fs(1-u)*fs(1-v)+ pbSrc[(yOr+2)*iWidth+(xOr+1)]*fs(1-u)*fs(2-v)+ pbSrc[(yOr-1)*iWidth+(xOr+2)]*fs(2-u)*fs(1+v)+ pbSrc[(yOr)*iWidth+(xOr+2)]*fs(2-u)*fs(v)+ pbSrc[(yOr+1)*iWidth+(xOr+2)]*fs(2-u)*fs(1-v)+ pbSrc[(yOr+2)*iWidth+(xOr+2)]*fs(2-u)*fs(2-v); if(middle<=255&&middle>=0) pbTag[y*newWidth+x]=middle; else if(middle>255) pbTag[y*newWidth+x]=255; else pbTag[y*newWidth+x]=0; } } } for (int y=0;y<1024;y++) { for (int x=0;x<1280;x++) { int y2=newHeight/2-512; int x2=newWidth/2-640; pbFinal[y*1280+x]=pbTag[(y2+y)*newWidth+(x2+x)]; } } return ERROR_SUCCESS; }
改为相位:
// Imagejoint.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "Imagejoint.h" #include "math.h" #include <afxwin.h> #ifdef _DEBUG #define new DEBUG_NEW #endif #include <atlimage.h>//CImage类 #include <locale.h> // The one and only application object CWinApp theApp; using namespace std; //双三次插值系数 double fs(double w) { double a=-0.5; double fs; if (abs(w)<=1) fs=(a+2)*pow(abs(w),3)-(a+3)*pow(abs(w),2)+1; else if (abs(w)>1&&abs(w)<=2) fs=a*pow(abs(w),3)-5*a*pow(abs(w),2)+8*a*abs(w)-4*a; else fs=0; return fs; } HRESULT Imagejoint(PBYTE pbSrc,int iWidth,int iHeight,double dbZoom,PBYTE pbTag,PBYTE pbFinal); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0;//表示整数类型的函数返回码。n表示整数类型,Ret是Return的缩写,表示返回值,Code表示代码。 setlocale(LC_ALL,"chs"); HMODULE hModule = ::GetModuleHandle(NULL); if (hModule != NULL) { // initialize MFC and print and error on failure CImage cImage; HRESULT hResult; //初始化一些变量 int iWidth,iHeight,iBytePerPixel,iPitch; int x,y; PBYTE pbSrc=NULL,pbTag=NULL,pbFinal=NULL;//源图、目标图 PBYTE pbImage=NULL;//load图像后存在这 PDWORD pdwImage=NULL;//用于保存图像 double dbZoom=5.0; CString str = "frame1.bmp"; LPCTSTR filename = (LPCTSTR)str; do{ if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed ")); nRetCode = 1; break; } //Load 图像到cImage对象中 hResult=cImage.Load(filename); if(hResult!=ERROR_SUCCESS) { _tprintf(_T("源图像文件名错误! ")); nRetCode= -3; break; } iWidth=cImage.GetWidth(); iHeight=cImage.GetHeight(); //分配源图内存 pbSrc = (PBYTE)malloc(iWidth*iHeight); pbFinal = (PBYTE)malloc(1280*1024); //while (dbZoom<3); //{ //分配目标图内存 if(dbZoom>1) pbTag = (PBYTE)malloc(ceil(iWidth*dbZoom)*ceil(iHeight*dbZoom)); else pbTag = (PBYTE)malloc(iWidth*iHeight); if(pbSrc==NULL || pbTag==NULL ) { _tprintf(_T("内存申请错误! ")); nRetCode= -4; break; } //cImage数据存到pbImage,后再转换为源灰度图pbSrc iPitch=cImage.GetPitch(); iBytePerPixel=(cImage.GetBPP()+7)/8; if(iBytePerPixel==3) { for(y=0;y<iHeight;y++) { //load的图像数据放到pbImage pbImage=(PBYTE)(PBYTE(cImage.GetBits())+iPitch*y);//得到的是图像初始像素地址 for(x=0;x<iWidth;x++) { //pbImage转换为灰度图pbSrc pbSrc[y*iWidth+x]=(pbImage[3*x]*0.15+pbImage[3*x+1]*0.55+pbImage[3*x+2]*0.3); } } } cImage.Destroy(); //TO DO:执行操作 hResult=Imagejoint(pbSrc,iWidth,iHeight,dbZoom,pbTag,pbFinal); if(hResult!=ERROR_SUCCESS) { _tprintf(_T("图像处理错误! ")); nRetCode= -5; break; } //处理后保存图像 iWidth=1280; iHeight=1024; cImage.Create(iWidth,-iHeight,32); iPitch=cImage.GetPitch(); for(y=0;y<iHeight;y++) { pdwImage=(PDWORD)(PBYTE(cImage.GetBits())+iPitch*y); for(x=0;x<iWidth;x++) { pdwImage[x]=pbFinal[y*iWidth+x]*0x10101; } } //待保存图像文件名 CString name1="target"; CString name2; name2.Format(_T("%.1lf"),dbZoom); CString csTagName; csTagName=name1+name2; //CString csTagName="target"; csTagName.Trim(); csTagName.MakeUpper(); if(csTagName.Right(4)!=_T(".BMP") ) csTagName.Append(_T(".BMP")); hResult=cImage.Save(csTagName); if(hResult!=ERROR_SUCCESS) { _tprintf(_T("图像结果保存错误! ")); nRetCode= -5; break; } _tprintf(_T("图像处理成功! ")); nRetCode= ERROR_SUCCESS; break; //dbZoom=dbZoom+0.5; //} }while(0); if(pbSrc) free(pbSrc); if(pbTag) free(pbTag); } else { _tprintf(_T("Fatal Error: GetModuleHandle failed ")); nRetCode = 1; } getchar(); return nRetCode; } HRESULT Imagejoint(PBYTE pbSrc,int iWidth,int iHeight,double dbZoom,PBYTE pbTag,PBYTE pbFinal) { double phase[33]={0};//16相位,包含端点存在33个距离 for (int i=0;i<33;i++) { double i2=1.0*i; phase[i]=fs(i2/16); } //旋转中心为图像中心 double rx0=iWidth; double ry0=iHeight; double srcx,srcy,u,v; int xOr,yOr; int newWidth=ceil(dbZoom*iWidth); int newHeight=ceil(dbZoom*iHeight); for (int y=0;y<ceil(dbZoom*iHeight);y++) { for (int x=0;x<ceil(dbZoom*iWidth);x++) { srcx=(double)x; srcy=(double)y ; srcx=srcx*1/dbZoom; srcy=srcy*1/dbZoom; xOr = floor(srcx); yOr = floor(srcy); u=srcx-xOr; v=srcy-yOr; int phasex=floor(16*u+0.5);//16相位 int phasey=floor(16*v+0.5); double A1,B1,C1,D1,A2,B2,C2,D2; A1=phase[16+phasex]; B1=phase[phasex]; C1=phase[16-phasex]; D1=phase[32-phasex]; A2=phase[16+phasey]; B2=phase[phasey]; C2=phase[16-phasey]; D2=phase[32-phasey]; if( !(srcx>=0 && srcx<=iWidth && srcy>=0 && srcy<=iHeight)) { pbTag[y*2*iWidth+x]=0;//255 } else { double middle= pbSrc[(yOr-1)*iWidth+(xOr-1)]*A1*A2+ pbSrc[(yOr)*iWidth+(xOr-1)]*A1*B2+ pbSrc[(yOr+1)*iWidth+(xOr-1)]*A1*C2+ pbSrc[(yOr+2)*iWidth+(xOr-1)]*A1*D2+ pbSrc[(yOr-1)*iWidth+(xOr)]*B1*A2+ pbSrc[(yOr)*iWidth+(xOr)]*B1*B2+ pbSrc[(yOr+1)*iWidth+(xOr)]*B1*C2+ pbSrc[(yOr+2)*iWidth+(xOr)]*B1*D2+ pbSrc[(yOr-1)*iWidth+(xOr+1)]*C1*A2+ pbSrc[(yOr)*iWidth+(xOr+1)]*C1*B2+ pbSrc[(yOr+1)*iWidth+(xOr+1)]*C1*C2+ pbSrc[(yOr+2)*iWidth+(xOr+1)]*C1*D2+ pbSrc[(yOr-1)*iWidth+(xOr+2)]*D1*A2+ pbSrc[(yOr)*iWidth+(xOr+2)]*D1*B2+ pbSrc[(yOr+1)*iWidth+(xOr+2)]*D1*C2+ pbSrc[(yOr+2)*iWidth+(xOr+2)]*D1*D2; if(middle<=255&&middle>=0) pbTag[y*newWidth+x]=middle; else if(middle>255) pbTag[y*newWidth+x]=255; else pbTag[y*newWidth+x]=0; } } } for (int y=0;y<1024;y++) { for (int x=0;x<1280;x++) { int y2=newHeight/2-512; int x2=newWidth/2-640; pbFinal[y*1280+x]=pbTag[(y2+y)*newWidth+(x2+x)]; } } return ERROR_SUCCESS; }