Haris
Haris

Reputation: 14053

Mat Single channel Element Access

Using three channel Mat we can access element like

Mat draw(480, 480, CV_8UC3);
Vec3b pix(255,0,0);
draw.at<Vec3b>(i,j)=pix;

But in the case of single channel Mat like

 Mat draw(480, 480, CV_8UC1);

how can I access Mat element. I already tried some thing like

 draw.at<float>(i,j)=255;

but wrong result. Am I wrong in the above case ?, any help will appreciated.....

Thanks in advance.............

Upvotes: 2

Views: 3395

Answers (2)

Sami Dalati
Sami Dalati

Reputation: 161

if you want to use CV_8UC1 then accessing an element in the Mat would be like this

(draw.at<Vec3b>(i,j)).val[k]=255;

where K is the channel number (0 to 3)

Upvotes: 1

Bull
Bull

Reputation: 11951

That draw.at<float>(i,j)=255; should be

draw.at<uchar>(i,j)=255;

You have declared the Mat as type 8 bit unsigned char, 1 channel:

Mat draw(480, 480, CV_8UC1);

so trying to write a 4 byte quantity to a single byte container is going to cause not only incorrect result, but probably corru[tion of other data structures.

The following is going to write (255.0) to 4 bytes not 1:

draw.at<float>(i,j)=255;

Upvotes: 3

Related Questions