由于opencv支持的图像格式有限,为了弥补这个不足,使用FreeImage进行格式转换。
基于磁盘文件的图像格式转换:
int main() { FreeImage_Initialise(); const char* srcImagePath = "E:/Desktop/01.tif"; const char* dstImagePath = "E:/Desktop/01.png"; int srcImageFormat = FreeImage_GetFIFFromFilename(srcImagePath); FIBITMAP *dib = FreeImage_Load(static_cast<FREE_IMAGE_FORMAT>(srcImageFormat), srcImagePath); if (dib == NULL) { cout << "图像加载失败" << endl; return 0; } unsigned int width = FreeImage_GetWidth(dib); unsigned int height = FreeImage_GetHeight(dib); int dstImageFormat = FreeImage_GetFIFFromFilename(dstImagePath); if (!FreeImage_Save(static_cast<FREE_IMAGE_FORMAT>(dstImageFormat), dib, dstImagePath)) { cout << "图像保存失败" << endl; return 0; } FreeImage_DeInitialise(); return 0; }
基于内存的图像格式转换:
IplImage* FIBITMAP2IplImage(FIBITMAP *dib) { if (!dib) return NULL; int nClrUsed = FreeImage_GetColorsUsed(dib); int nBpp = FreeImage_GetBPP(dib); int height = FreeImage_GetHeight(dib); int width = FreeImage_GetWidth(dib); RGBQUAD* pPalette = FreeImage_GetPalette(dib); int nChannel = 3; if (!nClrUsed && !pPalette) //无调色板图像处理 { IplImage* iplImg = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, nChannel); iplImg->origin = 1; for (int y = 0; y < height; y++) { BYTE* pLine = (BYTE*)iplImg->imageData + y * iplImg->widthStep; BYTE* psrcLine = (BYTE*)FreeImage_GetScanLine(dib, y); for (int x = 0; x < nChannel*width; x++) { *pLine++ = *psrcLine++; } } FreeImage_Unload(dib); FreeImage_DeInitialise(); return iplImg; } else if (pPalette)//索引图像处理 { IplImage* iplImg = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, nChannel); iplImg->origin = 1; BYTE intensity; BYTE* pIntensity = &intensity; for (int y = 0; y < height; y++) { BYTE* pLine = (BYTE*)iplImg->imageData + y * iplImg->widthStep; for (int x = 0; x < width; x++) { FreeImage_GetPixelIndex(dib, x, y, pIntensity); pLine[x * 3] = pPalette[intensity].rgbBlue; pLine[x * 3 + 1] = pPalette[intensity].rgbGreen; pLine[x * 3 + 2] = pPalette[intensity].rgbRed; } } FreeImage_Unload(dib); FreeImage_DeInitialise(); return iplImg; } else { FreeImage_Unload(dib); FreeImage_DeInitialise(); return NULL; } } int main() { const char* srcImagePath = "E:/Desktop/01.tif"; const char* dstImagePath = "E:/Desktop/01.png"; //read image file to bytes FILE* f = fopen(srcImagePath, "rb"); fseek(f, 0, SEEK_END); long size = ftell(f); rewind(f); unsigned char* data = new unsigned char[size]; fread(data, sizeof(char), size, f); fclose(f); FreeImage_Initialise(); //read bytes to image object FIMEMORY * memory = FreeImage_OpenMemory(data, size); FREE_IMAGE_FORMAT srcformat = FreeImage_GetFileTypeFromMemory(memory, 0); FIBITMAP *bitmap = FreeImage_LoadFromMemory(srcformat, memory, 0); FreeImage_CloseMemory(memory); //show image object /*IplImage *pImg = FIBITMAP2IplImage(bitmap); cvNamedWindow("Test"); cvShowImage("Test", pImg); cvWaitKey(0);*/ //convert image object type FIMEMORY* hmem = FreeImage_OpenMemory(); FreeImage_SaveToMemory(FreeImage_GetFIFFromFilename(dstImagePath), bitmap, hmem, 0); //save image object type to bytes FreeImage_SeekMemory(hmem, 0, SEEK_END); long bufferSize = FreeImage_TellMemory(hmem); unsigned char* buffer = new unsigned char[bufferSize]; unsigned int actualReadCount = bufferSize; FreeImage_SeekMemory(hmem, 0, SEEK_SET); FreeImage_ReadMemory(buffer, bufferSize, actualReadCount, hmem); FreeImage_CloseMemory(hmem); FreeImage_DeInitialise(); f = fopen(dstImagePath, "wb"); fwrite(buffer, sizeof(char), bufferSize, f); fclose(f); return 0; }