如何循环遍历 Matrix 数据的显示方式?
How do I loop through Matrix data how it appears?
情况
我有一个 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]
...形式为 I expect/want.
问题
但是,当我循环遍历它时,我想在每次迭代中获取每个单个值。但是,我得到的顺序略有不同(尽管有时它非常相似)。
代码
#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,而循环输出有很多 255,而不是很多开始的 0。
本质上,我想遍历首先显示的矩阵,并在每次迭代中获取每个值。所以 0,0,255,255...等等
你在看垃圾。
at
函数需要 (row, col)
,而不是 (x, y)
。请记住 row = y
和 col = x
.
如果你的矩阵只有一行,行索引必须是0
,而不是1
。
你的矩阵是unsigned char的单通道,所以你需要使用at<uchar>
在实践中,使用:
uchar color = smaller.at<uchar>(0, x);
cout << int(color);
或使用索引:
uchar color = smaller.at<uchar>(x);
情况
我有一个 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]
...形式为 I expect/want.
问题
但是,当我循环遍历它时,我想在每次迭代中获取每个单个值。但是,我得到的顺序略有不同(尽管有时它非常相似)。
代码
#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,而循环输出有很多 255,而不是很多开始的 0。
本质上,我想遍历首先显示的矩阵,并在每次迭代中获取每个值。所以 0,0,255,255...等等
你在看垃圾。
at
函数需要(row, col)
,而不是(x, y)
。请记住row = y
和col = x
.如果你的矩阵只有一行,行索引必须是
0
,而不是1
。你的矩阵是unsigned char的单通道,所以你需要使用
at<uchar>
在实践中,使用:
uchar color = smaller.at<uchar>(0, x);
cout << int(color);
或使用索引:
uchar color = smaller.at<uchar>(x);