Drawing and Plotting graph in OpenCV

后端 未结 5 1414
春和景丽
春和景丽 2020-12-14 23:09

Does OpenCV provide a function on how to draw and plot a graph?

I found this link by Shervin Emami http://www.shervinemami.info/graphs.html which was created by him

5条回答
  •  温柔的废话
    2020-12-14 23:44

    A rather crude but do-it-yourself approach, would be entail plotting the line graph on cv::Mat image:

    template 
    cv::Mat plotGraph(std::vector& vals, int YRange[2])
    {
    
        auto it = minmax_element(vals.begin(), vals.end());
        float scale = 1./ceil(*it.second - *it.first); 
        float bias = *it.first;
        int rows = YRange[1] - YRange[0] + 1;
        cv::Mat image = Mat::zeros( rows, vals.size(), CV_8UC3 );
        image.setTo(0);
        for (int i = 0; i < (int)vals.size()-1; i++)
        {
            cv::line(image, cv::Point(i, rows - 1 - (vals[i] - bias)*scale*YRange[1]), cv::Point(i+1, rows - 1 - (vals[i+1] - bias)*scale*YRange[1]), Scalar(255, 0, 0), 1);
        }
    
        return image;
    }
    

    Usage example:

    vector numbers(100);
    std::iota (numbers.begin(), numbers.end(), 0);
    
    int range[2] = {0, 100};
    cv::Mat lineGraph = plotGraph(numbers, range);
    

    One can then use imshow or Image Watch to view the graph

提交回复
热议问题