东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1013|回复: 1
打印 上一主题 下一主题

[C/C++] armadillo与opencv的mat(c++版)相互转换

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
跳转到指定楼层
楼主
发表于 2021-11-23 16:58:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

armadillo与opencv的mat(c++版)相互转换


  1. #include <iostream>
  2. #include <armadillo>
  3. #include <cmath>
  4. #include <complex>
  5. #include <vector>
  6. #include "opencv2/opencv.hpp"
  7. #include <string>


  8. using namespace std;

  9. //armadillo与opencv的mat(c++版)相互转换

  10. static void Cv_mat_to_arma_mat(const cv::Mat& cv_mat_in, arma::mat& arma_mat_out)
  11. {//convert unsigned int cv::Mat to arma::Mat<double>
  12.     for(int r=0;r<cv_mat_in.rows;r++){
  13.         for(int c=0;c<cv_mat_in.cols;c++){
  14.             // 这里除以了255.0
  15.             arma_mat_out(r,c)=cv_mat_in.data[r*cv_mat_in.cols+c]/255.0;
  16.         }
  17.     }
  18. }

  19. template<typename T>
  20. static void Arma_mat_to_cv_mat(const arma::Mat<T>& arma_mat_in,cv::Mat_<T>& cv_mat_out)
  21. {
  22.     //通过 .memptr() 访问 Armadillo 矩阵使用的内存
  23.     cv::transpose(cv::Mat_<T>(static_cast<int>(arma_mat_in.n_cols),
  24.                               static_cast<int>(arma_mat_in.n_rows),
  25.                               const_cast<T*>(arma_mat_in.memptr())),
  26.                   cv_mat_out);
  27. }

  28. int main(){

  29.     // opencv 读入一张图片 arma的矩阵 如何保存为图片呢? 用opencv
  30.     string img_path = "/home/jiang/图片/py_雷达算法仿真/bp成像建模与ifft_py_c++对比02.png";
  31.    
  32.     cv::Mat image_cv;
  33.     image_cv = cv::imread(img_path, 0);
  34.     cout << image_cv.channels() << endl;
  35.     // /图像尺寸
  36.         // cout << "size:" << image.size << endl;
  37.         //列宽
  38.         cout << "cols:" << image_cv.cols<<endl;
  39.         //行高
  40.         cout << "rows:" << image_cv.rows << endl;
  41.         //维度
  42.         cout << "dims:" << image_cv.dims << endl;


  43.     //opencv的图片矩阵 转换为 arma::mat
  44.     arma::mat img(image_cv.rows,image_cv.cols, arma::fill::zeros);
  45.     Cv_mat_to_arma_mat(image_cv, img);   // opencv的mat 转为 arma的mat
  46.     // img.save("img_arma.txt", arma::raw_ascii);

  47.     cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE);
  48.     cv::imshow("Display Image", image_cv);
  49.     cv::waitKey(0);

  50.     // arma::imat arma_img = arma::imat(100, 100, arma::fill::ones) * 255;
  51.     // cv::Mat_<int> cv_img;
  52.     // Arma_mat_to_cv_mat<int>(arma_img,cv_img);

  53.     //convert arma::mat to cv::Mat
  54.     cv::Mat_<int> cv_img;
  55.     arma::Mat<int> arma_img = arma::Mat<int>(100, 200, arma::fill::ones) * 255;
  56.     // umat         =         Mat<uword>
  57. // imat         =         Mat<sword>
  58. //sword is a typedef for a signed integer type
  59.    
  60.     Arma_mat_to_cv_mat<int>(arma_img,cv_img);
  61.     cv::imwrite("b.png", cv_img);


  62.     return 0;
  63. }
复制代码


让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

98

帖子

200

积分

中级会员

Rank: 3Rank: 3

积分
200
沙发
发表于 2021-11-23 19:20:22 | 只看该作者
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-19 18:18 , Processed in 0.163132 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表