Windows10下VS2019+Dlib19.20配置及相关问题解决

一、CMake与Dlib下载

1.CMake下载

官方下载:下载地址
在这里插入图片描述
点击即可下载,可能比较慢。
本人选择的是cmake-3.18.0-rc1-win64-x64.zip。

2.Dlib下载

Dlib:下载地址
在这里插入图片描述
点击下载。
本人已经将这两个文件上传到网盘中。
网盘地址:地址
提取码:pdip

将下载的dlib-19.20.0.tar.gz解压到dlibrelease文件夹(解压到哪里都可以,看你自己选择)

新建install和build两个文件夹:
在这里插入图片描述

二、使用CMake制作Dlib.lib

打开下载的CMake:
在这里插入图片描述
打开之后如下图:
在这里插入图片描述
当然我这是编译之后的,你打开下面应该是空白。

点击Configure,选择对应的VS版本,使用默认Compliers:
在这里插入图片描述
点击Finish

等一下就会产生下面的界面,将CMAKE_INSTALL_PREFIX的value选择为刚才建立的文件夹的install文件夹。
在这里插入图片描述
再点击Generate
显示Generate Done,生成build目录即可。
最后点击Open Project使用我们的VS2019打开生成的dlib.sln。
我们也可以在下面这个目录打开这个文件,也是使用VS2019:
在这里插入图片描述

三、用VS重新生成dlib_building解决方案,在Release文件夹中得到dlib.lib

在打开的VS工程中将图中的两个选项改为Release与X64:
在这里插入图片描述

  1. 右键点击ALL_BUILD进行生成,然后等待完成。
    在这里插入图片描述
  2. 右键点击INSTALL进行生成,然后等待完成。
    在这里插入图片描述
    等待项目编译完成,就可以发现“build\dlib\Release\”目录下多了dlib.lib文件。

四、新建项目配置属性

1.新建一个VS项目

在这里插入图片描述

2.VS工程属性配置

  1. 打开项目属性在这里插入图片描述
  2. 配置 include和lib
    配置选择Release,否则做视频会非常卡。
    在这里插入图片描述
  3. 写入依赖项
    在这里插入图片描述
    在刚才的install文件夹下的lib文件夹中有一个文件,文件名即为附加依赖项的名字,复制粘贴即可

在这里插入图片描述
在这里插入图片描述
点击确认。

五、测试

1.某个官方代码:

// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

    This is an example illustrating the use of the perspective_window tool
    in the dlib C++ Library.  It is a simple tool for displaying 3D point 
    clouds on the screen.

*/

#include <dlib/gui_widgets.h>
#include <dlib/image_transforms.h>
#include <cmath>

using namespace dlib;
using namespace std;

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

int main()
{
    // Let's make a point cloud that looks like a 3D spiral.
    std::vector<perspective_window::overlay_dot> points;
    dlib::rand rnd;
    for (double i = 0; i < 20; i+=0.001)
    {
        // Get a point on a spiral
        dlib::vector<double> val(sin(i),cos(i),i/4);

        // Now add some random noise to it
        dlib::vector<double> temp(rnd.get_random_gaussian(),
                                  rnd.get_random_gaussian(),
                                  rnd.get_random_gaussian());
        val += temp/20;

        // Pick a color based on how far we are along the spiral
        rgb_pixel color = colormap_jet(i,0,20);

        // And add the point to the list of points we will display
        points.push_back(perspective_window::overlay_dot(val, color));
    }

    // Now finally display the point cloud.
    perspective_window win;
    win.set_title("perspective_window 3D point cloud");
    win.add_overlay(points);
    win.wait_until_closed();
}

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

出现下面界面即是运行成功:
在这里插入图片描述
(图案是可以拖动的哟)

2.第二个测试代码

// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

    This is an example illustrating the use of the gui api from the dlib C++ Library.


    This is a pretty simple example.  It makes a window with a user
    defined widget (a draggable colored box) and a button.  You can drag the
    box around or click the button which increments a counter.
*/




#include <dlib/gui_widgets.h>
#include <sstream>
#include <string>


using namespace std;
using namespace dlib;

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

class color_box : public draggable
{
    /*
        Here I am defining a custom drawable widget that is a colored box that
        you can drag around on the screen.  draggable is a special kind of drawable
        object that, as the name implies, is draggable by the user via the mouse.
        To make my color_box draggable all I need to do is inherit from draggable.
    */
    unsigned char red, green, blue;

public:
    color_box(
        drawable_window& w,
        rectangle area,
        unsigned char red_,
        unsigned char green_,
        unsigned char blue_
    ) :
        draggable(w),
        red(red_),
        green(green_),
        blue(blue_)
    {
        rect = area;
        set_draggable_area(rectangle(10, 10, 400, 400));

        // Whenever you make your own drawable widget (or inherit from any drawable widget 
        // or interface such as draggable) you have to remember to call this function to 
        // enable the events.  The idea here is that you can perform whatever setup you 
        // need to do to get your object into a valid state without needing to worry about 
        // event handlers triggering before you are ready.
        enable_events();
    }

    ~color_box(
    )
    {
        // Disable all further events for this drawable object.  We have to do this 
        // because we don't want any events (like draw()) coming to this object while or 
        // after it has been destructed.
        disable_events();

        // Tell the parent window to redraw its area that previously contained this
        // drawable object.
        parent.invalidate_rectangle(rect);
    }

private:

    void draw(
        const canvas& c
    ) const
    {
        // The canvas is an object that represents a part of the parent window
        // that needs to be redrawn.  

        // The first thing I usually do is check if the draw call is for part
        // of the window that overlaps with my widget.  We don't have to do this 
        // but it is usually good to do as a speed hack.  Also, the reason
        // I don't have it set to only give you draw calls when it does indeed
        // overlap is because you might want to do some drawing outside of your
        // widget's rectangle.  But usually you don't want to do that :)
        rectangle area = c.intersect(rect);
        if (area.is_empty() == true)
            return;

        // This simple widget is just going to draw a box on the screen.   
        fill_rect(c, rect, rgb_pixel(red, green, blue));
    }
};

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

class win : public drawable_window
{
    /*
        Here I am going to define our window.  In general, you can define as
        many window types as you like and make as many instances of them as you want.
        In this example I am only making one though.
    */
public:
    win(
    ) : // All widgets take their parent window as an argument to their constructor.
        c(*this),
        b(*this),
        cb(*this, rectangle(100, 100, 200, 200), 0, 0, 255), // the color_box will be blue and 101 pixels wide and tall
        mbar(*this)
    {
        // tell our button to put itself at the position (10,60). 
        b.set_pos(10, 60);
        b.set_name("button");

        // let's put the label 5 pixels below the button
        c.set_pos(b.left(), b.bottom() + 5);


        // set which function should get called when the button gets clicked.  In this case we want
        // the on_button_clicked member to be called on *this.
        b.set_click_handler(*this, &win::on_button_clicked);
        // Alternatively, if you have a compiler which supports the lambda functions from the
        // new C++ standard then you can use a lambda function instead of telling the click
        // handler to call one of the member functions.  So for example, you could do this
        // instead (uncomment the code if you have C++0x support):
        /*
        b.set_click_handler([&](){
                ++counter;
                ostringstream sout;
                sout << "Counter: " << counter;
                c.set_text(sout.str());
                });
        */
        // In general, all the functions which register events can take either member 
        // functions or lambda functions.


        // Let's also make a simple menu bar.  
        // First we say how many menus we want in our menu bar.  In this example we only want 1.
        mbar.set_number_of_menus(1);
        // Now we set the name of our menu.  The 'M' means that the M in Menu will be underlined
        // and the user will be able to select it by hitting alt+M
        mbar.set_menu_name(0, "Menu", 'M');

        // Now we add some items to the menu.  Note that items in a menu are listed in the
        // order in which they were added.

        // First let's make a menu item that does the same thing as our button does when it is clicked.
        // Again, the 'C' means the C in Click is underlined in the menu. 
        mbar.menu(0).add_menu_item(menu_item_text("Click Button!", *this, &win::on_button_clicked, 'C'));
        // let's add a separator (i.e. a horizontal separating line) to the menu
        mbar.menu(0).add_menu_item(menu_item_separator());
        // Now let's make a menu item that calls show_about when the user selects it.  
        mbar.menu(0).add_menu_item(menu_item_text("About", *this, &win::show_about, 'A'));


        // set the size of this window
        set_size(430, 380);

        counter = 0;

        set_title("dlib gui example");
        show();
    }

    ~win(
    )
    {
        // You should always call close_window() in the destructor of window
        // objects to ensure that no events will be sent to this window while 
        // it is being destructed.  
        close_window();
    }

private:

    void on_button_clicked(
    )
    {
        // when someone clicks our button it will increment the counter and 
        // display it in our label c.
        ++counter;
        ostringstream sout;
        sout << "counter: " << counter;
        c.set_text(sout.str());
    }

    void show_about(
    )
    {
        message_box("About", "This is a dlib gui example program");
    }

    unsigned long counter;
    label c;
    button b;
    color_box cb;
    menu_bar mbar;
};

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

int main()
{
    // create our window
    win my_window;


    // wait until the user closes this window before we let the program 
    // terminate.
    my_window.wait_until_closed();

    return 0;
}

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

// Normally, if you built this application on MS Windows in Visual Studio you
// would see a black console window pop up when you ran it.  The following
// #pragma directives tell Visual Studio to not include a console window along
// with your application.  However, if you prefer to have the console pop up as
// well then simply remove these #pragma statements.
#ifdef _MSC_VER
#   pragma comment( linker, "/entry:mainCRTStartup" )
#   pragma comment( linker, "/SUBSYSTEM:WINDOWS" )
#endif

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

运行结果:
在这里插入图片描述
将得到这样一个图形用户界面,可以看出这是一个用鼠标单击并且进行计数的一个程序,每次点击button,都会是的数字加1。

3.打开图片并处理图片

#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 = "001.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;
	}
}

六、相关问题解决

https://blog.csdn.net/qq_29698809/article/details/75189771
https://blog.csdn.net/yvetteQXX/article/details/88982535
https://blog.csdn.net/MrCharles/article/details/81810305
https://blog.csdn.net/flyyufenfei/article/details/79176136
https://blog.csdn.net/u012525096/article/details/78276470
下面内容来自最后一个连接:
(1)如果遇到png.h文件找不到,确定附加库目录已经包含dlib,使用"…/external/libpng/png.h"去代替<png.h>即可。
(2)最好使用Release版本去编译使用,速度快。
(3)如在Debug模式下OBJ字节数组太大,则在属性 -> C/C++ -> 命令行 加入/bigobj即可。
(4)若读取jpeg图片错误或找不到jpeglib.h,在第五步中,加入DLIB_JPEG_STATIC即可。

  • 5
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值