简体   繁体   中英

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.

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM