C++ 如何将像素设置为 cv::Mat 对象中的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11776043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:31:12  来源:igfitidea点击:

How to set a pixel to a value in a cv::Mat object?

c++opencv

提问by A S

I need to set a single pixel in the Mat object to a certain value.

我需要将 Mat 对象中的单个像素设置为某个值。

How to do it?

怎么做?

I am using openCV 2.1 with visual studio 2010.

我在 Visual Studio 2010 中使用 openCV 2.1。

回答by Régis B.

If you are dealing with a uchar (CV_8U) matrix:

如果您正在处理 uchar (CV_8U) 矩阵:

 mat.at<uchar>(row, column, channel) = val;

回答by Jeff T.

In fact, there are 4 kinds of methods to get/set a pixel value in a cv::Mat object as described in the OpenCV tutorial.

实际上,如OpenCV 教程所述,有 4 种方法可以在 cv::Mat 对象中获取/设置像素值。

The one @Régis mentioned is called On-The-Fly RAin OpenCV tutorial. It's the most convenient but also time-consuming.

提到的@Régis在 OpenCV 教程中称为On-The-Fly RA。这是最方便的,但也是最耗时的。

Based on the tutorial's experiment, it also lists performance differences in all the 4 methods.

基于教程的实验,它还列出了所有 4 种方法的性能差异。

  • Efficient Way 79.4717 milliseconds
  • Iterator 83.7201 milliseconds
  • On-The-Fly RA93.7878 milliseconds
  • LUT function 32.5759 milliseconds
  • 高效方式 79.4717 毫秒
  • 迭代器 83.7201 毫秒
  • 即时RA93.7878 毫秒
  • LUT 函数 32.5759 毫秒

回答by Mona Jalal

Here's an example:

下面是一个例子:

vector<cv::Point3f> xyzBuffer;
cv::Mat xyzBuffMat = cv::Mat(307200, 1, CV_32FC3);
for (int i = 0; i < xyzBuffer.size(); i++) {
    xyzBuffMat.at<cv::Vec3f>(i, 1, 0) = xyzBuffer[i].x;
    xyzBuffMat.at<cv::Vec3f>(i, 1, 1) = xyzBuffer[i].y;
    xyzBuffMat.at<cv::Vec3f>(i, 1, 2) = xyzBuffer[i].z;
}