我正在基于纸张计算视觉图像,然后优化我的参数(焦距,旋转和平移)。因此,我通过旅行所有像素的真实图像和虚拟图像来创建成本函数。在我的成本函数中,我基本上从归一化的实像中减去了归一化的虚像。虚拟映像是在Evaluation_callback函子中计算的,成本是在成本函数函子中计算的。问题出在成本函子上。由于梯度等于0,因此优化在第一次迭代时终止。我正在使用ceres :: Central进行梯度计算,但虚拟图像创建器函子仅在每次迭代时调用一次。但是我需要分别调用f(x + h)和f(xh)的仿函数。当我通过9个邻居计算归一化的实像和归一化的虚像时,我有连续的迭代,但是每次迭代需要25秒,这对于我的情况。我需要这个Evaluation_callback函数,但无法正常工作。
我看一下Evaluation_callbacks定义。写道:“注意:评估回调与内部迭代不兼容。”
struct RcpAndFpOptimizer {
RcpAndFpOptimizer(cv::Mat &V, const cv::Mat I, int i,int j,double width, double height) : V_(V), I_(I), i_(i),
j_(j), width_(width), height_(height){}
bool operator()(const double* const fp, const double* const rotation, const double* const translation, double* residuals) const {
double intensity = V_.at<double>(j_, i_);
double tmp = (double)I_.at<double>(j_,i_)-(double)intensity;
residuals[0] = tmp;
//std::cout<<"pixels(i,j): "<<i_<<" "<<j_<<" residual: "<<residuals[0]<<std::endl;
return true;
}
const cv::Mat S_;
cv::Mat& V_;
const cv::Mat I_;
const int i_,j_;
double width_, height_;
};
virtual void PrepareForEvaluation(bool evaluateJacobians, bool newEvaluationPoint)
{
if(evaluateJacobians){
std::cout<<"evaluation jacobian is called"<<std::endl;
}
if (newEvaluationPoint)
{
// do your stuff here, e.g. calculate integral image
//Mat V(height_, width_, CV_8UC1);
std::cout<<"preperation is called"<<std::endl;
Intrinsic<double> intrinsicC = INTRINSIC_CAMERA;
Intrinsic<double> intrinsicP= {(double)fP_[0],(double)fP_[0], double(width_/2), double(height_/2), 0, 0};
//Convertion of array to point3d
Point3d bDist = Point3d(translation_[0],translation_[1], translation_[2]);
//Convertion euler array to rotation matrix
const Mat eulerAngles = (cv::Mat_<double>(3,1) << rotArray_[0], rotArray_[1], rotArray_[2]);
Mat rotM = rcpFinder::euler2rot(eulerAngles);
Mat tempVImg(height_, width_, CV_8UC1);
for (int i = 0; i < width_; ++i) {
for (int j = 0; j < height_ ; ++j) {
//std::cout<<"Virtual current x and y pixels: "<<i<<" "<<j<<std::endl;
Point3d unprojPRay = rcpFinder::unprojectPoints(Point2i(i,j),intrinsicC);
//Assigning the intensity from images
tempVImg.at<uchar>(j, i)= rcpFinder::genVirtualImg(S_, intrinsicP, bDist, unprojPRay,
planeNormalAndDistance_, rotM);
auto pixelIntensity = tempVImg.at<uchar>(Point(j, i));
//std::cout<<"pixel intensity "<< pixelIntensity<<std::endl;
}
}
//imshow("Virtual", tempVImg);
Mat integralV;
cv::integral(tempVImg, integralV);
//std::cout<<"integral image type is "<<integralV.type()<<std::endl;
rcpFinder::normalizePixelsImg(tempVImg, integralV, V_);
/*imshow("Normalized Img", V_);
waitKey(0);*/
}
}
// stuff here
const cv::Mat S_;
cv::Mat& V_;
int width_, height_;
map<int, vector<Point3d>> planeNormalAndDistance_;
double *translation_;
double* rotArray_;
double* fP_;
};
//调用函子就像下面的
cv::Mat integralImgI;
cv::integral(im1, integralImgI);
cv::Mat normalizedRealImg;
rcpFinder::normalizePixelsImg(im1, integralImgI, normalizedRealImg);
Mat normalizedVirtualImg;
//ceres::CostFunction* total_cost_function = 0;
for (int i = 1; i < width-1; ++i) {
for (int j = 1; j < height-1 ; ++j) {
ceres::CostFunction* cost_function =
new ceres::NumericDiffCostFunction<RcpAndFpOptimizer, ceres::CENTRAL, 1, 1, 3, 3>(
new RcpAndFpOptimizer(normalizedVirtualImg, normalizedRealImg, i, j, width, height));
problem.AddResidualBlock(cost_function, NULL, fp, rotationArray, translation);
}
}
ceres::Solver::Options options;
options.minimizer_progress_to_stdout = true;
options.max_num_iterations = 50;
options.update_state_every_iteration = true;
options.evaluation_callback = (new evaluation_callback_functor(S, normalizedVirtualImg,width, height,
mapNormalAndDist, translation,rotationArray, fp));
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
我希望让求解器至少运行一次以上的迭代,并且梯度应从某些值开始并且必须逐次减小。
我用9个邻居将薄饼标准化。我发现当前的解决方案仅在成本函子中计算9个像素的虚拟图像,并将其用于一个像素归一化,但这太慢了。我有640x480像素,每个像素有9次计算。再加上在Jacobing和NumericCOstFunction中的梯度计算太多了。这就是为什么我要在Evaluation_callback函子中计算虚拟图像,并在该函数内部对其进行规范化,然后在成本函子中使用规范化的图像。 谢谢您的帮助。
答案 0 :(得分:0)
您不能通过内部迭代调用评估回调
https://groups.google.com/forum/#!topic/ceres-solver/zjQLIaSuAdQ