bool EdgeDetection::DIBDWTStep(IplImage *pImage,int Step,int nInv) { // 循环变量 int i, j; // 获取图象的长度和宽度 int nWidth = pImage->width; int nHeight = pImage->height; int widthStep=pImage->widthStep; // 获取变换的最大层数 int nMaxWLevel = Log2(nWidth); int nMaxHLevel = Log2(nHeight); int nMaxLevel; if (nWidth == 1<<nMaxWLevel && nHeight == 1<<nMaxHLevel) nMaxLevel = MIN(nMaxWLevel, nMaxHLevel); // 临时变量 double *pDbTemp; uchar *pBits; // 如果小波变换的存储内存还没有分配,则分配此内存 if(!m_pDbImage){ m_pDbImage = new double[nWidth*nHeight]; if (!m_pDbImage) return false; // 将图象数据放入m_pDbImage中 for (j=0; j<nHeight; j++) { for (i=0; i<nWidth; i++) { double temp = (pImage->imageData + pImage->widthStep*j)[i]; if (temp < 0) temp += 256; m_pDbImage[j*nWidth+i]=temp; } } } // 进行小波变换(或反变换) for(Step;Step > 0;Step --) { if (!DWTStep_2D(m_pDbImage, nMaxWLevel-m_nDWTCurDepth, nMaxHLevel-m_nDWTCurDepth, nMaxWLevel, nMaxHLevel, nInv, 1, m_nSupp)) return false; // 如果是反变换,则当前层数减1 if (nInv) m_nDWTCurDepth --; // 否则加1 else m_nDWTCurDepth ++; } // 然后,将数据拷贝回原图中,并进行相应的数据转换 int lfw = nWidth>>m_nDWTCurDepth, lfh = nHeight>>m_nDWTCurDepth; for (j=0; j<nHeight; j++) { for (i=0; i<nWidth; i++) { //(pImage->imageData + pImage->widthStep*j)[i]=m_pDbImage[j*nWidth+i]; if (j<lfh && i<lfw) { /*if(!nInv) //小波增强 m_pDbImage[j*nWidth+i]*=1.2;*/ (pImage->imageData + pImage->widthStep*j)[i] = FloatToChar(m_pDbImage[j*nWidth+i]); } else { /*if(!nInv) //小波增强 m_pDbImage[j*nWidth+i]*=0.8;*/ (pImage->imageData + pImage->widthStep*j)[i] = FloatToChar(m_pDbImage[j*nWidth+i]) ^ 0x80; } } } if(nInv) { delete m_pDbImage; m_pDbImage = NULL; } // 返回 return true; }
//收缩算法 void EdgeDetection::PassShronk() { //CVideoFrameDoc* pDoc = (CVideoFrameDoc*)GetDocument(); //IplImage *pImage = pDoc->GetImage(); // 图象的高度和宽度 int nWidth = pImage->width; int nHeight = pImage->height; int widthStep=pImage->widthStep; //小波类型及变换层数 Step=3; m_nSupp=2; //阈值 double Q=0,t=0; // 循环变量 int i, j,k,p; //临时变量 double *pDbTemp; int W = nWidth >> Step; int H = nHeight >> Step; int N = nWidth * nHeight; //图像象素数 // 获取变换的最大层数 int nMaxWLevel = Log2(nWidth); int nMaxHLevel = Log2(nHeight); int nMaxLevel; if (nWidth == 1<<nMaxWLevel && nHeight == 1<<nMaxHLevel) nMaxLevel = MIN(nMaxWLevel, nMaxHLevel); // 进行小波变换 int rsl; rsl = DIBDWTStep(pImage,1,0); for(i = nHeight / 2;i < nHeight;i ++) { // pDbTemp = m_pDbImage + i*sizeImageSave.cx; for(j = nWidth / 2;j < nWidth;j ++) Q += fabs(m_pDbImage[i]); } Q = Q / (N / 4); t = 3 * Q; rsl = DIBDWTStep(pImage,Step - 1,0); for(k = 0 ;k < Step; k ++) { if(H < nHeight) { //域值去除高高频和低高频、高低频信息 for(p = 0;p < H; p ++) { pDbTemp = m_pDbImage + p*nWidth; for (i = W; i < W * 2 ; i ++) { if(pDbTemp[i] > t) pDbTemp[i] = pDbTemp[i] - (t / (1 << k)); else if(pDbTemp[i] < (t * (-1))) pDbTemp[i] = pDbTemp[i] + (t / (1 << k)); else pDbTemp[i] = 0; } } for(p = H;p < H * 2; p ++) { pDbTemp = m_pDbImage + p*nWidth; for (i = 0; i < W ; i ++) { if(pDbTemp[i] > t) pDbTemp[i] = pDbTemp[i] - (t / (1 << k)); else if(pDbTemp[i] < (t * (-1))) pDbTemp[i] = pDbTemp[i] + (t / (1 << k)); else pDbTemp[i] = 0; } } for(p = H;p < H * 2; p ++) { pDbTemp = m_pDbImage + p*nWidth; for(i = W; i < W * 2 ; i ++) { if(pDbTemp[i] > t) pDbTemp[i] = pDbTemp[i] - (t / (1 << k)); else if(pDbTemp[i] < (t * (-1))) pDbTemp[i] = pDbTemp[i] + (t / (1 << k)); else pDbTemp[i] = 0; } } H = H * 2; W = W * 2; } } // 进行小波反变换 for(Step;Step >0;Step --) { if (!DWTStep_2D(m_pDbImage, nMaxWLevel-m_nDWTCurDepth, nMaxHLevel-m_nDWTCurDepth, nMaxWLevel, nMaxHLevel, 1, 1, m_nSupp)) return; // 是反变换,则当前层数减1 m_nDWTCurDepth --; } // 然后,将数据拷贝回原CDib中,并进行相应的数据转换 int lfw = nWidth>>m_nDWTCurDepth, lfh = nHeight>>m_nDWTCurDepth; for (j=0; j<nHeight; j++) { pDbTemp = m_pDbImage + j*nWidth; //pBits = pDib->GetpBits() + (nHeight-1-j)*sizeImageSave.cx; for (i=0; i<nWidth; i++) { //(pImage->imageData + pImage->widthStep*j)[i]=m_pDbImage[j*nWidth+i]; if (j<lfh && i<lfw) (pImage->imageData + pImage->widthStep*j)[i] = FloatToChar(m_pDbImage[j*nWidth+i]); else (pImage->imageData + pImage->widthStep*j)[i] = FloatToChar(m_pDbImage[j*nWidth+i]) ^ 0x80; } } delete m_pDbImage; m_pDbImage = NULL; // 如果小波变换不成功,则直接返回 if (!rsl) return; //cv show }
char CFloatVariable::GetChar(void) const { return FloatToChar(m_Value); }
void CCharVariable::SetFloat(const double Value) { m_Value = FloatToChar(Value); }
bool Convert(const char* pFilePath) { ModelOBJ model; FILE* pOutput = 0; SubMesh subMesh; static char pOutPath[MAX_PATH]; //open mesh if (!model.import(pFilePath)) { printf("Failed to open file '%s'!\n", pFilePath); return false; } //open output file ConvertFileName(pFilePath, pOutPath); if (fopen_s(&pOutput, pOutPath, "wb") != 0) { printf("Could not open output file '%s'!\n", pOutPath); return false; } //write basic info int verticesCount = model.getNumberOfVertices(); int indiciesCount = model.getNumberOfIndices(); int subMeshesCount = model.getNumberOfMeshes(); printf("Veritces count: %i\n", verticesCount); printf("Indicies count: %i\n", indiciesCount); printf("Submeshes count: %i\n", subMeshesCount); fwrite("nfm", 4, 1, pOutput); //signature fwrite(&verticesCount, sizeof(int), 1, pOutput); fwrite(&indiciesCount, sizeof(int), 1, pOutput); fwrite(&subMeshesCount, sizeof(int), 1, pOutput); //write verticies XVertex* pVertices = (XVertex*)malloc(verticesCount * sizeof(XVertex)); const ModelOBJ::Vertex* pModelVertices = model.getVertexBuffer(); for (int i = 0; i < verticesCount; i++) { //vertex position pVertices[i].pos[0] = pModelVertices[i].position[0]; pVertices[i].pos[1] = pModelVertices[i].position[1]; pVertices[i].pos[2] = -pModelVertices[i].position[2]; //vertex texture coordinates pVertices[i].texCoord[0] = pModelVertices[i].texCoord[0]; pVertices[i].texCoord[1] = pModelVertices[i].texCoord[1]; //vertex normal if (ISNAN(pModelVertices[i].normal[0]) || ISNAN(pModelVertices[i].normal[1]) || ISNAN(pModelVertices[i].normal[2])) { pVertices[i].normal[0] = 0; pVertices[i].normal[1] = 127; pVertices[i].normal[2] = 0; } else { pVertices[i].normal[0] = FloatToChar(pModelVertices[i].normal[0]); pVertices[i].normal[1] = FloatToChar(pModelVertices[i].normal[1]); pVertices[i].normal[2] = FloatToChar(-pModelVertices[i].normal[2]); } //vertex tangent if (ISNAN(pModelVertices[i].tangent[0]) || ISNAN(pModelVertices[i].tangent[1]) || ISNAN(pModelVertices[i].tangent[2])) { pVertices[i].tangent[0] = 127; pVertices[i].tangent[1] = 0; pVertices[i].tangent[2] = 0; } else { pVertices[i].tangent[0] = FloatToChar(pModelVertices[i].tangent[0]); pVertices[i].tangent[1] = FloatToChar(pModelVertices[i].tangent[1]); pVertices[i].tangent[2] = FloatToChar(-pModelVertices[i].tangent[2]); } pVertices[i].normal[3] = 0; pVertices[i].tangent[3] = 0; } fwrite(pVertices, sizeof(XVertex), verticesCount, pOutput); free(pVertices); //write indicies fwrite(model.getIndexBuffer(), sizeof(int), indiciesCount, pOutput); //write submeshes UINT triCounter = 0; for (int i = 0; i < model.getNumberOfMeshes(); i++) { ModelOBJ::Mesh srcMesh = model.getMesh(i); subMesh.indexOffset = srcMesh.startIndex; subMesh.triangleCount = srcMesh.triangleCount; ZeroMemory(subMesh.materialName, MAT_NAME_MAX_LENGTH); strcpy(subMesh.materialName, srcMesh.pMaterial->name.c_str()); fwrite(&subMesh, sizeof(SubMesh), 1, pOutput); } //close output file fclose(pOutput); //generate material files int matCount = model.getNumberOfMaterials(); for (int i = 0; i < matCount; i++) { const ModelOBJ::Material& mat = model.getMaterial(i); char fileName[MAX_PATH]; ExtractFileDir(pFilePath, fileName); strcat(fileName, "..\\Materials\\"); strcat(fileName, mat.name.c_str()); strcat(fileName, ".cfg"); //check if material already exists if (FileExists(fileName)) { printf("Material %s already exists. Skipping cfg generation...\n", mat.name.c_str()); continue; } FILE* pMatFile = fopen(fileName, "w"); fprintf(pMatFile, "Layers = \n(\n\t{\n"); if (mat.colorMapFilename.length()) fprintf(pMatFile, "\t\tDiffuseTexture = \"%s\"\n", mat.colorMapFilename.c_str()); if (mat.bumpMapFilename.length()) fprintf(pMatFile, "\t\tNormalTexture = \"%s\"\n", mat.bumpMapFilename.c_str()); fprintf(pMatFile, "\t}\n);"); fclose(pMatFile); } }