opencv2.x版本和opencv3.x版本差异的一些问题

opencv2.x版本和opencv3.x版本差异的一些问题                         

    
      

             分类:        
                           

         

1.新版本的opencv3.X系列在提升稳定性的同时也砍掉了一些常用的函数库,例如opencv2\contrib\contrib.hpp  ,这个库在opencv2.X版本中就有,到opencv3.x版本之后发现被砍掉了,还好opencv是开源的,可以直接将需要的源码部分包含到工程文件中编译就行了,一般兼容性还是不错的。
例如:在contrib.hpp中包含了一种可以直接获取当前文件夹下文件的类
  1. class CV_EXPORTS Directory  
  2.    {  
  3.        public:  
  4.            static std::vector<std::string> GetListFiles  ( const std::string& path, const std::string & exten = "*", bool addPath = true );  
  5.            static std::vector<std::string> GetListFilesR ( const std::string& path, const std::string & exten = "*", bool addPath = true );  
  6.            static std::vector<std::string> GetListFolders( const std::string& path, const std::string & exten = "*", bool addPath = true );  
  7.    };  
 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文件:
  1. #ifndef __OPENCV_CONTRIB_HPP__  
  2. #define __OPENCV_CONTRIB_HPP__  
  3. #include "opencv2/core/core.hpp"  
  4. #include "opencv2/imgproc/imgproc.hpp"  
  5. #include "opencv2/features2d/features2d.hpp"  
  6. #include "opencv2/objdetect/objdetect.hpp"  
  7. class CV_EXPORTS Directory  
  8. {  
  9. public:  
  10.     static std::vector<std::string> GetListFiles  ( const std::string& path, const std::string & exten = "*", bool addPath = true );  
  11.     static std::vector<std::string> GetListFilesR ( const std::string& path, const std::string & exten = "*", bool addPath = true );  
  12.     static std::vector<std::string> GetListFolders( const std::string& path, const std::string & exten = "*", bool addPath = true );  
  13. };  
  14. #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
 
