我使用 Visual Studio 2010 和 OpenCV 2.3.0。
#include "stdafx.h"
#include <cv.h>
#include <ml.h>
#include <highgui.h>
#include <iostream>
#include <stdlib.h>
template<class T> class Image {
private:
IplImage* imgp;
public:
Image(IplImage* img = 0) {
imgp = img;
}
~Image() {
imgp = 0;
}
void operator=(IplImage* img) {
imgp = img;
}
inline T* operator[](const int rowIndx) {
return ((T *)(imgp->imageData + rowIndx * imgp->widthStep));
}
};
typedef struct {
unsigned char b,g,r;
} RgbPixel;
typedef struct {
float b,g,r;
} RgbPixelFloat;
//typedef Image<RgbPixel> RgbImage;
//typedef Image<RgbPixelFloat> RgbImageFloat;
typedef Image<unsigned char> BwImage;
//typedef Image<float> BwImageFloat;
double crossCorrelation( IplImage* img1, IplImage* img2 ) {
double corr;
int M = img1->width;
int N = img1->height;
BwImage img_1( img1 );
BwImage img_2( img2 );
CvScalar img1_avg = cvAvg( img1, NULL );
CvScalar img2_avg = cvAvg( img2, NULL );
double sum_img1_img2 = 0;
double sum_img1_2 = 0;
double sum_img2_2 = 0;
for( int m = 0; m < M; ++m ) {
for( int n = 0; n < N; ++n ) {
sum_img1_img2 = sum_img1_img2 + (img_1[m][n] - img1_avg.val[0]) * (img_2[m][n] - img2_avg.val[0]);
sum_img1_2 = sum_img1_2 + (img_1[m][n] - img1_avg.val[0]) * (img_1[m][n] - img1_avg.val[0]);
sum_img2_2 = sum_img2_2 + (img_2[m][n] - img2_avg.val[0]) * (img_2[m][n] - img2_avg.val[0]);
}
}
corr = sum_img1_img2/sqrt(sum_img1_2*sum_img2_2);
return corr;
}
int _tmain(int argc, _TCHAR* argv[]) {
IplImage *img1 = cvLoadImage("C:/Users/ANIMES~1/Documents/VISUAL~3/Projects/tutOpencv/tutOpencv/38.png", CV_LOAD_IMAGE_COLOR);
IplImage *img2 = cvLoadImage("C:/Users/ANIMES~1/Documents/VISUAL~3/Projects/tutOpencv/tutOpencv/39.png", CV_LOAD_IMAGE_COLOR);
IplImage *img1g = cvCreateImage( cvSize( img1->width, img1->height ), IPL_DEPTH_8U, 1 );
IplImage *img2g = cvCreateImage( cvSize( img2->width, img2->height ), IPL_DEPTH_8U, 1 );
std::cout << (double)crossCorrelation(img1g, img2g) << std::endl;
std::system("Pause");
return 0;
}
当我调试时,我总是得到这个异常:
tutOpencv.exe 中 0x013e157f 处的未处理异常:0xC0000005:访问冲突读取位置 0x00f3b00c。
我无法弄清楚,这个错误来自哪里?