bmp格式解析参考:http://blog.csdn.net/o_sun_o/article/details/8351037
void ResizeImage(IplImage **ipl, int nWidth, int nHeight) { if (*ipl){ while ((*ipl)->width >= nWidth || (*ipl)->height >= nHeight){ DWORD dwWidth = (*ipl)->width / 2; DWORD dwHeight = (*ipl)->height / 2; IplImage* iplResize = cvCreateImage(cvSize(dwWidth, dwHeight), 8, 3); if (iplResize){ cvResize((*ipl), iplResize); cvReleaseImage(&(*ipl)); (*ipl) = cvCloneImage(iplResize); cvReleaseImage(&iplResize); } } } } void WriteBMPFile(BYTE *pData, DWORD dwLen) { HANDLE hFile = CreateFile(L"F:\\1.bmp", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != INVALID_HANDLE_VALUE){ DWORD dwWrite = 0; WriteFile(hFile, pData, dwLen, &dwWrite, NULL); CloseHandle(hFile); } } void SaveBmp(IplImage *ipl, int nBpp) { BITMAPFILEHEADER bmpHeader; bmpHeader.bfType = 0x4d42; bmpHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + ipl->width * ipl->height * 3; bmpHeader.bfReserved1 = 0; bmpHeader.bfReserved2 = 0; bmpHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); BITMAPINFOHEADER bmpInfoHeader; bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfoHeader.biWidth = ipl->width; bmpInfoHeader.biHeight = ipl->height; bmpInfoHeader.biPlanes = 1; bmpInfoHeader.biBitCount = nBpp; bmpInfoHeader.biCompression = BI_RGB; bmpInfoHeader.biSizeImage = (ipl->width*nBpp + 31) / 32 * 4 * ipl->height; bmpInfoHeader.biXPelsPerMeter = 100; bmpInfoHeader.biYPelsPerMeter = 100; bmpInfoHeader.biClrUsed = 0; bmpInfoHeader.biClrImportant = 0; DWORD dwInfoSize = ipl->width*ipl->height*nBpp / 8; HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeof(bmpHeader) + sizeof(bmpInfoHeader) + dwInfoSize); BYTE *pvData = (BYTE*)GlobalLock(hGlobal); memcpy(pvData, &bmpHeader, sizeof(bmpHeader)); memcpy(pvData + sizeof(bmpHeader), &bmpInfoHeader, sizeof(bmpInfoHeader)); memcpy(pvData + sizeof(bmpHeader) + sizeof(bmpInfoHeader), ipl->imageData, dwInfoSize); GlobalUnlock(hGlobal); WriteBMPFile(pvData, sizeof(bmpHeader) + sizeof(bmpInfoHeader) + ipl->imageSize); GlobalFree(hGlobal); // 使用Bitmap完后,需要释放资源,以免造成内存泄漏。 }调用:
IplImage *ipl = cvLoadImage("F:\\nn\\m_4.bmp"); if (ipl){ //ResizeImage(&ipl, 300, 300); DWORD dwTime = GetTickCount(); cvFlip(ipl, NULL); SaveBmp(ipl, 24); cvReleaseImage(&ipl); WCHAR wcData[MAX_PATH] = {0}; wsprintf(wcData, L"%d", GetTickCount() - dwTime); OutputDebugString(wcData); } demo:http://download.csdn.net/detail/sz76211822/9812556