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 c+++实现
时间: 2024-04-27 20:22:00 浏览: 114
以下是C++实现:
```
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> map_data(vector<vector<vector<int>>> data_map, int axis_num) {
vector<vector<int>> result;
int height = data_map.size();
int width = data_map[0].size();
int depth = data_map[0][0].size();
// reduce the data_map along the given axis_num
for (int i = 0; i < height; i++) {
vector<int> row;
for (int j = 0; j < width; j++) {
int max_val = -1;
for (int k = 0; k < depth; k++) {
if (axis_num == 0) {
max_val = max(max_val, data_map[k][i][j]);
} else if (axis_num == 1) {
max_val = max(max_val, data_map[i][k][j]);
} else if (axis_num == 2) {
max_val = max(max_val, data_map[i][j][k]);
}
}
row.push_back(max_val);
}
result.push_back(row);
}
// normalize the result
int min_val = result[0][0];
int max_val = result[0][0];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
min_val = min(min_val, result[i][j]);
max_val = max(max_val, result[i][j]);
}
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
result[i][j] = (result[i][j] - min_val) * 255 / (max_val - min_val);
}
}
return result;
}
int main() {
// example usage
vector<vector<vector<int>>> data_map = {
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{9, 8, 7}, {6, 5, 4}, {3, 2, 1}},
{{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}
};
vector<vector<int>> result = map_data(data_map, 0);
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result[0].size(); j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
阅读全文