none
Hi, i never touch c++ before, Can anyone please help me to covert these c++ codes to c#?? Please. Or teach me how to compile into C++ and execute with C#. Or if any of you all happen where or even got the C# source code for the background subtraction. RRS feed

  • Question

  • Background subtraction c++ code 

    #include "opencv2\opencv.hpp"
    #include "opencv2\bgsegm.hpp" //for MOG, GMG
    #include <iostream>
    #include <time.h>

    #ifdef _DEBUG               
    #pragma comment(lib, "opencv_core320d.lib")       
    #pragma comment(lib, "opencv_highgui320d.lib")    
    #pragma comment(lib, "opencv_imgproc320d.lib")  
    #pragma comment(lib, "opencv_videoio320d.lib")    
    #pragma comment(lib, "opencv_bgsegm320d.lib")    
    #pragma comment(lib, "opencv_video320d.lib")    
    #else       
    #pragma comment(lib, "opencv_core320.lib")       
    #pragma comment(lib, "opencv_highgui320.lib")    
    #pragma comment(lib, "opencv_imgproc320.lib")  
    #pragma comment(lib, "opencv_videoio320.lib")    
    #pragma comment(lib, "opencv_bgsegm320.lib")    
    #pragma comment(lib, "opencv_video320.lib")    
    #endif        

    using namespace std;
    using namespace cv;

            void main()
            {

                //global variables
                Mat frame; //current frame
                Mat resizeF;
                Mat fgMaskMOG; //fg mask generated by MOG method
                Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
                Mat fgMaskGMG; //fg mask fg mask generated by GMG method
                Mat fgMaskKNN; //fg mask fg mask generated by KNN method

                Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor
                Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
                Ptr<BackgroundSubtractor> pGMG; //GMG Background subtractor
                Ptr<BackgroundSubtractor> pKNN; //KNN Background subtractor

                //create
                pMOG = cv::bgsegm::createBackgroundSubtractorMOG();
                pMOG2 = createBackgroundSubtractorMOG2();
                pGMG = cv::bgsegm::createBackgroundSubtractorGMG();
                pKNN = createBackgroundSubtractorKNN();

                char fileName[100] = "cctv2.mov";
                VideoCapture stream1(fileName);

                Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1, 1));

                //unconditional loop   
                while (true)
                {
                    Mat cameraFrame;
                    if (!(stream1.read(frame))) //get one frame form video   
                        break;

                    resize(frame, resizeF, Size(frame.size().width / 4, frame.size().height / 4));
                    pMOG->apply(resizeF, fgMaskMOG);
                    pMOG2->apply(resizeF, fgMaskMOG2);
                    pGMG->apply(resizeF, fgMaskGMG);
                    pKNN->apply(resizeF, fgMaskKNN);

                    //morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element); 

                    imshow("Origin", resizeF);
                    imshow("MOG", fgMaskMOG);
                    imshow("MOG2", fgMaskMOG2);
                    imshow("GMG", fgMaskGMG);
                    imshow("KNN", fgMaskKNN);

                    if (waitKey(30) >= 0)
                        break;
                }
            }
    Monday, March 12, 2018 2:03 AM

All replies

  • LOTS OF THANKS AND LOVE
    Monday, March 12, 2018 2:03 AM
  • That code isn't going to work in C#. You are relying on C++ classes which aren't available in managed code. Your best bet is to find a managed wrapper around the openCV API. There is one mentioned here. Then you can replicate the calls you're making to the actual API (not the pointer stuff) in C#.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, March 12, 2018 4:47 PM
    Moderator