Convert a single color with cvtColor(使用 cvtColor 转换单一颜色)
问题描述
我有一种颜色想要转换为不同的颜色空间.是否可以直接在 cv::Vec3f 上使用 cvtColor 而无需创建 1x1 cv::Mat 并用该像素填充它,使用cvtColor 在 cv::Mat 上,然后从输出中获取唯一的像素?我尝试了以下方法,但似乎不喜欢传递向量.
I have a color that I want to convert to a different color space. Is it possible to use cvtColor on a cv::Vec3f directly without creating a 1x1 cv::Mat and populating it with that pixel, using cvtColor on the cv::Mat, then getting the only pixel out of the output? I have tried the following, but it doesn't seem to like getting passed a vector.
有什么建议吗?
#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 get a different 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;
}
推荐答案
您的第二种方法是正确的,但是您在 cvtColor 中有不同类型的源和目标,这会导致错误.
Your second approach is correct, but you have source and destination of different types in cvtColor, and that causes the error.
确保 hsv 和 bgr 的类型相同,CV_32F 在这里:
Be sure to have both hsv and bgr of 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;
}
<小时>
为了简洁起见,您可以使用 Mat3f.这只是一个类型定义:
You can use Mat3f for brevity. It's just a typedef:
typedef Mat_<Vec3f> Mat3f;
                        这篇关于使用 cvtColor 转换单一颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 cvtColor 转换单一颜色
				
        
 
            
        基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
 - 我有静态或动态 boost 库吗? 2021-01-01
 - 如何检查GTK+3.0中的小部件类型? 2022-11-30
 - C++结构和函数声明。为什么它不能编译? 2022-11-07
 - 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
 - 常量变量在标题中不起作用 2021-01-01
 - 这个宏可以转换成函数吗? 2022-01-01
 - 在 C++ 中计算滚动/移动平均值 2021-01-01
 - 如何通过C程序打开命令提示符Cmd 2022-12-09
 - 如何在 C++ 中初始化静态常量成员? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				