我试图访问视频中的线路值。我已经找到了如何使用以下代码在屏幕上打印所有值,但我需要的是仅在值255(白色)出现时打印。
LineIterator li1(fgThreshold, Point(20, 0), Point(20, 479), 8);
vector<Vec3b> buf1;
for (int i = 0; i<li1.count; i++) {
buf1.push_back(Vec3b(*li1));
li1++;
}
cout << Mat(buf1) << endl;
原因是我需要在白色(由threshold
生成)穿过该线时保存帧。
答案 0 :(得分:1)
如果您正在使用阈值图像,那么它的类型为CV_8UC1
,其元素为uchar
,而不是Vec3b
。
在这种情况下,您可以检查行迭代器的值是否为白色(255),如:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
// Create a CV_8UC1 Mat with a white circle
// Your fgThreshold would be something like this
Mat1b fgThreshold(100, 100, uchar(0));
circle(fgThreshold, Point(20, 20), 3, Scalar(255), CV_FILLED);
// Init line iterator
LineIterator lit(fgThreshold, Point(20, 0), Point(20, 40));
// Value to check
const uchar white = 255;
// Save colors in buf, if value is ok
vector<uchar> buf;
// Save points in pts, if value is ok
vector<Point> pts;
for (int i = 0; i < lit.count; ++i, ++lit)
{
// Check if value is ok
if (**lit == white)
{
// Add to vectors
buf.push_back(**lit);
pts.push_back(lit.pos());
}
}
// Print
cout << "buf: " << Mat(buf) << endl;
cout << "pts: " << Mat(pts) << endl;
return 0;
}
如果您正在处理CV_8UC3
图片,则需要投射线迭代器,例如:
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat3b fgThreshold(100, 100, Vec3b(0,0,0));
circle(fgThreshold, Point(20, 20), 3, Scalar(255, 255, 255), CV_FILLED);
LineIterator lit(fgThreshold, Point(20, 0), Point(20, 40));
const Vec3b white(255, 255, 255);
vector<Vec3b> buf;
vector<Point> pts;
for (int i = 0; i < lit.count; ++i, ++lit)
{
// Cast to Vec3b
if ((Vec3b)*lit == white)
{
// Cast to Vec3b
buf.push_back((Vec3b)*lit);
pts.push_back(lit.pos());
}
}
cout << "buf: " << Mat(buf) << endl;
cout << "pts: " << Mat(pts) << endl;
return 0;
}