Double* from cv::Mat.ptr
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."
what is the size of your Mats ? what is _count ? what are you trying to achieve by that calculation ?
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?
"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 ?
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.
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.
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)