Improving the Rate at Which a File is Written To

asked 2017-07-28 16:00:51 -0600

Crazed gravatar image

The below code aims to track objects based on their shape (by means of vertices) from a camera feed. Although this code works without error, the file which stores information about the tracked objects (position, shape, time at which data was logged) is written to only about 30 times per second. I would like to significantly improve this rate (preferably to about 100 times per second). I have tried changing the camera being used, which can capture far more than 100 fps, but this has had no impact on the aforementioned issue, and yields the same results as a 30 fps webcam. Is there something in my code that slows the process down unnecessarily, or is there something I can add to my code to speed it up? Any help is more than appreciated.

Code:

#include <fstream>
#include <time.h>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>

std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
std::ofstream file_;

void morphOps(cv::Mat &thresh){

    //create structuring element that will be used to "dilate" and "erode" image.
    //the element chosen here is a 3px by 3px rectangle

    cv::Mat erodeElement = cv::getStructuringElement( cv::MORPH_RECT,cv::Size(10,10));
    //dilate with larger element so make sure object is nicely visible
    cv::Mat dilateElement = cv::getStructuringElement( cv::MORPH_RECT,cv::Size(10,10));

    cv::erode(thresh,thresh,erodeElement);
    cv::erode(thresh,thresh,erodeElement);


    cv::dilate(thresh,thresh,dilateElement);
    cv::dilate(thresh,thresh,dilateElement);
}

void process_contour(cv::Mat& frame, std::vector<cv::Point> const& contour)
{
    clock_t t;
    t = clock();

    int minArea = 100;
    int x1,y1;

    cv::Scalar TRIANGLE_COLOR(0, 0, 255);
    cv::Scalar QUADRILATERAL_COLOR(0, 255, 0);
    cv::Scalar HEPTAGON_COLOR(255, 0, 0);
    cv::Scalar DECA_COLOR(126, 126, 0);

    cv::Scalar color;

    if (contour.size() == 3 && contourArea(contour) > minArea) {
        color = TRIANGLE_COLOR;

        for (int index = 0; index >= 0; index = hierarchy[index][0]) {
            cv::Moments moment = cv::moments((cv::Mat)contours[index]);
            double area = moment.m00;
            if(area > minArea){
                x1 = moment.m10/area;
                y1 = moment.m01/area;
            } 
        }

        file_ << "Triangle  " << "X: " << x1 << " Y: " << y1 << "  " << /*"Area: " << contourArea(contour) << "  " <<*/ double(t)/CLOCKS_PER_SEC << " seconds" << "\n";

    } else if (contour.size() == 4 && contourArea(contour) > minArea) {
        color = QUADRILATERAL_COLOR;

        for (int index = 0; index >= 0; index = hierarchy[index][0]) {
            cv::Moments moment = cv::moments((cv::Mat)contours[index]);
            double area = moment.m00;
            if(area > minArea){
                x1 = moment.m10/area;
                y1 = moment.m01/area;
            }
        }

        file_ << "Quadrilateral  " << "X: " << x1 << " Y: " << y1 << "  " << /*"Area: " << contourArea(contour) << "  " <<*/ double(t)/CLOCKS_PER_SEC << " seconds" << "\n";

    } else if (contour.size() == 7 && contourArea(contour) > minArea) {
        color = HEPTAGON_COLOR;

        for (int index = 0; index >= 0; index = hierarchy[index][0]) {
            cv::Moments moment = cv::moments((cv::Mat)contours[index]);
            double area = moment.m00;
            if(area > minArea){
                x1 = moment.m10/area;
                y1 = moment.m01/area;
            }
        }

        file_ << "Heptagon  " << "X: " << x1 << " Y: " << y1 << "  " << /*"Area: " << contourArea(contour) << "  " <<*/ double(t)/CLOCKS_PER_SEC << " seconds" << "\n";

    } else if (contour.size() == 10 && contourArea(contour) > minArea) {
        color = DECA_COLOR;

        for (int index = 0; index >= 0; index = hierarchy[index][0]) {
            cv::Moments moment = cv::moments((cv::Mat)contours[index]);
            double area = moment.m00;
            if(area ...
(more)
edit retag flag offensive close merge delete

Comments

Can you at least provide an analysis of your code with some profiling tool? That will tell you exactly which function is taking longest. Writing to disk is in my experience NEVER the bottleneck, unless you are like writing data to an external disk on USB1 protocol.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-07-29 08:02:12 -0600 )edit