简体   繁体   English

从std :: basic_string撰写cv :: Mat图片<char>

[英]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. 因此,我检查了widthheight等,发现正确。

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);

A few caveats: 一些警告:

  • 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.

相关问题 无法从&#39;const std :: __ 1 :: basic_string转换 <char> 到&#39;std :: __ 1 :: basic_string <char> *” - No viable conversion from 'const std::__1::basic_string<char> to 'std::__1::basic_string<char> *' 从std :: map转换 <std::basic_string<char> ,std :: pair - conversion from std::map<std::basic_string<char>,std::pair<int,int(*)(const std::vector::Mat 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 转换 std::basic_string<Char> 串起来 - Convert std::basic_string<Char> to string std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 1 IntelliSense:没有合适的构造函数可以从“bool”转换为“std :: basic_string” <char, std::char_traits<char> ,std :: allocator <char> &gt;” - 1 IntelliSense: no suitable constructor exists to convert from “bool” to “std::basic_string<char, std::char_traits<char>, std::allocator<char>>” “ str2Int(std :: basic_string <char, std::char_traits<char> ,std :: allocator <char> &gt; const&)”,引用自: - “str2Int(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)”, referenced from: 为什么std :: string实现为char的basic_string而不是unsigned char的 - Why is std::string implemented as basic_string of `char` and not `unsigned char` 错误:无法从“向量”转换“标签” <std::vector<std::__cxx11::basic_string<char> &gt;&gt;' 到 ' 向量<std::__cxx11::basic_string<char> &gt;' </std::__cxx11::basic_string<char></std::vector<std::__cxx11::basic_string<char> - error: could not convert 'tab' from 'vector<std::vector<std::__cxx11::basic_string<char> >>' to 'vector<std::__cxx11::basic_string<char>>' 错误:无法转换 &#39;std::basic_string<char> &#39; 到 &#39;char&#39; 赋值 - error: cannot convert 'std::basic_string<char>' to 'char' in assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM