OpenCV访问MAT对象中的RGB值
OpenCV Access RGB values in a MAT object
我正试图使用OpenCV从网络摄像头获取帧,并将其转换为HSV(色调、饱和度、值)Mat对象并设置阈值。
当我打印阈值图像像素值时,它为所有像素提供[0,0,0],即使是黑色像素值也是[0,0,0]。如果选择的像素是黑色的,我需要做一些计算;如何访问像素值?。
imgOriginal=frame from camera
Mat imgHSV;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
Mat imgThresholded;
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
//morphological opening (remove small objects from the foreground)
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
//morphological closing (fill small holes in the foreground)
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
//************************************
std::vector<cv::Vec3b> pixels(imgThresholded.rows * imgThresholded.cols);
cv::Mat m(imgThresholded.rows, imgThresholded.cols, CV_8UC3, &pixels[0]);
imgThresholded.copyTo(m);
for(int i =0;i<1000;i++)
cout<<pixels[0];
if(pixels[0][0]==black)
// do some calculations!
for(int i =0;i<1000;i++)
cout<<pixels[0];
将只打印第一个像素1000次。我想你的意思是:
Vec3b black(0, 0, 0);
for(int i =0;i<1000;i++)
{
cout << pixels[i];
if pixels[i] == black)
{
/* ... */
}
}
但是,为什么要麻烦将像素复制到std::向量中呢?你可以做这个
Vec3b black(0, 0, 0);
Mat img(imgThresholded); // just to make a short name
for(int y = 0; y < img.rows; ++y)
{
Vec3b* row = img.ptr<Vec3b>(y);
for(int x = 0; x < img.cols; ++x)
{
Vec3b& pixel = row[x];
if(pixel == black)
{
/* ... */
}
}
}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- OpenCV访问MAT对象中的RGB值
- Vec3b 使用问题以及在 Mat 对象中的某个点获取 RGB 值