OpenCV - 缺少dll

时间:2017-08-09 11:54:28

标签: .net opencv dll c++-cli

使用OpenCV 3.3.0

使用Visual Studio 2017,我尝试运行我的代码,没有发现错误。 总是说“应用程序无法正确启动(0xc000007b)”(已翻译)。

已经在调试文件夹中有这个dll文件:

  • opencv_contrib_world330d.dll
  • opencv_ffmpeg.dll
  • opencv_ffmpeg330.dll
  • opencv_ffmpeg330_64.dll
  • opencv_ffmpeg_64.dll
  • opencv_img_hash330d.dll
  • opencv_world330.dll
  • opencv_world330d.dll

这是我的代码:

#pragma once
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\core\ocl.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include "opencv2\xfeatures2d.hpp"

using namespace cv;
using namespace cv::ocl;
using namespace cv::xfeatures2d;
using namespace std;

extern "C"
{
    __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
}

namespace ObjDetect_v1 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Runtime::InteropServices;

    Bitmap^ ConvertMatToBitmap(Mat matToConvert);

    /// <summary>
    /// Summary for Main
    /// </summary>
    public ref class Main : public System::Windows::Forms::Form
    {
    public:
        Main(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Main()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::PictureBox^  pictureBox_Cam;
    private: System::Windows::Forms::Button^  button_Start;
    private: System::Windows::Forms::PictureBox^  pictureBoxHist;
    private: System::Windows::Forms::PictureBox^  pictureBoxObj;


    private: System::ComponentModel::IContainer^  components;


    protected:

    protected:

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>


#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->pictureBox_Cam = (gcnew System::Windows::Forms::PictureBox());
            this->button_Start = (gcnew System::Windows::Forms::Button());
            this->pictureBoxHist = (gcnew System::Windows::Forms::PictureBox());
            this->pictureBoxObj = (gcnew System::Windows::Forms::PictureBox());
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox_Cam))->BeginInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBoxHist))->BeginInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBoxObj))->BeginInit();
            this->SuspendLayout();
            // 
            // pictureBox_Cam
            // 
            this->pictureBox_Cam->Location = System::Drawing::Point(12, 12);
            this->pictureBox_Cam->Name = L"pictureBox_Cam";
            this->pictureBox_Cam->Size = System::Drawing::Size(422, 288);
            this->pictureBox_Cam->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
            this->pictureBox_Cam->TabIndex = 0;
            this->pictureBox_Cam->TabStop = false;
            // 
            // button_Start
            // 
            this->button_Start->Location = System::Drawing::Point(359, 409);
            this->button_Start->Name = L"button_Start";
            this->button_Start->Size = System::Drawing::Size(75, 23);
            this->button_Start->TabIndex = 1;
            this->button_Start->Text = L"Start";
            this->button_Start->UseVisualStyleBackColor = true;
            this->button_Start->Click += gcnew System::EventHandler(this, &Main::button_Start_Click);
            // 
            // pictureBoxHist
            // 
            this->pictureBoxHist->Location = System::Drawing::Point(463, 12);
            this->pictureBoxHist->Name = L"pictureBoxHist";
            this->pictureBoxHist->Size = System::Drawing::Size(422, 288);
            this->pictureBoxHist->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
            this->pictureBoxHist->TabIndex = 2;
            this->pictureBoxHist->TabStop = false;
            // 
            // pictureBoxObj
            // 
            this->pictureBoxObj->Location = System::Drawing::Point(540, 324);
            this->pictureBoxObj->Name = L"pictureBoxObj";
            this->pictureBoxObj->Size = System::Drawing::Size(300, 163);
            this->pictureBoxObj->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
            this->pictureBoxObj->TabIndex = 3;
            this->pictureBoxObj->TabStop = false;
            // 
            // Main
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(900, 508);
            this->Controls->Add(this->pictureBoxObj);
            this->Controls->Add(this->pictureBoxHist);
            this->Controls->Add(this->button_Start);
            this->Controls->Add(this->pictureBox_Cam);
            this->Name = L"Main";
            this->Text = L"Main";
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox_Cam))->EndInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBoxHist))->EndInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBoxObj))->EndInit();
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void button_Start_Click(System::Object^  sender, System::EventArgs^  e)
    {
        cv::ocl::setUseOpenCL(true);
        if (!cv::ocl::haveOpenCL())
        {
            MessageBox::Show("OpenCL is not available...", "WARNING");
        }

        cv::ocl::Context context;
        if (!context.create(cv::ocl::Device::TYPE_GPU))
        {
            MessageBox::Show("Failed creating the context...", "WARNING");
        }
        for (int i = 0; i != context.ndevices(); i++)
        {
            cv::ocl::Device device = context.device(i);
            MessageBox::Show(gcnew System::String(device.name().c_str()), "WARNING");
        }

        cv::ocl::Device(context.device(0));

        // INIT
        VideoCapture videoCap;
        videoCap.open(0);

        Mat frame;
        Mat frame_gray;
        Mat ObjIM;
        Mat ObjDescript;
        Mat frameDescript;
        vector<KeyPoint> objKeys;
        vector<KeyPoint> frameKeys;
        FeatureDetector * detector = new SIFT();
        DescriptorExtractor * extractor = new SIFT();

        // OBJ TRATMENT
        OpenFileDialog ^ openFileDialog = gcnew OpenFileDialog();
        openFileDialog->Filter = "PNG file |*.png";
        openFileDialog->Title = "Select a File to decrypt";
        if (openFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)
        {
            IntPtr ptrToNativeString = Marshal::StringToHGlobalAnsi(openFileDialog->FileName);
            ObjIM = imread(static_cast<char*>(ptrToNativeString.ToPointer()), IMREAD_GRAYSCALE);
        }
        detector->detect(ObjIM, objKeys);
        extractor->compute(ObjIM, objKeys, ObjDescript);
        pictureBoxObj->Image = ConvertMatToBitmap(ObjIM);


        // FRAME TRATMENT
        videoCap.read(frame);
        pictureBox_Cam->Image = ConvertMatToBitmap(frame);
        cvtColor(frame, frame_gray, CV_BGR2GRAY);
        equalizeHist(frame_gray, frame_gray);
        detector->detect(frame, frameKeys);
        extractor->compute(frame, frameKeys, frameDescript);

        // FLANN
        Mat Result;
        Mat Dist;
        int k = 2;
        flann::Index flannIndex(frameDescript, flann::LshIndexParams(12,20,2), cvflann::FLANN_DIST_HAMMING);
        flannIndex.knnSearch(ObjDescript, Result, Dist, k, flann::SearchParams());

        if (Dist.type() == CV_32F)
        {
            Mat tmp;
            Dist.convertTo(tmp, CV_32F);
            Dist = tmp;
        }

        float nndrRatio = 0.8;
        vector<Point2f> mpts_1;
        vector<Point2f> mpts_2;
        vector<int> indexes_1;
        vector<int> indexes_2;
        vector<uchar> outlier_mask;

        for (unsigned int i = 0; i < ObjDescript.rows; i++)
        {
            if (Result.at<int>(i, 0) >= 0 &&
                Result.at<int>(i, 1) >= 0 &&
                Dist.at<float>(i, 0) <= nndrRatio * Dist.at<float>(i, 1))
            {
                mpts_1.push_back(objKeys.at(i).pt);
                indexes_1.push_back(i);
                mpts_2.push_back(frameKeys.at(Result.at<int>(i, 0)).pt);
                indexes_2.push_back(Result.at<int>(i,0));
            }
        }

        int nbMatches = 8;
        Mat FrameObj;
        if (mpts_1.size() >= nbMatches)
        {
            FrameObj = findHomography(mpts_1, mpts_2, RANSAC, 1, outlier_mask);
            //Rect(FrameObj,);
        }


        pictureBoxHist->Image = ConvertMatToBitmap(FrameObj);
    }
};
    Bitmap^ ConvertMatToBitmap(Mat matToConvert) {
        return gcnew Bitmap(matToConvert.cols, matToConvert.rows, 4 * matToConvert.rows, System::Drawing::Imaging::PixelFormat::Format24bppRgb, IntPtr(matToConvert.data));
    }
}

**************更新1 **************

Dependency Walker的结果:

https://mega.nz/#!Ohd10LwD!eFyyhngvuwXTW7fkejc8NJToyXLx0b_Kw5IO3ycbY1Q

1 个答案:

答案 0 :(得分:0)

我在编译lib时出错!

  1. official website下载Cmake并安装它;
  2. 转到OpenCV Repo;
  3. 下载最新版本并将其解压缩到文件夹中
  4. 转到OpenCV-contrib Repo,下载最新版本,然后 也要提取它;
  5. 打开Cmake-gui并将source code设置为opencv文件夹;
  6. build the binaries设置为一个空文件夹,然后将其configure设置为
  7. 搜索OPENCV_ENABLE_NONFREE并进行检查,也搜索 OPENCV_EXTRA_MODULES_PATH并将路径设置为modules文件夹 已提取的opencv-contrib内部;
  8. 再次打configure,然后打generate,最后打open project
  9. 只需按一下即可编译,所有文件都在那里。