contrib.cpp文件:
  1. #include "contrib.hpp"  
  2. //#include <cvconfig.h>  
  3. #if defined(WIN32) || defined(_WIN32)  
  4. #include <windows.h>  
  5. #include <tchar.h>  
  6. #else  
  7. #include <dirent.h>  
  8. #endif  
  9. std::vector<std::string> Directory::GetListFiles( const std::string& path, const std::string & exten, bool addPath )  
  10. {  
  11.     std::vector<std::string> list;  
  12.     list.clear();  
  13.     std::string path_f = path + "/" + exten;  
  14. #ifdef WIN32  
  15. #ifdef HAVE_WINRT  
  16.     WIN32_FIND_DATAW FindFileData;  
  17. #else  
  18.     WIN32_FIND_DATAA FindFileData;  
  19. #endif  
  20.     HANDLE hFind;  
  21. #ifdef HAVE_WINRT  
  22.     wchar_t wpath[MAX_PATH];  
  23.     size_t copied = mbstowcs(wpath, path_f.c_str(), MAX_PATH);  
  24.     CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));  
  25.     hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);  
  26. #else  
  27.     hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);  
  28. #endif  
  29.     if (hFind == INVALID_HANDLE_VALUE)  
  30.     {  
  31.         return list;  
  32.     }  
  33.     else  
  34.     {  
  35.         do  
  36.         {  
  37.             if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL  ||  
  38.                 FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||  
  39.                 FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN  ||  
  40.                 FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM  ||  
  41.                 FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)  
  42.             {  
  43.                 char* fname;  
  44. #ifdef HAVE_WINRT  
  45.                 char fname_tmp[MAX_PATH] = {0};  
  46.                 size_t copied = wcstombs(fname_tmp, FindFileData.cFileName, MAX_PATH);  
  47.                 CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));  
  48.                 fname = fname_tmp;  
  49. #else  
  50.                 fname = FindFileData.cFileName;  
  51. #endif  
  52.                 if (addPath)  
  53.                     list.push_back(path + "/" + std::string(fname));  
  54.                 else  
  55.                     list.push_back(std::string(fname));  
  56.             }  
  57.         }  
  58. #ifdef HAVE_WINRT  
  59.         while(FindNextFileW(hFind, &FindFileData));  
  60. #else  
  61.         while(FindNextFileA(hFind, &FindFileData));  
  62. #endif  
  63.         FindClose(hFind);  
  64.     }  
  65. #else  
  66.     (void)addPath;  
  67.     DIR *dp;  
  68.     struct dirent *dirp;  
  69.     if((dp = opendir(path.c_str())) == NULL)  
  70.     {  
  71.         return list;  
  72.     }  
  73.     while ((dirp = readdir(dp)) != NULL)  
  74.     {  
  75.         if (dirp->d_type == DT_REG)  
  76.         {  
  77.             if (exten.compare("*") == 0)  
  78.                 list.push_back(static_cast<std::string>(dirp->d_name));  
  79.             else  
  80.                 if (std::string(dirp->d_name).find(exten) != std::string::npos)  
  81.                     list.push_back(static_cast<std::string>(dirp->d_name));  
  82.         }  
  83.     }  
  84.     closedir(dp);  
  85. #endif  
  86.     return list;  
  87. }  
  88. std::vector<std::string> Directory::GetListFolders( const std::string& path, const std::string & exten, bool addPath )  
  89. {  
  90.     std::vector<std::string> list;  
  91.     std::string path_f = path + "/" + exten;  
  92.     list.clear();  
  93. #ifdef WIN32  
  94. #ifdef HAVE_WINRT  
  95.     WIN32_FIND_DATAW FindFileData;  
  96. #else  
  97.     WIN32_FIND_DATAA FindFileData;  
  98. #endif  
  99.     HANDLE hFind;  
  100. #ifdef HAVE_WINRT  
  101.     wchar_t wpath [MAX_PATH];  
  102.     size_t copied = mbstowcs(wpath, path_f.c_str(), path_f.size());  
  103.     CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));  
  104.     hFind = FindFirstFileExW(wpath, FindExInfoStandard, &FindFileData, FindExSearchNameMatch, NULL, 0);  
  105. #else  
  106.     hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);  
  107. #endif  
  108.     if (hFind == INVALID_HANDLE_VALUE)  
  109.     {  
  110.         return list;  
  111.     }  
  112.     else  
  113.     {  
  114.         do  
  115.         {  
  116. #ifdef HAVE_WINRT  
  117.             if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&  
  118.                 wcscmp(FindFileData.cFileName, L".") != 0 &&  
  119.                 wcscmp(FindFileData.cFileName, L"..") != 0)  
  120. #else  
  121.             if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&  
  122.                 strcmp(FindFileData.cFileName, ".") != 0 &&  
  123.                 strcmp(FindFileData.cFileName, "..") != 0)  
  124. #endif  
  125.             {  
  126.                 char* fname;  
  127. #ifdef HAVE_WINRT  
  128.                 char fname_tmp[MAX_PATH];  
  129.                 size_t copied = wcstombs(fname_tmp, FindFileData.cFileName, MAX_PATH);  
  130.                 CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));  
  131.                 fname = fname_tmp;  
  132. #else  
  133.                 fname = FindFileData.cFileName;  
  134. #endif  
  135.                 if (addPath)  
  136.                     list.push_back(path + "/" + std::string(fname));  
  137.                 else  
  138.                     list.push_back(std::string(fname));  
  139.             }  
  140.         }  
  141. #ifdef HAVE_WINRT  
  142.         while(FindNextFileW(hFind, &FindFileData));  
  143. #else  
  144.         while(FindNextFileA(hFind, &FindFileData));  
  145. #endif  
  146.         FindClose(hFind);  
  147.     }  
  148. #else  
  149.     (void)addPath;  
  150.     DIR *dp;  
  151.     struct dirent *dirp;  
  152.     if((dp = opendir(path_f.c_str())) == NULL)  
  153.     {  
  154.         return list;  
  155.     }  
  156.     while ((dirp = readdir(dp)) != NULL)  
  157.     {  
  158.         if (dirp->d_type == DT_DIR &&  
  159.             strcmp(dirp->d_name, ".") != 0 &&  
  160.             strcmp(dirp->d_name, "..") != 0 )  
  161.         {  
  162.             if (exten.compare("*") == 0)  
  163.                 list.push_back(static_cast<std::string>(dirp->d_name));  
  164.             else  
  165.                 if (std::string(dirp->d_name).find(exten) != std::string::npos)  
  166.                     list.push_back(static_cast<std::string>(dirp->d_name));  
  167.         }  
  168.     }  
  169.     closedir(dp);  
  170. #endif  
  171.     return list;  
  172. }  
  173. std::vector<std::string> Directory::GetListFilesR ( const std::string& path, const std::string & exten, bool addPath )  
  174. {  
  175.     std::vector<std::string> list = Directory::GetListFiles(path, exten, addPath);  
  176.     std::vector<std::string> dirs = Directory::GetListFolders(path, exten, addPath);  
  177.     std::vector<std::string>::const_iterator it;  
  178.     for (it = dirs.begin(); it != dirs.end(); ++it)  
  179.     {  
  180.         std::vector<std::string> cl = Directory::GetListFiles(*it, exten, addPath);  
  181.         list.insert(list.end(), cl.begin(), cl.end());  
  182.     }  
  183.     return list;  
  184. }  
#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;
}
 

 

posted @ 2017-05-03 10:23  sky20080101  阅读(952)  评论(0)    收藏  举报