void gradientDirection(cv::Mat &input, cv::Mat &aux) { cv::Mat ddxImage, ddyImage; ddx(input, ddxImage); ddy(input, ddyImage); aux = cv::Mat(input.rows, input.cols, CV_64FC1); for (int i = 0; i < input.rows; i++) for (int j = 0; j < input.cols; j++) { aux.at<double> (i, j) = atan( ddyImage.at<double> (i, j) / ddxImage.at<double> (i, j)); } }
void magnitude(cv::Mat &input, cv::Mat &aux) { cv::Mat ddxImage, ddyImage; ddx(input, ddxImage); ddy(input, ddyImage); aux = cv::Mat(input.rows, input.cols, CV_64FC1); for (int i = 0; i < input.rows; i++) for (int j = 0; j < input.cols; j++) { aux.at<double> (i, j) = sqrt( pow(ddxImage.at<double> (i, j), 2) + pow( ddyImage.at<double> (i, j), 2)); } }
VectorBase ADFun<Base>::ForTwo( const VectorBase &x, const VectorSize_t &j, const VectorSize_t &k) { size_t i; size_t j1; size_t k1; size_t l; size_t n = Domain(); size_t m = Range(); size_t p = j.size(); // check VectorBase is Simple Vector class with Base type elements CheckSimpleVector<Base, VectorBase>(); // check VectorSize_t is Simple Vector class with size_t elements CheckSimpleVector<size_t, VectorSize_t>(); CPPAD_ASSERT_KNOWN( x.size() == n, "ForTwo: Length of x not equal domain dimension for f." ); CPPAD_ASSERT_KNOWN( j.size() == k.size(), "ForTwo: Lenght of the j and k vectors are not equal." ); // point at which we are evaluating the second partials Forward(0, x); // dimension the return value VectorBase ddy(m * p); // allocate memory to hold all possible diagonal Taylor coefficients // (for large sparse cases, this is not efficient) VectorBase D(m * n); // boolean flag for which diagonal coefficients are computed CppAD::vector<bool> c(n); for(j1 = 0; j1 < n; j1++) c[j1] = false; // direction vector in argument space VectorBase dx(n); for(j1 = 0; j1 < n; j1++) dx[j1] = Base(0); // result vector in range space VectorBase dy(m); // compute the diagonal coefficients that are needed for(l = 0; l < p; l++) { j1 = j[l]; k1 = k[l]; CPPAD_ASSERT_KNOWN( j1 < n, "ForTwo: an element of j not less than domain dimension for f." ); CPPAD_ASSERT_KNOWN( k1 < n, "ForTwo: an element of k not less than domain dimension for f." ); size_t count = 2; while(count) { count--; if( ! c[j1] ) { // diagonal term in j1 direction c[j1] = true; dx[j1] = Base(1); Forward(1, dx); dx[j1] = Base(0); dy = Forward(2, dx); for(i = 0; i < m; i++) D[i * n + j1 ] = dy[i]; } j1 = k1; } } // compute all the requested cross partials for(l = 0; l < p; l++) { j1 = j[l]; k1 = k[l]; if( j1 == k1 ) { for(i = 0; i < m; i++) ddy[i * p + l] = Base(2) * D[i * n + j1]; } else { // cross term in j1 and k1 directions dx[j1] = Base(1); dx[k1] = Base(1); Forward(1, dx); dx[j1] = Base(0); dx[k1] = Base(0); dy = Forward(2, dx); // place result in return value for(i = 0; i < m; i++) ddy[i * p + l] = dy[i] - D[i*n+j1] - D[i*n+k1]; } } return ddy; }