OpenCV - 从文件夹加载最新图像
OpenCV - Load newest image from folder
我想开发一个 PhotoBooth,为此,我想在 Raspberry Pi 上显示我用 C++ 用 OpenCV 制作的最后一张照片。所以当源文件夹中有新照片时,必须加载这张照片。如何加载最新照片?
到目前为止我的代码。我加载了一个名为“1.bmp”的特定图像,但我想加载一个非特定名称:
int main()
{
Mat image;
image = imread("1.bmp");
namedWindow( "Display", WINDOW_AUTOSIZE );
imshow( "Display", image );
waitKey(0);
return 0;
}
感谢您的回答。
我不是 C++ 程序员,但这应该能让您很好地了解如何去做:
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
int main()
{
struct dirent *drnt;
struct stat stbuf;
DIR *dr;
// Open the current directory
dr=opendir(".");
long long newest=0;
std::string name;
// Iterate through directory entries
while (drnt=readdir(dr)){
// stat the entry to get its type and age
stat(drnt->d_name,&stbuf);
// Check files only - not directories. You may want to do a strcmp() against "BMP" or "bmp" here
if(S_ISREG(stbuf.st_mode)){
long long ns=stbuf.st_mtimespec.tv_sec * 1000000000 + stbuf.st_mtimespec.tv_nsec;
std::cout << drnt->d_name << ": " << ns << std::endl;
// Note this puppy if newer than current newest
if(ns>newest){
newest=ns;
name=drnt->d_name;
}
}
}
closedir(dr);
// Output name of newest
std::cout << name << std::endl;
}
我想开发一个 PhotoBooth,为此,我想在 Raspberry Pi 上显示我用 C++ 用 OpenCV 制作的最后一张照片。所以当源文件夹中有新照片时,必须加载这张照片。如何加载最新照片?
到目前为止我的代码。我加载了一个名为“1.bmp”的特定图像,但我想加载一个非特定名称:
int main()
{
Mat image;
image = imread("1.bmp");
namedWindow( "Display", WINDOW_AUTOSIZE );
imshow( "Display", image );
waitKey(0);
return 0;
}
感谢您的回答。
我不是 C++ 程序员,但这应该能让您很好地了解如何去做:
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
int main()
{
struct dirent *drnt;
struct stat stbuf;
DIR *dr;
// Open the current directory
dr=opendir(".");
long long newest=0;
std::string name;
// Iterate through directory entries
while (drnt=readdir(dr)){
// stat the entry to get its type and age
stat(drnt->d_name,&stbuf);
// Check files only - not directories. You may want to do a strcmp() against "BMP" or "bmp" here
if(S_ISREG(stbuf.st_mode)){
long long ns=stbuf.st_mtimespec.tv_sec * 1000000000 + stbuf.st_mtimespec.tv_nsec;
std::cout << drnt->d_name << ": " << ns << std::endl;
// Note this puppy if newer than current newest
if(ns>newest){
newest=ns;
name=drnt->d_name;
}
}
}
closedir(dr);
// Output name of newest
std::cout << name << std::endl;
}