获取图片名
将所有图片放到一个文件内,建立txt 输入
dir *.jpg/B > namelist.txt
保存好后,修改后缀名为.bat,运行,得到文件夹内所有jpg图片的namelist.(也可以是.png .bmp)
按名裁剪图片
#include<iostream>
#include<time.h>
#include<fstream>
#include <opencv2\highgui\highgui.hpp>
//#include <opencv2/highgui>
//dir *.jpg/B > namelist.txt
using namespace std;
using namespace cv;
int main()
{
ifstream file("D:/Reconstruction/test_room/namelist.txt");//namelist所在路径,所有路径按需修改
int img_index = 0;
//while (!file.eof())
while (file.peek()!=EOF)
{
char txt_cont[5000];
file.getline(txt_cont, 5000);
char img_file[5000], save_file[5000];
sprintf(img_file, "D:/Reconstruction/test_room/%s", txt_cont);
printf("crrent path: %s",img_file);
sprintf(save_file, "D:/Reconstruction/test_room/%d.jpg", img_index);
IplImage *src = cvLoadImage(img_file);
IplImage *outimg;
//cvNamedWindow("original img", 0);
//cvResizeWindow("original img", 500, 500);
//cvShowImage("original img", src);//can not show the src image in the window "originaln img"
cvWaitKey(0);
cvSetImageROI(src, cvRect(580, 170, 770, 750));//<---起点(左上点)(10,10) 宽770,长750,按需修改
//cvNamedWindow("imageROI", 0);
//cvShowImage("imageROI", src);
outimg = cvCreateImage(cvSize(770, 750), src->depth, src->nChannels);//<--根据你剪的ROI区域修改
cvCopy(src, outimg);
img_index++;
cvSaveImage(save_file, outimg);
//cvWaitKey(0);
cvReleaseImage(&src);
cvReleaseImage(&outimg);
}
return 0;
}
小问题: 0x76a64662 处有未经处理的异常: Microsoft C++ 异常: 内存位置 0x007eb760 处的 cv::Exception。
- C/c++语言中,反斜杠’\‘表示转义字符,所以绝对路径需要如下表示
FILE * fp;
fp = fopen("E:\\test\\file\\data\\d.txt","r");
- 也可以用相对路径表示,不受转义字符限制:
FILE * fp;
fp = fopen("E:/test/file/data/d.txt","r");