2016-03-01 86 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] 
    } 
}