9 bool calcCrop(cv::Size inputSize,
float targetWdivH,
int& cropW,
int& cropH,
int& width,
int& height)
13 width = inputSize.width;
14 height = inputSize.height;
16 float inWdivH = (float)inputSize.width / (
float)inputSize.height;
18 float outWdivH = targetWdivH < 0.0f ? inWdivH : targetWdivH;
24 if (inWdivH > outWdivH)
26 width = (int)((
float)inputSize.height * outWdivH);
27 height = inputSize.height;
28 cropW = (int)((
float)(inputSize.width - width) * 0.5f);
32 if (wModulo4 == 1) width--;
38 if (wModulo4 == 3) width++;
42 width = inputSize.width;
43 height = (int)((
float)inputSize.width / outWdivH);
44 cropH = (int)((
float)(inputSize.height - height) * 0.5f);
47 hModulo4 = height % 4;
48 if (hModulo4 == 1) height--;
54 if (hModulo4 == 3) height++;
62 void cropImage(cv::Mat& img,
float targetWdivH,
int& cropW,
int& cropH)
65 if (
calcCrop(img.size(), targetWdivH, cropW, cropH, width, height))
68 img = img(cv::Rect(cropW, cropH, width, height));
73 void cropImageTo(
const cv::Mat& img, cv::Mat& outImg,
float targetWdivH,
int& cropW,
int& cropH)
76 if (
calcCrop(img.size(), targetWdivH, cropW, cropH, width, height))
78 img(cv::Rect(cropW, cropH, width, height)).copyTo(outImg);
88 float inWdivH = (float)img.cols / (
float)img.rows;
89 float outWdivH = targetWdivH < 0.0f ? inWdivH : targetWdivH;
91 int addT = 0, addB = 0, addL = 0, addR = 0;
97 if (inWdivH > outWdivH)
100 height = (int)((
float)img.cols / outWdivH);
101 addT = (int)((
float)(height - img.rows) * 0.5f);
102 addB = height - addT - img.rows;
106 width = (int)((
float)img.rows * outWdivH);
111 if (img.channels() != 4)
114 int wModulo4 = width % 4;
116 width += (4 - wModulo4);
118 addL = (int)((
float)(width - img.cols) * 0.5f);
119 addR = width - addL - img.cols;
162 cv::Scalar value(0, 0, 0);
163 copyMakeBorder(img, img, addT, addB, addL, addR, cv::BORDER_CONSTANT, value);
165 assert((img.size().width * img.channels()) % 4 == 0);
176 cv::flip(img, mirrored, -1);
178 cv::flip(img, mirrored, 1);
185 cv::flip(img, mirrored, -1);
187 cv::flip(img, mirrored, 0);
194 float fovRad = 2.f * atanf(0.5f * (
float)imgLength / focalLengthPix);
202 float focalLengthPix = 0.5f * imgLength / tanf(0.5f * fovRad);
203 return focalLengthPix;
208 float f = (0.5f * (float)otherLength) / tanf(otherFovDeg * 0.5f * (
float)
SENS_DEG2RAD);
209 return 2.f * atan(0.5f * (
float)length / f) * (float)
SENS_RAD2DEG;
214 double scale = (double)newRefLength / (
double)oldRefLength;
215 cv::Mat newMat(3, 3, CV_64F);
216 newMat.at<
double>(0, 0) = scale * origMat.at<
double>(0, 0);
217 newMat.at<
double>(0, 1) = 0.0;
218 newMat.at<
double>(0, 2) = scale * origMat.at<
double>(0, 2);
219 newMat.at<
double>(1, 0) = 0.0;
220 newMat.at<
double>(1, 1) = scale * origMat.at<
double>(1, 1);
221 newMat.at<
double>(1, 2) = scale * origMat.at<
double>(1, 2);
222 newMat.at<
double>(2, 0) = 0.0;
223 newMat.at<
double>(2, 1) = 0.0;
224 newMat.at<
double>(2, 2) = 1.0;
void extendWithBars(cv::Mat &img, float targetWdivH)
void mirrorImage(cv::Mat &img, bool mirrorH, bool mirrorV)
float calcFocalLengthPixFromFOVDeg(const float fovDeg, const int imgLength)
float calcFOVDegFromFocalLengthPix(const float focalLengthPix, const int imgLength)
void cropImageTo(const cv::Mat &img, cv::Mat &outImg, float targetWdivH, int &cropW, int &cropH)
void cropImage(cv::Mat &img, float targetWdivH, int &cropW, int &cropH)
bool calcCrop(cv::Size inputSize, float targetWdivH, int &cropW, int &cropH, int &width, int &height)
cv::Mat adaptCameraMat(cv::Mat origMat, int newRefLength, int oldRefLength)
float calcFovDegFromOtherFovDeg(const float otherFovDeg, const int otherLength, const int length)