📄 registration.cpp
字号:
/* This is the registration function for two image data */
/*
* 1. input_img and refre_img are two image data registrated
* 2. img_width and img_height are image size;
* 3. x* and y* are translation parameters
* 4. angle* is rotated angle
* 5. interface starting by "cc" means this function is for char* dataArray;
* while statrting by "dd" means it is for double* dataArray.
* 注意:center是变换的中心,而L和R均是相对中心的距离
*/
#include "registration.h"
#include "imgfile.h"
void cc_registration(unsigned char* input_img,unsigned char* refer_img,
long img_width,long img_height,
double angleScope_L,double angleScope_R,
double angle_delta,double xScope_L,double xScope_R,
double yScope_L,double yScope_R,double xy_delta,
double* angle_center,double* x_center,double* y_center)
{
double corre,max_co=-2,best_x=0,best_y=0,best_angle=0;
double x,y,angle,xCenter,yCenter,angleCenter,pi_angle;
long dataLength,i;
dataLength=img_width*img_height;
//computing unchanged item of correlation for input image
double mean_in=0,sumSquare_in=0;
double* meanIn,*sumSquareIn;
meanIn=&mean_in;
sumSquareIn=&sumSquare_in;
c_Mean_SumSquare(input_img,meanIn,sumSquareIn,dataLength);
// printf("mean_in= %f; sumSquare_in= %f\n",mean_in,sumSquare_in);
double sum_inxre=0,mean_re=0,sumSquare_re=0;
//registration within the scope and find the best transform(theta,x,y)
unsigned char *image_out;
unsigned char *tmp1,*tmp2;
image_out = (unsigned char *)malloc(dataLength);
angle=angleScope_L;
x=xScope_L; y=yScope_L;
xCenter=*x_center; yCenter=*y_center;
angleCenter=*angle_center;
/* while(angle <= angleScope_R){
while(x <= xScope_R){
while(y <= yScope_R){*/
int l,j,k;
for(l=0;l<5;l++){
for(j=0;j<5;j++){
for(k=0;k<5;k++){
/*Transform the reference image to
(angleCenter,xCenter,yCenter)*/
//set the transform parameters
xCenter=*x_center+x;
yCenter=*y_center+y;
angleCenter=*angle_center+angle;
// printf("tx= %f, ty= %f, angle= %f\n",xCenter,yCenter,
// angleCenter);
//change angle to PI form
pi_angle=angleCenter*3.14159265359/180;
//do the transform of refer_img,
//result is stored in image_out.
memset(image_out,0,dataLength);
/* tmp=image_out;
for(i=0;i<img_width*img_height;i++){
*tmp=0;
tmp++;
}*/
c_resample(refer_img,image_out,img_width,img_height,
xCenter,yCenter,pi_angle);
#if 1
if(xCenter==-16&&yCenter==-16&&angleCenter==-16){
char* targetfilename,*name2; /* filename */
FILE *tfPtr;
targetfilename="re.bmp";name2="in.bmp";
tfPtr = imgOpen(targetfilename,&img_width,&img_height,1);
imgWrite(tfPtr,0,0,img_width,img_height,image_out);
fclose(tfPtr);
tfPtr = imgOpen(name2,&img_width,&img_height,1);
imgWrite(tfPtr,0,0,img_width,img_height,input_img);
printf("\ntmp is created!\n\n");
fclose(tfPtr);
}
#endif
//computing the correlation between
//the transformed refer_image and the input_image.
sum_inxre=0,mean_re=0,sumSquare_re=0;
tmp1=input_img; tmp2=image_out;
for(i=0;i<dataLength;i++){
mean_re = mean_re + (double)(*tmp2);
sumSquare_re = sumSquare_re+(double)(*tmp2)*(double)(*tmp2);
sum_inxre = sum_inxre+(double)(*tmp1)*(double)(*tmp2);
tmp1++;
tmp2++;
}
mean_re = mean_re/(double)dataLength;
// printf("mean_re= %f; sumSquare_re= %f\n",mean_re,sumSquare_re);
// printf("sum_inxre=%f\n",sum_inxre);
corre = correlation(sum_inxre,mean_in,mean_re,
sumSquare_in,sumSquare_re,dataLength);
// printf("correlation = %f\n",corre);
//update the parameters
if(corre > max_co){
max_co = corre;
best_x = xCenter;
best_y = yCenter;
best_angle = angleCenter;
printf("correlation = %f\n",corre);
}
y = y + xy_delta;
}//end of while y
x = x + xy_delta;
y = yScope_L;
}//end of while x
angle = angle + angle_delta;
x = xScope_L;
}//end of while angle
*x_center = best_x;
*y_center = best_y;
*angle_center = best_angle;
printf("correlation = %f\n",max_co);
printf("best_x is %f, best_y is %f, best_angle is %f\n",
best_x,best_y,best_angle);
free(image_out);
return;
}
void dd_registration(double* input_img,double* refer_img,
long img_width,long img_height,
double angleScope_L,double angleScope_R,
double angle_delta,double xScope_L,double xScope_R,
double yScope_L,double yScope_R,double xy_delta,
double* angle_center,double* x_center,double* y_center)
{
double corre,max_co=-2,best_x=0,best_y=0,best_angle=0;
double x,y,angle,xCenter,yCenter,angleCenter,pi_angle;
long dataLength,i;
dataLength=img_width*img_height;
//computing unchanged item of correlation for input image
double mean_in=0,sumSquare_in=0;
double* meanIn,*sumSquareIn;
meanIn=&mean_in;
sumSquareIn=&sumSquare_in;
d_Mean_SumSquare(input_img,meanIn,sumSquareIn,dataLength);
// printf("mean_in= %f; sumSquare_in= %f\n",mean_in,sumSquare_in);
double sum_inxre=0,mean_re=0,sumSquare_re=0;
//registration within the scope and find the best transform(theta,x,y)
double *image_out;
double *tmp1,*tmp2;
image_out = (double *)malloc(sizeof(double)*dataLength);
angle=angleScope_L;
x=xScope_L; y=yScope_L;
xCenter=*x_center; yCenter=*y_center;
angleCenter=*angle_center;
/* while(angle <= angleScope_R){
while(x <= xScope_R){
while(y <= yScope_R){*/
int l,j,k;
for(l=0;l<5;l++){
for(j=0;j<5;j++){
for(k=0;k<5;k++){
/*Transform the reference image to
(angleCenter,xCenter,yCenter)*/
//set the transform parameters
xCenter=*x_center+x;
yCenter=*y_center+y;
angleCenter=*angle_center+angle;
// printf("l=%d,j=%d,k=%d,tx= %f, ty= %f, angle= %f\n",
// l,j,k,xCenter,yCenter,angleCenter);
//change angle to PI form
pi_angle=angleCenter*3.14159265359/180;
//do the transform of refer_img,
//result is stored in image_out.
memset(image_out,0,sizeof(double)*dataLength);
/* tmp=image_out;
for(i=0;i<img_width*img_height;i++){
*tmp=0;
tmp++;
}*/
d_resample(refer_img,image_out,img_width,img_height,
xCenter,yCenter,pi_angle);
#if 0
if(xCenter==0&&yCenter==0&&angleCenter==-2){
char* targetfilename; /* filename */
FILE *tfPtr;
targetfilename="tmp.bmp";
tfPtr = imgOpen(targetfilename,&img_width,&img_height,1);
imgWrite(tfPtr,0,0,img_width,img_height,image_out);
printf("\ntmp is created!\n\n");
}
#endif
//computing the correlation between
//the transformed refer_image and the input_image.
sum_inxre=0,mean_re=0,sumSquare_re=0;
tmp1=input_img; tmp2=image_out;
for(i=0;i<dataLength;i++){
mean_re = mean_re + *tmp2;
sumSquare_re = sumSquare_re+(*tmp2)*(*tmp2);
sum_inxre = sum_inxre+(*tmp1)*(*tmp2);
tmp1++;
tmp2++;
}
mean_re = mean_re/(double)dataLength;
// printf("mean_re= %f; sumSquare_re= %f\n",mean_re,sumSquare_re);
// printf("sum_inxre=%f\n",sum_inxre);
corre = correlation(sum_inxre,mean_in,mean_re,
sumSquare_in,sumSquare_re,dataLength);
// printf("correlation = %f\n",corre);
//update the parameters
// if(xCenter==-40&&yCenter==40&&angleCenter==-24)
// printf("correlation = %f\n",corre);
if(corre > max_co){
max_co = corre;
best_x = xCenter;
best_y = yCenter;
best_angle = angleCenter;
// printf("correlation = %f\n",corre);
}
y = y + xy_delta;
}//end of while y
x = x + xy_delta;
y = yScope_L;
}//end of while x
angle = angle + angle_delta;
x = xScope_L;
}//end of while angle
*x_center = best_x;
*y_center = best_y;
*angle_center = best_angle;
printf("correlation = %f\n",max_co);
// printf("best_x is %f, best_y is %f, best_angle is %f\n\n",
// best_x,best_y,best_angle);
free(image_out);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -