1. 程式人生 > >OpenCV3.4.0+dlib+Visual Studio2017配置步驟及示例程式碼執行

OpenCV3.4.0+dlib+Visual Studio2017配置步驟及示例程式碼執行

模式選擇:Debug+x64

OpenCV官網:https://opencv.org/opencv-3-4.html

dlib官網:http://dlib.net/

Visual Studio2017:官網可以下載免費版

我用的是OpenCV3.4.0+Dlib+Visual Studio2017的組合。其中dlib是配置耗時最長的一個。一開始我下載的是官網的dlib19.9,但不知道為什麼之後在工程裡新增source.cpp檔案後一直報錯。後來我換成了網友提供的dlib-master就可以成功運行了。

一.建立vs控制檯專案

二.配置OpenCV

三.配置Dlib

四.執行示例程式

五.參考網址彙總

 一.建立vs控制檯專案

1.      建立vs控制檯空專案

這裡最好是建立空專案,也可以建立Windows控制檯應用程式。如果建立的是後者,vs會自動生成標頭檔案,這個標頭檔案對之後的程式執行有影響。

2.     將vs模式統一為Debug+x64

工具欄選項:


專案---屬性:


生成---配置管理器:

3.      增加main.cpp

工程---原始檔---新增---新建項---C++標頭檔案,此處我新增的是main.cpp



二.配置OpenCV(我之前單獨配置過OpenCV,參考的是:https://www.cnblogs.com/linshuhe/p/5764394.html,下述OpenCV配置有省略)

1.      新增專案屬性表OpenCV_Debug_X64

屬性管理器---Debug|x64---新增新專案屬性表

雙擊新建的屬性表進行修改:

VC++目錄---包含目錄,新增opencv相關資料夾的路徑:

..\build\include

..\build\include\opencv

..\build\include\opencv2

在VC++目錄---庫目錄,新增下面的資料夾路徑:

..\build\x64\vc15\lib

連結---輸入---附加依賴項,新增lib檔案

這一檔案可以在..\build\x64\vc15\lib下找到,在附加依賴項中新增opencv_world340d.lib或opencv_world340.lib。

有博主說二者的區別在於適用模式的不同,一個適用於Debug模式,一個適用於Release模式。我這邊新增的是opencv_world340d.lib。

2.      測試opencv是否配置成功

在main檔案中輸入   #include <opencv2/opencv.hpp>

生成---生成解決方案,若可生成成功,則證明OpenCV基本配置完畢

三.配置Dlib

1.      生成dlib.lib

cmake可直接到官網下載

在bin資料夾下找到,雙擊執行cmake

原始碼路徑:../dlib版本/dlib,比如我的路徑是G:/dlib/dlib-master/dlib

目標路徑:隨便選

所有路徑都儘量不要包含中文字元


點generate會跳出功能選擇框,如果沒跳出來的話就點configure,如果還沒跳出來就換一個目標地址再試。

在功能選擇下拉框裡選擇vs 15 2017 Win64,然後點選Finish開始生成


生成完畢的介面如下

生成好以後在之前設定的目標路徑下開啟dlib專案檔案

在vs中選擇重新生成解決方案,注意,這個時候也要選debug+x64環境

此時在目錄下多了一個Debug資料夾,開啟該資料夾,有已經生成好的.lib檔案


將該檔案改名為dlib.lib


2.      新增專案屬性表Dlib_Debug_x64

新增方法同上,屬性管理器---Debug|x64---新增新專案屬性表

雙擊開啟屬性表進行設定,首先將Dlib的儲存路徑新增到VC++目錄---包含目錄、引用目錄和庫目錄中

比如我的儲存路徑是   G:\dlib\dlib-master

連結---輸入---附加依賴項,輸入剛才生成的lib檔案:

dlib.lib

連結---輸入---常規---附加庫目錄,輸入之前生成dlib.lib資料夾路徑:

G:\dlib\dlib_build_64_debug\Debug

3.      測試Dlib是否配置成功

在main.cpp中新增以下標頭檔案:

#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>

#include <dlib/image_io.h>

若可成功生成解決方案,則可以直接跳轉至步驟四在.cpp檔案中加入原始碼測試

4.*      在專案中新增source.cpp檔案

右鍵點選專案---新增---現有項,在..\dlib\all路徑下找到source.cpp,新增這一檔案

5.*      把新加入的source.cpp檔案預編譯頭設定為否

如果開始時建的是空專案,則一般不需要做這一步

右鍵點選工程內source.cpp檔案,屬性---C/C++---預編譯頭---不使用預編譯頭

6.*      如果此時編譯報錯,則加入 /bigobj 

專案---屬性表---C/C++–---命令列---其他選項,加入 /bigobj

7.*      若涉及圖片,需在專案屬性裡新增前處理器

專案---屬性---C/C++---前處理器---前處理器定義,加入下列三項:

DLIB_PNG_SUPPORT

DLIB_JPEG_SUPPORT

DLIB_JPEG_STATIC

8.*     若報錯:無法開啟dlib_test11.exe

退出,以管理員身份執行vs,開啟原來的專案,生成解決方案。

若依然報錯,則將專案---屬性---常規---配置型別,改為動態庫


然後生成一次解決方案

再將配置型別改回應用程式:

重新生成解決方案

四.執行示例程式

將示例程式碼複製進main.cpp中

示例程式碼儲存在dlib\examples資料夾中

示例程式1:3d_point_cloud_ex.cpp

// 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();
}

以Debug+x64模式生成解決方案後,在該工程所在路徑下可找到.exe檔案。雙擊.exe檔案即可執行,執行介面如下:

示例程式2:face_landmark_detection_ex.cpp人臉特徵點提取

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

