Ask Your Question
-1

Hi I want to open camera in windows forms in cpp.

asked 2017-09-23 03:52:11 -0600

Ashiq KS gravatar image

updated 2017-09-23 03:55:53 -0600

I have an application to load images from files for processing, but I need to load live images from a camera. I edited the code but I am getting errors. Here I cite the original code and my edited version. Opencv 3.1.

            Older Code

           private: System::Void btnLoad_Click(System::Object^  sender, System::EventArgs^  e) {

    OpenFileDialog^ dgOpen = gcnew OpenFileDialog();
    dgOpen->Filter = "Image(*.bmp; *.jpg)|*.bmp;*.jpg|All files (*.*)|*.*||";
    if (dgOpen->ShowDialog() == System::Windows::Forms::DialogResult::Cancel)
    {
        return;
    }
    Bitmap^ bmpSrc = gcnew Bitmap(dgOpen->FileName);
    ptbSrc->Image = bmpSrc;
    ptbSrc->Refresh();
    srcImg = imread(stringTochar.ConvertString2Char(dgOpen->FileName));
    int dbSize_H = srcImg.rows;
    int dbSize_W = srcImg.cols;
    System::String^ h = gcnew System::String(to_string(dbSize_H).c_str());
    System::String^ w = gcnew System::String(to_string(dbSize_W).c_str());

    imgSize->Text = w + "x" + h;}

         Edited code


        //CvCapture* capture;
        VideoCapture capture;
         Mat frame, srcImg;
        private: System::Void btnLoad_Click(System::Object^  sender, System::EventArgs^  e) {

        capture.open(0);
        if (capture.isOpened())
        capture.read(frame);

    ptbSrc->Image = gcnew;
          System::Drawing::Bitmap(frame->width,frame->height,frame->widthStep,
          System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) frame->imageData);
    ptbSrc->Refresh();
    srcImg = frame;
    int dbSize_H = srcImg.rows;
    int dbSize_W = srcImg.cols;
    System::String^ h = gcnew System::String(to_string(dbSize_H).c_str());
    System::String^ w = gcnew System::String(to_string(dbSize_W).c_str());

    imgSize->Text = w + "x" + h;}

Here frame is for picture box 'ptbSrc' and srcImg for further processing.

edit retag flag offensive close merge delete

Comments

any chance, we can talk you out of this idea ?

opencv and managed c++ do never go well together (entirely orthogonal programming models)

being a noob, you'll run into more trouble than nessecary. can't you just stick with opencv's highgui window to show the video ?

berak gravatar imageberak ( 2017-09-23 03:58:25 -0600 )edit

Actually i have an application for reading characters from license plate by loading images from file by clicking a load button. Now I need to replace the image load button by a camera button. The problem comes in System::Drawing::Bitmap(frame.width,frame.height,frame.widthStep, System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) frame.imageData); where the error showing that class cv::Mat has no member width, height, widthStep, imageData. Moreover is there any substitute for Bitmap^ bmpSrc = gcnew Bitmap(dgOpen->FileName); in video class?

Ashiq KS gravatar imageAshiq KS ( 2017-09-23 04:15:44 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-30 04:14:20 -0600

Ashiq KS gravatar image

Dear Berak, I could open camera on windows form now. Thanks for your response.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-23 03:52:11 -0600

Seen: 970 times

Last updated: Sep 23 '17