[英]Compose cv::Mat Image from std::basic_string<char>
I am trying to compose cv::Mat
image from std::basic_string<char>
but the composed image is empty. 我正在尝试从
std::basic_string<char>
组成cv::Mat
图像,但是组成的图像为空。 Below is the code snippet: 下面是代码片段:
std::basic_string<char> color_pixels = color_frame.data();
cv::Mat image(cv::Size(color_frame.width(), color_frame.height()),
CV_8UC4,
&color_pixels,
cv::Mat::AUTO_STEP);
Before proceeding further, I wanted to verify the data. 在继续之前,我想验证数据。 Hence I checked
width
, height
etc and found correct. 因此,我检查了
width
, height
等,发现正确。
std::cout << "width:" << color_frame.width() <<
", height:" << color_frame.height() <<
", length:" << color_pixels.length() << std::endl;
width:1920, height:1080, length:8294400 // 1920 * 1080 * 4 = 8294400
I found a similar question here and tried to implement in my code as follows: 我在这里找到了类似的问题,并尝试在我的代码中实现如下:
cv::Mat image(cv::Size(color_frame.width(), color_frame.height()),
CV_8UC4,
color_pixels.data(),
cv::Mat::AUTO_STEP);
Unfortunately, it is throwing following error: 不幸的是,它引发以下错误:
error: invalid conversion from ‘const void*’ to ‘void*’
How to compose cv::Mat Image from std::basic_string? 如何从std :: basic_string编写cv :: Mat图像? Any workaround, please?
有什么解决方法吗?
color_pixels.data() is a const value. color_pixels.data()是const值。
http://en.cppreference.com/w/cpp/string/basic_string/data http://en.cppreference.com/w/cpp/string/basic_string/data
In C++, void*
can be used to represent a pointer to some unspecified, valid memory address (internally, this will be either passed to some low-level API, like C memcpy, or casted to some meaningful type according to the given interface contract). 在C ++中,
void*
可用于表示指向一些未指定的有效内存地址的指针(在内部,该地址将被传递给一些低级API,例如C memcpy,或根据给定的接口协定转换为某种有意义的类型。 )。 IMHO, this is seldom if ever necessary in the modern C++ type system (and function dealing with memory can use std::byte now), but C++ libraries using it sadly still exist. 恕我直言,这在现代C ++类型系统中几乎是没有必要的(处理内存的函数现在可以使用std :: byte了),但是可悲的是,使用它的C ++库仍然存在。
Anyway, coming back to your question, you can implicitly convert any pointer to void*, but you cannot implictly cast-away constness. 无论如何,回到您的问题,您可以将任何指针隐式转换为void *,但是您不能隐式转换常量。 You need
const_cast
for that: 为此,您需要
const_cast
:
cv::Mat image(cv::Size(color_frame.width(), color_frame.height()),
CV_8UC4,
const_cast<char*>(color_pixels.data()),
cv::Mat::AUTO_STEP);
in >=C++11, if color_pixels
were originally const, any attempt to modify color_pixels
data via image
will result in undefined behaviour . 在> = C ++ 11中,如果
color_pixels
最初是const,则任何通过image
修改color_pixels
数据的尝试都将导致不确定的行为 。
in C++<=03, data()
were not required to point to the original string characters, so any modification attempt would be UB in that case. 在C ++ <= 03中,不需要
data()
指向原始字符串字符,因此在这种情况下, 任何修改尝试都将是UB。
invoking color_pixels
modifiers invalidating its content will make invoking most image methods UB as well. 调用使
color_pixels
修饰符无效的内容也会使大多数图像方法UB color_pixels
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.