C ++中的函数类似于numpy flatten [英] function in C++ similar to numpy flatten

查看:418
本文介绍了C ++中的函数类似于numpy flatten的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++或opencv中是否有任何函数可以执行与numpy flatten()函数相同的操作?

Is there any function in C++ or opencv that can do the same operation as numpy flatten() function does ??

我认为我的问题含糊不清.我正在尝试在C ++ opencv环境中使用K-means分段,这里有python中给出的示例

I think my question was vague. I am trying to use K-means segmentation in C++ opencv environment, there is an example given in python here http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_ml/py_kmeans/py_kmeans_opencv/py_kmeans_opencv.html

center = np.uint8(center) res = center[label.flatten()]

center = np.uint8(center) res = center[label.flatten()]

在C ++中这种变平的事情之前,我已经能够完成大部分操作 标签的尺寸是921600 X 1,我也在C ++中也得到了它.中心尺寸为4X3矩阵.我想这里的诀窍是获取所有特定于中心值的标签值.因此最后"res"变为921600 X 3.

I have been able to do most of the operation until this flatten thing in C++ The dimension of label is 921600 X 1 which I also get in C++ as well. Center dimension is 4X3 matrix. I guess the trick here is to get all the label value specific to center value. so at the end "res" becomes 921600 X 3 .

所以我的问题是我该如何使用opencv在C ++中做到这一点?

So my question is How do i do this in C++ with opencv ?

谢谢

推荐答案

所以我已经弄清楚了,没有直线或一线解决方案(如果有的话,我知道哈哈)

So I have figured this out, there is no straight or 1 line solution of this ( if there is let me know haha)

res = center[label.flatten()] 

可以在C ++ opencv中使用以下命令执行

can be performed with following in C++ opencv

for(int i = 0; i<labels.rows ; i++)
{
  if(labels.at<int>(i) == 0) { 
    res.push_back(centers.row(0));
  } else if(labels.at<int>(i) == 1) {
      res.push_back(centers.row(1)); 
  } else if(labels.at<int>(i) == 2) {
      res.push_back(centers.row(2)); 
  } else{
      res.push_back(centers.row(3));
  } 
}

谢谢.

这篇关于C ++中的函数类似于numpy flatten的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