Ask Your Question
0

Double* from cv::Mat.ptr

asked 2014-01-25 06:12:07 -0600

MRDaniel gravatar image

updated 2014-01-25 07:14:22 -0600

I am using Opencv in Visual Studio 2012 with C++ in a Console Application.

fastJx and fastJy are both pointers to image matrices, a cv::Mat. When i reach iteration i=1194 fastJy crashes when assigning it a value. Using break points, it seems that fastJy has a value of 'Unable to read memory' when the variable is inspected.

Both Jx and Jy, the source cv::Mat for the pointers are the same size. It is only on this iteration that there is a problem.

void IterationStage(cv::Mat Jx, cv::Mat Jy) {
    double* fastJx;
    double* fastJy;
    for(int i=0; i<_count;i++)
    {
        fastJx = Jx.ptr<double>(0) + Jx.step*i;     
        fastJy = Jy.ptr<double>(0) + Jy.step*i;
        fastJx[0] = 255;    
        fastJx[1] = 255;
    } 
}

Making this change fixes fastJy.

void IterationStage(cv::Mat Jx, cv::Mat Jy)
{
    double* fastJx;
    double* fastJy;
    for(int i=0; i<_count;i++)
    {
        fastJx = Jx.ptr<double>(0) + Jx.step*i;
        fastJy = Jx.ptr<double>(0) + Jy.step*i;  //CHANGED HERE.

        fastJx[0] = 255;
        fastJx[1] = 255;
    }
}

This is odd as i am still using the Jy.step with the Jx matrix. Both matrices are initialized with the same constructor, just before the function is calledas shown below.

cv::Mat Jx = cv::Mat::zeros(__paw.nPix(), __shape.nModes()+4, CV_64FC1);
cv::Mat Jy = cv::Mat::zeros(__paw.nPix(), __shape.nModes()+4, CV_64FC1);
IterationStage(Jx, Jy);

Anyway, the error message is this.

"Unhandled exception at 0x003A5777 in OpencvShapeTest.exe: 0xC0000005: Access violation writing location 0x024290A0."

edit retag flag offensive close merge delete

Comments

what is the size of your Mats ? what is _count ? what are you trying to achieve by that calculation ?

berak gravatar imageberak ( 2014-01-25 07:35:58 -0600 )edit

This is part of a fast transformation from a vector to an image. The vector is a look up table, so not all pixels are present.... i.e. reshape won't work.

The size of the Jx mats are 4 x 9381. Four pixels per image pixel for texture analysis.

Count is the same as nPix which is 9381.

I have reduced this method down to the simplest case where the problem still exists. It is some sort of memory error, but why only this mat and no Jx also?

MRDaniel gravatar imageMRDaniel ( 2014-01-25 08:13:41 -0600 )edit
1

"why only this mat and no Jx also?" - if you go out of bounds, thats undefined behaviour. don't worry too much where the error shows up(totally arbitrary), worry about the out of bounds problem.

why this step thing ? why not simply: Jx.ptr<double>(i)

why is fastJy never used in your calculation ?

berak gravatar imageberak ( 2014-01-25 08:22:22 -0600 )edit

I will try that. But any tips on hunting down the error?

I just find it odd that it has this problem when all the matrices seem to be the correct size.

MRDaniel gravatar imageMRDaniel ( 2014-01-25 08:37:58 -0600 )edit

Wow. That seems to have fixed the error. :) I am updating an old OpenCV project for AAMs. They use this type of access to transform between pixel codes and image values.

MRDaniel gravatar imageMRDaniel ( 2014-01-25 08:49:27 -0600 )edit

what if you just skip all your pointer optimization for a moment, and just do:

Jx.at<double>(i,0) = 255;

Jx.at<double>(i,1) = 255;

(IF that's what you actually wanted)

berak gravatar imageberak ( 2014-01-25 08:50:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-01-25 14:52:15 -0600

keghn gravatar image

updated 2014-01-25 14:55:38 -0600

CV_64FC1 give you only one channel 0.

CV_64FC2 will give you channel 0 and 1

here:

fastJx[1] = 255;

is accessing a channel that does not exist.

Could be wrong,

http://docs.opencv.org/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html#matthebasicimagecontainer keghn

edit flag offensive delete link more

Comments

The problem is that the i am using CV_64FC1.

This answer worked and i stopped receiving issues now.

fastJx = Jx.ptr<double>(i)

MRDaniel gravatar imageMRDaniel ( 2014-01-26 20:56:42 -0600 )edit

Question Tools

Stats

Asked: 2014-01-25 06:12:07 -0600

Seen: 1,415 times

Last updated: Jan 25 '14