Visual C ++中PictureBox内的相机输入

时间:2017-12-19 11:56:51

标签: c++ opencv visual-c++ camera video-capture

我正在尝试将usb相机的实时反馈放在MyForm.h中的PictureBox中。我找到了在PictureBox上应用位图的代码(DrawCVImage)。问题是,在编译它时,表示pictureBox1未在.cpp文件中声明。什么可能导致这个问题?是否有一个缺少的指针?

    #pragma once

namespace Project1 {

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

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    private: System::IO::Ports::SerialPort^  serialPort1;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::Button^  button3;
    private: System::Windows::Forms::Button^  button4;
    private: System::Windows::Forms::Button^  button5;
    private: System::Windows::Forms::PictureBox^  pictureBox1;



    private: System::ComponentModel::IContainer^  components;
    protected:

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>


#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->components = (gcnew System::ComponentModel::Container());
            System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MyForm::typeid));
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->button3 = (gcnew System::Windows::Forms::Button());
            this->button4 = (gcnew System::Windows::Forms::Button());
            this->button5 = (gcnew System::Windows::Forms::Button());
            this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->BackgroundImage = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"button1.BackgroundImage")));
            this->button1->CausesValidation = false;
            this->button1->Location = System::Drawing::Point(952, 206);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(80, 80);
            this->button1->TabIndex = 0;
            this->button1->TabStop = false;
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
            this->button1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &MyForm::button1_KeyDown);
            this->button1->KeyUp += gcnew System::Windows::Forms::KeyEventHandler(this, &MyForm::button1_KeyUp);
            this->button1->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::button1_MouseDown);
            this->button1->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::button1_MouseUp);
            // 
            // serialPort1
            // 
            this->serialPort1->PortName = L"COM6";
            // 
            // button2
            // 
            this->button2->BackgroundImage = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"button2.BackgroundImage")));
            this->button2->CausesValidation = false;
            this->button2->Location = System::Drawing::Point(786, 206);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(80, 80);
            this->button2->TabIndex = 1;
            this->button2->TabStop = false;
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
            this->button2->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &MyForm::button2_KeyDown);
            this->button2->KeyUp += gcnew System::Windows::Forms::KeyEventHandler(this, &MyForm::button2_KeyUp);
            this->button2->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::button2_MouseDown);
            this->button2->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::button2_MouseUp);
            // 
            // button3
            // 
            this->button3->BackgroundImage = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"button3.BackgroundImage")));
            this->button3->Location = System::Drawing::Point(866, 120);
            this->button3->Name = L"button3";
            this->button3->Size = System::Drawing::Size(80, 80);
            this->button3->TabIndex = 2;
            this->button3->Text = L"button3";
            this->button3->UseVisualStyleBackColor = true;
            this->button3->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::button3_MouseDown);
            this->button3->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::button3_MouseUp);
            // 
            // button4
            // 
            this->button4->BackgroundImage = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"button4.BackgroundImage")));
            this->button4->Location = System::Drawing::Point(866, 292);
            this->button4->Name = L"button4";
            this->button4->Size = System::Drawing::Size(80, 80);
            this->button4->TabIndex = 3;
            this->button4->Text = L"button4";
            this->button4->UseVisualStyleBackColor = true;
            this->button4->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::button4_MouseDown);
            this->button4->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::button4_MouseUp);
            // 
            // button5
            // 
            this->button5->Location = System::Drawing::Point(782, 421);
            this->button5->Name = L"button5";
            this->button5->Size = System::Drawing::Size(250, 80);
            this->button5->TabIndex = 4;
            this->button5->Text = L"SHOOT";
            this->button5->UseVisualStyleBackColor = true;
            // 
            // pictureBox1
            // 
            this->pictureBox1->Location = System::Drawing::Point(31, 21);
            this->pictureBox1->Name = L"pictureBox1";
            this->pictureBox1->Size = System::Drawing::Size(640, 480);
            this->pictureBox1->TabIndex = 5;
            this->pictureBox1->TabStop = false;
            // 
            // MyForm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->AutoValidate = System::Windows::Forms::AutoValidate::EnablePreventFocusChange;
            this->ClientSize = System::Drawing::Size(1077, 538);
            this->Controls->Add(this->pictureBox1);
            this->Controls->Add(this->button5);
            this->Controls->Add(this->button4);
            this->Controls->Add(this->button3);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->KeyPreview = true;
            this->Name = L"MyForm";
            this->Text = L"MyForm";
            this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
            this->ResumeLayout(false);

        }
#pragma endregion 
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    }
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    }
    private: System::Void button1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
        if (e->KeyCode == Keys::NumPad6) {
            serialPort1->Open();
            serialPort1->Write("R");
            serialPort1->Close();
        }
    }
    private: System::Void button1_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
        if (e->KeyCode == Keys::NumPad6) {
            serialPort1->Open();
            serialPort1->Write("X");
            serialPort1->Close();
        }
    }
    private: System::Void button1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
        serialPort1->Open();
        serialPort1->Write("R");
        serialPort1->Close();
    }
    private: System::Void button1_MouseUp(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
        serialPort1->Open();
        serialPort1->Write("X");
        serialPort1->Close();
    }
    private: System::Void button2_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
        if (e->KeyCode == Keys::NumPad4) {
            serialPort1->Open();
            serialPort1->Write("L");
            serialPort1->Close();
        }
    }
    private: System::Void button2_KeyUp(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
        if (e->KeyCode == Keys::NumPad4) {
            serialPort1->Open();
            serialPort1->Write("X");
            serialPort1->Close();
        }
    }
};
}

cpp文件代码

    //#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <opencv2\imgcodecs.hpp>
#include "MyForm.h"

using namespace cv;
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;

void DrawCVImage(System::Windows::Forms::Control^ control, cv::Mat& colorImage)
{
    System::Drawing::Graphics^ graphics = control->CreateGraphics();
    System::IntPtr ptr(colorImage.ptr());
    System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows, colorImage.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
    System::Drawing::RectangleF rect(0, 0, control->Width, control->Height);
    graphics->DrawImage(b, rect);
    delete graphics;
}

int main() {
    Mat img; 
    VideoCapture cam(0);

    while (1) {
        cam >> img;

        DrawCVImage(pictureBox1, img);
        if (waitKey(1) == 27)
            break;
    }
    cam.release();

    return 0;
}

0 个答案:

没有答案