有一個提取圖像直線的程序,好不容易編譯過了,發現只可以處理寬度爲64的倍數的bmp圖片,因此寫了一個將某個目錄下的所有tif文件的寬度修改爲64的倍數(最接近原始寬度)的數值,然後另存爲bmp的程序。
1、修改圖像大小只需要用函數cvResize(srcColorImage,dstColorImage,CV_INTER_CUBIC);
2、另存爲圖像只要用函數cvSaveImage(outputfileName.c_str(),dstColorImage);至於存儲的類型,只要把後綴名定義好就直接存儲爲後綴名指定的類型了
該功能的源代碼如下:
#include <stdio.h>
#include <afx.h>
#include <Windows.h>
#include "opencv\highgui.h"
#include "opencv\cxcore.h"
#include "opencv\cv.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <math.h>
using namespace std;
using namespace cv;
#define LEN 1024
int main(int argc, char* argv[])
{
char* Path = "E:\\Documents\\KT\\居民地提取\\line\\ExtractStraightLine\\HoughOpenCV\\onepic";
vector<string> fileList;
string strTmp;
WIN32_FIND_DATA FindData;
HANDLE hError;
int FileCount = 0;
char FilePathName[LEN];
//構造路徑
char FullPathName[LEN];
strcpy(FilePathName, Path);
strcat(FilePathName, "\\*.tif");//這裏指定要讀取的文件類型
hError = FindFirstFile(FilePathName, &FindData);
if (hError == INVALID_HANDLE_VALUE)
{
printf("搜索失敗!");
return 0;
}
else
{
//記錄下第一個文件名
wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName);
strTmp = FullPathName;
fileList.push_back(strTmp);
}
while(::FindNextFile(hError, &FindData))
{
//過慮.和..
if (strcmp(FindData.cFileName, ".") == 0 || strcmp(FindData.cFileName, "..") == 0 )
{
continue;
}
//構造完整路徑
wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName);
FileCount++;
// 輸出本級的文件,如果需要遍歷子目錄則需要設計一個遞歸函數
printf("\n%d %s ", FileCount, FullPathName);
strTmp = FullPathName;
fileList.push_back(strTmp);
}
//////////////////////////////////////////////////////////////////////////////////////
// 循環處理每個待處理的圖像
int fileCount = fileList.size();
if(fileCount < 1)
{
printf("There is no file in this folder.\n");
return 0;
}
string tmpFileName;
char filename[100];
string outFileName;
CvSize srcSize;
CvSize dstSize;
IplImage* srcColorImage;
IplImage* dstColorImage;
for(int i = 0;i<fileCount;i++)
{
tmpFileName = fileList.at(i);
strcpy(filename,tmpFileName.c_str());
srcColorImage = cvLoadImage(filename,CV_LOAD_IMAGE_COLOR);
srcSize.width = srcColorImage->width;
srcSize.height = srcColorImage->height;
int nScale = srcSize.width / 64;
if(nScale < 1)
nScale = 1;
dstSize.width = nScale * 64;
dstSize.height = (double)(dstSize.width * srcSize.height/ srcSize.width) ;
dstColorImage = cvCreateImage(dstSize,srcColorImage->depth,srcColorImage->nChannels);
cvResize(srcColorImage,dstColorImage,CV_INTER_CUBIC);
///////////////////////////////////////////////////////////
string inputfileName = filename;
string outputfileName;
int filelength = inputfileName.size();
int aa = inputfileName.find_last_of("\\");
string OutfileName = inputfileName.substr(aa+1, filelength - aa);//獲取不包含路徑的輸入文件名,demo.tif
string OutfilePath = inputfileName.substr(0,aa); //獲取文件路徑,e:\typicaldata
int extIndex = OutfileName.find_last_of(".");
string OutfileNameWithoutExt = OutfileName.substr(0,extIndex); //獲取不包含擴展名的輸入文件名 demo
outputfileName = OutfilePath + "\\bmp\\" + OutfileNameWithoutExt + ".bmp";
//////////////////////////////////////////////////////////
cvSaveImage(outputfileName.c_str(),dstColorImage);
cvReleaseImage(&srcColorImage);
cvReleaseImage(&dstColorImage);
}
return 0;
}