#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
using namespace cv;
#define N 25
struct data
{
Point2d * point;
double * left_gray;
size_t n;
};
double Interpolate(Mat mask, double sx,double sy)
{
double pixel=0;
uchar* data = mask.data;
int lRow = static_cast<int>(sy); //最临近行,取整
int nRow = lRow + 1;
int lCol = static_cast<int>(sx); //最临近列,取整
int nCol = lCol + 1;
double u = (sy - lRow) * 2048;
double v = (sx - lCol) * 2048;
for (int i = 0; i < mask.rows; i++)
{
for (int j = 0; j < mask.cols; j++)
{
pixel = ((2048 - u)*(2048 - v)*data[lRow*mask.step[0] + lCol*mask.step[1]]
+ (2048 - u)*v*data[lRow * mask.step[0] + nCol * mask.step[1]]
+ u*(2048 - v)*data[nRow * mask.step[0] + lCol * mask.step[1]]
+ u*v*data[nRow * mask.step[0] + nCol * mask.step[1]]) / 4194304;
}
}
return pixel;
}
int main()
{
double keypointx = 2749.27;
double keypointy = 1642;
Point2d point[N];
double left_gray[N];
const size_t n = 25;
struct data fit_data = { point, left_gray, n };
Mat left = imread("rectifyImageL02.jpg", 0);
Mat right = imread("rectifyImageR02.jpg", 0);
size_t i;
//this is the data to be fitted
for (i = 0; i < 5; i++)
{
point[i].x = keypointx + i - 2;
point[i].y = keypointy - 2;
point[i+5].x = keypointx + i - 2;
point[i+5].y = keypointy - 1;
point[i+10].x = keypointx + i - 2;
point[i+10].y = keypointy ;
point[i+15].x = keypointx + i - 2;
point[i+15].y = keypointy + 1;
point[i + 20].x = keypointx + i - 2;
point[i + 20].y = keypointy + 2;
}
double a1 = 0.0;
double a2 = 0.0;
double a3 = 0.0;
double a4 = 0.0;
double a5 = -506.25;
double a6 = 1.17;
for (i = 0; i < 25; i++)
{
double x1i = point[i].x;
double y1i = point[i].y;
double x2i = (1 + a1)*x1i + a3*y1i + a5;
double y2i = a2*x1i + (1 + a4)*y1i + a6;
//x方向的近似导数,卷积运算
double Gxi = Interpolate(right, x2i + 1, y2i - 1) + 2 * Interpolate(right, x2i + 1, y2i)
+ Interpolate(right, x2i + 1, y2i + 1) - Interpolate(right, x2i - 1, y2i - 1)
- 2 * Interpolate(right, x2i - 1, y2i) - Interpolate(right, x2i - 1, y2i + 1);
//y方向的近似导数,卷积运算
double Gyi = Interpolate(right, x2i - 1, y2i - 1) + 2 * Interpolate(right, x2i, y2i - 1)
+ Interpolate(right, x2i + 1, y2i - 1) - Interpolate(right, x2i - 1, y2i + 1)
- 2 * Interpolate(right, x2i, y2i + 1) - Interpolate(right, x2i + 1, y2i + 1);
cout << Gxi << " " << Gyi << endl;
cout << " " << endl;
}
return 0;
}
评论1