C++ – How to convert cv::Mat type from CV_16UC1 to CV_8UC1

copencv

I have a cv::Mat image of type CV_16UC1 and I need it in CV_8UC1, so I can run cv::integral on it. I am not worried about overflow during the conversion – essentially I just want to bulk convert the image from unsigned short to unsigned char.

I asked elsewhere and someone suggested cvtColor, but I don't know what conversion code to use.

Best Answer

How about using Mat::convertTo? Documentation

A quick example: (not sure if you need scalefactor though, since I haven't tried)

Your16Image.convertTo(outputImage, CV_8UC1, scalefactor)