Abhijit Balaji
Abhijit Balaji

Reputation: 1940

opencv python is faster than c++?

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

Versions

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()
    circles=cv2.HoughCircles(gray,
                             cv2.HOUGH_GRADIENT,
                             dp = dp,
                             minDist = minDist,
                             param1=param1,
                             param2=param2,
                             minRadius=minRadius,
                             maxRadius=maxRadius)
    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]
    #         cv2.circle(src, center, radius, (255,0,0), 5)
    #
    #     cv2.namedWindow("Hough circle",cv2.WINDOW_NORMAL)
    #     cv2.imshow("Hough circle",src)
    #     cv2.waitKey(0)
    #     cv2.destroyAllWindows()
    #
    #
    return

if __name__ == "__main__":
    if len(sys.argv) != 2:
        raise ValueError("usage: python hough_circle.py <path to image>")
    image = cv2.imread(sys.argv[1])
    image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    hough_transform(image,1.7,100,50,30,690,700)

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();
  // }
  return;
}

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]);
  cvtColor(image,image,COLOR_BGR2RGB);
  hough_transform(image,1.7,100,50,30,690,700);
  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?

Upvotes: 0

Views: 3563

Answers (2)

Abhijit Balaji
Abhijit Balaji

Reputation: 1940

I was actually comparing 2 different times. Namely wall and CPU. In Linux, in C++ clock() gives CPU time and in Windows it gives wall time. So when I changed my python code to time.clock() Both gave same results. enter image description here

As explained by @UKMonkey, The time to calculate hough in python and C++ did not have any difference at all. But, running the entire program in c++ was almost 2.5 times faster (looped 100 times).Hands down to C++ :P.

Upvotes: 3

UKMonkey
UKMonkey

Reputation: 7013

I wouldn't expect any difference between the two at all to be honest. The python library more than likely is a wrapper around the C++ library; meaning that once they get into the core of the opencv they will have identical performance if compiled with the same optimisation flags.

The only slight slowdown I'd EXPECT is python getting to that point; and with so little python code actually there; the difference is unlikely to be measureable. The fact that you're seeing it the other way around I don't think proves anything as you're performing a single test; and getting a difference of 0.2s which could trivially be the difference in just the hard disk seeking to the file to process.

Upvotes: 3

Related Questions