A S
A S

Reputation: 2974

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

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

How to do it?

I am using openCV 2.1 with visual studio 2010.

Upvotes: 23

Views: 68220

Answers (3)

Mona Jalal
Mona Jalal

Reputation: 38145

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;
}

Upvotes: 2

Jeff T.
Jeff T.

Reputation: 2271

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.

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

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

  • Efficient Way 79.4717 milliseconds
  • Iterator 83.7201 milliseconds
  • On-The-Fly RA 93.7878 milliseconds
  • LUT function 32.5759 milliseconds

Upvotes: 6

R&#233;gis B.
R&#233;gis B.

Reputation: 10588

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

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

Upvotes: 22

Related Questions