2013-03-06 93 views
0

我已經得到了代碼(使用VS2010,C++,Windows窗體應用程序)以下:處理圖像 - OpenCV的

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

#ifdef _DEBUG 
//debug 
#pragma comment(lib,"cv210d.lib") 
#pragma comment(lib,"cxcore210d.lib") 
#pragma comment(lib,"cvaux210d.lib") 
#pragma comment(lib,"highgui210d.lib") 
#else 
//release 
#pragma comment(lib,"cv210.lib") 
#pragma comment(lib,"cxcore210.lib") 
#pragma comment(lib,"cvaux210.lib") 
#pragma comment(lib,"highgui210.lib") 
#endif 
//Global variables 
IplImage* src = NULL; 
IplImage* hsv = NULL; 
IplImage* dst = NULL; 
IplImage* v_plane = NULL; 
IplImage* h_plane = NULL; 
IplImage* s_plane = NULL; 

namespace HW4 { 

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> 
/// Summary for Form1 
/// </summary> 
public ref class Form1 : public System::Windows::Forms::Form 
{ 
public: 
    Form1(void) 
    { 
     InitializeComponent(); 
     // 
     //TODO: Add the constructor code here 
     // 
    } 

protected: 
    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    ~Form1() 
    { 
     if (components) 
     { 
      delete components; 
     } 
    } 
private: System::Windows::Forms::PictureBox^ pictureBox1; 
protected: 
private: System::Windows::Forms::PictureBox^ pictureBox2; 
private: System::Windows::Forms::Button^ ExitButton; 

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->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); 
     this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox()); 
     this->ExitButton = (gcnew System::Windows::Forms::Button()); 
     (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit(); 
     (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox2))->BeginInit(); 
     this->SuspendLayout(); 
     // 
     // pictureBox1 
     // 
     this->pictureBox1->Location = System::Drawing::Point(12, 49); 
     this->pictureBox1->Name = L"pictureBox1"; 
     this->pictureBox1->Size = System::Drawing::Size(255, 212); 
     this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; 
     this->pictureBox1->TabIndex = 0; 
     this->pictureBox1->TabStop = false; 
     // 
     // pictureBox2 
     // 
     this->pictureBox2->Location = System::Drawing::Point(354, 49); 
     this->pictureBox2->Name = L"pictureBox2"; 
     this->pictureBox2->Size = System::Drawing::Size(255, 212); 
     this->pictureBox2->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; 
     this->pictureBox2->TabIndex = 1; 
     this->pictureBox2->TabStop = false; 
     // 
     // ExitButton 
     // 
     this->ExitButton->Location = System::Drawing::Point(272, 278); 
     this->ExitButton->Name = L"ExitButton"; 
     this->ExitButton->Size = System::Drawing::Size(64, 31); 
     this->ExitButton->TabIndex = 2; 
     this->ExitButton->Text = L"Exit"; 
     this->ExitButton->UseVisualStyleBackColor = true; 
     this->ExitButton->Click += gcnew System::EventHandler(this, &Form1::ExitButton_Click); 
     // 
     // Form1 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(635, 333); 
     this->Controls->Add(this->ExitButton); 
     this->Controls->Add(this->pictureBox2); 
     this->Controls->Add(this->pictureBox1); 
     this->Name = L"Form1"; 
     this->Text = L"Form1"; 
     this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); 
     (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) { 
       src = cvLoadImage("D:\\Pictures\\hungry.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); 
      //Before you debug, choose SizeMode property of PictureBoxs = StretchImage. 
      pictureBox1->Image = gcnew //replacement of cvShowImage 
      System::Drawing::Bitmap(src->width,src->height,src->widthStep, 
      System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) src->imageData); 
      pictureBox1->Refresh(); 

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

      /*cvReleaseImage(&src); 
      cvReleaseImage(&hsv); 
      cvReleaseImage(&dst); 
      cvReleaseImage(&h_plane); 
      cvReleaseImage(&s_plane); 
      cvReleaseImage(&v_plane);*/ 
     } 
private: System::Void ExitButton_Click(System::Object^ sender, System::EventArgs^ e) { 
      cvReleaseImage(&src); 
      cvReleaseImage(&hsv); 
      cvReleaseImage(&dst); 
      cvReleaseImage(&h_plane); 
      cvReleaseImage(&s_plane); 
      cvReleaseImage(&v_plane); 
      this->Close(); 
     } 
}; 

}


考慮代碼的一部分:

cvReleaseImage(&src); 
cvReleaseImage(&hsv); 
cvReleaseImage(&dst); 
cvReleaseImage(&h_plane); 
cvReleaseImage(&s_plane); 
cvReleaseImage(&v_plane); 

如果我把這部分放在Form1_Load的末尾,會出現如下圖所示的錯誤: enter image description here

但是,如果我把這部分放在ExitButton_Click函數中,那沒關係! 我不知道爲什麼?希望有人能給我一個解釋!感謝您的幫助!^〜^

+0

請添加您的錯誤記錄以幫助其他人找到此頁面。 – Yamaneko 2013-03-06 03:48:48

回答

0

在您使用OpenCV的圖像緩衝區以下行,如果你費的形式加載函數的最後這些緩衝區,.NET無法加載引擎圖像了

但如果您免費形式退出緩衝區,也不會在意......在.net

pictureBox1->Image = gcnew ... 
+0

起初,我認爲行「pictureBox1-> Image = gcnew ...」執行一次,然後我釋放緩衝區,它可能不會影響前面的指令。 – 2013-03-10 13:13:16

+0

但正如您所說的,「pictureBox1-> Image = gcnew ...」這一行仍然受到釋放功能的影響。爲什麼? – 2013-03-10 13:14:58

0

表單Load事件可用於分配之前的形式加載和渲染資源。通過在加載結束時釋放圖像緩衝區,可以刪除表單需要顯示的圖像資源。試想一下,有些內部方法會調用Load()方法,然後調用內部方法Display(),該方法使用Load()方法中的數據。如果在調用「Display()」方法之前加載數據並釋放它,則可以釋放Display()方法中所需的內存。

正如Roozbeh所說,如果你把它放在出口處,表格並不試圖顯示任何東西,所以它不需要該圖像資源(來自緩衝區)。退出方法自然是釋放事物的好地方。