OpenCV探索之路(二十一)如何生成能在无opencv环境下运行的exe

咱们常常遇到这样的需求:咱们在VS写好的程序,须要在一个没有装opencv甚至没有装vs的电脑下运行,跑出效果。好比,你在你的电脑用opencv+vs2015写出一个程序,而后老师叫你把程序发给他,他要看看功能实现的怎么样。老师的电脑确定没有整套的开发环境的,若是你想只把代码发给他,让他本身编译,确定会出现问题。因此,咱们须要掌握如何生成一个不依赖开发环境的exe的方法。ios

下面将以一个实际例子说明如何生成一个不依赖开发环境的exe的方法。优化

好比我如今在VS2015下实现了一个图像拼接功能的程序ui

#include "highgui/highgui.hpp"    
#include "opencv2/nonfree/nonfree.hpp"    
#include "opencv2/legacy/legacy.hpp"   
#include <iostream>  

using namespace cv;
using namespace std;

void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst);

typedef struct
{
    Point2f left_top;
    Point2f left_bottom;
    Point2f right_top;
    Point2f right_bottom;
}four_corners_t;

four_corners_t corners;

void CalcCorners(const Mat& H, const Mat& src)
{
    double v2[] = { 0, 0, 1 };//左上角
    double v1[3];//变换后的坐标值
    Mat V2 = Mat(3, 1, CV_64FC1, v2);  //列向量
    Mat V1 = Mat(3, 1, CV_64FC1, v1);  //列向量

    V1 = H * V2;
    //左上角(0,0,1)
    cout << "V2: " << V2 << endl;
    cout << "V1: " << V1 << endl;
    corners.left_top.x = v1[0] / v1[2];
    corners.left_top.y = v1[1] / v1[2];

    //左下角(0,src.rows,1)
    v2[0] = 0;
    v2[1] = src.rows;
    v2[2] = 1;
    V2 = Mat(3, 1, CV_64FC1, v2);  //列向量
    V1 = Mat(3, 1, CV_64FC1, v1);  //列向量
    V1 = H * V2;
    corners.left_bottom.x = v1[0] / v1[2];
    corners.left_bottom.y = v1[1] / v1[2];

    //右上角(src.cols,0,1)
    v2[0] = src.cols;
    v2[1] = 0;
    v2[2] = 1;
    V2 = Mat(3, 1, CV_64FC1, v2);  //列向量
    V1 = Mat(3, 1, CV_64FC1, v1);  //列向量
    V1 = H * V2;
    corners.right_top.x = v1[0] / v1[2];
    corners.right_top.y = v1[1] / v1[2];

    //右下角(src.cols,src.rows,1)
    v2[0] = src.cols;
    v2[1] = src.rows;
    v2[2] = 1;
    V2 = Mat(3, 1, CV_64FC1, v2);  //列向量
    V1 = Mat(3, 1, CV_64FC1, v1);  //列向量
    V1 = H * V2;
    corners.right_bottom.x = v1[0] / v1[2];
    corners.right_bottom.y = v1[1] / v1[2];

}

int main(int argc, char *argv[])
{
    Mat image01 = imread(".\\src_pic\\right.jpg", 1);    //右图
    Mat image02 = imread(".\\src_pic\\left.jpg", 1);    //左图
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度图转换  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征点    
    SurfFeatureDetector surfDetector(800);  // 海塞矩阵阈值,在这里调整精度,值越大点越少,越精准 
    vector<KeyPoint> keyPoint1, keyPoint2;
    surfDetector.detect(image1, keyPoint1);
    surfDetector.detect(image2, keyPoint2);

    //特征点描述,为下边的特征点匹配作准备    
    SurfDescriptorExtractor SurfDescriptor;
    Mat imageDesc1, imageDesc2;
    SurfDescriptor.compute(image1, keyPoint1, imageDesc1);
    SurfDescriptor.compute(image2, keyPoint2, imageDesc2);

    //得到匹配特征点,并提取最优配对     
    FlannBasedMatcher matcher;
    vector<DMatch> matchePoints;
    matcher.match(imageDesc1, imageDesc2, matchePoints, Mat());
    cout << "total match points: " << matchePoints.size() << endl;
   // sort(matchePoints.begin(), matchePoints.end()); //特征点排序    

    Mat img_match;
    drawMatches(image01, keyPoint1, image02, keyPoint2, matchePoints, img_match);
    imshow("match points",img_match);

    //获取排在前N个的最优匹配特征点  
    vector<Point2f> imagePoints1, imagePoints2;

    for (int i = 0; i<matchePoints.size(); i++)
    {
        imagePoints1.push_back(keyPoint1[matchePoints[i].queryIdx].pt);
        imagePoints2.push_back(keyPoint2[matchePoints[i].trainIdx].pt);
    }



    //获取图像1到图像2的投影映射矩阵 尺寸为3*3  
    Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC);
    ////也可使用getPerspectiveTransform方法得到透视变换矩阵,不过要求只能有4个点,效果稍差  
    //Mat   homo=getPerspectiveTransform(imagePoints1,imagePoints2);  
    cout << "变换矩阵为:\n" << homo << endl << endl; //输出映射矩阵      

    //计算配准图的四个顶点坐标
    CalcCorners(homo, image01);
    cout << "left_top:" << corners.left_top << endl;
    cout << "left_bottom:" << corners.left_bottom << endl;
    cout << "right_top:" << corners.right_top << endl;
    cout << "right_bottom:" << corners.right_bottom << endl;

    //图像配准  
    Mat imageTransform1, imageTransform2;
    warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x),image02.rows));
    //warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8));
    imshow("直接通过透视矩阵变换", imageTransform1);
    imwrite(".\\dst_pic\\trans1.jpg", imageTransform1);


    //建立拼接后的图,需提早计算图的大小
    int dst_width = imageTransform1.cols;  //取最右点的长度为拼接图的长度
    int dst_height = image02.rows;

    Mat dst(dst_height, dst_width,CV_8UC3);
    dst.setTo(0);

    imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows)));
    image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows)));

    OptimizeSeam(image02, imageTransform1, dst);


    imshow("dst", dst);
    imwrite(".\\dst_pic\\dst.jpg", dst);

    waitKey();

    return 0;
}


//优化两图的链接处,使得拼接天然
void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst)
{
    int start = MIN(corners.left_top.x, corners.left_bottom.x);//开始位置,即重叠区域的左边界  
    
    double processWidth = img1.cols - start;//重叠区域的宽度  
    int rows = dst.rows;
    int cols = img1.cols; //注意,是列数*通道数
    double alpha = 1;//img1中像素的权重  
    for (int i = 0; i < rows; i++)
    {
        uchar* p = img1.ptr<uchar>(i);  //获取第i行的首地址
        uchar* t = trans.ptr<uchar>(i);
        uchar* d = dst.ptr<uchar>(i);
        for (int j = start; j < cols; j++)
        {
            if (t[j*3] == 0 && t[j*3+1] == 0 && t[j*3+2] == 0)
            {
                alpha = 1;
            }
            else
            {
                alpha = (processWidth - (j - start)) / processWidth;
            }

            d[j*3] = p[j*3] * alpha + t[j*3] * (1 - alpha);
            d[j*3+1] = p[j*3+1] * alpha + t[j*3+1] * (1 - alpha);
            d[j*3+2] = p[j*3+2] * alpha + t[j*3+2] * (1 - alpha);
            
        }
    }

}

那么怎样才能够生成一个不依赖于环境的可执行程序exe呢?spa

1.选择release方式

为何要选择release而不选择debug模式?由于debug模式我也尝试过了,由于debug模式要加入某些vs的debug dll,可能比较难找,就不使用debug模式了,relase模式更为方便。debug

2.从新生成解决方案

3.找到生成的exe的存放位置

由于我生成的是x64文件,因此就选择X64。生成X86的就选X86文件夹。
3d

咱们选relsease文件夹
code

发现有四项东西
orm

3.创建本身的文件夹

本身新建一个文件夹(我命名为my_exe),之后全部东西都放这里了。并将上面提到的四项东西拷贝到这里。并根据咱们程序写的读取图片和存储图片的位置,生成以下的两个文件夹src_pic和dst_pic。
blog

4.找出opencv dll库的位置

将里面的东西全选,并拷贝到刚新建的文件夹内。
排序

5.根据程序写的读取图片的位置放入待处理的图片

6.运行exe文件

这个exe文件是你从vs工程copy过来的那个exe,别弄错了。

完美运行。

再看看dst_pic文件夹,生成的图片已经如咱们所愿存进去了!

可能遇到的问题

在实际操做中可能遇到exe没法执行或者出错的状况,这时应第一时间查看依赖项是否填写正确。

由于咱们选择的是release版本,因此依赖项填写的是不带d的!这个要确认清楚。

相关文章
相关标签/搜索