Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to compile Opencv 3.4.1 program in VS 2019 with Opencv 4.1.1?

I've a program written for Opencv 3.4.1 in order to capture image by basler camera and store it in "IplImage*" type of image then show it in Opencv window. I'm using Visual Studio 2019 with Opencv 4.1.1 and when i compiled it it had some errors:

At "static IplImage* s_pCvSrcImg = NULL;" : Missing type specifier-assumed int.Note: C++ does not support the default -int

memcpy(s_pCvSrcImg->imageData, ptrGrabResult->GetBuffer(), ptrGrabResult->GetImageSize());: Identifier " memcpy" cannot be found

And the same errors for cvCanny ,cvShowImage, cvWaitKey in the below Lines Here is the program:

#define OPENCV
#if defined OPENCV
#include "opencv2/opencv.hpp"
#endif  //#if defined OPENCV
#include <conio.h>
// Include files to use the PYLON API.
#include <pylon/PylonIncludes.h>
#ifdef PYLON_WIN_BUILD
#    include <pylon/PylonGUI.h>
#endif

// Namespace for using pylon objects.
using namespace Pylon;

// Namespace for using cout.
using namespace std;

// Namespace for using GenApi objects.
using namespace GenApi;

// Number of images to be grabbed.
static const uint32_t c_countOfImagesToGrab = 1;

#if defined OPENCV
static char s_szCvWindowNameSrc[64] = "Source";
static char s_szCvWindowNameProc[64] = "Processed";
static IplImage* s_pCvSrcImg = NULL;    //Source image
static IplImage* s_pCvProcImg = NULL;   // Destination image
#endif  //#if defined OPENCV

 int main(int argc, char* argv[])
{
// The exit code of the sample application.
int exitCode = 0;

// Before using any pylon methods, the pylon runtime must be initialized. 
PylonInitialize();

try
{

// Create an instant camera object with the camera device found first.
    CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());
   // Print the model name of the camera.
    cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;

    // The parameter MaxNumBuffer can be used to control the count of buffers
    // allocated for grabbing. The default value of this parameter is 10.
    camera.MaxNumBuffer = 5;

    // Start the grabbing of c_countOfImagesToGrab images.
    // The camera device is parameterized with a default configuration which
    // sets up free-running continuous acquisition.
    // camera.StartGrabbing( c_countOfImagesToGrab);
    camera.StartGrabbing(-1);

#if defined OPENCV
    INodeMap& nodemap = camera.GetNodeMap();

    CIntegerPtr width(nodemap.GetNode("Width"));
    unsigned int uiWidth = (unsigned int)width->GetValue();

    CIntegerPtr height(nodemap.GetNode("Height"));
    unsigned int uiHeight = (unsigned int)height->GetValue();

    // Create OpenCV window for mono8
    s_pCvSrcImg = cvCreateImage(cvSize(uiWidth, uiHeight), IPL_DEPTH_8U, 1);
    s_pCvProcImg = cvCreateImage(cvSize(uiWidth, uiHeight), IPL_DEPTH_8U, 1);

    // for change fps automatically 
    CBooleanPtr AcquisitionFrameRateEnable(nodemap.GetNode("AcquisitionFrameRateEnable"));
    AcquisitionFrameRateEnable->SetValue(true);

 #endif //#if defined OPENCV

    // This smart pointer will receive the grab result data.
    CGrabResultPtr ptrGrabResult;

    // Camera.StopGrabbing() is called automatically by the RetrieveResult() method
    // when c_countOfImagesToGrab images have been retrieved.
    int iCount = 0;
    bool bQuit = false;

    while ( false == bQuit )
    {
        // Wait for an image and then retrieve it. A timeout of 5000 ms is used.
        camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);

        //cout << iCount << ", " << endl;
        // Image grabbed successfully?
        if (ptrGrabResult->GrabSucceeded())
        {
#if defined OPENCV
    DWORD dwTimeStart = GetTickCount();

  memcpy(s_pCvSrcImg->imageData, ptrGrabResult->GetBuffer(), ptrGrabResult->GetImageSize());// Retleave image 
  from SDK
  cvCanny(s_pCvSrcImg, s_pCvProcImg, 64, 128);  // Edge detection
  cvShowImage(s_szCvWindowNameSrc, s_pCvSrcImg);    // Show camera image
  cvShowImage(s_szCvWindowNameProc, s_pCvProcImg); // Show processed image
  cvWaitKey(50);    // To keep show cvShowImage

  DWORD dwProcTime = GetTickCount() - dwTimeStart;
  //cout << "Process Time = " << dwProcTime<< "[ms] img" << ptrGrabResult->GetBlockID() << endl;
     printf("\rProcess Time = %ld[ms] img %ld", dwProcTime, (DWORD)ptrGrabResult->GetBlockID());

 // change fps automatically. Target is 80% of process time
   CFloatPtr AcquisitionFrameRate(nodemap.GetNode("AcquisitionFrameRate"));
   double dCamFps = AcquisitionFrameRate->GetValue();
   double dTargetFps = 1000.0 / (double)dwProcTime * 0.8;
// Adjustment fps when the camera fps is out of +-50% 
   if ((dTargetFps*1.5 < dCamFps) || (dTargetFps*0.5 > dCamFps))
 {
   AcquisitionFrameRate->SetValue(dTargetFps);
   printf("\nChange Frame Rate from %5.1f to %5.1f [fps] Process time%d[ms]\n", dCamFps, dTargetFps, dwProcTime);
 }

 #endif  //#if defined OPENCV

 #ifdef PYLON_WIN_BUILD
            // Display the grabbed image.
            //Pylon::DisplayImage(1, ptrGrabResult);
 #endif
        }
        else
        {
            cout << "Error: " << ptrGrabResult->GetErrorCode() << " " << ptrGrabResult->GetErrorDescription() << endl;
        }
        iCount++;

        if (::_kbhit())
    {
    switch (_getch())
      {
        case 'q':  // Quit this program
        case 'Q':
          bQuit = true;
          break;
      }
    }
    }
}
catch (const GenericException &e)
{
    // Error handling.
    cerr << "An exception occurred." << endl
    << e.GetDescription() << endl;
    exitCode = 1;
}

// Comment the following two lines to disable waiting on exit.
cerr << endl << "Press Enter to exit." << endl;
while( cin.get() != '\n');

// Releases all pylon resources. 
PylonTerminate();  

return exitCode;
}

So My question is how to fix those errors and make the program compile in VS 2019 with Opencv 4.1.1? Can those errors variables be replaced by the appropriate ones in Opencv 4.1.1 version? If it can so what are those variable's name? Thank you so much.