gpt4 book ai didi

c++ - 将 32SC1 转换为 CV_8U

转载 作者:行者123 更新时间:2023-11-28 03:02:33 25 4
gpt4 key购买 nike

我在 OpenCV 中使用 kmeans 聚类函数。返回的标签类型为 CV_32SC1,因为它们是整数(1、2、3、4 代表标签),我将其转换为 CV_8U:

lev.convertTo(lev, CV_8U)

转换后它转换为完全不同的数字:

33686018
33686018
33686018
33686018
33686018
33686018

有人可以为我解释一下这个现象吗?我认为 CV_8U 中的 2 应该与 CV_32SC1 中的一样吗?

最佳答案

CV_8U 矩阵不能包含这些整数值 (33686018)。您使用什么代码来检查结果矩阵的内容?您可能从矩阵中错误地读取了 uchar 值。

转换对我来说效果很好。这是我的测试代码:

// declare CV_32SC1 matrix
Mat A(3,3,CV_32SC1);

// initialize A with random numbers
randu(A, Scalar(0), Scalar(100));
cout << "original A = " << A << endl;

// convert A to CV_8U
A.convertTo(A, CV_8U);
// output the result
cout << "after converting A = " << A << endl;

// if you want to print out a single pixel of A, say at location (0,0)
int pixel = static_cast<int>(A.at<uchar>(0,0));
cout << "pixel at A(0,0) = " << pixel << endl;

关于c++ - 将 32SC1 转换为 CV_8U,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20384450/

25 4 0