Dlib在VS2015上的配置(亲测可用)

首先声明,这个是自己装的时候按照别人的教程装的,但是没有一个完美安装成功的,中间遇到好多问题,所以把这些教程拼凑到一块了,中间可能有一些地方不严谨,但凡稍微有点基础应该都能看懂。

1.解压Dlib

  1.首先将Dlib-19.2下载到D盘中(其他盘亦可)。 
   2.解压放置在D盘目录下。 
  

2.使用CMake制作Dlib.lib

  解压得到的文件不包含lib文件,需要用CMake制作出来,建立一个空的build文件夹作为输出文件夹。 
  


   这里写图片描述 
  

  点击Generate,选择对应的VS版本,19.2只支持VS2015,故选择VS2015 64,使用默认Compliers。 
  

这里写图片描述 

  点击finish,最后编译完成 
  

这里写图片描述 

提醒:它会报一个跟CUDA相关的错误,不用管它,我猜测是关于深度学习的应用,我电脑没有安装深度学习相关的东西所以就没理这个错误,最后结果表明确实这个错误不影响使用。

3.VS2015添加DLib库

编译完成后在build里出现一个dlib工程项目,用VS2015打开dlib.sln。 
这里写图片描述 
生成解决方案得到debug文件夹,里面既是所需要的dlib。
build的时候推荐Release模式。因为Debug模式超级慢) 
这里写图片描述 
接着在VS2015中添加DLib附加库,随便新建一个Win32控制台程序,为保证对所有工程都有效,在属性管理器里(注意如果你之前编译的Release模式的话,这里在属性管理器中也要找到对应的Release|Win32或Release|64文件夹,具体看你项目情况)进行全局更改。 
这里写图片描述 
双击Microsoft.Cpp.x64.user弹出属性页,在链接器-常规里添加附加包含目录(注意路径名字) 
这里写图片描述 
然后附加库目录 
这里写图片描述 
以及附加依赖项 
这里写图片描述 
为了打开jpg或png图片  还要在预处理器中加上DLIB_JPEG_SUPPORT和DLIB_PNG_SUPPORT(这里我本来加进去了,但是最后运行的时候总出问题,后来就先加一项,运行一下通过了,然后加上第二项,运行通过了,莫名其妙的。大家注意就行)

以上就是dlib的库添加过程。上述包含目录和附加库目录添加过程也可在VC++目录完成。测试代码在最后。

可能会出现的问题:

VS中无法解析的外部符号:

_USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent

_use_of_DEBUG_or_ENABLE_ASSERTS_preprocessor_directives_

dlib下载后,编译还是比较顺利的,但是使用后,总提示 无法解析的外部符号:

USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent

_use_of_DEBUG_or_ENABLE_ASSERTS_preprocessor_directives

看到这个错误简直要崩溃了,找了好久都没有找到正确的解决办法。最后呢当然是解决啦。

解决办法如下:

1、先找到threads文件夹下的threads_kernel_shared.h;

2、将下面这段代码注释掉就可以了:

//#ifdef ENABLE_ASSERTS  
//    extern int USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent_use_of_DEBUG_or_ENABLE_ASSERTS_preprocessor_directives;  
//    inline int dlib_check_consistent_assert_usage() { USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent_use_of_DEBUG_or_ENABLE_ASSERTS_preprocessor_directives = 0; return 0; }  
//#else  
//    extern int USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent_use_of_DEBUG_or_ENABLE_ASSERTS_preprocessor_directives_;  
//    inline int dlib_check_consistent_assert_usage() { USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent_use_of_DEBUG_or_ENABLE_ASSERTS_preprocessor_directives_ = 0; return 0; }  
//#endif  
//    const int dlib_check_assert_helper_variable = dlib_check_consistent_assert_usage();  
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
#include <fstream>


using namespace std;
using namespace dlib;

//  ----------------------------------------------------------------------------

int main(int argc, char** argv)
{
    try
    {

        // 声明图像
        array2d<rgb_pixel> img;

        string img_path = "lena.jpg";
        load_image(img, img_path);

        // 高斯模糊
        array2d<unsigned char> blurred_img;
        gaussian_blur(img, blurred_img);

        // sobel边缘检测
        array2d<short> horz_gradient, vert_gradient;
        array2d<unsigned char> edge_image;
        sobel_edge_detector(blurred_img, horz_gradient, vert_gradient);

        //非极大边缘抑制
        suppress_non_maximum_edges(horz_gradient, vert_gradient, edge_image);

        // 显示结果
        image_window my_window(edge_image, "Normal Edge Image");

        // We can also easily display the edge_image as a heatmap or using the jet color
        // scheme like so.
        image_window win_hot(heatmap(edge_image));
        image_window win_jet(jet(edge_image));

        // also make a window to display the original image
        image_window my_window2(img, "Original Image");

        // Sometimes you want to get input from the user about which pixels are important
        // for some task.  You can do this easily by trapping user clicks as shown below.
        // This loop executes every time the user double clicks on some image pixel and it
        // will terminate once the user closes the window.
        point p;
        while (my_window.get_next_double_click(p))
        {
            cout << "User double clicked on pixel:         " << p << endl;
            cout << "edge pixel value at this location is: " << (int)edge_image[p.y()][p.x()] << endl;
        }

        // wait until the user closes the windows before we let the program 
        // terminate.
        win_hot.wait_until_closed();
        my_window2.wait_until_closed();


        // Finally, note that you can access the elements of an image using the normal [row][column]
        // operator like so:
        cout << horz_gradient[0][3] << endl;
        cout << "number of rows in image:    " << horz_gradient.nr() << endl;
        cout << "number of columns in image: " << horz_gradient.nc() << endl;
    }
    catch (exception& e)
    {
        cout << "exception thrown: " << e.what() << endl;
    }
}



猜你喜欢

转载自blog.csdn.net/flyyufenfei/article/details/79176136