使用从缓冲区复制的图像更改cv :: Mat会影响原始图像 [英] Changing cv::Mat with image copied from buffer affects original image

查看:158
本文介绍了使用从缓冲区复制的图像更改cv :: Mat会影响原始图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有24位RGB格式的图像缓冲区。使用

I have image buffer in 24bit RGB format. This buffer is copied to cv::Mat using

cv::Mat mat = cv::Mat(image->height, image->width, CV_8UC3, image->data);

由于此缓冲区为RGB格式且OpenCV使用BGR格式,我正在转换 mat 到BGR

Since this buffer is in RGB format and OpenCV uses BGR format, I'm converting matto BGR with

cv::cvtColor(mat, mat, CV_RGB2BGR);

这是有效的,但是当我检查原始图像时,其频道也会被反转(因此它们会出错)我不希望这种情况发生。

This works, but when I check original image its channels are inverted as well (and so they become wrong) and I don't want this to happen.

我想反转 mat 渠道订单离开 image-data (我的图片缓冲区)未受影响。我怎么能这样做?

I'd like to invert mat channels order leaving image-data (my image buffer) untouched. How can I do that?

推荐答案

我认为(我不确定)如果你使用 cv :: cvtColor(mat,mat,CV_RGB2BGR); ,实际上是重新创建mat,但是用RGB-> BGR转换数据覆盖数据。由于使用指针将数据传递到mat,如果覆盖mat中的数据,则也会更改image-> data。

I presume (I am not certain) that if you use cv::cvtColor(mat, mat, CV_RGB2BGR);, you actually recreate mat, but you overwrite data with the RGB->BGR converted data. Since you pass data to your "mat" using pointer, if you overwrite the data in mat, you change "image->data" as well.

因此,我认为表现不会低于:

Therefore, I do not expect less performance than:

cv::Mat mat = cv::Mat(image->height, image->width, CV_8UC3, image->data);
cv::Mat mat2;
cv::cvtColor(mat, mat2, CV_RGB2BGR);
//Work with mat 2 now

您可以编写新数据,而不是覆盖。这应该具有相同的性能成本...
我不知道在颜色转换后你的图像有什么计划,但即使性能不同,也可能会产生整体的轻微影响。

Rather than overwriting, you write new data. This should bear the same performance cost... I do not know what plan to do with your image after colour conversion, but even if the performance was different, it is likely to have an overall minor impact.

这篇关于使用从缓冲区复制的图像更改cv :: Mat会影响原始图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