gpt4 book ai didi

c++ - BGR 到 HSV 并再次返回

转载 作者:搜寻专家 更新时间:2023-10-31 02:17:28 24 4
gpt4 key购买 nike

文档似乎没有解释 HSV->BGR 转换的预期输入范围。这是一些示例代码,我试图在将其转换为 HSV 后取回原始 BGR 值。有谁知道预期的缩放比例?

#include <iostream>

#include <opencv2/opencv.hpp>

cv::Vec3b HSVtoBGR(const cv::Vec3f& hsv)
{
cv::Vec3f hsvAdjusted = hsv; // If we use this directly, the output is (0,0,1) which is very wrong
//hsvAdjusted[0] *= 360.; // If we do this to bring all of the values into the range (0,1), the output is (0,1,0), which is also very wrong

// If we do this to bring all of the values into the range (0,255), the output is (0,0,200), which is still very wrong
hsvAdjusted[1] *= 255./360.;
hsvAdjusted[1] *= 255.;
hsvAdjusted[2] *= 255.;

cv::Mat_<cv::Vec3f> hsvMat(hsvAdjusted);

cv::Mat_<cv::Vec3f> bgrMat;

cv::cvtColor(hsvMat, bgrMat, CV_HSV2BGR);

cv::Vec3b bgr = static_cast<cv::Vec3b>(bgrMat(0,0));
return bgr;
}

/** Input 0 <= B <= 255, 0 <= G <= 255, 0 <= R <= 255
* Output 0 <= H <= 360, 0 <= S <= 1, 0 <= V <= 1 */
cv::Vec3f BGRtoHSV(const cv::Vec3b& bgr)
{
cv::Mat3f bgrMat(static_cast<cv::Vec3f>(bgr));

bgrMat *= 1./255.;

cv::Mat3f hsvMat;
cv::cvtColor(bgrMat, hsvMat, CV_BGR2HSV);

cv::Vec3f hsv = hsvMat(0,0);

return hsv;
}

int main()
{
// Create a BGR color
cv::Vec3b bgr(5, 100, 200);
std::cout << "bgr: " << bgr << std::endl;

// Convert BGR to HSV
cv::Vec3f hsv = BGRtoHSV(bgr);
std::cout << "hsv: " << hsv << std::endl; // outputs // (29.23, .976, .7843), which seems correct

// Convert back from HSV to BGR
cv::Vec3b bgr2 = HSVtoBGR(hsv);
std::cout << "bgr2: " << bgr2 << std::endl;

return 0;
}

最佳答案

函数 HSVtoBGR 中存在一些缩放问题。

您需要将转换后的结果乘以 255,而不是之前。请记住,CV_32FCV_8U 类型的 Mat 会发生不同的转换。

这应该按预期工作:

cv::Vec3b HSVtoBGR(const cv::Vec3f& hsv)
{
cv::Mat_<cv::Vec3f> hsvMat(hsv);
cv::Mat_<cv::Vec3f> bgrMat;

cv::cvtColor(hsvMat, bgrMat, CV_HSV2BGR);

bgrMat *= 255; // Upscale after conversion

// Conversion to Vec3b is handled by OpenCV, no need to static_cast
return bgrMat(0);
}

关于c++ - BGR 到 HSV 并再次返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35737942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com