gpt4 book ai didi

python - opencv python比c++快吗?

转载 作者:行者123 更新时间:2023-12-02 16:46:39 34 4
gpt4 key购买 nike

我正在尝试在python和c++中计时的houghcircle时间,以查看c++在处理时间上是否具有优势(直觉上应该!)

版本号

  • python:3.6.4
  • gcc编译器: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



    我正在使用的图像在这里给出:
    enter image description here

    Python代码
    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++代码
    #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;
    }

    我希望将C++ hough转换为ace python,但实际上是相反的。

    Python结果:

    enter image description here

    C++结果:

    enter image description here

    即使C++将完整程序的运行速度提高了约2倍,但霍夫变换的速度却非常慢。为什么会这样呢?这非常不直观。我在这里想念什么?

    最佳答案

    老实说,我不希望两者之间有任何区别。 python库很可能是C++库的包装器。这意味着一旦它们进入了opencv的核心,如果使用相同的优化标志进行编译,它们将具有相同的性能。

    我所期望的唯一的轻微减速是python达到了这一点。实际上那里只有很少的python代码;这种差异不太可能被衡量。从另一个 Angular 看,您认为我没有事实证明您在执行单个测试;并获得0.2s的差异,这可能只是寻求文件处理的硬盘上的差异。

    关于python - opencv python比c++快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49428298/

    34 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com