我现在正在OpenCV中尝试BackgroundSubtractorMOG和BackgroundSubtractorMOG2,我想尝试jpg图像序列作为我的帧的来源
frame = imread("C:\images\001-capture.jpg");
if(!frame.data){
//error in opening the first image
cerr << "Unable to open first image frame: " << fistFrameFilename << endl;
//exit(EXIT_FAILURE);
}
另外,因为当我使用VideoCapture访问我的.avi视频片段时出现错误。
VideoCapture capture("C:\movie1.avi");
if(!capture.isOpened()){
//error in opening the video input
cerr << "Unable to open video file: " << videoFilename << endl;
exit(EXIT_FAILURE);
}
//read input data. ESC or 'q' for quitting
while( (char)keyboard != 'q' && (char)keyboard != 27 ){
//read the current frame
if(!capture.read(frame)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
答案 0 :(得分:4)
如果图像编号正确,VideoCapture也可以读取图像序列:
VideoCapture capture(“C:/images/%3d-capture.jpg”); //顺便说一句,与上面相同的斜线修改
答案 1 :(得分:2)
@berak 所声明的VideoCapture方法是正确的;虽然我在使用它时不可避免地遇到了问题。在阅读连续图像时,我总是更喜欢下面所述的更直接的方法。它可以让您更好地控制遍历数据的方式,同时不限制速度。
char* Dataset_Dir( "C:/Data/" ); // Or take it from argv[1]
cv::Mat normal_matrix;
std::vector<cv::Mat>* image_stack;
for( int i=1; i<=endNumber; ++i )
{
// Gives the entire stack of images for you to go through
image_stack->push_back(cv::imread(std::format("%s/%03d-capture.png", Dataset, i), CV_LOAD_IMAGE_COLOR));
normal_matrix = cv::imread(std::format("%s/%03d-capture.png", Dataset, i), CV_LOAD_IMAGE_COLOR);
}
答案 2 :(得分:1)
我尝试过这段代码cv::VideoCapture cap("G:/var/cache/zoneminder/events/1/13/10/21/07/50/00/%3d-capture.jpg");
,但它确实有效。但是,图像文件的文件名应该按顺序读取。
答案 3 :(得分:-1)
步骤:
我的文本文件有条目:
/home/user/Desktop/Test/001.ak47/001_ /home/user/Desktop/Test/002.american-flag/002_ /home/user/Desktop/Test/007.bat/007_ /home/user/Desktop/Test/014.blimp/014_ /home/user/Desktop/Test/022.buddha-101/022 _
sprintf(path,"%s%04d.jpg",name,i);
部分代码会附加文件名。
我的文件格式001_0001.jpg
适用于文件夹001.ak47
,依此类推
代码从每个文件夹中读取20张图像并显示它。
int main ()
{
ifstream file("/home/user/Desktop/Test/train.txt");
string temp;
string string2;
int count = 0; // number of folders
int number_of_folders = 5 ;
char name[70];
char path[70];
while(count != number_of_folders)
{
getline(file, temp); // read first line of folder //basically path to first folder
int i=1;
strcpy(name, temp.c_str());
while(1)
{
sprintf(path,"%s%04d.jpg",name,i);
Mat src= imread(path,1);
if(!src.data || src.rows == 0 || i == 21 ) break; //use only 20 images for training
imshow("src",src);
i++;
waitKey();
}
count = count+1 ;
if(count == number_of_folders)
break;
}
file.close();
return(0);
}