Ask Your Question
0

OpenCV ref count: function returning cv::Mat?

asked 2014-11-23 02:39:38 -0600

wal-o-mat gravatar image

I am tracing a strange bug in my very simple code, and I suspect that I am using cv::Mat in a wrong way, although with reference counting, there is not much that should go wrong.

My code is this:

cv::Mat rowwise_maximum(const cv::Mat& src, int32_t width)
{        
    cv::Mat dest = src.clone();

    for(int row = 0; row < dest.rows; ++row)
    {
        for(int col = 0; col < dest.cols; ++col)
        {
            dest.at< double >(row, col) = 0.5 * src.at< double >(row, col);
        }
    }

    std::cout << "ok" << std::endl;

    return dest;
}

// call
cv::Mat max_foo = rowwise_maximum(thresholded, 7);

I am pretty sure that the depth of src is CV_64F and therefore the way I use at is correct. I observe a program crash after leaving the program block calling this code, and the debugger displays max_foo.refcount to be 0.

I would expect it to be 1 ...

Can someone provide me a hint?

edit retag flag offensive close merge delete

Comments

Stupid me ... see comment below the accepted answer. The code above is ok, it's my assumption about the depth which was wrong. I used 'cv::matchTemplate' before, and this gives me a 32 bit floating point image, not 64 bit ...

wal-o-mat gravatar imagewal-o-mat ( 2014-11-28 14:14:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-11-23 12:32:25 -0600

Hi!

This should not be a problem... Please add:

 CV_ASSERT(src.depth()==CV_64F);

That way, you are sure the matrix type is OK... You should also use direct access to speedup a little the iterations:

 cv::Mat rowwise_maximum(const cv::Mat& src, int32_t width)
 {
    CV_ASSERT(src.depth()==CV_64F);
    cv::Mat dest = src.clone();
    double* singleRowSrc, singleRowDest;
    for(int row = 0; row < dest.rows; ++row)
    {
        singleRowSrc = src.ptr<double>(row);
        singleRowDest= dest.ptr<double>(row);
        for(int col = 0; col < dest.cols; ++col)
        {
            singleRowDest[col] = 0.5 * singleRowSrc[col];
        }
    }

    std::cout << "ok" << std::endl;

    return dest;
 }

max_foo.refcount is still 0 because code probably crash before assignment (I think it occurs while removing that data from the stack).

edit flag offensive delete link more

Comments

You are totally right! Crashed before, and yes, I reated my image as 'CV_64F', but this is not what 'cv::matchTemplate' creates ... the documentation clearly states that 32 bit are the result.

wal-o-mat gravatar imagewal-o-mat ( 2014-11-28 14:13:01 -0600 )edit

Question Tools

Stats

Asked: 2014-11-23 02:39:38 -0600

Seen: 1,077 times

Last updated: Nov 23 '14