I am using OpenCV 4.1.1. and have the following goal
Given a
Mat
of size (m, n, 2) of float32, create separateMat
headers for each individual channel, so that it can be written to, without copying the image data
What I am trying to do is this:
const int mat_sz[]{shape_.height, shape_.width, 2};
Mat cost(3, mat_sz, CV_32F);
Mat channel1(
shape_, CV_32FC1, /* 1 channel Mat has FC1 type*/
cost.ptr<float>(), /* data ptr */
cost.step[2] * 2 /* orig stride/step should be 4 for float, x2 because
we want every other */
);
channel1(Rect(0, 0, 100, 100)) = 255;
Mat channel2(
shape_, CV_32FC1, cost.ptr<float>() + 1, // +1 so we start at the second channel element
cost.step[2] * 2);
channel2(Rect(0, 0, 100, 100)) = 128;
I can see by normalize
ing and imshow
ing the two channels that
- rectangle selection seems not to work in this case, instead pixels get filled from the top, maybe
operator(Rect)
doesnt do what I want in this case. - the values which should go into channel 2 show up in channel 1 and vise versa
What am I doing wrong?
I'm using this code to display the matrix:
#define DISPLAY(m) \
{ \
Mat _tmp##m; \
normalize(m, _tmp##m, 0, 1, NORM_MINMAX); \
imshow(#m, _tmp##m); \
waitKey(0); \
}