Ask Your Question

How to access Basler GigE camera through OpenCV in Ubuntu 14.04

asked 2015-10-15 00:58:50 -0500

lm35 gravatar image

updated 2015-10-16 04:01:25 -0500

Can we access GigE camera just like ip Camera. I followed this link but was unable to access the camera. I am getting the message as Cannot open the video cam. I gave the ip address as the one I have set for my cam. Can someone please help

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2015-10-15 04:15:39 -0500

pklab gravatar image

updated 2015-10-15 04:19:19 -0500

I don't think that the method you are referring works for all cameras. IP cameras delivers MPEG or similar stream, generally is used for survelliance. The URL for the stream depends from manufacured/model.

GigE is a standard for Machine Vision and gives you total control over the camera/acquisition process.

Anyway Basler uses Pylon Driver/Library wich is available on Basler website

After you are shure that your camera is reacheable from your pc (try a ping to the cam or use the pylon Viewer), there are 2 way to use the Pylon driver:

1st easy way is available for Windows (I don't know in linux)

Pylon driver installs a standard VFW interface so you can read your cam using cv::VideoCapture(camIdx) where of camIdx is the the id of capturing device assigned by your OS. If there is a single camera connected, (not even a webcam) just pass 0 else try to detect the id. Because might be difficult to locate the id in your computer you could start with a searching loop like this

bool EnumerateCameras(std::vector<int> &camIdx)
    std::string winName;
    Mat frame;
    std::cout << "Searching for cameras IDs...";
    for(int idx=0; idx<10; idx++) 
       VideoCapture cap(idx);       // open the camera
       if(cap.isOpened())           // check if we succeeded
            camIdx.push_back(idx);  // ad the ID to list of available cameras
            std::cout << idx << "OK ";
            cap >> frame;
            winName = "A frame from camID: " + std::to_string(idx);
            imshow(winName,frame);  // display a frame from the current camera
    cout << std::endl << camCnt << " cam(s) available";
    cout "Press a key..."

    return (camIdx.size()>0); // returns success

2nd way is more powerful and gives you total control of camera features on Linux too

You have to use the Basler Pylon Camera Software Suite as library to grab the image from the cam. Once you have grabbed the image in Pylon you can map it to a cv::Mat

Pylon offers many different grabbing techniques (and many examples in C++). Link the Pylon Lib to your OpenCV project and include needed Pylon function calls to catch a pylon::CGrabResultPtr. Once you have the ptrGrabResul map it to a Mat as code below;

pylon::CGrabResultPtr ptrGrabResult;
cv::Mat theFrame;

// Use your preferred Pylon grabbing technique to obtain ptrGrabResult

// suppose your camera is monochrome... get a pointer to pylon image 
const pylon::uint8_t *pImageBuffer = (uint8_t *)ptrGrabResult->GetBuffer();
int frameCols = ptrGrabResult->GetWidth();
int frameRows = ptrGrabResult->GetHeight();

// Map the pylon image buffer to a cv::Mat (create a cv::Mat from external buffer)
theFrame cv::Mat(cv::Size(frameCols, frameRows), CV_8U, (void*)pImageBuffer, cv::Mat::AUTO_STEP);

// Go on using OpenCV
// Take care to buffer life cycle because it's out of your control
// and it will be destroyed/changed on next grab

// in case keep a copy of it
cv::Mat myFrame;
theFrame.copyTo(myFrame); // myFrame life cycle is now under your control
edit flag offensive delete link more


If I use C language, how can I create a IplImage by PylonImage?

zhang gravatar imagezhang ( 2017-06-17 07:07:44 -0500 )edit

I would create an image header than map it over Pylon Buffer

CvSize sz = cvSize(ptrGrabResult->GetWidth(), ptrGrabResult->GetHeight());
// suppose your camera is monochrome... 
int ch = 1;
IplImage* theFrame = cvCreateImageHeader(sz,IPL_DEPTH_8U,ch); //Allocates header
int step = ch * ptrGrabResult->GetWidth(); //Full row length in bytes 
cvSetData(theFrame,(void*)ptrGrabResult->GetBuffer(),step); //Assigns user data
... use the image ...
cvReleaseImageHeader(&theFrame); //Deallocates image header.
pklab gravatar imagepklab ( 2017-06-19 04:40:40 -0500 )edit

answered 2015-11-04 03:22:23 -0500

lm35 gravatar image

I successfully accessed the cam by setting a separate Ethernet connection in my Ubuntu Network Manager. I used 'Cloned MAC address' instead of 'Device MAC address'. So when I set IP for my Ethernet adapter and ran the 'PylonViewer App' , which comes with Pylon Software suite, I could see my GigE camera listed.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools



Asked: 2015-10-15 00:58:50 -0500

Seen: 8,557 times

Last updated: Nov 04 '15