用opencv批量处理一个文件夹里的图片,运行完会出现一个警告,帮忙看一下该怎么修改,谢谢。
#include<iostream>
#include<time.h>
#include<opencv2/opencv.hpp>
#include<fstream>
using namespace std;
using namespace cv;
void main()
{
ifstream file("d:/filelist.txt");
int img_index = 0;
while (!file.eof())//当文件没有读到末尾继续循环
{
char txt_cont[24];
file.getline(txt_cont,24);
char img_file[24],save_file[24];
sprintf(img_file, "d:/pic2/%s", txt_cont);
sprintf(save_file, "d:/pic3/%d.jpg", img_index);
Mat src = imread(img_file);
Mat crs;
cvtColor(src,crs,CV_RGB2GRAY);
img_index++;
imwrite(save_file,crs);
}
}
这个看了下应该是内存越界问题 char数组那里 你可以采取把24变大的方式如改为256 或者用string来代替char数组
下面是用string的方式
#include<iostream>
#include<time.h>
#include<opencv2/opencv.hpp>
#include<fstream>
#include<string>
using namespace std;
using namespace cv;
int main()
{
ifstream file("filelist.txt");
int img_index = 0;
string txt_cont;
while (getline(file,txt_cont))//当文件没有读到末尾继续循环
{
string img_file,save_file;
img_file = "/home/pi/cv_test/pic2/" + txt_cont;
save_file = "/home/pi/cv_test/pic3/" + to_string(img_index) + ".jpg";
Mat src = imread(img_file);
Mat crs;
cvtColor(src,crs,CV_RGB2GRAY);
img_index++;
imwrite(save_file,crs);
}
return 0;
}
数组越界错误,楼上说的也是一种解决办法,或者你把你的数组带下增大点也行