2016-07-27 79 views
-3

现状如何通过矩阵数据循环显示它?

我有一个矩阵,该矩阵是300列和行1。当我cout <<它,我得到:

[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

...这是我期望/希望的形式。

问题

然而,当我通过它循环,我想每个单独的值的每个迭代。然而,相反,我得到一个稍微不同的顺序(有时它是非常相似的,虽然)。

代码

#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/core/core.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/imgcodecs/imgcodecs.hpp" 

using namespace std; 
using namespace cv; 

int main(){ 


    Mat test(1,300,CV_8UC1, 255); 
    cout << test; 

    Mat frame, grayFrame,threshFrame,smaller; 

    VideoCapture cap(0); 

    while(true){ 
     cap.read(frame); 
     cvtColor(frame, grayFrame, cv::COLOR_RGB2GRAY); 
     threshold(grayFrame, threshFrame, 160, 255, THRESH_BINARY); 
     smaller = threshFrame(Rect(0,0,300,1)); 
     cout << smaller; 

     for(int x=0;x<smaller.cols;x++){ 
      int color = smaller.at<Vec3b>(x,1)[0]; 
      cout << color; 

     } 

     break;   
    } 

} 

...这不符合0和255秒的原始矩阵完全相同的顺序怪异输出:

00000000000000000000000000000000000000000000000000000000000000000000000000000000000002552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552552550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

矩阵有许多0起初只有几乎255秒,因为循环输出有很多的255s,并没有太多的开始0。从本质上讲,我想循环显示第一个矩阵,并为每次迭代获取每个值。所以0,0,255,255 ...等

+0

你不打印你的值之间的空格... – jaggedSpire

+0

我的问题没有任何东西o与空间,数据结构。一个是数组,一个是int输出...我的意思是ORDER。你会注意到订单仍然不同,原来的开始“0,0,255”和循环开始“255,255,255” –

+0

所以它是。你会提供[MCVE](http://stackoverflow.com/help/mcve)吗? – jaggedSpire

回答

2

你正在阅读垃圾。

  • at功能需要(row, col),而不是(x, y)。请记住row = ycol = x

  • 如果您的矩阵只是一行,那么行索引必须是0而不是1

  • 你的矩阵是无符号的字符的单通道,所以你需要使用at<uchar>

在实践中,使用:

uchar color = smaller.at<uchar>(0, x); 
cout << int(color); 

或使用索引:

uchar color = smaller.at<uchar>(x); 
+0

辉煌。作品。谢谢 –