關於我自己

2014年12月8日 星期一

VS2008 WindowsForm + OpenCV

利用VS2008本身的視窗表單應用在OpenCV

1.

2.

3.因為出現了 

error C3862: 'cvRound': 不能以 /clr:pure 或 /clr:safe 編譯 Unmanaged 函式~ 所以加了這一步

要看詳細的話~請參看這個網址:

http://msdn.microsoft.com/zh-tw/library/ms235211%28v=vs.90%29.aspx

4. 在表單上拉元件  ~ 用PictureBox 跟 Button這兩個元件拉

5.拉一個正在關閉的事件

6. 執行結果畫面

程式碼:

#pragma once
#include <cv.h>
#include <highgui.h>

//Global variables
IplImage* src = NULL;
IplImage* hsv = NULL;
IplImage* dst = NULL;
IplImage* v_plane = NULL;
IplImage* h_plane = NULL;
IplImage* s_plane = NULL;
namespace WF001 {

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

    /// <summary>
    /// Form1 的摘要
    ///
    /// 警告: 如果您變更這個類別的名稱,就必須變更與這個類別所依據之所有 .resx 檔案關聯的
    ///          Managed 資源編譯器工具的 'Resource File Name' 屬性。
    ///          否則,這些設計工具
    ///          將無法與這個表單關聯的當地語系化資源
    ///          正確互動。
    /// </summary>

    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: 在此加入建構函式程式碼
            //

        }

    protected:
        /// <summary>
        /// 清除任何使用中的資源。
        /// </summary>

        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    private: System::Windows::Forms::PictureBox^  pictureBox1;
    private: System::Windows::Forms::PictureBox^  pictureBox2;
    protected:

    private:
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>

        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
        ///
        /// </summary>

        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
            this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox2))->BeginInit();
            this->SuspendLayout();
            //
            // button1
            //

            this->button1->Location = System::Drawing::Point(272, 207);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(215, 53);
            this->button1->TabIndex = 0;
            this->button1->Text = L"載入轉換";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            //
            // pictureBox1
            //

            this->pictureBox1->Location = System::Drawing::Point(30, 30);
            this->pictureBox1->Name = L"pictureBox1";
            this->pictureBox1->Size = System::Drawing::Size(194, 153);
            this->pictureBox1->TabIndex = 1;
            this->pictureBox1->TabStop = false;
            //
            // pictureBox2
            //

            this->pictureBox2->Location = System::Drawing::Point(272, 28);
            this->pictureBox2->Name = L"pictureBox2";
            this->pictureBox2->Size = System::Drawing::Size(215, 154);
            this->pictureBox2->TabIndex = 2;
            this->pictureBox2->TabStop = false;
            //
            // Form1
            //

            this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(516, 284);
            this->Controls->Add(this->pictureBox2);
            this->Controls->Add(this->pictureBox1);
            this->Controls->Add(this->button1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &Form1::Form1_Closing);
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox2))->EndInit();
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {


             }

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

            src = cvLoadImage("D:\\C3.jpg");
              dst = cvCreateImage(cvGetSize(src),8,3);
              hsv = cvCreateImage(cvGetSize(src),8,3);
              h_plane = cvCreateImage(cvGetSize(src),8,1);
              s_plane = cvCreateImage(cvGetSize(src),8,1);
              v_plane = cvCreateImage(cvGetSize(src),8,1);

             cvCvtColor(src,hsv,CV_BGR2HSV);
             cvSplit(hsv,h_plane,s_plane,v_plane,0);
             cvEqualizeHist(v_plane,v_plane);
             cvMerge(h_plane,s_plane,v_plane,0,hsv);
             cvCvtColor(hsv,dst,CV_HSV2BGR);
        
             pictureBox1->Image = gcnew
             System::Drawing::Bitmap(src->width,src->height,src->widthStep,
             System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) src->imageData);
             pictureBox1->Refresh();

             pictureBox2->Image = gcnew
             System::Drawing::Bitmap(dst->width,dst->height,dst->widthStep,
             System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) dst->imageData);
             pictureBox2->Refresh();

             }

     
           private: System::Void Form1_Closing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
          
           String^ message = "釋放圖片資源";
           String^ caption = "關閉視窗提示";
           MessageBoxButtons buttons = MessageBoxButtons::YesNo;
           System::Windows::Forms::DialogResult result;
           // Displays the MessageBox.
           result = MessageBox::Show( this, message, caption, buttons );
           cvReleaseImage(&src);
           cvReleaseImage(&hsv);
           cvReleaseImage(&dst);
           cvReleaseImage(&h_plane);
           cvReleaseImage(&s_plane);
           cvReleaseImage(&v_plane);
         }

};
}

1 則留言:

  1. Hi, how are you?
    I realize a project which one is about image process with windows form, and I am looking information about this topic but I don´t find.
    Can you send me the project?
    Thanks for your attention
    Alejandro
    alejos1@hotmail.es

    回覆刪除