# Initial internal parameter matrix

Camera internal parameter matrix:

```        fx    s    x0
K =  0    fy   y0
0    0    1```
Among them, fx and fy are focal lengths, which are generally set equal to each other, initially set as the long side length of the image.
x0 and y0 are the coordinates of the main point (relative to the imaging plane) and the coordinates of the image center point.
s is the distortion coefficient, initially set to 0

```	int Focalf = max(image.rows, image.cols);//Initial focal length

Mat K (Matx33d (
Focalf, 0, img1.rows/2,
0, Focalf, img1.cols/2,
0, 0, 1)); // Camera matrix (initial internal parameter)
```

Which focal length is rough on it,
Next, use a jhead.exe to read some information in the image to calculate a more accurate focal length:

```	system("jhead et000.jpg >et000.tmp");//Run jhead
printf("focal length(Image number) = %0.3f\n", getfocal(name));//Display focus
```

getfocal function

```float getfocal(
const char * name
){
//char make[50]; / / manufacturer
//char model[50]; / / model
float focal_mm=0.0f;//Focal length (mm)
float ccd_width_mm=0.0f;//ccd width (mm)
int res_x=0;//x resolution
int res_y=0;//y resolution
float focal_pixels=0.0f;//Focal length (image number)

//printf("load camera parameter file% s...\n", name);

FILE * file = fopen(name, "r");
if( file == NULL ){
printf("Error, no file opened\n");
return focal_pixels;
}

char* res ;
int i=0;char*t;
char tt[60];
while( 1 ){

//Printf (""% d \ n ", I + +); / / line number

// Read the first string in this line
res= fgets(lineHeader, 128, (FILE*)file);
if (res == NULL)
break; // EOF = End Of File. Exit the loop.

//strstr(s1, s2);
//Returns a pointer to the first occurrence of string s2 in string s1.

if ( strstr( lineHeader, "Camera make" )  ){  //Manufacturer

if(t){
t++;//Skip ':'
//printf("manufacturer:% s\n",tt);
}

}
else if ( strstr( lineHeader, "Camera model" )  ){  //Model
if(t){
t++;//Skip ':'
//printf("model:% s\n",tt);
}
}else if ( strstr( lineHeader, "Focal length" )  ){  //Focal length (mm)
if(t){
t++;//Skip ':'
//printf("focal length:% s\n",tt);
t=strstr( tt, "mm" );
if (t)
{
char temp[50];int t0=t-tt;
memcpy(temp,  tt, t0);//Copy before mm
//printf("focal length:% s\n",temp);
t=temp;t+=t0;*t='\0';//Delete trailing garbled
//printf("focal length:%sn", temp);
t=temp;while (*t == ' ' )  {t++;t0--;}//Remove leading space
memcpy(tt,  t, t0);t=tt;t+=t0;*t='\0';
//printf("focal length:% smm\n",tt);
focal_mm=atof(tt);
//printf("focal length (floating-point number):% 0.3fmm\n",focal_mm);

}
}
}else if ( strstr( lineHeader, "CCD width" )  ){  //CCD width
if(t){
t++;//Skip ':'
//printf("focal length:% s\n",tt);
t=strstr( tt, "mm" );
if (t)
{
char temp[50];int t0=t-tt;
memcpy(temp,  tt, t0);//Copy before mm

t=temp;t+=t0;*t='\0';//Delete trailing garbled

t=temp;while (*t == ' ' )  {t++;t0--;}//Remove leading space
memcpy(tt,  t, t0);t=tt;t+=t0;*t='\0';
//printf("CCD width:% smm\n",tt);
ccd_width_mm=atof(tt);
//printf("CCD width (floating-point number):% 0.3fmm \ n", CCD "width" mm);

}
}
}else if ( strstr( lineHeader, "Resolution" )  ){  //resolving power
if(t){
t++;//Skip ':'
memcpy(tt,  t, t0);t=tt;t+=t0;*t='\0';
//printf("resolution:% s\n",tt);
t=strchr(tt, 'x');
if(t){
char temp[50];char *p0,*p1;
memcpy(temp,  p0, t0);p0=temp;p0+=t0;*p0='\0';
res_x=atoi(temp);
//printf("resolution X (integer):% d\n",res_x);
res_y=atoi(p0);
//printf("resolution y (integer):% d\n",res_y);
}
}
}

}
int has_focal;
if (focal_mm == 0 || ccd_width_mm == 0 || res_x == 0) {
has_focal = 0;
} else {
has_focal = 1;
}
if (res_x < res_y) {
// If the aspect ratio is wrong, exchange
int tmp = res_x;
res_x = res_y;
res_y = tmp;
}
if (has_focal == 1) {
// Calculate focal length in pixels
focal_pixels = res_x * (focal_mm / ccd_width_mm);
//printf("focal length (image number) =% 0.3f\n", focal_pixels);
}
return focal_pixels;
}
```

Posted on Mon, 04 May 2020 16:48:20 -0700 by Kodak07