利用OpenCV修改影象大小,轉換影象格式
阿新 • • 發佈:2019-02-04
有一個提取影象直線的程式,好不容易編譯過了,發現只可以處理寬度為64的倍數的bmp圖片,因此寫了一個將某個目錄下的所有tif檔案的寬度修改為64的倍數(最接近原始寬度)的數值,然後另存為bmp的程式。
1、修改影象大小隻需要用函式cvResize(srcColorImage,dstColorImage,CV_INTER_CUBIC);
2、另存為影象只要用函式cvSaveImage(outputfileName.c_str(),dstColorImage);至於儲存的型別,只要把字尾名定義好就直接儲存為字尾名指定的型別了
該功能的原始碼如下:
#include "stdafx.h"#include
#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
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
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;
}