makePtr不起作用?

时间:2016-02-27 19:25:41

标签: c++-cli opencv3.0

所以我是c ++和opencv的新手,现在我正在使用visual studio 2013上的Windows窗体应用程序尝试stitching sample from opencv。这是我的代码:

#include <opencv2/opencv.hpp>
#include <opencv2/opencv_modules.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/stitching/detail/blenders.hpp>
#include <opencv2/stitching/detail/camera.hpp>
#include <opencv2/stitching/detail/exposure_compensate.hpp>
#include <opencv2/stitching/detail/matchers.hpp>
#include <opencv2/stitching/detail/motion_estimators.hpp>
#include <opencv2/stitching/detail/seam_finders.hpp>
#include <opencv2/stitching/detail/util.hpp>
#include <opencv2/stitching/detail/warpers.hpp>
#include <opencv2/stitching/warpers.hpp>

#include <iostream>
#include <string>
#include <vector>

#pragma once

namespace Stitch {

    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::IO;

    /// <summary>
    /// Summary for MyForm
    /// </summary>
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public :
        std::vector<cv::Mat> *gbr_arr;
        array<System::String^>^ dir_arr;
        cv::Mat *hasil_gbr;
        int jml_gbr;

        MyForm(void)
        {
            InitializeComponent();
            gbr_arr = new std::vector<cv::Mat>;
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }

