opencv如何把16位图像转换为8位图像

一、opencv自带的方法:

Mat src16,tmp;
Mat dst8 = Mat::zeros(src16.size(), CV_8U);
normalize(src16, tmp, 0, 255, NORM_MINMAX);
convertScaleAbs(tmp, dst8);

二、自己代码实现

		Mat src16;
		Mat dst8 = Mat::zeros(src16.size(), CV_8U);
		double mymin, mymax;
		cv::minMaxIdx(src16, &mymin, &mymax);
		assert(mymax>mymin);
		double delt = 255.0 / (mymax - mymin);
		for (int i = 0; i < src16.rows; i++)
		{
			for (int j = 0; j < src16.cols; j++)
			{
				ushort s = src16.at<ushort>(i, j);
				uchar d = (s - mymin) * delt;
				dst8.at<uchar>(i, j) = d;
			}
		}

你可能感兴趣的:(opencv,C++,opencv,16位转8位)