程序见下,我测试了cvMat和Mat的数据是一样的,但是求出来的dx和dy值是不一样的,是我调用的有问题还是本身存在的问题。
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void main()
{
Mat src1 =imread("D:\\data_img\\MatchImage\\2.jpg",0);
Mat gx1, gy1;
Sobel(src1, gx1, CV_16S, 1, 0, 3); //gradient in X direction
Sobel(src1, gy1, CV_16S, 0, 1, 3); //gradient in Y direction
IplImage imgTmp= src1;
IplImage *templateImage = cvCloneImage(&imgTmp);
CvMat *gx = 0; //Matrix to store X derivative
CvMat *gy = 0; //Matrix to store Y derivative
CvMat *nmsEdges = 0;//Matrix to store temp restult
CvSize Ssize;
const short* _sdx;
const short* _sdy;
const uchar* _src;
short fdx, fdy, fdx0, fdy0;
// Convert IplImage to Matrix for integer operations
CvMat srcstub, *src = (CvMat*)templateImage;
src = cvGetMat(src, &srcstub);
Ssize.width = src->width;
Ssize.height = src->height;
gx = cvCreateMat(Ssize.height, Ssize.width, CV_16SC1); //create Matrix to store X derivative
gy = cvCreateMat(Ssize.height, Ssize.width, CV_16SC1); //create Matrix to store Y derivative
cvSobel(src, gx, 1, 0, 3); //gradient in X direction
cvSobel(src, gy, 0, 1, 3); //gradient in Y direction
for (int i = 0; i < src1.rows; i++)
{
uchar *p = src1.ptr<uchar>(i);
short *pdx = gx1.ptr<short>(i);
short *pdy = gy1.ptr<short>(i);
for (int j = 0; j < src1.cols; j++)
{
_sdx = (short*)(gx->data.ptr + gx->step*i);
_sdy = (short*)(gy->data.ptr + gy->step*i);
_src= (uchar*)(src->data.ptr + src->step*i);
uchar usrc = p[j];
uchar usrc2= _src[j];
fdx0 = pdx[j];
fdx = _sdx[j];
fdy0 = pdy[j];
fdy = _sdy[j];
if (fdy0 != fdy || fdx0 != fdx )
{
int qqqqqqq = 1111;
}
if (usrc != usrc2)
{
int qqqqqqq = 1111;
}
}
}
}