2016-03-01 62 views
1

我正在做一个使用GUI的图像处理项目,无论如何我想将位图图像转换成一个垫(opencv),所以我循环通过位图图像,并获取红色为了将其放置在CPP垫子变量,我有,组件,因为每当我申报表单内垫变量,具有图形用户界面的所有功能,我得到一个错误,所以我做如下在c + +和Windows窗体中的阵列

#pragma once 



namespace TestGUI1 { 

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

     } 

    protected: 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     ~TestForm() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 
    private: System::Windows::Forms::Button^ button1; 
    public: int static test; 

    public: Bitmap^ picture; 
    public: 
      int static width; 
      int static height; 

      static int* Red; 

      //static int Green; 
      //static int Blue; 
    private: System::Windows::Forms::TextBox^ textBox1; 
    private: System::Windows::Forms::PictureBox^ pictureBox1; 
    private: System::Windows::Forms::PictureBox^ pictureBox2; 




    protected: 

    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->button1 = (gcnew System::Windows::Forms::Button()); 
      this->textBox1 = (gcnew System::Windows::Forms::TextBox()); 
      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(12, 372); 
      this->button1->Name = L"button1"; 
      this->button1->Size = System::Drawing::Size(75, 23); 
      this->button1->TabIndex = 0; 
      this->button1->Text = L"button1"; 
      this->button1->UseVisualStyleBackColor = true; 
      this->button1->Click += gcnew System::EventHandler(this, &TestForm::button1_Click); 
      // 
      // textBox1 
      // 
      this->textBox1->Location = System::Drawing::Point(12, 346); 
      this->textBox1->Name = L"textBox1"; 
      this->textBox1->Size = System::Drawing::Size(639, 20); 
      this->textBox1->TabIndex = 1; 
      this->textBox1->TextChanged += gcnew System::EventHandler(this, &TestForm::textBox1_TextChanged); 
      // 
      // pictureBox1 
      // 
      this->pictureBox1->Location = System::Drawing::Point(12, 12); 
      this->pictureBox1->Name = L"pictureBox1"; 
      this->pictureBox1->Size = System::Drawing::Size(295, 304); 
      this->pictureBox1->TabIndex = 2; 
      this->pictureBox1->TabStop = false; 
      // 
      // pictureBox2 
      // 
      this->pictureBox2->Location = System::Drawing::Point(330, 12); 
      this->pictureBox2->Name = L"pictureBox2"; 
      this->pictureBox2->Size = System::Drawing::Size(329, 304); 
      this->pictureBox2->TabIndex = 3; 
      this->pictureBox2->TabStop = false; 
      // 
      // TestForm 
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
      this->ClientSize = System::Drawing::Size(671, 407); 
      this->Controls->Add(this->pictureBox2); 
      this->Controls->Add(this->pictureBox1); 
      this->Controls->Add(this->textBox1); 
      this->Controls->Add(this->button1); 
      this->Name = L"TestForm"; 
      this->Text = L"TestForm"; 
      (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit(); 
      (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->EndInit(); 
      this->ResumeLayout(false); 
      this->PerformLayout(); 

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

      //Select Image from computer using the dialog box 
      OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog; 
      openFileDialog1->ShowDialog(); 
      textBox1->Text = openFileDialog1->FileName; 


      picture = gcnew Bitmap(openFileDialog1->FileName); 
      width = picture->Width; 
      height = picture->Height; 
      Red = new int[width*height]; 
      int Red1[50][50]; 
      pictureBox1->Image = picture; 
      // Loop through the images pixels to reset color. 
      for (int x = 0; x < width; x++) 
      { 
       for (int y = 0; y < height; y++) 
       { 
        Color pixelColor = picture->GetPixel(x, y); 

        //Red[x + y] = picture->GetPixel(x,y).R; 
        //cout << Red[x + y] << " "; 
        //Red1[x][y] = picture->GetPixel(x, y).R; 
        //Red[x + y] = pixelColor.R; 
        Red1[x][y] = pixelColor.R; 
        cout << Red1[x][y] << " " ; 
       } 
       cout << endl; 

      } 


    } 

}; 
} 

我在按钮函数内部声明的数组仅仅取得了Red comp的实际值,而我声明为public:static int* Red的数组能够在主函数中访问它的数组已填充与255的所有值 ,我真的不知道这是什么问题。

回答

0

遍历大小widthheight的2D阵列:

for(int y = 0; y < height; ++y) { 
    for(int x = 0; x < width; ++x) { 
    do_something(arr[y][x]); 
    } 
} 

遍历相同阵列的一维版本:

for(int y = 0; y < height; ++y) { 
    for(int x = 0; x < width; ++x) { 
    do_something(arr[y * width + x]; //NOT arr[y + x] 
    } 
}