为什么要把CV_8UC3(Vec3b)无符号整型转换成CV_32F(Vec3F)32位浮点数据类型?(在高精度下处理)

因为用CV_32F做处理精度较高,后面显示最终还得转换回cv_8UC3

示例:提高图像对比度(rgb和灰度都已实现)

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, const char* argv[]) {

	Mat src, dst;

	//src = imread("./test.jpg");
	src = imread("./test.jpg", IMREAD_GRAYSCALE);

	//cvtColor(src, src, COLOR_BGR2GRAY);

	//if (src.empty()) {
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input img");	//默认自动窗口大小
	imshow("input img", src);

	Mat m1;
	src.convertTo(m1, CV_32F);
	//src.convertTo(dst, CV_32F, 1 / 255.0);

	int height = src.rows;
	int width = src.cols;
	int sc = src.channels();
	dst = Mat::zeros(src.size(), src.type());

	float alpha = 1.8;
	float beta = -50;

	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			for (int c = 0; c < 3; c++)	//三个通道
			{
				if (sc == 3) {
					float b = m1.at<Vec3f>(row, col)[0];	//34.0000000
					float g = m1.at<Vec3f>(row, col)[1];	//14.0000000
					float r = m1.at<Vec3f>(row, col)[2];

					dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b * alpha + beta);
					dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g * alpha + beta);
					dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r * alpha + beta);
				}
				else if (sc == 1) {
					float v = m1.at<float>(row, col);
					dst.at<uchar>(row, col) = saturate_cast<uchar>(v * alpha + beta);
				}
			}
		}
	}
	imshow("output img", dst);

	waitKey(0);
	return 0;
}

VS编译运行:
为什么要把CV_8UC3(Vec3b)无符号整型转换成CV_32F(Vec3F)32位浮点数据类型?(在高精度下处理)
为什么要把CV_8UC3(Vec3b)无符号整型转换成CV_32F(Vec3F)32位浮点数据类型?(在高精度下处理)
参考文章:opencv用浮点数记录像素值的意义何在,用8UC3不就可以了吗? - William Wu的回答 - 知乎

上一篇:Slow HTTP Denial of Service Attack漏洞修复


下一篇:manifest