首页 > 其他分享 >ARS展览项目(二)——环境搭建:opencv、dlib、VS2017

ARS展览项目(二)——环境搭建:opencv、dlib、VS2017

时间:2024-09-24 10:15:11浏览次数:9  
标签:box ARS VS2017 C++ opencv window include dlib

先说用到的软件和函数库

  • VS2017——我用VS2017社区版来开发,原因是软件免费而且好用,本项目用C++来做
  • opencv——Open Computer Vision是计算机视觉的库,有多种语言的接口,而且函数库也很丰富
  • dlib——Dlib是一个包含机器学习算法的C++开源工具包,提供大量的机器学习 / 图像处理算法(网上看的 :-) )
    本项目用的是VS2017社区版+opencv3.4.2版本+dlib9.0.3 。用其它版本也可以,新一点的版本吧。

容易遇到的问题

  1. dlib 配置的时候是需要release|x64 的模式,因为debug模式会让程序运行得非常缓慢,这在dlib官网上有说明http://dlib.net/faq.html#Whyisdlibslow

opencv 在 VS2017 里面的配置

关于opencv的配置,这个文档已经非常清楚了,https://blog.csdn.net/qq_41175905/article/details/80560429,非常感谢这位作者的帮助。如果看不懂,多找几篇关于配置的来看,方法都一样,只是详细与否的区别。
总结是:

  1. 比如把opencv安装在了D:\opencv342\ ,安装好后
  2. 右键“我的电脑”->属性->高级系统设置->环境变量,在后面添加环境变量: D:\opencv342\opencv\build\x64\vc15\bin
  3. 配置好环境变量后,打开VS2017,新建一个C++空项目
  4. 打开属性管理器,菜单栏->视图->其他窗口->属性管理器,对release|X64进行配置:右键release|X64->新建一个属性表,重命名为opencv
  5. 双击opencv属性表,属性中的 VC++目录->包含目录
    D:\opencv342\opencv\build\include
    D:\opencv342\opencv\build\include\opencv
    D:\opencv342\opencv\build\include\opencv2
  6. VC++目录->库目录
    D:\opencv342\opencv\build\x64\vc15\lib
  7. 链接器->输入->附加依赖项中加入
    opencv_world341d.lib
  8. 到这里,opencv就配置好了,第2步时配置环境变量的重点,第5、6、7步是配置vs的重点,第8步可有可无,只是为了未来方便。就是打开第3步的项目文件夹,把刚刚配置好的那个新建的opencv属性表保存在另外一个容易保存的地方就好,到时候要用就把它导入进来,就不用重新配置了。

配置好后测试一下:

  1. 新建一个C++空项目
  2. 打开属性管理表,右键release|X64,添加->现有项,然后把刚刚的opencv属性表导入进去
  3. 然后转到解决方案的管理器,右键源文件->添加新建项,然后添加一个cpp文件,比如是1.cpp
  4. 然后把下面的代码粘贴进去
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
 
using namespace cv;
using namespace std;
 
int main()
{
    cout << "Built with OpenCV " << CV_VERSION << endl;
 
	VideoCapture capture(0);    // 打开摄像头
	if(!capture.isOpened())    // 判断是否打开成功
	{
		cout << "open camera failed. " << endl;
		return -1;
	}
	
	while(true)
	{
		Mat frame;
		capture >> frame;    // 读取图像帧至frame
		if(!frame.empty())	// 判断是否为空
		{
			imshow("camera", frame);
		}
		
		if(waitKey(30) > 0)		// delay 30 ms等待按键
		{
			break;
		}
	}
 
    return 0;
}
  1. 这个代码的意思是打开笔记本电脑自带的摄像头,要是没有摄像头的话要借一个usb摄像头,然后把代码里的 VideoCapture capture(0); 改成 VideoCapture capture(1);,就可以打开usb摄像头。
  2. 如果可以,那么opencv的配置就成功了。

dlib 在 VS2017 里面的配置

关于dlib的配置,这个文档,https://www.cnblogs.com/dapeng-bupt/p/7807679.html和这个文档https://blog.csdn.net/czp19940223/article/details/78577570,啊也非常感谢这两位作者的帮助。
具体就是:

  1. 下载并且解压安装包
  2. cmake 编译dlib (这是为了得到dlib.lib)(比如编译报存的文件夹是D:\dlib-9.0.3\dlibmake)
  3. 打开vs,打开刚刚cmake编译的dlib项目,然后用release x64来重新编译dlib
  4. 然后新建空项目,然后release|x64 新建属性表
  5. VC++目录->包含目录,把dlib刚开始解压的目录写进去(比如D:\dlib-9.0.3)
  6. VC++目录->库目录,把刚刚生成的lib目录放进去(比如D:\dlib-9.0.3\dlibmake\release)
  7. 链接器->输入->附加依赖项,加入dlib.lib (如果不是这个名字就改成你文件夹里的那个lib的名字)
  8. 把属性表保存好

