Tony Luo
Tony Luo

Reputation: 110

CV_8UC4 convertTo CV_8UC1 doesn't work

I got image from kinect v2 with type CV_8UC4. Now I try to use in in type CV_8UC1 with the following code.

cv::Mat mImg(iHeight, iWidth, CV_8UC4);
cv::Mat mImg2(iHeight, iWidth, CV_8UC1);
....
get image from kinect to mImg
....
mImg.convertTo(mImg2, CV_8UC1);

After that, mImg2 type is still 24 ( CV_8UC4 ), not sure whether I use this function correctly. Please help.

Upvotes: 1

Views: 3879

Answers (2)

ZuOverture
ZuOverture

Reputation: 233

Depends on what exactly you want to achieve. Kinect probably returns data in form {x,y,depth,somethingelse_forexamplecolor}, so if you wish to get depth data in separate Mat, you'd go with split():

vector<cv::Mat> channels(4);
cv::split(mImg, channels);
cv::Mat depth = channels[2]; //or other than 2

But you can keep data together, accessing it with:

char depth = mImg.at<cv::Vec4b>(x,y)[0];

Upvotes: 2

Tony Luo
Tony Luo

Reputation: 110

Find some clues from other link.

CV_8UC4 to CV_8UC1 is not channel issue so don't use convertTo.

cvtColor(mImg,mImg2, CV_BGR2GRAY);

Use cvtColor instead and now the type is changed. Although the image comes to be grey, I think I got the right way.

Upvotes: 2

Related Questions