利用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;
}
#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;
}
- 利用OpenCV修改图像大小,转换图像格式
- opencv--转换图像大小
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCV中IplImage图像格式与 BYTE图像数据的转换
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCV中IplImage图像格式与 BYTE图像数据的转换
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- OpenCv中IplImage图像格式与BYTE图像数据的转换
- opencv中的图像格式转换
- OpenCV批量修改图像大小
- opencv 3.0 图像格式转换 cvtColor
- Android图像格式类及图像转换方法
- OpenCV学习——图像格式转换
- OpenCV与Qt 中图像格式的转换
- QImage与OpenCV中的MAT图像格式转换
- opencv中怎么修改图像大小,增加几行
- SNMP协议详解<一>
- Wp8开发环境搭建总结
- 40道易错C++面试题
- 托管线程状态
- 字符串之间转换
- 利用OpenCV修改图像大小,转换图像格式
- Makefile与Kconfig和.config使用
- 窗口window 和 视口 viewport 恐龙平铺 源代码
- cocos2d-x的多点触碰事件
- libevent学习资料
- 织梦DEDECMS安装发现fsockopen 函数被禁用,解决自带采集问题
- Javascript 面向对象编程(一):封装
- UserLock用于学校防止用户共享Windows网络登录
- 数据库设计三范式