配置好后测试一下:

  1. 新建空项目,release|x64 导入刚刚的属性表
  2. 在源文件那里,新建一个cpp源文件
  3. 写入下面的代码
  4. 把这里的代码复制进去运行,(这位作者的代码)http://dlib.net/gui_api_ex.cpp.html,如果可以运行就行了。
  5. 为了方便也贴在下面,但是比较多
// 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

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

标签:box,ARS,VS2017,C++,opencv,window,include,dlib
From: https://www.cnblogs.com/atanchen/p/18428486

相关文章

  • ARS展览项目(七)——C-多线程:Socket-表情识别整合
    说明一下我这边做表情识别和Socket,表情识别要实时,Socket要一直监听表情识别的结果,那么就只好用C++多线程来解决这个“两个功能一直并且同时运行”的问题。否则,如果是单线程的话,只能运行表情识别一段时间,切换发送一段时间,又切换回来,这样没有多线程好。还要说解决的难点写成多......
  • OpenCV(cv::convertScaleAbs())
    目录1.函数定义2.原理3.示例4.参数作用详解4.1alpha的作用4.2beta的作用5.应用场景6.cv::convertScaleAbs()与cv::normalize()的区别总结cv::convertScaleAbs()是OpenCV中用于将图像像素值缩放并转换为8位无符号整数类型的函数。它常用于处理计算结果为浮点......
  • OpenCV(cv::Laplacian())
    目录1.函数定义2.拉普拉斯算子的原理3.示例4.应用场景总结cv::Laplacian()是OpenCV中用于计算图像拉普拉斯算子(Laplacian)的函数。拉普拉斯算子是一种边缘检测方法,它通过计算每个像素点的二阶导数来识别快速变化的区域(如边缘)。1.函数定义voidcv::Laplacian(In......
  • QCustomPlot QCPBars横向柱状图示例
    #include"qcustomplot.h"intmain(intargc,char*argv[]){QApplicationa(argc,argv);QCustomPlotcustomPlot;customPlot.resize(700,500);customPlot.show();QCPAxis*keyAxis=customPlot.yAxis;QCPAxis*valueAxis=......
  • QCustomPlot QCPBars纵向柱状图示例
    #include"qcustomplot.h"intmain(intargc,char*argv[]){QApplicationa(argc,argv);QCustomPlotcustomPlot;customPlot.resize(700,500);customPlot.show();QCPAxis*keyAxis=customPlot.xAxis;QCPAxis*valueAxis=......
  • OpenCV(Canny 边缘检测算法)
    目录1.高斯滤波(GaussianBlur)2.计算梯度强度和方向(GradientCalculation)3.非极大值抑制(Non-MaximumSuppression)3.1示例1.梯度强度矩阵(7x7)2.每个像素的梯度方向(7x7)3.非极大值抑制过程4.非极大值抑制后的矩阵(7x7)4.双阈值处理(DoubleThresholding)5.边缘连接(EdgeTracking......
  • OpenCV(图像锐化)
    目录1.图像锐化2.原理3.示例1.图像锐化图像锐化是一种图像增强技术,旨在通过增强图像的边缘信息,使图像看起来更加清晰和具有细节。图像锐化的核心思想是突出图像中的高频分量,这通常与图像中的边缘和快速变化的区域相关。2.原理图像锐化的基本原理是通过增强图像中像素......
  • [OpenCV] 数字图像处理 C++ 学习——16直方图均衡化、直方图比较 详细讲解+附完整代码
    文章目录前言1.直方图均衡化的理论基础(1)什么是直方图(2)直方图均衡化原理(3)直方图均衡化公式2.直方图比较理论基础(1)相关性(Correlation)——HISTCMP_CORREL(2)卡方(Chi-Square)——HISTCMP_CHISQR(3)十字交叉性(Intersection)——HISTCMP_INTERSECT(4)巴氏距离......
  • OpenCV(图像对比度)
    目录1.图像对比度2.对比度调整的原理3.示例4.调整对比度的效果1.图像对比度图像对比度是指图像中亮部和暗部之间的差异程度。对比度越高,亮区更亮,暗区更暗;对比度低时,亮区和暗区的差异不明显,图像显得平淡。2.对比度调整的原理图像对比度可以通过线性变换实现,公式如下:......
  • OpenCV(图像明度)
    目录1.图像明度(Brightness)2.明度调整的原理3.示例1.图像明度(Brightness)明度(Brightness)是图像中反映光亮程度的一个属性,通常用于描述像素的亮度水平。对于RGB图像,明度(Luminance)每个像素中红、绿、蓝通道的加权和。\[\text{Luminance}=0.299\timesR+0.587\timesG......