Convert single color with cvtColor

I have a color that I want to convert to another color space. Is it possible to use cvtColor on cv::Vec3f directly without creating 1x1 cv::Mat and filling it with that pixel, using cvtColor in cv::Mat and then getting a single pixel from the output? I tried the following, but it seems to me that there is no need to skip the vector.

Any suggestions?

 #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; } 

I also tried this, but I get another error:

 #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; } 
+5
source share
1 answer

Your second approach is correct, but you have the source and destination of different types in cvtColor , and this causes an error.

Be sure to use both hsv and bgr the same type, CV_32F here:

 #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; } 

You can use Mat3f for short. This is just a typedef:

 typedef Mat_<Vec3f> Mat3f; 
+3
source

Source: https://habr.com/ru/post/1244265/


All Articles