英文:
Converting single pixel from RGB to LAB with OpenCV (C++)
问题
我正在尝试使用 OpenCV 将 RGB 值转换为 LAB 颜色空间。在研究过程中,我发现有人有类似的目标这里,并尝试复制这种方法。
在进行一些尝试之后,我设法让以下代码编译并运行:
int main(){
int r_a = 168, g_a = 93, b_a = 201, r_b = 60, g_b = 117, b_b = 59;
cv::Mat3f rgb_a (cv::Vec3f(r_a, g_a, b_a));
cv::Mat3f rgb_b (cv::Vec3f(r_b, g_b, b_b));
cv::Mat3f lab_a;
cv::Mat3f lab_b;
cv::cvtColor(rgb_a,lab_a,cv::COLOR_RGB2Lab);
cv::cvtColor(rgb_b,lab_b,cv::COLOR_RGB2Lab);
std::cerr << ">> rgb_a = " << rgb_a << "\n";
std::cerr << ">> rgb_b = " << rgb_b << "\n";
std::cerr << ">> lab_a = " << lab_a << "\n";
std::cerr << ">> lab_b = " << lab_b << "\n";
return 0;
}
当我运行这段代码时,两个 LAB 值都计算为 [100, 0, 0]
。
在进一步浏览中,我发现有人在使用 Python 中的 OpenCV 时遇到了类似的问题,参见这个问题。
我在 Python 中复制了这个可行的解决方案,但仍然无法找到 C++ 的解决方法。
对于如何修复这个问题,你有什么想法吗?这是矩阵形状的问题吗?我对于 OpenCV 在 C++ 中的确切图像格式并不熟悉。
英文:
I am trying to convert a RGB value to the LAB color space using OpenCV. While doing some research I found someone with a similar goal here and have tried to replicate this approach.
After some messing around I was able to get the following code to compile and run:
int main(){
int r_a = 168, g_a = 93, b_a = 201, r_b = 60, g_b = 117, b_b = 59;
cv::Mat3f rgb_a (cv::Vec3f(r_a, g_a, b_a));
cv::Mat3f rgb_b (cv::Vec3f(r_b, g_b, b_b));
cv::Mat3f lab_a;
cv::Mat3f lab_b;
cv::cvtColor(rgb_a,lab_a,cv::COLOR_RGB2Lab);
cv::cvtColor(rgb_b,lab_b,cv::COLOR_RGB2Lab);
std::cerr << ">> rgb_a = " << rgb_a << "\n";
std::cerr << ">> rgb_b = " << rgb_b << "\n";
std::cerr << ">> lab_a = " << lab_a << "\n";
std::cerr << ">> lab_b = " << lab_b << "\n";
return 0;
}
When I run this, both LAB values are calculated as [100, 0, 0]
.
After a bit more browsing I found someone else had a similar issue when using OpenCV in python, see this question.
I was able to replicate this working solution in Python, but am still unable to find a fix for c++.
Any idea on how I can fix this? Is it a matrix shape issue? I am quite unfamiliar with the exact image formats for OpenCV in c++.
答案1
得分: 0
以下是要翻译的部分:
"Posting an answer here in case anyone in the future runs into the same issue.
As @M. Spiller pointed out I needed to scale my vector.
I divided each value by 255.0 and then the conversion was able to execute correctly!"
英文:
Posting an answer here in case anyone in the future runs into the same issue.
As @M. Spiller pointed out I needed to scale my vector.
I divided each value by 255.0 and then the conversion was able to execute correctly!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论