C 如何计算两幅图像中相同元素之间的距离(以像素为单位)
我想用C语言编写一个程序来确定两幅图像中相同元素之间的距离,这两幅图像在两个镜头之间进行平移(通过立体镜测量距离) 我用一个像素一个像素地计算两幅图像之间的估计“距离”来计算这个距离。但是,这真的很慢 我听说有一种方法叫互相关和FFT,可以更快地完成这项工作。但我在网上找不到任何关于它的代码或信息 你有一些信息吗 谢谢C 如何计算两幅图像中相同元素之间的距离(以像素为单位),c,opencv,C,Opencv,我想用C语言编写一个程序来确定两幅图像中相同元素之间的距离,这两幅图像在两个镜头之间进行平移(通过立体镜测量距离) 我用一个像素一个像素地计算两幅图像之间的估计“距离”来计算这个距离。但是,这真的很慢 我听说有一种方法叫互相关和FFT,可以更快地完成这项工作。但我在网上找不到任何关于它的代码或信息 你有一些信息吗 谢谢 附言:我使用OpenCV加载和处理图像。你基本上是在说什么。这方面有很多算法,但您提到的可能是。感谢您的回答,我成功地编写了一个几乎可以运行的代码 struct Peak Fin
附言:我使用OpenCV加载和处理图像。你基本上是在说什么。这方面有很多算法,但您提到的可能是。感谢您的回答,我成功地编写了一个几乎可以运行的代码
struct Peak Find_FFT(IplImage* src, IplImage* tpl, int hamming)
{
int i, j, k;
double tmp; //To store the modulus temporarily
//src and tpl must be the same size
assert(src->width == tpl->width);
assert(src->height == tpl->height);
// Get image properties
int width = src->width;
int height = src->height;
int step = src->widthStep;
int fft_size = width * height;
//fftw_init_threads(); //Initialize FFTW for multithreading with a max number of 4 threads
//fftw_plan_with_nthreads(4);
//Allocate arrays for FFT of src and tpl
fftw_complex *src_spatial = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
fftw_complex *src_freq = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
fftw_complex *tpl_spatial = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
fftw_complex *tpl_freq = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
fftw_complex *res_spatial = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height ); //Result = Cross correlation
fftw_complex *res_freq = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
// Setup pointers to images
uchar *src_data = (uchar*) src->imageData;
uchar *tpl_data = (uchar*) tpl->imageData;
// Fill the structure that will be used by fftw
for(i = 0; i < height; i++)
{
for(j = 0 ; j < width ; j++, k++)
{
src_spatial[k][0] = (double) src_data[i * step + j];
src_spatial[k][1] = 0.0;
tpl_spatial[k][0] = (double) tpl_data[i * step + j];
tpl_spatial[k][1] = 0.0;
}
}
// Hamming window to improve FFT (but slightly slower to compute)
if(hamming == 1)
{
double omega = 2.0*M_PI/(fft_size-1);
double A= 0.54;
double B= 0.46;
for(i=0,k=0;i<height;i++)
{
for(j=0;j<width;j++,k++)
{
src_spatial[k][0]= (src_spatial[k][0])*(A-B*cos(omega*k));
tpl_spatial[k][0]= (tpl_spatial[k][0])*(A-B*cos(omega*k));
}
}
}
// Setup FFTW plans
fftw_plan plan_src = fftw_plan_dft_2d(height, width, src_spatial, src_freq, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_plan plan_tpl = fftw_plan_dft_2d(height, width, tpl_spatial, tpl_freq, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_plan plan_res = fftw_plan_dft_2d(height, width, res_freq, res_spatial, FFTW_BACKWARD, FFTW_ESTIMATE);
// Execute the FFT of the images
fftw_execute(plan_src);
fftw_execute(plan_tpl);
// Compute the cross-correlation
for(i = 0; i < fft_size ; i++ )
{
res_freq[i][0] = tpl_freq[i][0] * src_freq[i][0] + tpl_freq[i][1] * src_freq[i][1];
res_freq[i][1] = tpl_freq[i][0] * src_freq[i][1] - tpl_freq[i][1] * src_freq[i][0];
tmp = sqrt(pow(res_freq[i][0], 2.0) + pow(res_freq[i][1], 2.0));
res_freq[i][0] /= tmp;
res_freq[i][1] /= tmp;
}
// Get the phase correlation array = compute inverse fft
fftw_execute(plan_res);
// Find the peak
struct Peak pk;
IplImage* peak_find = cvCreateImage(cvSize(tpl->width,tpl->height ), IPL_DEPTH_64F, 1);
double *peak_find_data = (double*) peak_find->imageData;
for( i = 0 ; i < fft_size ; i++ )
{
peak_find_data[i] = res_spatial[i][0] / (double) fft_size;
}
CvPoint minloc, maxloc;
double minval, maxval;
cvMinMaxLoc(peak_find, &minval, &maxval, &minloc, &maxloc, 0);
pk.pt = maxloc;
pk.maxval = maxval;
// Clear memory
fftw_destroy_plan(plan_src);
fftw_destroy_plan(plan_tpl);
fftw_destroy_plan(plan_res);
fftw_free(src_spatial);
fftw_free(tpl_spatial);
fftw_free(src_freq);
fftw_free(tpl_freq);
fftw_free(res_spatial);
fftw_free(res_freq);
cvReleaseImage(&peak_find);
//fftw_cleanup_threads(); //Cleanup everything else related to FFTW
return pk;
}
struct Peak Find_FFT(IplImage*src,IplImage*tpl,int-hamming)
{
int i,j,k;
double tmp;//临时存储模数
//src和tpl的尺寸必须相同
断言(src->width==tpl->width);
断言(src->height==tpl->height);
//获取图像属性
int width=src->width;
int height=src->height;
int step=src->widthStep;
int fft_size=宽度*高度;
//fftw_init_threads();//初始化fftw以进行最多4个线程的多线程处理
//fftw_平面图___________n条(4条);
//为src和tpl的FFT分配阵列
fftw_复合体*src_空间=(fftw_复合体*)fftw_malloc(尺寸(fftw_复合体)*宽度*高度);
fftw_复合体*src_freq=(fftw_复合体*)fftw_malloc(尺寸(fftw_复合体)*宽度*高度);
fftw_综合体*tpl_空间=(fftw_综合体*)fftw_malloc(尺寸(fftw_综合体)*宽度*高度);
fftw_复合体*tpl_频率=(fftw_复合体*)fftw_malloc(尺寸(fftw_复合体)*宽度*高度);
fftw_complex*res_spatial=(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*宽度*高度);//结果=互相关
fftw_复合体*res_freq=(fftw_复合体*)fftw_malloc(尺寸(fftw_复合体)*宽度*高度);
//设置指向图像的指针
uchar*src_data=(uchar*)src->imageData;
uchar*tpl_数据=(uchar*)tpl->imageData;
//填写fftw将使用的结构
对于(i=0;i图像数据;
对于(i=0;i
问题在于fftw_free(src_freq);
返回错误(无效指针),我就是找不到原因
谢谢这是OpenCV文档中的教程。