0

我有下面的代码(使用 VS2010、C++、Windows 窗体应用程序):

#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 的末尾,就会出现如下图所示的错误: 在此处输入图像描述

但是,如果我把这部分放在 ExitButton_Click 函数中,就可以了!我不知道为什么?希望有人能给我一个解释!谢谢你的帮助!^~^

4

2 回答 2

0

在以下行中,您使用的是 opencv 图像缓冲区,如果您在表单加载功能结束时对这些缓冲区进行收费,.net 引擎将无法再加载图像

但是,如果您在表单退出中释放缓冲区,它就不会在意...

 pictureBox1->Image = gcnew ...
于 2013-03-06T03:42:46.177 回答
0

.Net 中的 Form-Load 事件可用于在加载和呈现表单之前分配资源。通过在加载结束时释放图像缓冲区,您可以删除表单需要显示的图像资源。想象一下,有一些内部方法调用您的Load()方法,然后它调用一个内部Display()方法,该方法使用来自该Load()方法的数据。如果您加载数据然后在调用“Display()”方法之前释放它,您将释放该Display()方法所需的内存。

正如 Roozbeh 所说,如果您将其放在出口中,则表单不会尝试显示任何内容,因此它不需要该图像资源(来自缓冲区)。exit 方法自然是释放事物的好地方。

于 2014-08-13T03:58:19.697 回答