.net - C++ 和 Windows 窗体中的数组

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

我正在使用 GUI 制作图像处理项目,无论如何我想将位图图像转换为 Mat (opencv) 所以我循环遍历位图图像并获取红色组件的值为了将它放在我拥有的 cpp 中的 Mat 变量中,因为每当我在具有 GUI 的所有功能的 Form 中声明一个 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 的数组能够在main 函数的所有值只填充了 255 我真的不知道这有什么问题。

最佳答案

遍历大小为 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]
  }
}

关于.net - C++ 和 Windows 窗体中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35726387/

相关文章:

c# - 编译错误 - ICE80 : The 64BitComponent . .. 使用 32BitDirectory

.net - msbuild 的/确定性开关不起作用

c# - byte[] 的最大长度?

ios - 为什么克隆变量仍然影响原始对象? swift

opencv - 计算机视觉 : nudity detection solutions comparision (opencv based or custom code) & hash-lists & client side

python - 如何使用 Python OpenCV cv2.PCAProject 压缩单个数组

.net - 如何将 "like"或 "Contains"参数传递给 ssrs 报告中的 fetchXML

c# - DataTable中如何选择数据单个字符的大小写?

C - 将字符串添加到数组 x 中并将其从数组 y 中删除

c++ - 无法在动态链接库 libstdc++-6.dll 中找到过程入口点 _gxx_personality_v0