Opencv python faster than C++?

asked 2018-03-22 07:09:02 -0500

abhijit gravatar image

I am trying to time the houghcircle in python and c++ to see if c++ gives edge over processing time (intuitively it should!)


  • python: 3.6.4
  • gcc compiler: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
  • cmake : 3.5.1
  • opencv : 3.4.1

I actually installed opencv using anaconda. Surprisingly c++ version also worked

The image I am using is given here: enter image description here

Python code

import cv2
import time
import sys

def hough_transform(src,dp,minDist,param1=100,param2=100,minRadius=0,maxRadius=0):
    gray = cv2.cvtColor(src,cv2.COLOR_RGB2GRAY)
    start_time = time.time()
                             dp = dp,
                             minDist = minDist,
    end_time = time.time()
    print("Time taken for hough circle transform is : {}".format(end_time-start_time))
    # if circles is not None:
    #         circles = circles.reshape(circles.shape[1],circles.shape[2])
    # else:
    #     raise ValueError("ERROR!!!!!! circle not detected try tweaking the parameters or the min and max radius")
    # a = input("enter 1 to visualize")
    # if int(a) == 1 :
    #     for circle in circles:
    #         center = (circle[0],circle[1])
    #         radius = circle[2]
    #, center, radius, (255,0,0), 5)
    #     cv2.namedWindow("Hough circle",cv2.WINDOW_NORMAL)
    #     cv2.imshow("Hough circle",src)
    #     cv2.waitKey(0)
    #     cv2.destroyAllWindows()

if __name__ == "__main__":
    if len(sys.argv) != 2:
        raise ValueError("usage: python <path to image>")
    image = cv2.imread(sys.argv[1])
    image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

C++ code

#include <iostream>
#include <opencv2/opencv.hpp>
#include <ctime>
using namespace std;
using namespace cv;

void hough_transform(Mat src, double dp, double minDist, double param1=100, double param2=100, int minRadius=0, int maxRadius=0 )
  Mat gray;
  cvtColor( src, gray, COLOR_RGB2GRAY);
  vector<Vec3f> circles;
  int start_time = clock();
  HoughCircles( gray, circles, HOUGH_GRADIENT, dp, minDist, param1, param2, minRadius, maxRadius);
  int end_time = clock();
  cout<<"Time taken hough circle transform: "<<(end_time-start_time)/double(CLOCKS_PER_SEC)<<endl;
  // cout<<"Enter 1 to visualize the image";
  // int vis;
  // cin>>vis;
  // if (vis == 1)
  // {
  //   for( size_t i = 0; i < circles.size(); i++ )
  //   {
  //       Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  //       int radius = cvRound(circles[i][2]);
  //       circle( src, center, radius, Scalar(255,0,0), 5);
  //   }
  //   namedWindow( "Hough Circle", WINDOW_NORMAL);
  //   imshow( "Hough Circle", src);
  //   waitKey(0);
  //   destroyAllWindows();
  // }

int main(int argc, char** argv)
  if( argc != 2 ){
    cout<<"Usage hough_circle <path to image.jpg>";
    return -1;
  Mat image;
  image = imread(argv[1]);
  return 0;

I was hoping for C++ hough transform to ace python but what happened was actually opposite.

Python result:

enter image description here

C++ result:

enter image description here

Even though C++ ran the complete program ~2X faster it is very slow in hough transform. Why is it so? This is very counter intuitive. What am I missing here?

edit retag flag offensive close merge delete



btw, your python code measures wall time, while the c++ one measures cpu time.

berak gravatar imageberak ( 2018-03-22 08:01:23 -0500 )edit

@berak Could you please elaborate? How can I measure the same type of time in both? Thanks

abhijit gravatar imageabhijit ( 2018-03-22 09:26:25 -0500 )edit

time.clock() in python should do the same as clock() in c++

also, you should not measure a single call, but put it in a loop, and do like 100 iterations, and average.

the python one is from conda ? it also might be interesting to look at getBuildInformation() and compare the compiled in hardware support.

make sure, you compile the c++ code in release mode, with proper compiler optimizations enabled -NDEBUG -O3, etc.

berak gravatar imageberak ( 2018-03-22 09:32:33 -0500 )edit

@berak thanks that solved my issue

abhijit gravatar imageabhijit ( 2018-03-22 10:22:58 -0500 )edit

so, the c++ version was too slow, because like DEBUG flags set and such ?

what's the outcome ?

berak gravatar imageberak ( 2018-03-22 10:41:46 -0500 )edit

No no I was comparing wall and cpu times. I didn't compile it with any flags. Thanks

abhijit gravatar imageabhijit ( 2018-03-22 10:47:57 -0500 )edit