函数背景:假如sensor出的图的尺寸是3264*2448,但认为选择的capture的尺寸(也就是拍照尺寸)是3264*1836,这个时候如果需要将sensor上的roi区域映射到capture上
操作步骤:
1.首先判断roi是不是正方形,如果是,则坐标映射到capture上的roi理应也是正方形
2.判断sensor的宽高比和capture的宽高比是否相等,如果不相等,需要对capture的size进行裁剪(并往小了裁)来适配sensor的宽高比,以便于后面做坐标映射
3.将sensor上的roi 坐标映射到capture上
4.如果sensor上的roi是正方形,则需要保证映射到capture上的roi 也需要是正方形
具体实现及示例:
#if 1
#include <iostream>
using namespace std;
struct img_size {
unsigned int width;
unsigned int height;
};
int camera_convert_coor_sensor_to_capture(const struct img_size *sensor_size,
const struct img_size *capture_size, unsigned int*cropRegion);
int main(){
struct img_size sensor = { 3264, 2448 };
struct img_size capture = { 3264, 1836 };
unsigned int cropRegion[4] = {1042, 856, 2120, 1590};
camera_convert_coor_sensor_to_capture(&sensor, &capture, cropRegion);
return 0;
}
int camera_convert_coor_sensor_to_capture(const struct img_size *sensor_size,
const struct img_size *capture_size, unsigned int*cropRegion) {
float left = 0, top = 0, width = 0, height = 0, zoomWidth = 0, zoomHeight = 0;
int ret = 0;
int flag_square = 0;
unsigned int roiW = 0, roiH = 0;
unsigned int capture_width = 0, capture_height = 0;
unsigned int sensor_width = 0, sensor_height = 0;
printf("before_crop_rect_calculated: (sx=%d,sy=%d,ex=%d,ey=%d)\n",
cropRegion[0], cropRegion[1], cropRegion[2], cropRegion[3]);
roiW = cropRegion[2] - cropRegion[0];
roiH = cropRegion[3] - cropRegion[1];
if (roiW == 0 || roiH == 0) {
printf("parameters error.");
return 1;
}
//if roi is square,it should be square aftre conversion
if (roiW == roiH)
flag_square = 1;
capture_width = capture_size->width;
capture_height = capture_size->height;
sensor_width = sensor_size->width;
sensor_height = sensor_size->height;
float sensorAspect = (float)sensor_width / sensor_height;
float captureAspect = (float)capture_width / capture_height;
//if sensorAspect and captureAspect are not equal,
//the size of capture should be cut to fit the aspect of sensor
if (sensorAspect > captureAspect) {
width = capture_width;
height = captureAspect * capture_height / sensorAspect;
left = 0;
top = 0 + (capture_height - height) / 2;
} else {
width = sensorAspect * capture_width / captureAspect;
height = capture_height;
left = 0 + (capture_width - width) / 2;
top = 0;
}
zoomWidth = width / (float)sensor_width;
zoomHeight = height / (float)sensor_height;
printf("sensorAspect=%f, captureAspect=%f,"
"width=%f, height=%f, left=%f, top=%f, zoomWidth=%f, zoomHeight=%f\n",
sensorAspect, captureAspect, width, height, left, top,
zoomWidth, zoomHeight);
cropRegion[0] = (unsigned int)((float)cropRegion[0] * zoomWidth + left);
cropRegion[1] = (unsigned int)((float)cropRegion[1] * zoomHeight + top);
cropRegion[2] = (unsigned int)((float)cropRegion[2] * zoomWidth + left);
cropRegion[3] = (unsigned int)((float)cropRegion[3] * zoomHeight + top);
//If it was originally square, it should be square after conversion
roiW = cropRegion[2] - cropRegion[0];
roiH = cropRegion[3] - cropRegion[1];
if (flag_square == 1) {
printf("Crop calculated before correct (sx=%d,sy=%d,ex=%d,ey=%d)\n",
cropRegion[0], cropRegion[1], cropRegion[2], cropRegion[3]);
if (roiW > roiH) {
cropRegion[2] = cropRegion[0] + roiH;
} else if (roiH > roiW) {
cropRegion[3] = cropRegion[1] + roiW;
}
flag_square = 0;
}
printf("after_crop_rect_calculated: (sx=%d,sy=%d,ex=%d,ey=%d)",
cropRegion[0], cropRegion[1], cropRegion[2], cropRegion[3]);
return ret;
}
#endif