5 Replies Latest reply on Jun 11, 2014 1:26 AM by pinform

    cv::ocl::oclMat issue in CodeXL

    nagesh_gupta
      Hi,              

       

      I am trying to debug an application in CodeXL that uses ocl based data structures and calls. ocl is the OpenCL implementation of some of the OpenCV functions. ocl source can be found in the standard OpenCV release at modules/ocl directory.

       

      I see that CodeXL consistently crashes when I load any of the applications that use ocl data structures, specifically cv::ocl::oclMat::release(). Here is the code snippet where is fails,

       

      inline oclMat::~oclMat()

      {

          release(); // Fails here, no further stack trace

      }

       

      and the definition for release() is as below,

       

      void cv::ocl::oclMat::release()

      {

      //cout << "cv::ocl::oclMat::release()" << endl;
      if( refcount && CV_XADD(refcount, -1) == 1 )
      {
          fastFree(refcount);
          openCLFree(datastart);
      }
      data = datastart = dataend = 0;
      step = rows = cols = 0;
      offset = wholerows = wholecols = 0;
      refcount = 0;

      }

       

      Am I missing any initialization step here?

       

      Thanks,

      Nagesh

        • Re: cv::ocl::oclMat issue in CodeXL
          himanshu.gautam

          Just with this code i can not say anything.

          Please give us the complete compiling code along with your system configuration information. like installed drivers info, GPU, etc.


            • Re: cv::ocl::oclMat issue in CodeXL
              nagesh_gupta

              Thanks Himanshu. I don't know how to attach files here. I will paste the source code and build command in my next two replies - Nagesh

                • Re: cv::ocl::oclMat issue in CodeXL
                  nagesh_gupta

                  #include <stdio.h>

                  #include <iostream>

                  #include <sstream>

                  #include <iomanip>  //for setprecision

                   

                  #include <CL/cl.h>

                  #include <opencv2/video/tracking.hpp>

                  #include <opencv2/highgui/highgui.hpp>

                  #include <opencv2/imgproc/imgproc.hpp>

                  #include <opencv2/ocl/ocl.hpp>

                   

                  using namespace std;

                  using namespace cv;

                   

                  RNG rng(cvGetTickCount());

                   

                  static Scalar randomColor(RNG& rng)

                  {

                    int icolor = (unsigned)rng;

                    return Scalar(icolor&255, (icolor>>8)&255, (icolor>>16)&255);

                  }

                   

                  static int dist(int x1, int y1, int x2, int y2){

                    int dx = x1 - x2;

                    int dy = y1 - y2;

                    int result = sqrt(dx * dx + dy * dy);

                    return result;

                  }

                   

                  static string toString(double value, int precision){

                    stringstream sstr;

                    sstr << fixed << setprecision(precision) << value;

                    return sstr.str();

                  }

                   

                  //--------------------------------------------------

                  static string toString(int value){

                    stringstream sstr;

                    sstr << value;

                    return sstr.str();

                  }

                   

                  // using namespace cv::ocl;

                   

                  //Ball class

                  class Ball

                  {

                  private:

                    Mat *canvas;

                    float x, y, dim;

                    float speedX, speedY;

                    Scalar color;

                    int lives;

                   

                    Ball();

                    void setup();

                   

                  public:

                    Ball(Mat *can)

                    {

                      canvas = can;

                      setup();

                    }

                   

                    void update();

                    void draw();

                   

                    int getDim();

                    int getX();

                    int getY();

                   

                    void decrementLives();

                    bool checkMouseOver(int _mouseX, int _mouseY);

                    bool isEndLive();

                  };

                  enum

                  {

                    APP_MOUSE_LEFT   = 1,

                    APP_MOUSE_RIGHT  = 2,

                    APP_MOUSE_MIDDLE = 3,

                  };

                  void mouse_callback(int event, int x, int y, int flags, void* param);

                   

                  //MainApp singelton class

                  class MainApp

                  {

                  private:

                    Mat cam_frame, img_gray, img_first, img_prev, img_diff, img_bin;

                    Mat canvas;            // Image for drawing

                    CvMat *_cam_frame;

                   

                    Scalar bgr_color;         // Background color

                    vector <Ball> myBall;

                    bool pauseBallsMove;

                    bool m_mousePressed;

                   

                  public: // Some global params:

                    static int DELAY;

                    static int CANVAS_WIDTH;

                    static int CANVAS_HEIGHT;

                    static int INITIAL_BALLS_NUMBER;

                   

                  private:

                    MainApp() {}

                    MainApp(const MainApp &);

                    MainApp& operator=(const MainApp&);

                   

                    void setup();   // Initial commands for setup processing

                    void update();  // Commands to modify the parameters

                    void draw();    // Drawing functions:

                   

                  public:

                    static MainApp& getInstance()

                    {

                      static MainApp instance;

                      return instance;

                    }

                   

                    // Main loop function with displaying image support

                    // and handle mouse and keyboard events

                    void run();

                   

                    void keyPressed(int key);

                    void mouseMoved(int mouseX, int mouseY );

                    void mouseDragged(int mouseX, int mouseY);

                    void mousePressed(int mouseX, int mouseY, int button);

                    void mouseReleased(int mouseX, int mouseY, int button);

                    void mouseDoubleClick(int mouseX, int mouseY, int button);

                  };

                   

                  //--------------------------------------------------------------

                  // Best place to initialize global MainApp params:

                  int MainApp::DELAY        = 10;

                  int MainApp::CANVAS_WIDTH = 640;

                  int MainApp::CANVAS_HEIGHT= 480;

                  int MainApp::INITIAL_BALLS_NUMBER = 30;

                   

                  //--------------------------------------------------------------

                  void MainApp::run() {

                    setup();

                   

                    const char *win_canvas = "Canvas";

                    namedWindow(win_canvas, CV_WINDOW_AUTOSIZE);

                    cvSetMouseCallback(win_canvas, mouse_callback, this);

                    std::vector<cv::ocl::Info> oclinfo;

                    int devnums = cv::ocl::getDevice(oclinfo);

                    if(devnums < 1)

                    {

                      std::cout << "Device not found!" << std::endl;

                    }

                   

                  //start: Camera initialisation:

                   

                    CvCapture* camera = cvCreateCameraCapture(0);

                   

                    if (!camera) {

                      printf("No capture device found!\n\n");

                      exit(1);

                    }

                    // Try to set the camera resolution to 640 x 480.

                    cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_WIDTH, 640);

                    cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_HEIGHT, 480);

                   

                    IplImage* frame = cvQueryFrame(camera);

                    if(!frame) {

                      printf("Caputuring error\n\n");

                      exit(1);

                    }

                    _cam_frame  = cvCreateMat(frame->height, frame->width, CV_8UC3);

                    cvSetIdentity(_cam_frame);

                   

                    cvGetImage(_cam_frame, frame);

                    cam_frame = cvarrToMat(_cam_frame);

                   

                    // Wait a little, so that the camera can auto-adjust its brightness.

                    //sleep(1);    // (in seconds)

                   

                  //stop: Camera initialisation:

                   

                    int   motionType      = 1;

                    int   motionThreshold = 20;

                    float motionSigmaBlur = 2.0f;

                   

                    int key;

                    bool first_frame = true;

                    while(1) {

                      key = cvWaitKey(DELAY);  // wait for keyboard input

                      if (key == 'q') break;   // 'q' pressed, quit the program

                      if (key != -1 ) keyPressed(key);

                   

                      IplImage* frame = cvQueryFrame(camera);

                      if(!frame) break;

                   

                      cvGetImage(_cam_frame, frame);

                      cam_frame = cvarrToMat(_cam_frame);

                   

                      flip(cam_frame,cam_frame,1);

                      cvtColor(cam_frame, img_gray, CV_BGR2GRAY);

                      GaussianBlur(img_gray, img_gray, Size(0,0), motionSigmaBlur, motionSigmaBlur);

                   

                      if (first_frame) {

                        img_prev=img_gray.clone();

                        first_frame = false;

                        continue;

                      }

                   

                      cv::ocl::oclMat ocl_img_gray;

                      cv::ocl::oclMat ocl_img_prev;

                      cv::ocl::oclMat ocl_img_diff;

                      cv::ocl::oclMat ocl_img_bin;

                      ocl_img_gray = img_gray;

                      ocl_img_prev = img_prev;

                      //ocl_img_diff = img_diff;

                      cv::ocl::absdiff(ocl_img_gray, ocl_img_prev, ocl_img_diff);

                      //absdiff(img_gray, img_prev, img_diff);

                      //printf ("After ocl functions\n");

                   

                      img_gray = ocl_img_gray;

                      img_prev = ocl_img_prev;

                      img_diff = ocl_img_diff;

                      // absdiff(img_gray, img_prev, img_diff);

                   

                  //    cv::ocl::threshold(ocl_img_diff, ocl_img_bin, motionThreshold, 255, THRESH_BINARY);

                  //    img_bin = ocl_img_bin;

                      threshold(img_diff, img_bin, motionThreshold, 255, THRESH_BINARY);

                   

                      cv::Mat myk(3,3,CV_8UC1);

                   

                      erode(img_bin,  img_bin, Mat(), Point(-1,-1), 3);

                  //     cv::ocl::erode(ocl_img_bin,  ocl_img_bin, Mat(3,3,CV_8U,cv::Scalar(1))); //, Point(-1,-1), 3);

                      dilate(img_bin, img_bin, Mat(), Point(-1,-1), 1);

                   

                      update();

                      draw();

                   

                      imshow(win_canvas, canvas);

                   

                      if (motionType == 1) img_prev=img_gray.clone();

                    }

                  }

                   

                  //--------------------------------------------------------------

                  void MainApp::setup()

                  {

                    pauseBallsMove = false;

                    bgr_color = Scalar(200,200,200);

                    canvas = Mat(CANVAS_HEIGHT, CANVAS_WIDTH, CV_8UC3, bgr_color);

                   

                    for (int i = 0; i < INITIAL_BALLS_NUMBER; i++){

                      myBall.push_back( Ball(&canvas) );

                    }

                  }

                   

                   

                  //--------------------------------------------------------------

                  void MainApp::update()

                  {

                    int max_motion_points = 10;

                   

                    int movementAmount, ball_dim, ball_x, ball_y;

                    int canvas_ind_x, canvas_ind_y;

                   

                    for (int k = 0; k < myBall.size(); k++) {

                      movementAmount = 0;

                      ball_dim = myBall[k].getDim();

                      ball_x   = myBall[k].getX();

                      ball_y   = myBall[k].getY();

                   

                      for (int i=(-1*ball_dim); i<ball_dim; i++)

                        for (int j=(-1*ball_dim); j<ball_dim; j++)

                          if ( dist(0,0,i,j) < ball_dim ) {

                            canvas_ind_x = ball_x + i;

                            canvas_ind_y = ball_y + j;

                   

                            if ( canvas_ind_x<=0 || canvas_ind_y<=0 || canvas_ind_x>CANVAS_WIDTH || canvas_ind_y>CANVAS_HEIGHT) continue;

                   

                            if ( img_bin.at<uchar>(canvas_ind_y,canvas_ind_x) > 30) {

                              movementAmount++;

                            }

                          }

                   

                      if (movementAmount > max_motion_points)

                        myBall[k].decrementLives();

                      else

                        if (!pauseBallsMove) myBall[k].update();

                    }

                   

                    for(int i=0; i<myBall.size(); i++) {

                      if(myBall[i].isEndLive()) {

                        myBall.erase(myBall.begin()+i);

                      }

                    }

                   

                    if (myBall.size() == 0) {

                      for (int i = 0; i < INITIAL_BALLS_NUMBER; i++){

                        myBall.push_back( Ball(&canvas) );

                      }

                    }

                  }

                   

                  //--------------------------------------------------------------

                  void MainApp::draw() {

                    //canvas = bgr_color;

                    canvas = cam_frame;

                   

                    for (int i = 0; i < myBall.size(); i++){

                      myBall[i].draw();

                    }

                  }

                   

                  //--------------------------------------------------------------

                  void MainApp::keyPressed(int key){

                   

                    switch (key){

                      case ' ':

                        pauseBallsMove = !pauseBallsMove;

                        break;

                      case 'a':

                        myBall.push_back( Ball(&canvas) );

                        break;

                      case 'd':

                        if (myBall.size()>=1) myBall.pop_back();

                        break;

                    }

                  }

                   

                  //--------------------------------------------------------------

                  void MainApp::mouseMoved(int mouseX, int mouseY ){

                    if (m_mousePressed) mouseDragged(mouseX, mouseY);

                  }

                   

                  //--------------------------------------------------------------

                  void MainApp::mouseDragged(int mouseX, int mouseY){

                  }

                   

                  //--------------------------------------------------------------

                  void MainApp::mousePressed(int mouseX, int mouseY, int button){

                    m_mousePressed = true;

                   

                    for(int i=0; i<myBall.size(); i++){

                      if(myBall[i].checkMouseOver(mouseX, mouseY)){

                        myBall[i].decrementLives();

                   

                        if(myBall[i].isEndLive()) myBall.erase(myBall.begin()+i);

                      }

                    }

                  }

                   

                  //--------------------------------------------------------------

                  void MainApp::mouseReleased(int mouseX, int mouseY, int button){

                    m_mousePressed = false;

                  }

                   

                  //--------------------------------------------------------------

                  void MainApp::mouseDoubleClick(int mouseX, int mouseY, int button){

                  }

                   

                  //--------------------------------------------------------------

                   

                  void mouse_callback(int event, int x, int y, int flags, void* param)

                  {

                    MainApp *app = ((MainApp *)param);

                    switch (event) {

                      case  CV_EVENT_LBUTTONDBLCLK:

                        app->mouseDoubleClick(x,y,APP_MOUSE_LEFT);   break;

                      case  CV_EVENT_RBUTTONDBLCLK:

                        app->mouseDoubleClick(x,y,APP_MOUSE_RIGHT);  break;

                      case  CV_EVENT_MBUTTONDBLCLK:

                        app->mouseDoubleClick(x,y,APP_MOUSE_MIDDLE); break;

                      case  CV_EVENT_LBUTTONDOWN:

                        app->mousePressed(x,y,APP_MOUSE_LEFT);       break;

                      case  CV_EVENT_RBUTTONDOWN:

                        app->mousePressed(x,y,APP_MOUSE_RIGHT);      break;

                      case  CV_EVENT_MBUTTONDOWN:

                        app->mousePressed(x,y,APP_MOUSE_MIDDLE);     break;

                      case  CV_EVENT_LBUTTONUP:

                        app->mouseReleased(x,y,APP_MOUSE_LEFT);      break;

                      case  CV_EVENT_RBUTTONUP:

                        app->mouseReleased(x,y,APP_MOUSE_RIGHT);     break;

                      case  CV_EVENT_MBUTTONUP:

                        app->mouseReleased(x,y,APP_MOUSE_MIDDLE);    break;

                      case  CV_EVENT_MOUSEMOVE:

                        app->mouseMoved(x,y);                        break;

                    }

                  }

                   

                  void Ball::setup()

                  {

                    x = rng.uniform(0, MainApp::CANVAS_WIDTH);       // give some random positioning

                    y = rng.uniform(0, MainApp::CANVAS_HEIGHT);

                    speedX = rng.uniform((float)-10, (float)10);   // and random speed and direction

                    speedY = rng.uniform((float)-10, (float)10);

                    dim    = rng.uniform(20,60);

                   

                    color = randomColor(rng);

                    lives = 3;

                  }

                   

                  void Ball::update()

                  {

                    if(x < 0 ){

                      x = 0;

                      speedX *= -1;

                    } else if(x > MainApp::CANVAS_WIDTH){

                      x = MainApp::CANVAS_WIDTH;

                      speedX *= -1;

                    }

                   

                    if(y < 0 ){

                      y = 0;

                      speedY *= -1;

                    } else if(y > MainApp::CANVAS_HEIGHT){

                      y = MainApp::CANVAS_HEIGHT;

                      speedY *= -1;

                    }

                   

                    x+=speedX;

                    y+=speedY;

                  }

                   

                  void Ball::draw()

                  {

                    circle(*canvas, Point(x,y), dim, color,-1,CV_AA);

                    putText( *canvas, toString(lives), cvPoint(x,y),CV_FONT_HERSHEY_SIMPLEX,0.5f,CV_RGB(255,255,255));

                  }

                   

                  bool Ball::checkMouseOver(int _mouseX, int _mouseY) {

                    bool mouseIsOver = false;

                   

                    if ( dist(_mouseX,_mouseY,x,y) < dim ) mouseIsOver = true;

                   

                    return mouseIsOver;

                  }

                   

                  void Ball::decrementLives() {

                    lives--;

                  }

                   

                  bool Ball::isEndLive() {

                    return (lives <= 0);

                  }

                   

                  int Ball::getDim() {

                    return dim;

                  }

                   

                  int Ball::getX() {

                    return x;

                  }

                   

                  int Ball::getY() {

                    return y;

                  }

                   

                   

                  int main(int, char**)

                  {

                    MainApp::getInstance().run();

                   

                    return 0;

                  }

                    • Re: cv::ocl::oclMat issue in CodeXL
                      nagesh_gupta

                      g++ -g ball.cpp \

                      -I ${OpenCV_DIR}/include \

                      -I ${AMDAPPSDKROOT}/include \

                      -L ${OpenCV_DIR}/lib \

                      -L ${AMDAPPSDKROOT}/lib/x86_64 \

                      -l opencv_calib3d \

                      -l opencv_contrib \

                      -l opencv_core \

                      -l opencv_features2d \

                      -l opencv_flann \

                      -l opencv_gpu \

                      -l opencv_highgui \

                      -l opencv_imgproc \

                      -l opencv_legacy \

                      -l opencv_ml \

                      -l opencv_nonfree \

                      -l opencv_objdetect \

                      -l opencv_ocl \

                      -l opencv_photo \

                      -l opencv_stitching \

                      -l opencv_superres \

                      -l opencv_ts \

                      -l opencv_video \

                      -l opencv_videostab \

                      -l OpenCL \

                      -o ball