【问题标题】:c OpenCV; How to convert 2-dimension array to IplImage?c OpenCV;如何将二维数组转换为 IplImage?
【发布时间】:2014-04-04 18:43:38
【问题描述】:

我正在使用 c-api 并且是 OpenCV 的新手。

假设我有二维数组,包括;红色[][]、绿色[][]、蓝色[][]。我想将 3 个二维数组转换为 IplImage 并将其保存为 .png 文件。但是,在我的系统中,我无法使用 CvMat。

你能帮我解决这个问题吗?

谢谢。

【问题讨论】:

  • IplImage 是过时的 c-api,而不是 c++。避免它。
  • 感谢您提供更多信息,但我无法控制我现在使用的系统。

标签: c opencv iplimage


【解决方案1】:

如果您使用 C++,请使用 C++ API:

cv::Mat image = cv::Mat::zeros(w,h, CV_8UC3);
for(int x=0;x<w;x++)
   for(int y=0;y<h;y++)
        image.at<cv::Vec3b>(y,x) = cv::Vec3b(red[x][y], blue[x][y], green[x][y]);
cv::imwrite("image.png", image);

但如果你真的想拥有 c-api,从 cv::Mat 到 IplImage 的转换是可行的:

IplImage *frameConverted = new IplImage(image);

如果不再需要 frameConverted,请记住释放内存。

【讨论】:

  • 感谢您的代码,但我收到了这条消息“错误:没有匹配函数调用'cv::Mat::zeros(int&, int&)'”当我编译它时。也许我的系统安装了旧版本的 OpencV。我无法自己控制系统。您能否建议不使用 Mat 的其他方式。
  • 我忘记了第三个参数,它是像素类型。在我们的例子中,我们有 3 个无符号字符,这正是 CV_8UC3 的含义(CV_{bits per pixel}{U|S|F}{channels} where U - unsigned, S - signed, F - float
  • 我添加了对 IplImage 的转换。即使你不想使用 C++ api 并且你有 opencv 版本。 > 2.0 你必须有 c++ api 才能使用它。
猜你喜欢
  • 2023-04-08
  • 2010-09-28
  • 2010-09-28
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 2021-11-23
  • 2013-05-17
相关资源
最近更新 更多