i have mat2d matrix, each element 2d vector. example:
[[x0, y0], [x1, y1] [x2, y2], [x3, y3]]
i want left multiply each of these vectors mat1d camera matrix:
[fx, 0, cx, 0, fy, cy, 0, 0, 1]
(each vector represents location of vertex in grid want convert camera space pixel space.)
the resulting matrix, example, be:
[[x0 * fx + cx, y0 * fy + cy], [x1 * fx + cx, y1 * fy + cy] [x2 * fx + cx, y2 * fy + cy], [x3 * fx + cx, y3 * fy + cy]]
what straightforward , efficient way accomplish this?
here current approach:
mat2d points = getmesh(); mat1d cameramtrx = getcameramtrx(); for(int col = 0; col < points.cols; col++){ for(int row = 0; row < points.rows; row++){ points.at<vec2d>(row, col).val[0] = points.at<vec2d>(row, col)[0] * cameramtrx.at<double>(0, 0) + cameramtrx.at<double>(0, 2); points.at<vec2d>(row, col).val[1] = points.at<vec2d>(row, col)[1] * cameramtrx.at<double>(1, 1) + cameramtrx.at<double>(1, 2); } }
opencv documentation has detailed various methods of iterating cv::mat
efficiently, out of presented methods, efficient way use cv::lut()
, context of question, guess range of input matrix values not fixed, look-up table can't created, helpful in case of rgb
images, because know beforehand min value 0
, max value 255
, can create lookup table, in problem, need multiply 2 matrices assume not images go the efficient way.
int cameramatrix[] = {2, 0, 10, 0, 4, 20, 0, 0, 1}; cv::mat mat(2, 2, cv_32fc2, cv::scalar(100, 20)); cv::size contsize = mat.size(); // calculate length of array if input matrix flatten, in case of continuous matrix only. if (mat.iscontinuous()) { contsize.width *= contsize.height; contsize.height = 1; } cv::vec2f* ptr; (int = 0; < contsize.height; ++i) { ptr = mat.ptr<cv::vec2f>(i); (int j = 0; j < contsize.width; ++j) { ptr[j] = cv::vec2f(ptr[j].val[0]*cameramatrix[0] + cameramatrix[2], ptr[j].val[1] * cameramatrix[4] + cameramatrix[5]); } }
Comments
Post a Comment