以下のコード (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関数に入れておけばOK!どうしてか分かりません ?誰かが私に説明してくれることを願っています! 助けてくれてありがとう!^~^