opencv2.x版本和opencv3.x版本差异的一些问题
opencv2.x版本和opencv3.x版本差异的一些问题
版权声明:本文为博主原创文章,未经博主允许不得转载。
1.新版本的opencv3.X系列在提升稳定性的同时也砍掉了一些常用的函数库,例如opencv2\contrib\contrib.hpp ,这个库在opencv2.X版本中就有,到opencv3.x版本之后发现被砍掉了,还好opencv是开源的,可以直接将需要的源码部分包含到工程文件中编译就行了,一般兼容性还是不错的。
contrib.cpp文件:
例如:在contrib.hpp中包含了一种可以直接获取当前文件夹下文件的类
- class CV_EXPORTS Directory
- {
- public:
- static std::vector<std::string> GetListFiles ( const std::string& path, const std::string & exten = "*", bool addPath = true );
- static std::vector<std::string> GetListFilesR ( const std::string& path, const std::string & exten = "*", bool addPath = true );
- static std::vector<std::string> GetListFolders( const std::string& path, const std::string & exten = "*", bool addPath = true );
- };
class CV_EXPORTS Directory
{
public:
static std::vector<std::string> GetListFiles ( const std::string& path, const std::string & exten = "*", bool addPath = true );
static std::vector<std::string> GetListFilesR ( const std::string& path, const std::string & exten = "*", bool addPath = true );
static std::vector<std::string> GetListFolders( const std::string& path, const std::string & exten = "*", bool addPath = true );
};
这个类读取文件还是很方便的,不然直接用C++来获取,还是有点麻烦,不然就只能用先进行批处理将文件信息保存在文本文件中,然后读取文本文件的方法了。但这个类在opencv3.x版本中并未发现。可以在opencv2.x找到源码.在工程中建立contrib.hpp文件和contrib.cpp文件,在主程序中直接引用就好了。
contrib.hpp文件:
- #ifndef __OPENCV_CONTRIB_HPP__
- #define __OPENCV_CONTRIB_HPP__
- #include "opencv2/core/core.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/features2d/features2d.hpp"
- #include "opencv2/objdetect/objdetect.hpp"
- class CV_EXPORTS Directory
- {
- public:
- static std::vector<std::string> GetListFiles ( const std::string& path, const std::string & exten = "*", bool addPath = true );
- static std::vector<std::string> GetListFilesR ( const std::string& path, const std::string & exten = "*", bool addPath = true );
- static std::vector<std::string> GetListFolders( const std::string& path, const std::string & exten = "*", bool addPath = true );
- };
- #endif
#ifndef __OPENCV_CONTRIB_HPP__
#define __OPENCV_CONTRIB_HPP__
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
class CV_EXPORTS Directory
{
public:
static std::vector<std::string> GetListFiles ( const std::string& path, const std::string & exten = "*", bool addPath = true );
static std::vector<std::string> GetListFilesR ( const std::string& path, const std::string & exten = "*", bool addPath = true );
static std::vector<std::string> GetListFolders( const std::string& path, const std::string & exten = "*", bool addPath = true );
};
#endif
- #include "contrib.hpp"
- //#include <cvconfig.h>
- #if defined(WIN32) || defined(_WIN32)
- #include <windows.h>
- #include <tchar.h>
- #else
- #include <dirent.h>
- #endif
- std::vector<std::string> Directory::GetListFiles( const std::string& path, const std::string & exten, bool addPath )
- {
- std::vector<std::string> list;
- list.clear();
- std::string path_f = path + "/" + exten;
- #ifdef WIN32
- #ifdef HAVE_WINRT
- WIN32_FIND_DATAW FindFileData;
- #else
- WIN32_FIND_DATAA FindFileData;
- #endif
- HANDLE hFind;
- #ifdef HAVE_WINRT
- wchar_t wpath[MAX_PATH];
- size_t copied = mbstowcs(wpath, path_f.c_str(), MAX_PATH);
- CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
- hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
- #else
- hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
- #endif
- if (hFind == INVALID_HANDLE_VALUE)
- {
- return list;
- }
- else
- {
- do
- {
- if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL ||
- FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||
- FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN ||
- FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM ||
- FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)
- {
- char* fname;
- #ifdef HAVE_WINRT
- char fname_tmp[MAX_PATH] = {0};
- size_t copied = wcstombs(fname_tmp, FindFileData.cFileName, MAX_PATH);
- CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
- fname = fname_tmp;
- #else
- fname = FindFileData.cFileName;
- #endif
- if (addPath)
- list.push_back(path + "/" + std::string(fname));
- else
- list.push_back(std::string(fname));
- }
- }
- #ifdef HAVE_WINRT
- while(FindNextFileW(hFind, &FindFileData));
- #else
- while(FindNextFileA(hFind, &FindFileData));
- #endif
- FindClose(hFind);
- }
- #else
- (void)addPath;
- DIR *dp;
- struct dirent *dirp;
- if((dp = opendir(path.c_str())) == NULL)
- {
- return list;
- }
- while ((dirp = readdir(dp)) != NULL)
- {
- if (dirp->d_type == DT_REG)
- {
- if (exten.compare("*") == 0)
- list.push_back(static_cast<std::string>(dirp->d_name));
- else
- if (std::string(dirp->d_name).find(exten) != std::string::npos)
- list.push_back(static_cast<std::string>(dirp->d_name));
- }
- }
- closedir(dp);
- #endif
- return list;
- }
- std::vector<std::string> Directory::GetListFolders( const std::string& path, const std::string & exten, bool addPath )
- {
- std::vector<std::string> list;
- std::string path_f = path + "/" + exten;
- list.clear();
- #ifdef WIN32
- #ifdef HAVE_WINRT
- WIN32_FIND_DATAW FindFileData;
- #else
- WIN32_FIND_DATAA FindFileData;
- #endif
- HANDLE hFind;
- #ifdef HAVE_WINRT
- wchar_t wpath [MAX_PATH];
- size_t copied = mbstowcs(wpath, path_f.c_str(), path_f.size());
- CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
- hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
- #else
- hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
- #endif
- if (hFind == INVALID_HANDLE_VALUE)
- {
- return list;
- }
- else
- {
- do
- {
- #ifdef HAVE_WINRT
- if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
- wcscmp(FindFileData.cFileName, L".") != 0 &&
- wcscmp(FindFileData.cFileName, L"..") != 0)
- #else
- if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
- strcmp(FindFileData.cFileName, ".") != 0 &&
- strcmp(FindFileData.cFileName, "..") != 0)
- #endif
- {
- char* fname;
- #ifdef HAVE_WINRT
- char fname_tmp[MAX_PATH];
- size_t copied = wcstombs(fname_tmp, FindFileData.cFileName, MAX_PATH);
- CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
- fname = fname_tmp;
- #else
- fname = FindFileData.cFileName;
- #endif
- if (addPath)
- list.push_back(path + "/" + std::string(fname));
- else
- list.push_back(std::string(fname));
- }
- }
- #ifdef HAVE_WINRT
- while(FindNextFileW(hFind, &FindFileData));
- #else
- while(FindNextFileA(hFind, &FindFileData));
- #endif
- FindClose(hFind);
- }
- #else
- (void)addPath;
- DIR *dp;
- struct dirent *dirp;
- if((dp = opendir(path_f.c_str())) == NULL)
- {
- return list;
- }
- while ((dirp = readdir(dp)) != NULL)
- {
- if (dirp->d_type == DT_DIR &&
- strcmp(dirp->d_name, ".") != 0 &&
- strcmp(dirp->d_name, "..") != 0 )
- {
- if (exten.compare("*") == 0)
- list.push_back(static_cast<std::string>(dirp->d_name));
- else
- if (std::string(dirp->d_name).find(exten) != std::string::npos)
- list.push_back(static_cast<std::string>(dirp->d_name));
- }
- }
- closedir(dp);
- #endif
- return list;
- }
- std::vector<std::string> Directory::GetListFilesR ( const std::string& path, const std::string & exten, bool addPath )
- {
- std::vector<std::string> list = Directory::GetListFiles(path, exten, addPath);
- std::vector<std::string> dirs = Directory::GetListFolders(path, exten, addPath);
- std::vector<std::string>::const_iterator it;
- for (it = dirs.begin(); it != dirs.end(); ++it)
- {
- std::vector<std::string> cl = Directory::GetListFiles(*it, exten, addPath);
- list.insert(list.end(), cl.begin(), cl.end());
- }
- return list;
- }
#include "contrib.hpp"
//#include <cvconfig.h>
#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#include <tchar.h>
#else
#include <dirent.h>
#endif
std::vector<std::string> Directory::GetListFiles( const std::string& path, const std::string & exten, bool addPath )
{
std::vector<std::string> list;
list.clear();
std::string path_f = path + "/" + exten;
#ifdef WIN32
#ifdef HAVE_WINRT
WIN32_FIND_DATAW FindFileData;
#else
WIN32_FIND_DATAA FindFileData;
#endif
HANDLE hFind;
#ifdef HAVE_WINRT
wchar_t wpath[MAX_PATH];
size_t copied = mbstowcs(wpath, path_f.c_str(), MAX_PATH);
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
#else
hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
#endif
if (hFind == INVALID_HANDLE_VALUE)
{
return list;
}
else
{
do
{
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL ||
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN ||
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM ||
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)
{
char* fname;
#ifdef HAVE_WINRT
char fname_tmp[MAX_PATH] = {0};
size_t copied = wcstombs(fname_tmp, FindFileData.cFileName, MAX_PATH);
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
fname = fname_tmp;
#else
fname = FindFileData.cFileName;
#endif
if (addPath)
list.push_back(path + "/" + std::string(fname));
else
list.push_back(std::string(fname));
}
}
#ifdef HAVE_WINRT
while(FindNextFileW(hFind, &FindFileData));
#else
while(FindNextFileA(hFind, &FindFileData));
#endif
FindClose(hFind);
}
#else
(void)addPath;
DIR *dp;
struct dirent *dirp;
if((dp = opendir(path.c_str())) == NULL)
{
return list;
}
while ((dirp = readdir(dp)) != NULL)
{
if (dirp->d_type == DT_REG)
{
if (exten.compare("*") == 0)
list.push_back(static_cast<std::string>(dirp->d_name));
else
if (std::string(dirp->d_name).find(exten) != std::string::npos)
list.push_back(static_cast<std::string>(dirp->d_name));
}
}
closedir(dp);
#endif
return list;
}
std::vector<std::string> Directory::GetListFolders( const std::string& path, const std::string & exten, bool addPath )
{
std::vector<std::string> list;
std::string path_f = path + "/" + exten;
list.clear();
#ifdef WIN32
#ifdef HAVE_WINRT
WIN32_FIND_DATAW FindFileData;
#else
WIN32_FIND_DATAA FindFileData;
#endif
HANDLE hFind;
#ifdef HAVE_WINRT
wchar_t wpath [MAX_PATH];
size_t copied = mbstowcs(wpath, path_f.c_str(), path_f.size());
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);
#else
hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
#endif
if (hFind == INVALID_HANDLE_VALUE)
{
return list;
}
else
{
do
{
#ifdef HAVE_WINRT
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
wcscmp(FindFileData.cFileName, L".") != 0 &&
wcscmp(FindFileData.cFileName, L"..") != 0)
#else
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
strcmp(FindFileData.cFileName, ".") != 0 &&
strcmp(FindFileData.cFileName, "..") != 0)
#endif
{
char* fname;
#ifdef HAVE_WINRT
char fname_tmp[MAX_PATH];
size_t copied = wcstombs(fname_tmp, FindFileData.cFileName, MAX_PATH);
CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
fname = fname_tmp;
#else
fname = FindFileData.cFileName;
#endif
if (addPath)
list.push_back(path + "/" + std::string(fname));
else
list.push_back(std::string(fname));
}
}
#ifdef HAVE_WINRT
while(FindNextFileW(hFind, &FindFileData));
#else
while(FindNextFileA(hFind, &FindFileData));
#endif
FindClose(hFind);
}
#else
(void)addPath;
DIR *dp;
struct dirent *dirp;
if((dp = opendir(path_f.c_str())) == NULL)
{
return list;
}
while ((dirp = readdir(dp)) != NULL)
{
if (dirp->d_type == DT_DIR &&
strcmp(dirp->d_name, ".") != 0 &&
strcmp(dirp->d_name, "..") != 0 )
{
if (exten.compare("*") == 0)
list.push_back(static_cast<std::string>(dirp->d_name));
else
if (std::string(dirp->d_name).find(exten) != std::string::npos)
list.push_back(static_cast<std::string>(dirp->d_name));
}
}
closedir(dp);
#endif
return list;
}
std::vector<std::string> Directory::GetListFilesR ( const std::string& path, const std::string & exten, bool addPath )
{
std::vector<std::string> list = Directory::GetListFiles(path, exten, addPath);
std::vector<std::string> dirs = Directory::GetListFolders(path, exten, addPath);
std::vector<std::string>::const_iterator it;
for (it = dirs.begin(); it != dirs.end(); ++it)
{
std::vector<std::string> cl = Directory::GetListFiles(*it, exten, addPath);
list.insert(list.end(), cl.begin(), cl.end());
}
return list;
}


浙公网安备 33010602011771号