我有一种颜色,我想转换为不同的颜色空间.是否有可能直接在cv :: Vec3f上使用cvtColor而不创建1×1 cv :: Mat并使用cv :: Mat上的cvtColor填充该像素,然后从输出中获取唯一的像素?我尝试了以下
有什么建议?
#include <iostream> #include <opencv2/opencv.hpp> int main(int, char*[]) { cv::Vec3f hsv; hsv[0] = .9; hsv[1] = .8; hsv[2] = .7; std::cout << "HSV: " << hsv << std::endl; cv::Vec3b bgr; cvtColor(hsv, bgr, CV_HSV2BGR); // OpenCV Error: Assertion failed (scn == 3 && (dcn == 3 || dcn == 4) && (depth == CV_8U || depth == CV_32F)) in cvtColor std::cout << "BGR: " << bgr << std::endl; return EXIT_SUCCESS; }
我也尝试了这个,但得到了一个不同的错误:
#include <iostream> #include <opencv2/opencv.hpp> int main(int, char*[]) { cv::Mat_<cv::Vec3f> hsv(cv::Vec3f(0.7, 0.7, 0.8)); std::cout << "HSV: " << hsv << std::endl; cv::Mat_<cv::Vec3b> bgr; cvtColor(hsv, bgr, CV_HSV2BGR); // OpenCV Error: Assertion failed (!fixedType() || ((Mat*)obj)->type() == mtype) in create std::cout << "BGR: " << bgr << std::endl; return EXIT_SUCCESS; }你的第二种方法是正确的,但你在cvtColor中有不同类型的源和目标,这会导致错误.
请确保同时具有相同类型的hsv和bgr,CV_32F:
#include <opencv2/opencv.hpp> #include <iostream> int main() { cv::Mat3f hsv(cv::Vec3f(0.7, 0.7, 0.8)); std::cout << "HSV: " << hsv << std::endl; cv::Mat3f bgr; cvtColor(hsv, bgr, CV_HSV2BGR); std::cout << "BGR: " << bgr << std::endl; return 0; }
您可以使用Mat3f简洁.它只是一个typedef:
typedef Mat_<Vec3f> Mat3f;