This example program shows how to find frontal human faces in an image and
estimate their pose.  The pose takes the form of 68 landmarks.  These are
points on the face such as the corners of the mouth, along the eyebrows, on
the eyes, and so forth.



The face detector we use is made using the classic Histogram of Oriented
Gradients (HOG) feature combined with a linear classifier, an image pyramid,
and sliding window detection scheme.  The pose estimator was created by
using dlib's implementation of the paper:
One Millisecond Face Alignment with an Ensemble of Regression Trees by
Vahid Kazemi and Josephine Sullivan, CVPR 2014
and was trained on the iBUG 300-W face landmark dataset (see
https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/):
C. Sagonas, E. Antonakos, G, Tzimiropoulos, S. Zafeiriou, M. Pantic.
300 faces In-the-wild challenge: Database and results.
Image and Vision Computing (IMAVIS), Special Issue on Facial Landmark Localisation "In-The-Wild". 2016.
You can get the trained model file from:
http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2.
Note that the license for the iBUG 300-W dataset excludes commercial use.
So you should contact Imperial College London to find out if it's OK for
you to use this model file in a commercial product.


Also, note that you can train your own models using dlib's machine learning
tools.  See train_shape_predictor_ex.cpp to see an example.




Finally, note that the face detector is fastest when compiled with at least
SSE2 instructions enabled.  So if you are using a PC with an Intel or AMD
chip then you should enable at least SSE2 instructions.  If you are using
cmake to compile this program you can enable them by using one of the
following commands when you create the build project:
cmake path_to_dlib_root/examples -DUSE_SSE2_INSTRUCTIONS=ON
cmake path_to_dlib_root/examples -DUSE_SSE4_INSTRUCTIONS=ON
cmake path_to_dlib_root/examples -DUSE_AVX_INSTRUCTIONS=ON
This will set the appropriate compiler options for GCC, clang, Visual
Studio, or the Intel compiler.  If you are using another compiler then you
need to consult your compiler's manual to determine how to enable these
instructions.  Note that AVX is the fastest but requires a CPU from at least
2011.  SSE4 is the next fastest and is supported by most current machines.
*/


#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <iostream>

using namespace dlib;
using namespace std;

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

int main(int argc, char** argv)
{
	try
	{
		// This example takes in a shape model file and then a list of images to
		// process.  We will take these filenames in as command line arguments.
		// Dlib comes with example images in the examples/faces folder so give
		// those as arguments to this program.
		if (argc == 1)
		{
			cout << "Call this program like this:" << endl;
			cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
			cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
			cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
			return 0;
		}

		// We need a face detector.  We will use this to get bounding boxes for
		// each face in an image.
		frontal_face_detector detector = get_frontal_face_detector();
		// And we also need a shape_predictor.  This is the tool that will predict face
		// landmark positions given an image and face bounding box.  Here we are just
		// loading the model from the shape_predictor_68_face_landmarks.dat file you gave
		// as a command line argument.
		shape_predictor sp;
		deserialize(argv[1]) >> sp;


		image_window win, win_faces;
		// Loop over all the images provided on the command line.
		for (int i = 2; i < argc; ++i)
		{
			cout << "processing image " << argv[i] << endl;
			array2d<rgb_pixel> img;
			load_image(img, argv[i]);
			// Make the image larger so we can detect small faces.
			pyramid_up(img);

			// Now tell the face detector to give us a list of bounding boxes
			// around all the faces in the image.
			std::vector<rectangle> dets = detector(img);
			cout << "Number of faces detected: " << dets.size() << endl;

			// Now we will go ask the shape_predictor to tell us the pose of
			// each face we detected.
			std::vector<full_object_detection> shapes;
			for (unsigned long j = 0; j < dets.size(); ++j)
			{
				full_object_detection shape = sp(img, dets[j]);
				cout << "number of parts: " << shape.num_parts() << endl;
				cout << "pixel position of first part:  " << shape.part(0) << endl;
				cout << "pixel position of second part: " << shape.part(1) << endl;
				// You get the idea, you can get all the face part locations if
				// you want them.  Here we just store them in shapes so we can
				// put them on the screen.
				shapes.push_back(shape);
			}

			// Now let's view our face poses on the screen.
			win.clear_overlay();
			win.set_image(img);
			win.add_overlay(render_face_detections(shapes));

			// We can also extract copies of each face that are cropped, rotated upright,
			// and scaled to a standard size as shown here:
			dlib::array<array2d<rgb_pixel> > face_chips;
			extract_image_chips(img, get_face_chip_details(shapes), face_chips);
			win_faces.set_image(tile_images(face_chips));

			cout << "Hit enter to process the next image..." << endl;
			cin.get();
		}
	}
	catch (exception& e)
	{
		cout << "\nexception thrown!" << endl;
		cout << e.what() << endl;
	}
}

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

執行方法:

(1)將shape_predictor_68_face_landmarks.dat 及所需圖片複製到.exe所在的資料夾

(2)命令列定位至.exe檔案所在目錄,在命令列中輸入:

專案名.exe   shape_predictor_68_face_landmarks.dat   圖片路徑

如:dlib_test11.exe(此處有空格)shape_predictor_68_face_landmarks.dat(此處有空格)faces/2.jpg

執行介面如下:


五.參考網址彙總

OpenCV配置參考:https://www.cnblogs.com/linshuhe/p/5764394.html

Dlib+vs2017配置參考:https://www.cnblogs.com/dapeng-bupt/p/7807679.html

人臉程式執行教程:http://blog.csdn.net/qq_34915398/article/details/79105878

link2005錯誤解決方案:http://blog.csdn.net/xl928471061/article/details/71940862

特別感謝上述教程提供的幫助!!!