在 OpenCV 上获取特定的行值
Getting specific Line values on OpenCV
我正在尝试访问视频中的行值。我已经找到了如何使用以下代码在屏幕上打印所有值,但我需要的是仅在出现值 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
生成(越过线时,我需要保存帧。
如果您正在处理阈值图像,则其类型为 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;
}
相关文章:
- OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- 如何在 ORB 检测 OpenCV ++ 中获取校正角度?
- 使用 opencv 凸胡尔 获取给定点的面积. - 点到垫子转换期间发生错误
- OPENCV 3.4.1获取经过定制训练的线性SVM HOG DETECTMULTISCALE的原始形式
- 遍历区域并在 OpenCV 中获取平均像素值
- 如何在 OpenCV 3.0 c++ 中获取链码 xml 文件
- 从直方图OPENCV中的峰中获取色调值
- 获取具有X,Y和距离的OPENCV中的3D坐标
- opencv::Mat,从原始数据中获取像素值
- OpenCV - 获取图像的一部分
- OpenCV从2D像素获取3D坐标
- 从OpenCV MLP响应中获取最高值(概率)位置
- OpenCV获取连接的边
- c++opencv获取编码的网络摄像头流
- OpenCV:获取相机分辨率C++
- "&":对绑定成员函数表达式的非法操作。从关键点矢量 OpenCV 获取容量
- 从mat C openCV获取数据
- 视频AR无人机的随机帧为空,然后无法使用OpenCV获取新图像
- 如何使用opencv获取位置并绘制矩形
- 使用Opencv获取多张图片的标准偏差