如何从保存有RGBA格式图像的缓冲区中的opencv中创建图像 [英] How to create an image in opencv from a buffer that holds image in RGBA format
问题描述
我在缓冲区中有一张图像,该图像保存着RGBA格式的图像.我想从中创建一个openCV图像(cv :: Mat),但看来我做不到.
I have an image in a buffer that hold an image with format of RGBA. I want to create an openCV image from it (cv::Mat), but it seems that I can not do this.
我拥有的代码是这样:
cv::Mat image=cv::Mat(cvSize(Width,Height), CV_8UC4,Buffer, cv::Mat::AUTO_STEP).clone();
主要问题是,由于OpenCV将其图像保存为BGRA格式,并且缓冲区具有RGBA格式的图像,因此生成的图像的颜色不正确.
The main problem is that since OpenCV is saving its images in BGRA format, and the buffer has image in RGBA format, the colours of generated image is not correct.
我知道我可以将图像转换为BGRA格式,但是有什么方法可以在OpenCV中创建以RGBA格式保存图像的图像?
I know that I can convert the image to BGRA format, but is there any way that I can create an image in OpenCV that holds images in RGBA format?
推荐答案
您可以使用 cvtColor
将 Mat
从RGBA转换为BGRA.而且,如果您在从缓冲区创建原始" Mat
之后立即执行此操作,则无需使用 clone()
,因为无论如何都会分配新缓冲区用于转换后的图像.
You can use cvtColor
to convert the Mat
from RGBA to BGRA. And if you will do it right after creation of the "original" Mat
from the buffer, you need not to use clone()
, as anyway the new buffer will be allocated for the converted image.
cv::Mat image=cv::Mat(cvSize(Width,Height), CV_8UC4, Buffer, cv::Mat::AUTO_STEP);
cv::Mat converted;
cvtColor(image, converted, CV_RGBA2BGRA);
这篇关于如何从保存有RGBA格式图像的缓冲区中的opencv中创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!