問題描述
我有一種顏色想要轉換為不同的顏色空間.是否可以直接在 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 轉換單一顏色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!