void ShapeWindowFromBitmap(CImage &image,CWnd *pWnd,COLORREF keyColor) { pWnd->MoveWindow(0,0,image.GetWidth(),image.GetHeight()); if (!image.IsNull()) { CRgn crRgn, crRgnTmp; crRgn.CreateRectRgn(0,0,0,0); for (int nY = 0; nY <= image.GetHeight(); nY++) { int nX = 0; do { while (nX <= image.GetWidth() && image.GetPixel(nX, nY) == keyColor) nX++; int iLeftX = nX; while (nX <= image.GetWidth() && image.GetPixel(nX, nY) != keyColor) ++nX; crRgnTmp.CreateRectRgn(iLeftX, nY, nX, nY+1); crRgn.CombineRgn(&crRgn, &crRgnTmp, RGN_OR); crRgnTmp.DeleteObject(); }while(nX < image.GetWidth()); nX = 0; } pWnd->SetWindowRgn(crRgn,TRUE); crRgn.DeleteObject(); } }
void CChildView::OnSrtpEnvironmentbrdf() { CWaitCursor wait;//在函数执行过程中使鼠标图标变成等待图标 int newX = 256, newY = 256; CImage tmpimg; tmpimg.Create(newX, newY, 24, 0); float NoV, roughness; float* res = new float[2]; for (int x = 0; x < newX; x++){ NoV = x*1.0f / newX; for (int y = 0; y < newY; y++){ roughness = y*2.0f / newY; IntegrateBRDF(res, roughness, NoV); tmpimg.SetPixelRGB(x, y, (byte)(res[0] * 255), (byte)(res[1] * 255), 0); } } delete[] res; imgOriginal.Destroy(); imgOriginal.Create(newX, newY, tmpimg.GetBPP());//根据新的大小建立CImage,GetBPP是获取其大小 for (int x = 0; x < newX; x++){ for (int y = 0; y < newY; y++){ imgOriginal.SetPixel(x, y, tmpimg.GetPixel(x, y)); } } tmpimg.Destroy(); //刷新显示图像 Invalidate(); UpdateWindow(); }
//绘画头像 void CExpression::DrawItem(CDC * pDC, int iXPos, int iYPos, bool bSelectec) { CExpressionItem * pExpressionItem=GetExpressItem(iYPos*m_wMaxRow+iXPos); int nFaceIndex = iYPos*m_wMaxRow+iXPos; if (pExpressionItem!=NULL) { if (bSelectec) { pDC->FillSolidRect(iXPos*(EP_PIC_WHIDE+PIC_SPACE)+PIC_SPACE/2,iYPos*(EP_PIC_HIGHT+PIC_SPACE)+PIC_SPACE/2, EP_PIC_WHIDE+PIC_SPACE,EP_PIC_HIGHT+PIC_SPACE,RGB(255,255,255)); pDC->Draw3dRect(iXPos*(EP_PIC_WHIDE+PIC_SPACE)+PIC_SPACE/2,iYPos*(EP_PIC_HIGHT+PIC_SPACE)+PIC_SPACE/2, EP_PIC_WHIDE+PIC_SPACE,EP_PIC_HIGHT+PIC_SPACE,RGB(28,57,208),RGB(28,57,208)); } else { pDC->FillSolidRect(iXPos*(EP_PIC_WHIDE+PIC_SPACE)+PIC_SPACE/2,iYPos*(EP_PIC_HIGHT+PIC_SPACE)+PIC_SPACE/2, EP_PIC_WHIDE+PIC_SPACE,EP_PIC_HIGHT+PIC_SPACE,RGB(255,255,255)); } //装载图片 TCHAR szFileName[MAX_PATH]=TEXT(""); _snprintf(szFileName,sizeof(szFileName),TEXT("%s\\Face\\%d.gif"),m_szPath, nFaceIndex); CImage ImageFace; if ( SUCCEEDED(ImageFace.Load(szFileName) ) ) { //背景判断 COLORREF rfColor = ImageFace.GetPixel(0,0); TransparentBlt(pDC->GetSafeHdc(), iXPos*(EP_PIC_WHIDE+PIC_SPACE)+PIC_SPACE,iYPos*(EP_PIC_HIGHT+PIC_SPACE)+PIC_SPACE, EP_PIC_WHIDE, EP_PIC_HIGHT, ImageFace.GetDC(), 0, 0, EP_PIC_WHIDE, EP_PIC_HIGHT, rfColor); ImageFace.ReleaseDC(); } } return; }
int main() { int a;//use for pause char readParaFlag;//if read the parameters (network) which have been trained //char modifyParaFlag;// if the user want to modify the training parameters bool needTrainningFlag=false; vector<vector<double> > inSet; vector<vector<double> > outSet; vector<vector<double> > testSet; vector<vector<double> > testoutSet;//use to calculate the hit ratio in test default vector<vector<double> > selfDefSet; vector<double> temp_vec; int sample_num = SAMPLE_NUM; int test_num = TEST_NUM; int input_dimension = INPUT_DIMENSION; int output_dimension = OUTPUT_DIMENSION; int hidden_layer_neuron_num = HIDDEN_LAYER_NEURON_NUM; int max_epoch = MAX_EPOCH; double trainning_speed = TRAINNING_SPEED; double min_error =MIN_ERROR; ifstream infile; ofstream outfile;//to save the parameters //deal with the trainning data CImage img; COLORREF pixel; int maxY=LENGTH; int maxX=LENGTH; int avg; byte r,g,b; string path; char temp_char[10]; string temp_string; string pathHead="..\\Data\\Train\\"; for (int i=0;i<OUTPUT_DIMENSION;++i){ for (int j=1;j<=SAMPLE_NUM;++j){ temp_string=itoa(j,temp_char,10);//change j to an int path=pathHead+char('0'+i)+" ("+temp_string+").bmp"; img.Load((char*)path.data()); // maxY=img.GetHeight(); // maxX=img.GetWidth(); for (int x=0;x<maxX;++x){ for (int y=0;y<maxY;++y){ pixel=img.GetPixel(x,y); r=GetRValue(pixel); g=GetGValue(pixel); b=GetBValue(pixel); avg=(int)((r+g+b)/3); if (avg>128) temp_vec.push_back(1); else temp_vec.push_back(0); } } inSet.push_back(temp_vec); temp_vec.clear(); for (int x=0;x<i;++x) temp_vec.push_back(0); temp_vec.push_back(1); for (int x=i+1;x<OUTPUT_DIMENSION;++x) temp_vec.push_back(0); outSet.push_back(temp_vec); temp_vec.clear(); img.Destroy();//important!!!! } } //deal with the test data pathHead="..\\Data\\Test_Default\\"; for (int i=0;i<OUTPUT_DIMENSION;++i){ for (int j=1;j<=TEST_NUM;++j){ temp_string=itoa(j,temp_char,10);//change j to an int path=pathHead+char('0'+i)+" ("+temp_string+").bmp"; img.Load((char*)path.data()); //maxY=img.GetHeight(); //maxX=img.GetWidth(); for (int x=0;x<maxX;++x){ for (int y=0;y<maxY;++y){ pixel=img.GetPixel(x,y); r=GetRValue(pixel); g=GetGValue(pixel); b=GetBValue(pixel); avg=(int)((r+g+b)/3); if (avg>128) temp_vec.push_back(1); else temp_vec.push_back(0); } } testSet.push_back(temp_vec); temp_vec.clear(); for (int x=0;x<i;++x) temp_vec.push_back(0); temp_vec.push_back(1); for (int x=i+1;x<OUTPUT_DIMENSION;++x) temp_vec.push_back(0); testoutSet.push_back(temp_vec); temp_vec.clear(); img.Destroy(); } } /*cout<<"Do you need to read the trained parameters? y or n >> "; cin>>modifyParaFlag; if (modifyParaFlag=='y'){ needTrainningFlag = true; } */ NeuralNet network(INPUT_DIMENSION,OUTPUT_DIMENSION,HIDDEN_LAYER_NEURON_NUM,TRAINNING_SPEED,MAX_EPOCH,MIN_ERROR); cout<<"Do you need to read the trained parameters? y or n >> "; cin>>readParaFlag; if (readParaFlag=='y'){ needTrainningFlag=false; infile.open("..\\Trained_Parameters.txt"); network.readParameter(infile); infile.close(); } else if (readParaFlag=='n'){ needTrainningFlag=true; outfile.open("..\\Trained_Parameters.txt"); network.train(inSet,outSet); network.saveParameter(outfile); outfile.close(); } else { cout<<"error input!\n"; return -1; } cout<<endl<<endl; //default test int count=0; int hitcount=0; for (int i=0;i<OUTPUT_DIMENSION;++i) { for (int j=0;j<TEST_NUM;++j) { network.calculateOutput(testSet[count],temp_vec); cout<<"recognize bmp "<<i<<"("<<j+1<<")"<<endl; for (int k=0;k<OUTPUT_DIMENSION;++k) cout<<temp_vec[k]<<" "; cout<<endl; int index=0; for (int k=0;k<OUTPUT_DIMENSION;++k){ if (temp_vec[k]>temp_vec[index]) index=k; } cout<<"This number is likely to be "<<index<<endl; if (testoutSet[count][index]==1) {cout<<"hit\n";++hitcount;} temp_vec.clear(); ++count; } } cout<<"------------------------------------------\n"; cout<<"Hit ratio: "<<double(hitcount)/count<<endl; int self_define_num; cout<<"\nPlz input the image number you want to recognize>> "; cin>>self_define_num; pathHead="..\\Data\\Test_User\\"; for (int i=0;i<self_define_num;++i){ // if input n need bmp from 0.bmp to N-1.bmp temp_string=itoa(i,temp_char,10);//change j to an int path=pathHead+temp_string+".bmp"; img.Load((char*)path.data()); for (int x=0;x<maxX;++x){ for (int y=0;y<maxY;++y){ pixel=img.GetPixel(x,y); r=GetRValue(pixel); g=GetGValue(pixel); b=GetBValue(pixel); avg=(int)((r+g+b)/3); if (avg>128) temp_vec.push_back(1); else temp_vec.push_back(0); } } selfDefSet.push_back(temp_vec); temp_vec.clear(); img.Destroy(); } for (int i=0;i<self_define_num;++i){ network.calculateOutput(selfDefSet[i],temp_vec); cout<<"recognize "<<i<<".bmp"<<endl; //for (int k=0;k<OUTPUT_DIMENSION;++k) cout<<temp_vec[k]<<" "; //cout<<endl; int index=0; for (int k=0;k<OUTPUT_DIMENSION;++k){ if (temp_vec[k]>temp_vec[index]) index=k; } cout<<"This number is likely to be "<<index<<endl; temp_vec.clear(); } cout<<"Press any key and enter to exit>> "; cin>>a; return 0; }
BOOL TransformImage(const CImage& original,CImage& transformed,CImage& originalProcessed,CImage& transformedProcessed) { COMPLEX_NUMBER** data = (COMPLEX_NUMBER**)malloc(sizeof(COMPLEX_NUMBER*)*original.GetHeight()); for(long y = 0; y < original.GetHeight(); ++y) { data[y] = (COMPLEX_NUMBER*)malloc(sizeof(COMPLEX_NUMBER)*original.GetWidth()); for(long x = 0; x < original.GetWidth(); ++x) { data[y][x].real = ((LONG)GetRValue(original.GetPixel(x,y)) + (LONG)GetGValue(original.GetPixel(x,y)) + (LONG)GetBValue(original.GetPixel(x,y))) / 3.0f; data[y][x].imag = 0; } } FFT2D(data,original.GetWidth(),original.GetHeight(),1); if(!transformed.Create(original.GetWidth(),original.GetHeight(),24)) { for(long y = 0; y < original.GetHeight(); ++y) free(data[y]); free(data); return FALSE; } for(long y = 0; y < original.GetHeight(); ++y) { for(long x = 0; x < original.GetWidth(); ++x) { FLOAT power = sqrtf(data[y][x].real * data[y][x].real + data[y][x].imag * data[y][x].imag); transformed.SetPixel(x < original.GetWidth()/2 ? original.GetWidth()/2 + x : x - original.GetWidth()/2,y < original.GetHeight()/2 ? original.GetHeight()/2 + y : y - original.GetHeight()/2,RGB(power*128,power*128,power*128)); } } /// AmplitudeBandpassFilter(data,original.GetWidth(),original.GetHeight(),0/128.0f,150/128.0f); //FrequencyBandpassFilter(data,original.GetWidth(),original.GetHeight(),50,100); /// if(!transformedProcessed.Create(original.GetWidth(),original.GetHeight(),24)) { for(long y = 0; y < original.GetHeight(); ++y) free(data[y]); free(data); return FALSE; } for(long y = 0; y < original.GetHeight(); ++y) { for(long x = 0; x < original.GetWidth(); ++x) { FLOAT power = sqrtf(data[y][x].real * data[y][x].real + data[y][x].imag * data[y][x].imag); transformedProcessed.SetPixel(x < original.GetWidth()/2 ? original.GetWidth()/2 + x : x - original.GetWidth()/2,y < original.GetHeight()/2 ? original.GetHeight()/2 + y : y - original.GetHeight()/2,RGB(power*128,power*128,power*128)); } } FFT2D(data,original.GetWidth(),original.GetHeight(),-1); if(!originalProcessed.Create(original.GetWidth(),original.GetHeight(),24)) { for(long y = 0; y < original.GetHeight(); ++y) free(data[y]); free(data); return FALSE; } for(long y = 0; y < original.GetHeight(); ++y) { for(long x = 0; x < original.GetWidth(); ++x) { FLOAT power = sqrt(data[y][x].real * data[y][x].real + data[y][x].imag * data[y][x].imag); originalProcessed.SetPixel(x,y,RGB(power,power,power)); } } for(long y = 0; y < original.GetHeight(); ++y) free(data[y]); free(data); return TRUE; }