Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

opencv

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.

like image 960
A S Avatar asked Aug 02 '12 10:08

A S


People also ask

How can I see my CV mat pixels?

To access individual pixels, the safest way, though not the most efficient, is to use cv::Mat::at<T>(r,c) method where r is the row of the matrix and c is the column.

What is CV_32FC1?

CV_32F defines the depth of each element of the matrix, while. CV_32FC1 defines both the depth of each element and the number of channels.

What is Vec3b?

Vec3b is the abbreviation for "vector with 3 byte entries" Here those byte entries are unsigned char values to represent values between 0 .. 255. Each byte typically represents the intensity of a single color channel, so on default, Vec3b is a single RGB (or better BGR) pixel.


2 Answers

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

 mat.at<uchar>(row, column, channel) = val;
like image 176
Régis B. Avatar answered Sep 18 '22 11:09

Régis B.


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
like image 22
Jeff T. Avatar answered Sep 18 '22 11:09

Jeff T.