    private: System::Windows::Forms::MenuStrip^  menuStrip1;
    protected:
    private: System::Windows::Forms::ToolStripMenuItem^  fileToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  loadImageToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  saveImageToolStripMenuItem;
    private: System::Windows::Forms::ToolStripSeparator^  toolStripSeparator1;
    private: System::Windows::Forms::ToolStripMenuItem^  closeToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  aboutToolStripMenuItem;
    private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;
    private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
    private: System::Windows::Forms::Button^  button1;
    private: System::Windows::Forms::ProgressBar^  progressBar1;
    private: System::Windows::Forms::Panel^  panel1;
    private: System::ComponentModel::BackgroundWorker^  backgroundWorker1;
    private: System::Windows::Forms::Label^  label1;




    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#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->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
            this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->loadImageToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->saveImageToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->toolStripSeparator1 = (gcnew System::Windows::Forms::ToolStripSeparator());
            this->closeToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->aboutToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
            this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
            this->panel1 = (gcnew System::Windows::Forms::Panel());
            this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->menuStrip1->SuspendLayout();
            this->SuspendLayout();
            // 
            // menuStrip1
            // 
            this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {
                this->fileToolStripMenuItem,
                    this->aboutToolStripMenuItem
            });
            this->menuStrip1->Location = System::Drawing::Point(0, 0);
            this->menuStrip1->Name = L"menuStrip1";
            this->menuStrip1->Size = System::Drawing::Size(829, 24);
            this->menuStrip1->TabIndex = 0;
            this->menuStrip1->Text = L"menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(4) {
                this->loadImageToolStripMenuItem,
                    this->saveImageToolStripMenuItem, this->toolStripSeparator1, this->closeToolStripMenuItem
            });
            this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
            this->fileToolStripMenuItem->Size = System::Drawing::Size(37, 20);
            this->fileToolStripMenuItem->Text = L"File";
            // 
            // loadImageToolStripMenuItem
            // 
            this->loadImageToolStripMenuItem->Name = L"loadImageToolStripMenuItem";
            this->loadImageToolStripMenuItem->Size = System::Drawing::Size(136, 22);
            this->loadImageToolStripMenuItem->Text = L"Load Image";
            this->loadImageToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::loadImageToolStripMenuItem_Click);
            // 
            // saveImageToolStripMenuItem
            // 
            this->saveImageToolStripMenuItem->Name = L"saveImageToolStripMenuItem";
            this->saveImageToolStripMenuItem->Size = System::Drawing::Size(136, 22);
            this->saveImageToolStripMenuItem->Text = L"Save Image";
            this->saveImageToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::saveImageToolStripMenuItem_Click);
            // 
            // toolStripSeparator1
            // 
            this->toolStripSeparator1->Name = L"toolStripSeparator1";
            this->toolStripSeparator1->Size = System::Drawing::Size(133, 6);
            // 
            // closeToolStripMenuItem
            // 
            this->closeToolStripMenuItem->Name = L"closeToolStripMenuItem";
            this->closeToolStripMenuItem->Size = System::Drawing::Size(136, 22);
            this->closeToolStripMenuItem->Text = L"Close";
            this->closeToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::closeToolStripMenuItem_Click);
            // 
            // aboutToolStripMenuItem
            // 
            this->aboutToolStripMenuItem->Name = L"aboutToolStripMenuItem";
            this->aboutToolStripMenuItem->Size = System::Drawing::Size(52, 20);
            this->aboutToolStripMenuItem->Text = L"About";
            // 
            // openFileDialog1
            // 
            this->openFileDialog1->Filter = L"JPEG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|All Files (*.*)|*.*";
            this->openFileDialog1->FilterIndex = 3;
            this->openFileDialog1->InitialDirectory = L"E:\\\\Gambar";
            this->openFileDialog1->Multiselect = true;
            this->openFileDialog1->RestoreDirectory = true;
            // 
            // button1
            // 
            this->button1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Right));
            this->button1->Location = System::Drawing::Point(717, 293);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(100, 23);
            this->button1->TabIndex = 1;
            this->button1->Text = L"Process";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
            // 
            // progressBar1
            // 
            this->progressBar1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>(((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left)
                | System::Windows::Forms::AnchorStyles::Right));
            this->progressBar1->Location = System::Drawing::Point(0, 322);
            this->progressBar1->Name = L"progressBar1";
            this->progressBar1->Size = System::Drawing::Size(829, 23);
            this->progressBar1->TabIndex = 2;
            // 
            // panel1
            // 
            this->panel1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
                | System::Windows::Forms::AnchorStyles::Left)
                | System::Windows::Forms::AnchorStyles::Right));
            this->panel1->AutoSizeMode = System::Windows::Forms::AutoSizeMode::GrowAndShrink;
            this->panel1->BackColor = System::Drawing::SystemColors::ControlLightLight;
            this->panel1->Location = System::Drawing::Point(0, 27);
            this->panel1->Name = L"panel1";
            this->panel1->Size = System::Drawing::Size(829, 260);
            this->panel1->TabIndex = 3;
            // 
            // backgroundWorker1
            // 
            this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &MyForm::backgroundWorker1_DoWork);
            // 
            // label1
            // 
            this->label1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left));
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(12, 298);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(0, 13);
            this->label1->TabIndex = 4;
            // 
            // MyForm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(829, 345);
            this->Controls->Add(this->label1);
            this->Controls->Add(this->panel1);
            this->Controls->Add(this->progressBar1);
            this->Controls->Add(this->button1);
            this->Controls->Add(this->menuStrip1);
            this->MainMenuStrip = this->menuStrip1;
            this->Name = L"MyForm";
            this->Text = L"Stitcher";
            this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
            this->menuStrip1->ResumeLayout(false);
            this->menuStrip1->PerformLayout();
            this->ResumeLayout(false);
            this->PerformLayout();

        }

        void MarshalString(System::String ^ s, cv::String& os) {
            using namespace Runtime::InteropServices;
            const char* chars =
                (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
            os = chars;
            Marshal::FreeHGlobal(IntPtr((void*)chars));
        }

#pragma endregion
    private: System::Void loadImageToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
        using namespace Runtime::InteropServices;

        panel1->AutoScroll = true;
        panel1->Controls->Clear();

        int x = 20;
        int y = 20;
        int maxHeight = -1;

        if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
        {
            dir_arr = openFileDialog1->FileNames;
            gbr_arr->clear();

            for each (System::String^ gbr in dir_arr)
            {
                cv::String nama_gbr;
                MarshalString(gbr, nama_gbr);
                cv::Mat gambar = imread(nama_gbr);
                gbr_arr->push_back(gambar);

                PictureBox^ pic = gcnew PictureBox;
                pic->Width = 0.3*(gambar.cols); pic->Height = 0.3*(gambar.rows);
                pic->SizeMode = PictureBoxSizeMode::StretchImage;
                pic->Image = System::Drawing::Image::FromFile(gbr);
                pic->Location = System::Drawing::Point(x, y);
                x += pic->Width + 10;
                maxHeight = Math::Max(pic->Height, maxHeight);
                if (x > this->panel1->Width)
                {
                    x = 20;
                    y += pic->Height + 10;
                }
                this->panel1->Controls->Add(pic);
            }

            jml_gbr = static_cast<int> (gbr_arr->size());
        }
    }

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
        label1->Controls->Clear();
        label1->Text = "Stitching...";
        panel1->Controls->Clear();
        backgroundWorker1->RunWorkerAsync(1);
    }

    private: System::Void saveImageToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
        if (saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
        {

        }
    }

    private: System::Void closeToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
            Application::Exit();
    }

    private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {  
        cv::Ptr<cv::detail::FeaturesFinder> finder;
        finder = cv::makePtr<cv::detail::SurfFeaturesFinder>();

        std::vector<cv::detail::ImageFeatures> feature(jml_gbr);
        std::vector<cv::detail::MatchesInfo> pairwise_matching;

        for (int i = 0; i < jml_gbr; i++)
        {
            cv::Mat key;
            (*finder) (gbr_arr[i], feature[i]);
            feature[i].img_idx = i;

            cv::drawKeypoints(gbr_arr[i], feature[i].keypoints, key, cv::Scalar::all(-1), 4);
            cv::namedWindow("KeyPoints", CV_WINDOW_KEEPRATIO);
            cv::imshow("KeyPoints", gbr_arr[i]);

            cv::waitKey(0);
            MessageBox::Show("Hello!");
        }

        finder->collectGarbage();

        cv::detail::BestOf2NearestMatcher matcher(false, 0.65);
        matcher(feature, pairwise_matching);

        matcher.collectGarbage();


    }

    };
}

我第一次尝试使用此代码时出错,但它自行修复(我不知道为什么以及如何)。如果我没记错的话,这一行是System.Runtime.InteropServices.SEHException:

finder = cv::makePtr<cv::detail::SurfFeaturesFinder>();

错误消失后,我试图运行这个程序,它运行得很好,但程序似乎停留在那个“makePtr”行。我的代码中有什么问题吗?

0 个答案:

没有答案