Reputation: 2974
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
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
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.
Upvotes: 6
Reputation: 10588
If you are dealing with a uchar (CV_8U) matrix:
mat.at<uchar>(row, column, channel) = val;
Upvotes: 22