描写叙述
一般来说,我们採用针孔相机模型,也就是觉得它用到的是小孔成像原理。
在相机坐标系下,一般来说,我们用到的单位长度,不是“米”这种国际单位,而是相邻像素的长度。而焦距在相机坐标系中的大小,是在图像处理领域的一个很重要的物理量。
如果我们已经依据相机參数,得到镜头的物理焦距大小(focal length),和相机胶片的宽度(CCD width),以及照片的横向分辨率(image width),则详细计算公式为:
Focal length in pixels = (image width in pixels) * (focal length on earth) / (CCD width on earth)
比方说对于Canon PowerShot S100, 带入公式得
Focal length in pixels = 1600 pixels * 5.4mm / 5.27mm = 1639.49 pixels
如今,请您写一段通用的程序,来求解焦距在相机坐标系中的大小。
输入
多组測试数据。首先是一个正整数T,表示測试数据的组数。
每组測试数据占一行,分别为
镜头的物理焦距大小(focal length on earth)
相机胶片的宽度(CCD width on earth)
照片的横向分辨率大小(image width in pixels),单位为px。
之间用一个空格分隔。
输出
每组数据输出一行,格式为“Case X: Ypx”。 X为測试数据的编号,从1開始;Y为焦距在相机坐标系中的大小(focallength in pixels),保留小数点后2位有效数字,四舍五入取整。
数据范围
对于小数据:focal length on earth和CCD width on earth单位都是毫米(mm)
对于大数据:长度单位还可能为米(m), 分米(dm), 厘米(cm), 毫米(mm), 微米(um),纳米(nm)
2 5.4mm 5.27mm 1600px 5400um 0.00527m 1600px例子输出
Case 1: 1639.47px Case 2: 1639.47px
#include <string>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
double trans_unit(string unit, double x)
{
if (unit == "mm")
{
return x;
}
else if (unit == "m")
{
return x*1000;
}
else if (unit =="cm")
{
return x*10;
}
else if (unit == "dm")
{
return x*100;
}
else if (unit == "um")
{
return x/1000;
}
else if (unit == "nm")
{
return x/1000000;
}
}
int main()
{
int T;
scanf("%d", &T);
for(int t = 1; t <= T; t++)
{
string focal, ccd, width;
cin>>focal>>ccd>>width;
double _width, _focal, _ccd;
int px = atoi(width.substr(0, width.size()-2).c_str());
_width = (double)px;
int index_ccd;
for (int i = ccd.size()-1; i >= 0; i--)
{
if (ccd[i] >= '0' && ccd[i] <= '9')
{
index_ccd = i;
break;
}
}
_ccd = atof(ccd.substr(0, index_ccd+1).c_str());
string ccd_unit = ccd.substr(index_ccd+1 , ccd.size()-index_ccd-1);
_ccd = trans_unit(ccd_unit, _ccd);
for (int i = focal.size() -1 ; i >=0; i--)
{
if (focal[i] >= '0' && focal[i] <= '9')
{
index_ccd = i;
break;
}
}
_focal = atof(focal.substr(0, index_ccd+1).c_str());
string focal_unit = focal.substr(index_ccd+1, focal.size()-index_ccd-1);
_focal = trans_unit(focal_unit, _focal);
double ret = _width*_focal/_ccd;
printf("Case %d: %.2fpx\n",t,ret);
}
//system("pause");
return 0;
}
post java code, the unit transform function is the same with above.
import java.util.Scanner;
import java.lang.String;
import java.lang.Math;
/*
class TreeNode
{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; left = null; right = null;}
}
class ListNode
{
int val;
ListNode next;
ListNode(int x){val = x; next = null;}
}
*/
public class Solution {
public static void main(String[] args)
{
int T;
Scanner jin = new Scanner(System.in);
T = jin.nextInt();
for (int t = 1; t <= T; t++) {
String focal = jin.next();
String ccd = jin.next();
String width = jin.next();
double _width = Double.parseDouble(width.substring(0, width.length()-2));
int index_unit = -1;
for (int i = ccd.length()-1; i >= 0; i--) {
if (ccd.charAt(i) >= '0' && ccd.charAt(i) <= '9') {
index_unit = i+1;
break;
}
}
String ccd_unitString = ccd.substring(index_unit, ccd.length());
double _ccd = Double.parseDouble(ccd.substring(0, index_unit));
for (int i = focal.length()-1; i >= 0; i--) {
if (focal.charAt(i) >= '0' && focal.charAt(i) <= '9') {
index_unit = i+1;
break;
}
}
String focal_uniString = focal.substring(index_unit, focal.length());
double _focal = Double.parseDouble(focal.substring(0, index_unit));
double ret = _width * _focal / _ccd;
java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
//df.format(ret);
System.out.println("Case " + t + ": " + df.format(ret) + "px");
}
}
}