首先需要有Cmake软件和Dlib19.17(也可以自己选其他版本,过程大致一样)
如果不想自己编译的话可以看我的这篇博客,里面有已经编译好的可以直接下载,如果只是为了配置建议下载这个,更快更简便
(仅限于VS17 x64 Release+Dlib19.17)
https://blog.youkuaiyun.com/wilsonass/article/details/90581852
首先去下载Dlib文件
解压出来Dlib-19.17
打开Cmake
修改CMAKE_INSTALL_PREFIX的路径,为dlib-19.17/install
先点击Configure等待完成然后点击Generate
中间会出来一个界面,选择自己相对应的版本
完成后点击Open Project
在打开的VS工程中右键点击ALL_BUILD进行生成,然后等待完成。
右键点击INSTALL进行生成,然后等待完成。
然后在自己的VS项目中进行配置 include和lib
写入输入的附加依赖项
dlib19.17.0_release_64bit_msvc1916.lib;
写入测试代码
其中的string img_path = "001.jpg";是当前目录下的图片,通过把图片重命名移动到项目的当前目录下可进行测试
当前目录为:右键项目,选择在资源管理器中打开文件夹
#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;
}
}