C – Convert a single color using CVTColor

I have a color and I want to convert to a different color space. Is it possible to directly use cvtColor on cv::Vec3f without creating 1×1 cv::Mat and use cv :: The cvtColor on the Mat fills the pixel and then gets the only pixel from the output? I tried the following, but it doesn’t seem to like passing a vector.

Any suggestions?

#include 

#include

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

< p>I also tried this, but got a different error:

#include 

#include

int main(int, char*[])
{
cv::Mat_ hsv(cv::Vec3f(0.7, 0.7, 0.8));

std::cout << "HSV: "<< hsv << std::endl;

cv::Mat_ 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;
}

Your second method is correct, but you have different types of sources and targets in cvtColor, this Will cause errors.

Please make sure to have the same type of hsv and bgr, CV_32F at the same time:

#include  
#include

int main()
{
cv::Mat3f hsv(cv::Vec3f(0.7, 0.7, 0.8));< br />
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 to be concise. It is just a typedef:

typedef Mat_ Mat3f;

< p>I have a color and I want to convert to a different color space. Is it possible to directly use cvtColor on cv::Vec3f without creating a 1×1 cv::Mat and fill that pixel with cvtColor on cv::Mat And then get the only pixel from the output? I tried the following, but it doesn't seem to like passing a vector.

Any suggestions?

#include 

#include

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

< p>I also tried this, but got a different error:

#include 

#include

int main(int, char*[])
{
cv::Mat_ hsv(cv::Vec3f(0.7, 0.7, 0.8));

std::cout << "HSV: "<< hsv << std::endl;

cv::Mat_ bgr;

cvtColor(hsv, bgr, CV_HSV2BGR); // OpenCV Error: Assertion failed (!fixedType() || ((Mat*)obj)->type() == mtype) in cr eate

std::cout << "BGR: "<< bgr << std::endl;

return EXIT_SUCCESS;
}

< /p>

Your second method is correct, but you have different types of sources and targets in cvtColor, which will cause errors.

Please make sure to have both hsv and bgr of the same type, CV_32F:

#include 
#include

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 concisely. It just A typedef:

typedef Mat_ Mat3f;

Leave a Comment

Your email address will not be published.