我在c ++中的openCV中有一个Mat矩阵,我想通过以矢量形式提供行和列位置来更改此矩阵中的值。例如:
Mat M = Mat::zeros(10,10,CV_8UC1);
Mat IJ_coord = (Mat_<uchar>(3,2) << 0,0, 1,0, 2,7);
M.at<uchar>(IJ_coord) = 255;
有没有办法在不使用&#34; for&#34;周期太慢了?下面是代码。在进行一些初始化之前,为简单起见省略了。循环的最后一个是关键的。它们需要0.6秒,几乎是整个代码的一半时间。
int64 t0 = getTickCount();
if (num_frame < 10){
prefix_num = "000";
} else if(num_frame < 100){
prefix_num = "00";
} else if(num_frame < 1000){
prefix_num = "0";
} else prefix_num = "";
string frame_num = to_string(num_frame);
Mat Point_w = readMatrix(folder_pnt_w + prefix_num + frame_num + ".txt",2);
Mat Point_I = readMatrix(folder_pnt_I + prefix_num + frame_num + ".txt",2);
I = imread(folder_frm + prefix_num + frame_num + ".jpg", 1);
split(I, planes);
I_ir = planes[2]-planes[1];
Mat H = findProjectiveTransformation(Point_I, Point_w);
addWeighted(i0, H.at<double>(0), j0, H.at<double>(1), H.at<double>(2), Num,
-1);
addWeighted(i0, H.at<double>(6), j0, H.at<double>(7), 1, Den, -1);
divide(Num, Den, X_geo);
addWeighted(i0, H.at<double>(3), j0, H.at<double>(4), H.at<double>(5), Num,
-1);
divide(Num, Den, Y_geo);
// here I will calculate the coordinates to access in the matrix
J_x_geo = (X_geo-X_min)/dx;
I_y_geo = (Y_max - Y_geo)/dy;
J_x_geo.convertTo(J_x_geo, CV_32S, 1, 0);
I_y_geo.convertTo(I_y_geo, CV_32S, 1, 0);
for(size_t i=0; i<I_ir.rows; i++){
for(size_t j=0; j<I_ir.cols; j++){
if (Y_geo.at<float>(i,j) >= Y_min && Y_geo.at<float>(i,j) <= Y_max &&
X_geo.at<float>(i,j) >= X_min && X_geo.at<float>(i,j) <= X_max){
if (Map.at<uchar>(I_y_geo.at<int>(i,j),J_x_geo.at<int>(i,j)) < I_ir.at<uchar>
(i,j)){
Map.at<uchar>(I_y_geo.at<int>(i,j),J_x_geo.at<int>(i,j)) =
I_ir.at<uchar>(i,j);
}
}
}
}
int64 t1 = getTickCount();
double secs = (t1-t0)/getTickFrequency();
cout << secs << endl;