std::map<int, int> labelCountMap; for (int i = 0; i < labelImg.rows; i++) for (int j = 0; j < labelImg.cols; j++) { int labelValue = labelImg.at<int>(i, j); if (labelValue > 0) { if (labelCountMap.count(labelValue) == 0) labelCountMap[labelValue] = 1; else labelCountMap[labelValue]++; } }
时间: 2023-06-20 16:05:20 浏览: 39
这段代码使用了 C++ STL 中的 `std::map` 容器,用于统计图像中每个标签(label)的像素数目。具体来说,代码首先定义了一个 `labelCountMap` 的 `std::map` 对象,键类型为 `int`,值类型也为 `int`,即将标签值映射到像素数目。然后,代码通过两个嵌套的 `for` 循环遍历图像中的所有像素,获取当前像素的标签值 `labelValue`。如果 `labelValue` 大于 0,说明当前像素属于某个标签,需要将该标签的像素数目加 1。为了实现这一功能,代码使用了 `std::map` 的 `count` 方法判断当前标签是否已经在 `labelCountMap` 中出现过,如果没有出现过,则将该标签的值设置为 1;否则将该标签的值加 1。最终,`labelCountMap` 中保存了每个标签的像素数目,可以根据需要进行进一步处理或输出。
相关问题
void NCut::GetPatchFeature(const MatrixXf &points, MatrixXf *features_in) { MatrixXf &features = *features_in; const int num_points = static_cast<int>(points.rows()); const int dim = _patch_size * _patch_size; features.resize(num_points, dim); for (int i = 0; i < num_points; ++i) { // .1 patch int irow = 0; int jcol = 0; _ff_feature_grid.Pos2d(points.coeffRef(i, 0), points.coeffRef(i, 1), &irow, &jcol); cv::Mat patch; cv::Point2f pt(static_cast<float>(irow), static_cast<float>(jcol)); cv::getRectSubPix(_cv_feature_map, cv::Size(_patch_size, _patch_size), pt, patch); // .2 maybe later i will add other features based on patch // .3 add to features assert(dim == patch.rows * patch.cols); int p = 0; for (int r = 0; r < patch.rows; ++r) { for (int c = 0; c < patch.cols; ++c) { float val = patch.at<float>(r, c); features.coeffRef(i, p++) = static_cast<float>( (std::isnan(val) || std::isinf(val)) ? 1.e-50 : val); // features.coeffRef(i, p++) = patch.at<float>(r, c); } } } }
这段代码是用于计算NCut算法中的图像块的特征向量。给定一个点集points,对于每个点,首先通过_ff_feature_grid将其映射到_feature_map图像上,并取该点为中心的_patch_size * _patch_size大小的矩形区域作为图像块。然后,对于每个图像块,将像素值添加到特征向量中,特征向量的维度为_patch_size * _patch_size。在添加每个像素值时,如果该值为NaN或Inf,则设置为1.e-50。最终,返回一个特征向量矩阵features,其行数为points中点的个数,列数为_patch_size * _patch_size。
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++
这是一段 Python 代码,转换为 C++ 的代码如下:
```cpp
#include <vector>
#include <algorithm>
std::vector<std::vector<int>> map_data(std::vector<std::vector<int>> data_map, int axis_num) {
int rows = data_map.size();
int cols = data_map[0].size();
if (axis_num == 0) {
for (int i = 0; i < rows; ++i) {
int max_val = *std::max_element(data_map[i].begin(), data_map[i].end());
for (int j = 0; j < cols; ++j) {
data_map[i][j] = (int)(255.0 * (data_map[i][j] - 0.0) / (max_val - 0.0));
}
}
}
else {
for (int j = 0; j < cols; ++j) {
int max_val = 0;
for (int i = 0; i < rows; ++i) {
max_val = std::max(max_val, data_map[i][j]);
}
for (int i = 0; i < rows; ++i) {
data_map[i][j] = (int)(255.0 * (data_map[i][j] - 0.0) / (max_val - 0.0));
}
}
}
return data_map;
}
```
注意,这段代码的数据类型是 `std::vector<std::vector<int>>`,需要先将输入数据转换为 `std::vector<std::vector<int>>` 类型,然后调用这个函数进行处理。