def map_data(data_map, axis_num): data_map = np.max(data_map, axis=axis_num) data_map -= data_map.min() data_map = data_map / data_map.max() data_map = np.array(data_map * 255, dtype=np.uint8) return data_map Eigon实现
时间: 2024-03-05 15:48:31 浏览: 75
以下是Eigen实现:
```
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
MatrixXi map_data(MatrixXi data_map, int axis_num) {
int height = data_map.rows();
int width = data_map.cols();
// reduce the data_map along the given axis_num
VectorXi max_vals(width);
if (axis_num == 0) {
max_vals = data_map.rowwise().maxCoeff();
} else if (axis_num == 1) {
max_vals = data_map.colwise().maxCoeff();
}
// normalize the result
int min_val = max_vals.minCoeff();
int max_val = max_vals.maxCoeff();
MatrixXi result = (data_map.array() - min_val) * 255 / (max_val - min_val);
return result;
}
int main() {
// example usage
MatrixXi data_map(3, 3);
data_map << 1, 2, 3,
4, 5, 6,
7, 8, 9;
MatrixXi result = map_data(data_map, 0);
cout << result << endl;
return 0;
}
```
需要注意的是,Eigen中的Matrix类模板参数中有两个参数,第一个参数是矩阵元素的类型,第二个参数是矩阵的行数和列数,用RowMajor表示按行存储,用ColMajor表示按列存储。在这个实现中,我们用MatrixXi表示矩阵元素为整型,且按列存储。
阅读全文