利用OpenCV修改图像大小,转换图像格式

来源:互联网 发布:大数据包括哪些数据 编辑:程序博客网 时间:2024/05/13 07:29

有一个提取图像直线的程序,好不容易编译过了,发现只可以处理宽度为64的倍数的bmp图片,因此写了一个将某个目录下的所有tif文件的宽度修改为64的倍数(最接近原始宽度)的数值,然后另存为bmp的程序。

1、修改图像大小只需要用函数cvResize(srcColorImage,dstColorImage,CV_INTER_CUBIC);

2、另存为图像只要用函数cvSaveImage(outputfileName.c_str(),dstColorImage);至于存储的类型,只要把后缀名定义好就直接存储为后缀名指定的类型了


该功能的源代码如下:

#include "stdafx.h"
#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;
}


原创粉丝点击