Gilad
Gilad

Reputation: 6575

opencv cvtColor set/get matrix element

cv::cvtColor(dst,bwImage, CV_RGB2GRAY);

how can I set a specific pixel for example 0,0 value it this bwImage after usign cvtColor?

I usually use:

bwImage.at<float>(0,0) = 0;

but it now throws an exception.

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si
ze.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channel
s()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3
) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file C:\Users\gdarmon\Downloads
\opencv\build\include\opencv2/core/mat.hpp, line 538

Update I have found a work around

bwImage.convertTo(bwImage,CV_32F);
bwImage.at<float>(0,0)=0;

Upvotes: 0

Views: 177

Answers (1)

flix
flix

Reputation: 609

As you already found out, your black and white image does not hold floats, but 8-bit unsigned integers. Besides your workaround you could also do

bwImage.at<uint8_t>(0,0) = 0;

if you include stdint.h. As this is just a typedef for unsigned char, you can also not include the header and do this:

bwImage.at<unsigned char>(0,0) = 0;

On a side note: the default channel ordering of OpenCV is BGR, so using CV_RGB2GRAY here would be wrong, if you did not reorder the channels beforehand.

Upvotes: 3

Related Questions