【问题标题】:Arrays in c++ and windows formc++和windows形式的数组
【发布时间】:2016-03-01 15:06:29
【问题描述】:

我正在使用 GUI 制作一个图像处理项目,无论如何我想将位图图像转换为 Mat (opencv) 所以我循环遍历位图图像并获取红色组件的值为了将它放在我拥有的 cpp 中的 Mat 变量中,因为每当我在 Form 中声明一个具有 GUI 的所有功能的 Mat 变量时,我都会收到一个错误,所以我按照以下方式进行操作

    #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价值观 我真的不知道这有什么问题。

【问题讨论】:

    标签: .net arrays opencv bitmap c++-cli


    【解决方案1】:

    遍历大小为widthheight 的二维数组:

    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]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 2022-12-09
      • 2012-12-13
      • 2016-08-19
      相关资源
      最近更新 更多